Listbox
A list on the page to pick one row or several, with the keyboard or the pointer.
Installation
pnpm dlx @human-kit/design add listboxnpx @human-kit/design add listboxyarn dlx @human-kit/design add listboxbunx @human-kit/design add listboxThe command copies the files to src/lib/components/ui/listbox. It also
installs the packages that the files import.
Usage
<script lang="ts">
import * as Listbox from '$lib/components/ui/listbox';
let clinic = $state(['Medellín']);
</script>
<Listbox.Root aria-label="Clinic" bind:value={clinic}>
<Listbox.Item id="Bogotá">Bogotá</Listbox.Item>
<Listbox.Item id="Medellín">Medellín</Listbox.Item>
<Listbox.Item id="Cali">Cali</Listbox.Item>
</Listbox.Root> Examples
Several rows
With selectionMode="multiple" each press adds or takes away a row, so a plain click never wipes the rest. Shift picks a range. A disabled row stays in view and cannot be chosen.
A long list
Give the rows as items with a row snippet, and virtualizer draws only the ones near the viewport. Give the list a height, and getItemKey, so a Shift range reaches the rows that are not drawn.
Guidelines
- Use a listbox when the choices should stay in view: a short list in a panel, a filter beside a table. When they can hide until asked for, use a
Select.Rootor aCombobox.Root. - Use
outlinewhere the list stands on the page,ghostinside a card or a panel that already frames it. - Keep rows to one short line each. A row cut by an ellipsis hides what tells it apart.
- Turn on
typeaheadfor a list longer than a screen, so a letter jumps to the rows that start with it. - The value is always a set of ids, one or several. Read it with
bind:value, or withonChangeto refuse a change.
Accessibility
- It renders
role="listbox"with arole="option"per row, andaria-multiselectablein the multiple mode. - The list is one tab stop. The arrow keys move through the rows, Home and End jump to the ends, and Space or Enter chooses the focused row.
- With several rows, Shift with an arrow, Home or End extends a range, and Ctrl or Cmd with A chooses all.
- It needs a name:
aria-label, oraria-labelledbypointing at a visible heading. - In a virtualized list the rows carry
aria-setsizeandaria-posinset, so a screen reader announces the real count, not the rows drawn.
API reference
Listbox.Root
| Prop | Type | Default |
|---|---|---|
variant | 'outline' | 'filled' | 'ghost' A card of its own around the rows, a soft grey, or the rows bare. | 'outline' |
selectionMode | 'single' | 'multiple' One row, or several. | 'single' |
selectionBehavior | 'toggle' | 'replace'
| 'replace' (single), 'toggle' (multiple) |
value | Iterable<string | number> The ids of the chosen rows. Bindable. | — |
defaultValue | Iterable<string | number> The rows chosen at the start, when you give no | — |
onChange | (value: Set<string | number>) => void Called when the selection changes. | — |
controlledValue | boolean Stops writing back to | false |
disabledKeys | Iterable<string | number> The ids of the rows that cannot be chosen. | — |
typeahead | boolean A typed letter moves the focus to the row whose text starts with it. | false |
loop | boolean The arrows go from the last row to the first, and back. | false |
items | Iterable<T> The rows as an array, drawn by a | — |
getItemKey | (item: T) => string | number Reads the id of an item. Needed with | — |
virtualizer | { rowHeight?: number; overscan?: number } Draws only the rows near the viewport. Works with | — |
emptyPlaceholder | string | Snippet What it shows while it has no rows. | — |
header | Snippet Rows above the items, such as a "Create …" action. | — |
children | Snippet | Snippet<[T]> The rows, or with | — |
element | HTMLElement The rendered element. Bindable. | — |
Plus every attribute of a <div>, but role.
Listbox.Item
| Prop | Type | Default |
|---|---|---|
id | string | number What the row stands for in the value. Required. | — |
disabled | boolean The row stays in view and cannot be chosen. | false |
textValue | string The text the typeahead matches, when the content is not plain text. | — |
Plus every attribute of a <div>.