Search field
A labeled native search input with a leading icon and a clear button that restores focus to the field.
pnpm dlx shadcn@latest add @haze/search-field---
import { Search, X } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";
import Button from "./button.astro";
import Input from "./input.astro";
interface Props {
id: string;
name?: string;
label?: string;
hideLabel?: boolean;
placeholder?: string;
value?: string;
clearLabel?: string;
/** Adds a submit button in the same row as the field. */
submitLabel?: string;
class?: string;
[key: string]: unknown;
}
const { id, name = id, label = "Search", hideLabel = false, placeholder, value,
clearLabel = "Clear search", submitLabel, class: className, ...rest } = Astro.props;
---
<div data-haze-search-field class={cn("grid min-w-0 gap-2", className)}>
<label for={id} class={cn("text-sm/5 font-medium", hideLabel && "sr-only")}>{label}</label>
<div class="flex min-w-0 items-center gap-2">
<div class="group relative min-w-0 flex-1">
<Search class="pointer-events-none absolute inset-s-3 top-1/2 size-4 -translate-y-1/2 text-muted-foreground" aria-hidden="true" />
<Input {...rest} id={id} name={name} type="search" placeholder={placeholder} value={value}
class="ps-9 pe-9 [[data-haze-search-field][data-enhanced]_&]:[&::-webkit-search-cancel-button]:appearance-none" />
<Button variant="ghost" size="icon" aria-label={clearLabel} aria-controls={id} data-search-clear hidden
class="absolute inset-e-1 top-1/2 size-7 -translate-y-1/2 text-muted-foreground [&[hidden]]:hidden group-has-[input:disabled]:hidden group-has-[input[readonly]]:hidden">
<X aria-hidden="true" class="size-3.5" />
</Button>
</div>
{submitLabel && <Button type="submit" variant="secondary">{submitLabel}</Button>}
</div>
</div>
<script>
function enhance(root: HTMLElement) {
if (root.hasAttribute("data-enhanced")) return;
const input = root.querySelector<HTMLInputElement>("input[type=search]");
const clear = root.querySelector<HTMLButtonElement>("[data-search-clear]");
if (!input || !clear) return;
const sync = () => { clear.hidden = !input.value; };
clear.addEventListener("click", () => {
if (input.matches(":disabled, [readonly]")) return;
input.value = "";
input.focus();
input.dispatchEvent(new Event("input", { bubbles: true }));
input.dispatchEvent(new Event("change", { bubbles: true }));
});
input.addEventListener("input", sync);
input.addEventListener("change", sync);
input.form?.addEventListener("reset", () => queueMicrotask(sync));
sync();
root.setAttribute("data-enhanced", "");
}
const init = () => document.querySelectorAll<HTMLElement>("[data-haze-search-field]").forEach(enhance);
init();
if (!document.documentElement.hasAttribute("data-haze-search-field-listener")) {
document.documentElement.setAttribute("data-haze-search-field-listener", "");
document.addEventListener("astro:page-load", init);
}
</script>When to use
- A catalog or list needs a clearable query field.
- A native GET form should submit text to a search results page.
When not to use
- The user must choose a valid option from a list of suggestions.
Installation
Add the Haze registry to components.json once, then install Search field with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/search-fieldnpx shadcn@latest add @haze/search-fieldyarn dlx shadcn@latest add @haze/search-fieldbunx --bun shadcn@latest add @haze/search-fieldFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| id* | string | – | Unique ID for the input and its label association. |
| name | string | id | Submitted query parameter name. |
| label | string | Search | Accessible name of the field. |
| hideLabel | boolean | false | Keeps the label available to assistive technology while hiding it visually. |
| placeholder | string | – | Short hint for the empty query. |
| value | string | – | Initial query text. |
| clearLabel | string | Clear search | Accessible name of the clear button. |
| submitLabel | string | – | When set, renders a submit button in the same row as the field. |
| class | string | – | Classes on the field wrapper. |
Keyboard interactions
- Enter
- Submit the query in a search form.
- EnterSpace
- Activate the focused clear button.
Accessibility
- Clearing the field sends input and change events and returns focus to the query.
- The field works as a native search control when scripts are unavailable.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
Install and use
Install the Haze UI component "Search field" with `npx shadcn@latest add @haze/search-field`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "http://localhost:4329/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import SearchField from "@/components/haze/ui/search-field.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: http://localhost:4329/ui/search-field.md
Customize with real content
Adapt the Haze search-field ui for our product. Replace the demo values of these props with our real content: id, name, label, hideLabel, placeholder, value, clearLabel, submitLabel, class. Keep the layout, the heading level rules and these accessibility details: Clearing the field sends input and change events and returns focus to the query. The field works as a native search control when scripts are unavailable. Do not add raw color classes; use the Haze tokens.
Choose the right block
Before using search-field, check it fits: use it when a catalog or list needs a clearable query field.; a native get form should submit text to a search results page.. Avoid it when the user must choose a valid option from a list of suggestions.. Alternatives: @haze/input-group, @haze/command-menu, @haze/combobox.
Dependencies
- npm: @lucide/astro
- @haze/button
- @haze/input
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.