Combobox
A field that filters a list as you type, and takes one of its rows.
Installation
pnpm dlx @human-kit/design add comboboxnpx @human-kit/design add comboboxyarn dlx @human-kit/design add comboboxbunx @human-kit/design add comboboxThe command copies the files to src/lib/components/ui/combobox. It also
installs the packages that the files import.
Usage
<script lang="ts">
import * as Combobox from '$lib/components/ui/combobox';
let doctor = $state<string | number | null | (string | number)[]>(null);
</script>
<Combobox.Root aria-label="Doctor" placeholder="Search a doctor" bind:value={doctor}>
<Combobox.Item id="ramirez">Dr. Ana Ramírez</Combobox.Item>
<Combobox.Item id="ortega">Dr. Luis Ortega</Combobox.Item>
</Combobox.Root> Examples
In a field
Inside a Field.Root, pass its aria-labelledby and aria-describedby so the label names the combobox and a screen reader reads the hint or the error with it. invalid turns the ring red.
Only labs with a signed agreement.
A long list
For thousands of rows, give the array as items on the root, a snippet that draws one row, and virtualizer: the combobox filters the array itself and the list draws only the rows near its viewport. Rows are one height, 24px, the rowHeight it is told. The arrows, Page Up, Page Down, Home and End walk the whole filtered list, and the list says its full count to a screen reader.
Guidelines
- Use a combobox to choose one value from a list too long to scan: doctors, labs, countries. For fewer than ten options, a select is quicker to read.
- The value must be one of the rows. When any typed text is a fair answer and the list only suggests, use the autocomplete.
- Use the
defaultvariant in a form,ghostin a table cell or a toolbar, andunderlinein a form drawn with lines. - Match the
sizeto the buttons beside it, so the row lines up. - Write the
placeholderas what to type, "Search a doctor", not as a label: it goes when the typing starts. - Give
itemswhen the value is set before the list has opened, so the input can show the name of the chosen row.
Accessibility
- The input is a
role="combobox"that points at the list witharia-activedescendant: the focus stays in the input while the arrows move through the rows. - Down and Up open the list and move through it, Home, End, Page Up and Page Down jump, and Enter takes the row. Escape closes the list, and in a dialog it closes only the list.
- The open button and the clear button take no tab stop: the keys above already do their work.
- A hidden live region says how many rows match while the list is open.
- Name it with
aria-label, or witharia-labelledbyfrom aField.Root.toggleLabelnames the open button andlistLabelthe list.
API reference
Combobox.Root
| Prop | Type | Default |
|---|---|---|
value | string | number | null | (string | number)[] The | null |
defaultValue | string | number | null | (string | number)[] The value at the start, when no | — |
onChange | (value: string | number | null | (string | number)[]) => void Runs when a row is taken or the value is cleared. | — |
inputValue | string The text in the input. Bindable. | — |
onInputChange | (value: string) => void Runs as the text changes. | — |
open | boolean Whether the list shows. Bindable. | — |
onOpenChange | (open: boolean) => void Runs when the list opens or closes. | — |
variant | 'default' | 'filled' | 'ghost' | 'underline' The look of the box, as the input has. | 'default' |
size | 'sm' | 'default' | 'lg' The height, matched to the buttons of each size. | 'default' |
placeholder | string What the empty input shows. | — |
items | T[] The rows as data, so the name of a chosen value is known before the list opens. | — |
trigger | 'focus' | 'input' | 'press' What opens the list: the focus, typing, or a press on the box. | 'press' |
filter | ComboBoxFilter | null How the text filters the rows. | — |
selectionMode | 'single' | 'multiple' One value, or an array of them. | 'single' |
closeOnSelect | boolean Closes the list after a row is taken. On by default in the single mode, off in the multiple. | — |
emptyText | string What the list says when the text matches nothing. | 'No results' |
invalid | boolean Marks the value as refused, for a | false |
pending | boolean Marks the root busy with | false |
disabled | boolean Turns the combobox off. | false |
readonly | boolean Shows the value and lets it be read, but not changed. | false |
aria-describedby | string The id of the hint or the error under the field. | — |
toggleLabel | string The name of the open button, read aloud. | 'Show options' |
listLabel | string The name of the list, read aloud. | — |
virtualizer | { rowHeight?: number; overscan?: number } Draws only the rows near the viewport. Needs | — |
class styles the box. The root takes only these props: give the input its name with aria-label or aria-labelledby.
Combobox.Item
| Prop | Type | Default |
|---|---|---|
id | string | number What the value holds when this row is taken. Required. | — |
textValue | string The text the filter and the input use, when the row shows more than its name. | — |
disabled | boolean Keeps the row in the list but stops it being taken. | false |
onAction | ComboBoxItemActionHandler Makes the row an action that runs instead of being chosen, as "Add a new doctor". | — |
closeOnAction | boolean Closes the list after | — |
Plus every attribute of a <div>. The text of the row is what the filter matches, unless textValue says otherwise.