# Autocomplete

A text input with suggestions filtered as you type.

Source: https://www.zazz.sh/api/primitives/autocomplete/

## Installation

```bash
pnpm dlx zazz-ui add autocomplete
```

Also adds `fields`, `kbd`, `button`, `popover`, `menu`, `input`, `select`, which it depends on.

Run `zazz-ui init` first if the project has no `zazz/` folder yet.

## Examples

### Default

```html
<div data-ui="field" style="--max-w: 96">
  <label data-field-slot="label" for="ac-fruit">Fruit</label>
  <ui-autocomplete>
    <input
      data-ui="input"
      id="ac-fruit"
      name="fruit"
      type="text"
      placeholder="Search fruits..."
      role="combobox"
      aria-expanded="false"
      aria-autocomplete="list"
      autocomplete="off"
    />
    <div
      data-autocomplete-slot="panel"
      data-popover-side="bottom"
      data-popover-align="start"
      popover="manual"
    >
      <ul role="listbox" data-autocomplete-slot="list" aria-label="Fruit suggestions">
        <li
          role="option"
          data-autocomplete-slot="item"
          data-ui="button"
          data-button-variant="ghost"
          data-autocomplete-value="Apple"
          style="--justify: start"
        >
          Apple
        </li>
        <li
          role="option"
          data-autocomplete-slot="item"
          data-ui="button"
          data-button-variant="ghost"
          data-autocomplete-value="Banana"
          style="--justify: start"
        >
          Banana
        </li>
        <li
          role="option"
          data-autocomplete-slot="item"
          data-ui="button"
          data-button-variant="ghost"
          data-autocomplete-value="Blueberry"
          style="--justify: start"
        >
          Blueberry
        </li>
        <li
          role="option"
          data-autocomplete-slot="item"
          data-ui="button"
          data-button-variant="ghost"
          data-autocomplete-value="Cherry"
          style="--justify: start"
        >
          Cherry
        </li>
        <li
          role="option"
          data-autocomplete-slot="item"
          data-ui="button"
          data-button-variant="ghost"
          data-autocomplete-value="Grape"
          style="--justify: start"
        >
          Grape
        </li>
      </ul>
      <p data-autocomplete-slot="empty" style="--text: var(--color-muted-foreground)">
        No results.
      </p>
    </div>
  </ui-autocomplete>
</div>
```

### Groups

```html
<div data-ui="field" style="--max-w: 96">
  <label data-field-slot="label" for="ac-produce">Produce</label>
  <ui-autocomplete data-autocomplete-sort="score">
    <input
      data-ui="input"
      id="ac-produce"
      name="produce"
      type="text"
      placeholder="Search produce..."
      role="combobox"
      aria-expanded="false"
      aria-autocomplete="list"
      autocomplete="off"
    />
    <div
      data-autocomplete-slot="panel"
      data-popover-side="bottom"
      data-popover-align="start"
      popover="manual"
    >
      <ul role="listbox" data-autocomplete-slot="list" aria-label="Produce suggestions">
        <li role="group" data-autocomplete-slot="group" aria-labelledby="ac-group-fruit">
          <span
            data-autocomplete-slot="group-label"
            id="ac-group-fruit"
            style="
              --display: inline-flex;
              --p: 2;
              --font-size: var(--font-size-eyebrow);
              --font-weight: strong;
              --font-weight: var(--font-weight-eyebrow);
              --leading: var(--leading-eyebrow);
              --tracking: var(--tracking-eyebrow);
              --text-transform: uppercase;
              --text: var(--color-muted-foreground);
            "
            >Fruit</span
          >
          <div
            role="option"
            data-autocomplete-slot="item"
            data-ui="button"
            data-button-variant="ghost"
            data-autocomplete-value="Apple"
            data-autocomplete-keywords="fruit red"
            style="--justify: start"
          >
            Apple
          </div>
          <div
            role="option"
            data-autocomplete-slot="item"
            data-ui="button"
            data-button-variant="ghost"
            data-autocomplete-value="Cherry"
            data-autocomplete-keywords="fruit red"
            style="--justify: start"
          >
            Cherry
          </div>
        </li>
        <li role="group" data-autocomplete-slot="group" aria-labelledby="ac-group-veg">
          <span
            data-autocomplete-slot="group-label"
            id="ac-group-veg"
            style="
              --display: inline-flex;
              --p: 2;
              --font-size: var(--font-size-eyebrow);
              --font-weight: strong;
              --font-weight: var(--font-weight-eyebrow);
              --leading: var(--leading-eyebrow);
              --tracking: var(--tracking-eyebrow);
              --text-transform: uppercase;
              --text: var(--color-muted-foreground);
            "
            >Vegetables</span
          >
          <div
            role="option"
            data-autocomplete-slot="item"
            data-ui="button"
            data-button-variant="ghost"
            data-autocomplete-value="Carrot"
            data-autocomplete-keywords="vegetable orange"
            style="--justify: start"
          >
            Carrot
          </div>
          <div
            role="option"
            data-autocomplete-slot="item"
            data-ui="button"
            data-button-variant="ghost"
            data-autocomplete-value="Cauliflower"
            data-autocomplete-keywords="vegetable white"
            style="--justify: start"
          >
            Cauliflower
          </div>
        </li>
      </ul>
      <p data-autocomplete-slot="empty" style="--text: var(--color-muted-foreground)">
        No results.
      </p>
    </div>
  </ui-autocomplete>
</div>
```

## Behavior

`primitives/autocomplete/autocomplete.js`

`<ui-autocomplete>`: an input that suggests options as you type.
Light-DOM custom element on the shared typeahead engine
(`base/typeahead.ts`). The visible input is the form value (free text is
allowed) and the anchored `popover="manual"` panel suggests matches ranked
by the vendored cmdk scorer. Committing a suggestion (Enter or click) fills
the input; without JavaScript the markup degrades to a plain `[data-ui~="input"]`.

Attributes on the root:
- `data-autocomplete-sort="score"`: re-rank visually by match score (default: DOM order).
- `data-autocomplete-filter="none"`: show every item; another source filters.
- `data-autocomplete-min-length="<n>"`: query length before the panel opens (default 0).

Parts: `autocomplete-panel` (popover="manual"), `autocomplete-list`
([role="listbox"]), `autocomplete-item` ([role="option"], `data-autocomplete-value`,
optional `data-autocomplete-keywords`), `autocomplete-group` / `autocomplete-group-label`,
`autocomplete-empty`.

## Attributes

| Attribute | Kind | Values |
| --- | --- | --- |
| `data-ui="autocomplete"` | identity | free value |
| `<ui-autocomplete>` | tag | free value |
| `data-autocomplete-slot` | slot | `empty` `group` `group-label` `item` `list` `panel` |
| `data-autocomplete-state` | state | `highlighted` |
| `data-autocomplete-filter` | config | free value |
| `data-autocomplete-keywords` | config | free value |
| `data-autocomplete-min-length` | config | free value |
| `data-autocomplete-sort` | config | free value |
| `data-autocomplete-value` | config | free value |

Requires: popover, button, fields, select, input.

## Hooks

| Hook | Default | State |
| --- | --- | --- |
| `--ui-autocomplete-panel-max-h` | `calc(var(--spacing) * 72)` | — |
| `--ui-autocomplete-shadow` | `var(--ui-menu-shadow)` | — |
