Autocomplete
A list always in sight, with a search row at its head that filters it as you type.
Installation
pnpm dlx @human-kit/design add autocompletenpx @human-kit/design add autocompleteyarn dlx @human-kit/design add autocompletebunx @human-kit/design add autocompleteThe command copies the files to src/lib/components/ui/autocomplete. It also
installs the packages that the files import.
Usage
<script lang="ts">
import * as Autocomplete from '$lib/components/ui/autocomplete';
let value = $state(new Set<string | number>());
</script>
<Autocomplete.Root>
<Autocomplete.Input aria-label="Search tests" placeholder="Search tests…" />
<Autocomplete.List aria-label="Tests" {value} onChange={(next) => (value = next)}>
<Autocomplete.Item id="ferritin">Ferritin</Autocomplete.Item>
<Autocomplete.Item id="vitamin-d">Vitamin D</Autocomplete.Item>
<Autocomplete.Empty>No tests match</Autocomplete.Empty>
</Autocomplete.List>
</Autocomplete.Root> Examples
Filtered by a server
With filter={null} the list stops filtering on its own and shows every row it is given. Bind inputValue and fetch the rows that match it.
A long list
For thousands of rows, give the array as items on the root, a snippet that draws one row, and virtualizer on the list: the root filters the array itself and the list draws only the rows near its viewport. Rows are one height, 24px, the rowHeight it is told. Put the empty text in the empty snippet, since the row snippet has no place for it.
Guidelines
- Use an autocomplete where the list is always in sight, in a side panel, a card or a popover of your own. For a field that opens a list, use a combobox.
- It has no card of its own: put it inside the box that holds it.
- Keep the default local filter for a list the page already has, up to a few hundred rows. Pass
filter={null}when a server does the search. - Use
selectionMode="multiple"on the list to pick several rows; each press then adds or takes away one. - Always add an
Autocomplete.Emptythat says what found nothing: "No tests match", not "No results".
Accessibility
- The input is a
searchboxand the list alistboxofoptions. The focus stays in the input: the arrows move a highlight through the rows, read througharia-activedescendant. ArrowDownandArrowUpmove the highlight,PageDownandPageUpmove it a page,Enterpicks the highlighted row andEscapeclears the search.- A hidden live region says how many rows the search left, each time the search changes.
- Name the input and the list, with
aria-labeloraria-labelledby: a placeholder is not a name. - A press anywhere on the search row lands in the input, not only on its text.
API reference
Autocomplete.Root
| Prop | Type | Default |
|---|---|---|
inputValue | string The search text. Bindable. | — |
defaultInputValue | string The search text at the start, when you give no | — |
onInputChange | (value: string) => void Called when the search text changes. | — |
filter | ((textValue: string, inputValue: string) => boolean) | null Which rows the search keeps. By default, the rows that contain the text, in any case. | — |
autoHighlight | boolean Highlights the first row that matches each time the search changes. | true |
disabled | boolean Turns off the search and the rows. | false |
readonly | boolean Shows the list and the choice, and refuses changes. | false |
id | string The base of the ARIA ids inside. Give a stable one when the page is drawn on a server. | — |
It takes these props alone: put attributes on the input or the list.
Autocomplete.Input
| Prop | Type | Default |
|---|---|---|
aria-label | string The name of the search, when no visible label gives one. | — |
aria-labelledby | string The id of the element that names the search. | — |
aria-describedby | string The id of the element that describes the search, such as a hint. | — |
Plus every attribute of an <input>, such as placeholder.
Autocomplete.List
| Prop | Type | Default |
|---|---|---|
selectionMode | 'single' | 'multiple' Whether one row can be chosen, or several. | 'single' |
selectionBehavior | 'replace' | 'toggle' What a press on a row does: replace the choice with it, or add it and take it away. | 'replace', 'toggle' for multiple |
value | Iterable<string | number> The ids of the chosen rows. | — |
defaultValue | Iterable<string | number> The rows chosen at the start, when you give no | — |
onChange | (value: Set<string | number>) => void Called with the chosen ids when the user picks a row. | — |
controlledValue | boolean Stops the list keeping a choice of its own: it only reports through | false |
disabledKeys | Iterable<string | number> The ids of the rows that cannot be picked. | — |
items | Iterable<T> The rows as an array. | — |
aria-label | string The name of the list. | — |
Plus every attribute of a <div>. The choice lives here, not on the root.
Autocomplete.Item
| Prop | Type | Default |
|---|---|---|
id | string | number Required. The id of the row, the one the choice holds. | — |
textValue | string The text the search compares. Without it, the text of the row. | — |
disabled | boolean Shows the row and keeps it from being picked. | false |
Plus every attribute of a <div>.
Autocomplete.Empty
| Prop | Type | Default |
|---|---|---|
children | Snippet What the row says. | 'No results' |
Plus every attribute of a <div>. It shows only while the search leaves no row.