# Combobox

A filtered select, single or multiple, with a typed input.

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

## Installation

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

Also adds `fields`, `badge`, `kbd`, `button`, `popover`, `menu`, `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="cb-country">Country</label>
  <ui-combobox>
    <input type="hidden" name="country" data-combobox-slot="value" value="" />
    <div data-combobox-slot="control">
      <input
        id="cb-country"
        type="text"
        placeholder="Select a country..."
        role="combobox"
        aria-expanded="false"
        aria-autocomplete="list"
        autocomplete="off"
      />
      <button
        type="button"
        data-combobox-slot="trigger"
        tabindex="-1"
        aria-label="Show options"
      ></button>
    </div>
    <div
      data-combobox-slot="panel"
      data-popover-side="bottom"
      data-popover-align="start"
      popover="manual"
    >
      <ul role="listbox" data-combobox-slot="list" aria-label="Countries">
        <li
          role="option"
          data-combobox-slot="item"
          data-ui="button"
          data-button-variant="ghost"
          data-combobox-value="au"
          style="--justify: start"
        >
          Australia
        </li>
        <li
          role="option"
          data-combobox-slot="item"
          data-ui="button"
          data-button-variant="ghost"
          data-combobox-value="ca"
          style="--justify: start"
        >
          Canada
        </li>
        <li
          role="option"
          data-combobox-slot="item"
          data-ui="button"
          data-button-variant="ghost"
          data-combobox-value="jp"
          style="--justify: start"
        >
          Japan
        </li>
        <li
          role="option"
          data-combobox-slot="item"
          data-ui="button"
          data-button-variant="ghost"
          data-combobox-value="nz"
          style="--justify: start"
        >
          New Zealand
        </li>
        <li
          role="option"
          data-combobox-slot="item"
          data-ui="button"
          data-button-variant="ghost"
          data-combobox-value="us"
          style="--justify: start"
        >
          United States
        </li>
      </ul>
      <p
        data-combobox-slot="empty"
        style="
          --font-size: var(--font-size-sm);
          --leading: var(--leading-sm);
          --tracking: var(--tracking-sm);
          --text: var(--color-muted-foreground);
        "
      >
        No matches.
      </p>
    </div>
  </ui-combobox>
</div>
```

### Multiselect

```html
<div data-ui="field" style="--max-w: 96">
  <label data-field-slot="label" for="cb-tags">Tags</label>
  <ui-combobox data-combobox-variant="multiselect" data-combobox-label-remove="Remove {label}">
    <!-- Pre-selected rows carry aria-selected="true"; the authored input holds
         the first value and combobox.js stamps a same-name sibling per extra -->
    <input type="hidden" name="tags" data-combobox-slot="value" value="design" />
    <div data-combobox-slot="control">
      <!-- Optional: the tag blueprint, cloned per selection. Omit it and the
           kit's default applies; keep it to restyle tags in HTML -->
      <template data-combobox-slot="tag-template">
        <span data-ui="badge" data-combobox-slot="tag">
          <span data-combobox-slot="tag-label"></span>
          <button type="button" tabindex="-1" data-combobox-slot="tag-remove"></button>
        </span>
      </template>

      <input
        id="cb-tags"
        type="text"
        placeholder="Add tags..."
        role="combobox"
        aria-expanded="false"
        aria-autocomplete="list"
        autocomplete="off"
        hidden
        aria-hidden="true"
      />
      <button
        type="button"
        data-combobox-slot="trigger"
        tabindex="-1"
        aria-label="Show options"
      ></button>
    </div>
    <div
      data-combobox-slot="panel"
      data-popover-side="bottom"
      data-popover-align="start"
      popover="manual"
    >
      <ul role="listbox" aria-multiselectable="true" data-combobox-slot="list" aria-label="Tags">
        <li
          role="option"
          aria-selected="true"
          data-combobox-slot="item"
          data-ui="button"
          data-button-variant="ghost"
          data-combobox-value="design"
          style="--justify: start"
        >
          Design
        </li>
        <li
          role="option"
          aria-selected="true"
          data-combobox-slot="item"
          data-ui="button"
          data-button-variant="ghost"
          data-combobox-value="engineering"
          style="--justify: start"
        >
          Engineering
        </li>
        <li
          role="option"
          aria-selected="false"
          data-combobox-slot="item"
          data-ui="button"
          data-button-variant="ghost"
          data-combobox-value="marketing"
          style="--justify: start"
        >
          Marketing
        </li>
        <li
          role="option"
          aria-selected="false"
          data-combobox-slot="item"
          data-ui="button"
          data-button-variant="ghost"
          data-combobox-value="research"
          style="--justify: start"
        >
          Research
        </li>
        <li
          role="option"
          aria-selected="false"
          data-combobox-slot="item"
          data-ui="button"
          data-button-variant="ghost"
          data-combobox-value="support"
          style="--justify: start"
        >
          Support
        </li>
      </ul>
      <p
        data-combobox-slot="empty"
        style="
          --font-size: var(--font-size-sm);
          --leading: var(--leading-sm);
          --tracking: var(--tracking-sm);
          --text: var(--color-muted-foreground);
        "
      >
        No matches.
      </p>
    </div>
  </ui-combobox>
</div>
```

## Behavior

`primitives/combobox/combobox.js`

`<ui-combobox>`: an input restricted to a predefined list.
Light-DOM custom element on the shared typeahead engine
(`base/typeahead.ts`). A relative of select: the panel filters as you type,
but free text can never submit. The form value lives in an authored hidden
input (`data-combobox-slot="value"`), synced whenever the selection changes.

The visible input carries a **display value, not the filter**. Committing
writes the option label into it and clears the query, so reopening shows
the full list with the committed row checked, matching `[data-ui~="select"]`.
Opening selects that label so the first keystroke replaces it, and focus
alone never opens the panel (a click, the chevron, an arrow key or typing
does). On blur, stray text reverts to the committed label and cleared text
clears the selection.

