Skip to the content
human-kit design
23 — Pickers

Combobox

A field that filters a list as you type, and takes one of its rows.

Installation

pnpm dlx @human-kit/design add combobox
npx @human-kit/design add combobox
yarn dlx @human-kit/design add combobox
bunx @human-kit/design add combobox

The 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 default variant in a form, ghost in a table cell or a toolbar, and underline in a form drawn with lines.
  • Match the size to the buttons beside it, so the row lines up.
  • Write the placeholder as what to type, "Search a doctor", not as a label: it goes when the typing starts.
  • Give items when 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 with aria-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 with aria-labelledby from a Field.Root. toggleLabel names the open button and listLabel the list.

API reference

Combobox.Root

PropTypeDefault
valuestring | number | null | (string | number)[]

The id of the chosen row, or of the chosen rows in the multiple mode. Bindable.

null
defaultValuestring | number | null | (string | number)[]

The value at the start, when no value is bound.

—
onChange(value: string | number | null | (string | number)[]) => void

Runs when a row is taken or the value is cleared.

—
inputValuestring

The text in the input. Bindable.

—
onInputChange(value: string) => void

Runs as the text changes.

—
openboolean

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'
placeholderstring

What the empty input shows.

—
itemsT[]

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'
filterComboBoxFilter | null

How the text filters the rows. null turns the filter off, for rows a server has already filtered.

—
selectionMode'single' | 'multiple'

One value, or an array of them.

'single'
closeOnSelectboolean

Closes the list after a row is taken. On by default in the single mode, off in the multiple.

—
emptyTextstring

What the list says when the text matches nothing.

'No results'
invalidboolean

Marks the value as refused, for a Field.Root with an error.

false
pendingboolean

Marks the root busy with data-pending, while the rows load.

false
disabledboolean

Turns the combobox off.

false
readonlyboolean

Shows the value and lets it be read, but not changed.

false
aria-describedbystring

The id of the hint or the error under the field.

—
toggleLabelstring

The name of the open button, read aloud.

'Show options'
listLabelstring

The name of the list, read aloud.

—
virtualizer{ rowHeight?: number; overscan?: number }

Draws only the rows near the viewport. Needs items and a row snippet as children. rowHeight is 24; without it the first row is measured.

—

class styles the box. The root takes only these props: give the input its name with aria-label or aria-labelledby.

Combobox.Item

PropTypeDefault
idstring | number

What the value holds when this row is taken. Required.

—
textValuestring

The text the filter and the input use, when the row shows more than its name.

—
disabledboolean

Keeps the row in the list but stops it being taken.

false
onActionComboBoxItemActionHandler

Makes the row an action that runs instead of being chosen, as "Add a new doctor".

—
closeOnActionboolean

Closes the list after onAction runs.

—

Plus every attribute of a <div>. The text of the row is what the filter matches, unless textValue says otherwise.