# Search field

> A labeled native search input with a leading icon and a clear button that restores focus to the field.

- Kind: ui · Category: Forms · Interactivity: script
- Install: `npx shadcn@latest add @haze/search-field`
- File: `src/components/haze/ui/search-field.astro`
- npm dependencies: @lucide/astro
- Registry dependencies: @haze/button, @haze/input, @haze/utils
- Docs page: https://hazeui.com/ui/search-field

## Use when
- A catalog or list needs a clearable query field.
- A native GET form should submit text to a search results page.

## Avoid when
- The user must choose a valid option from a list of suggestions.

## Alternatives
- [input-group](https://hazeui.com/ui/input-group.md)
- [command-menu](https://hazeui.com/ui/command-menu.md)
- [combobox](https://hazeui.com/ui/combobox.md)

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| id | `string` |  | yes | Unique ID for the input and its label association. |
| name | `string` | id | no | Submitted query parameter name. |
| label | `string` | Search | no | Accessible name of the field. |
| hideLabel | `boolean` | false | no | Keeps the label available to assistive technology while hiding it visually. |
| placeholder | `string` |  | no | Short hint for the empty query. |
| value | `string` |  | no | Initial query text. |
| clearLabel | `string` | Clear search | no | Accessible name of the clear button. |
| submitLabel | `string` |  | no | When set, renders a submit button in the same row as the field. |
| class | `string` |  | no | Classes on the field wrapper. |

## 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.
- Key Enter: Submit the query in a search form.
- Key Enter / Space: Activate the focused clear button.

## Agent prompts

### 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": "https://hazeui.com/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: https://hazeui.com/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.

## Example requests that should lead here
- Add a clearable search control to the library finder with q as the query parameter.

## Source

```astro
---
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>
```