`data-combobox-variant="multiselect"` selects a set instead: committing toggles a row
and keeps the panel open, and every selected value renders as a removable
tag stamped inside the control. The tag markup is HTML, not script: author
a `<template data-combobox-slot="tag-template">` to control its classes and
structure, and this file fills in the label, the value, and the remove
button accessible name. Without one, the badge default below applies.
Backspace on an empty input drops the last tag. Options carrying
`aria-selected="true"` are the single source of truth: the tags and the
hidden inputs are both derived from them in DOM order so nothing can drift.
The form submits repeated `name` pairs like `<select multiple>`: the authored
hidden input carries the first value, and stamped siblings carry the rest.

Where a no-JS fallback matters, prefer `[data-ui~="select"]` / `<ui-multiselect>`:
this control is inert without its script (the hidden input still submits a
server-set value, but no tags render).

Scripts read and set the selection through the element's `value` property,
which mirrors `<select>.value`: setting it moves the selection, the label,
and the hidden input without focusing the input or dispatching `change`; an
unknown value clears the selection.

Attributes on the root: `data-combobox-variant="multiselect"`, `data-combobox-sort="score"`,
`data-combobox-filter="none"` (another source filters; the engine only navigates),
`data-combobox-min-length="<n>"`, `data-combobox-label-remove="Remove {label}"`.
Parts: `combobox-value` (hidden input), `combobox-control` (select-look
shell), `combobox-tag-template` (authored `<template>`), `combobox-tag` /
`combobox-tag-label` / `combobox-tag-remove` (cloned from it),
`combobox-trigger` (chevron), `combobox-panel`, `combobox-list`,
`combobox-item`, `combobox-group` / `combobox-group-label`, `combobox-empty`.

## Attributes

| Attribute | Kind | Values |
| --- | --- | --- |
| `data-ui="combobox"` | identity | free value |
| `<ui-combobox>` | tag | free value |
| `data-combobox-variant` | preset | `multiselect` |
| `data-combobox-slot` | slot | `control` `empty` `group` `group-label` `item` `list` `panel` `tag` `tag-label` `tag-remove` `tag-template` `trigger` `value` |
| `data-combobox-state` | state | `highlighted` |
| `data-combobox-filter` | config | free value |
| `data-combobox-keywords` | config | free value |
| `data-combobox-label-remove` | config | free value |
| `data-combobox-min-length` | config | free value |
| `data-combobox-sort` | config | free value |
| `data-combobox-value` | config | free value |

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

## Hooks

| Hook | Default | State |
| --- | --- | --- |
| `--ui-combobox-border-color` | `var(--ui-select-border-color)` | — |
| `--ui-combobox-border-style` | `var(--ui-select-border-style)` | — |
| `--ui-combobox-border-width` | `var(--ui-select-border-width)` | — |
| `--ui-combobox-control-flex-wrap` | `nowrap` | — |
| `--ui-combobox-control-gap` | `var(--ui-field-gap)` | — |
| `--ui-combobox-control-h` | `var(--ui-field-h)` | — |
| `--ui-combobox-control-items` | `center` | — |
| `--ui-combobox-control-min-h` | `auto` | — |
| `--ui-combobox-control-py` | `0` | — |
| `--ui-combobox-corner-shape` | `var(--ui-select-corner-shape)` | — |
| `--ui-combobox-icon-color` | `var(--ui-select-picker-icon-color)` | — |
| `--ui-combobox-icon-mask` | `var(--ui-select-picker-icon-mask)` | — |
| `--ui-combobox-icon-size` | `var(--ui-select-picker-icon-size)` | — |
| `--ui-combobox-input-min-h` | `auto` | — |
| `--ui-combobox-input-min-w` | `0` | — |
| `--ui-combobox-min-w` | `var(--ui-select-min-w)` | — |
| `--ui-combobox-option-gap` | `var(--ui-field-option-gap)` | — |
| `--ui-combobox-panel-max-h` | `calc(var(--spacing) * 72)` | — |
| `--ui-combobox-pl` | `var(--ui-select-pl)` | — |
| `--ui-combobox-pr` | `var(--ui-select-pr)` | — |
| `--ui-combobox-radius` | `var(--ui-select-radius)` | — |
| `--ui-combobox-shadow` | `var(--ui-menu-shadow)` | — |
| `--ui-combobox-tag-gap` | `var(--spacing)` | — |
| `--ui-combobox-tag-max-w` | `100%` | — |
| `--ui-combobox-tag-py` | `var(--spacing)` | — |
| `--ui-combobox-tag-remove-color` | `currentColor` | — |
| `--ui-combobox-tag-remove-corner-shape` | `var(--radius-corner-shape)` | — |
| `--ui-combobox-tag-remove-mask` | `url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 256 256'%3E%3Crect width='256' height='256' fill='none'/%3E%3Cline x1='200' y1='56' x2='56' y2='200' stroke='currentColor' stroke-linecap='round' stroke-linejoin='round' stroke-width='16'/%3E%3Cline x1='200' y1='200' x2='56' y2='56' stroke='currentColor' stroke-linecap='round' stroke-linejoin='round' stroke-width='16'/%3E%3C/svg%3E")` | — |
| `--ui-combobox-tag-remove-opacity` | `0.65` | — |
| `--ui-combobox-tag-remove-opacity--hover` | `1` | hover |
| `--ui-combobox-tag-remove-radius` | `var(--radius-sm)` | — |
| `--ui-combobox-tag-remove-size` | `var(--ui-badge-icon-size)` | — |
| `--ui-combobox-w` | `var(--ui-select-w)` | — |
