Skip to the content
human-kit design
26 — Pickers

Listbox

A list on the page to pick one row or several, with the keyboard or the pointer.

Bogotá
Medellín
Cali
Barranquilla
Cartagena

Installation

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

The 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.

Monday
Tuesday
Wednesday
Thursday
Friday

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.

Z0000 · Procedure 1
Z0001 · Procedure 2
Z0002 · Procedure 3
Z0003 · Procedure 4
Z0004 · Procedure 5
Z0005 · Procedure 6
Z0006 · Procedure 7
Z0007 · Procedure 8
Z0008 · Procedure 9
Z0009 · Procedure 10
Z0010 · Procedure 11
Z0011 · Procedure 12
Z0012 · Procedure 13
Z0013 · Procedure 14
Z0014 · Procedure 15
Z0015 · Procedure 16
Z0016 · Procedure 17
Z0017 · Procedure 18
Z0018 · Procedure 19
Z0019 · Procedure 20

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.Root or a Combobox.Root.
  • Use outline where the list stands on the page, ghost inside 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 typeahead for 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 with onChange to refuse a change.

Accessibility

  • It renders role="listbox" with a role="option" per row, and aria-multiselectable in 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, or aria-labelledby pointing at a visible heading.
  • In a virtualized list the rows carry aria-setsize and aria-posinset, so a screen reader announces the real count, not the rows drawn.

API reference

Listbox.Root

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

toggle: a press adds or takes away a row. replace: a press chooses that row alone.

'replace' (single), 'toggle' (multiple)
valueIterable<string | number>

The ids of the chosen rows. Bindable.

—
defaultValueIterable<string | number>

The rows chosen at the start, when you give no value.

—
onChange(value: Set<string | number>) => void

Called when the selection changes.

—
controlledValueboolean

Stops writing back to value: the parent decides through onChange, and can refuse a change.

false
disabledKeysIterable<string | number>

The ids of the rows that cannot be chosen.

—
typeaheadboolean

A typed letter moves the focus to the row whose text starts with it.

false
loopboolean

The arrows go from the last row to the first, and back.

false
itemsIterable<T>

The rows as an array, drawn by a children snippet that receives one item.

—
getItemKey(item: T) => string | number

Reads the id of an item. Needed with virtualizer.

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

Draws only the rows near the viewport. Works with items only, and the rows must share one height.

—
emptyPlaceholderstring | Snippet

What it shows while it has no rows.

—
headerSnippet

Rows above the items, such as a "Create …" action.

—
childrenSnippet | Snippet<[T]>

The rows, or with items the snippet that draws one.

—
elementHTMLElement

The rendered element. Bindable.

—

Plus every attribute of a <div>, but role.

Listbox.Item

PropTypeDefault
idstring | number

What the row stands for in the value. Required.

—
disabledboolean

The row stays in view and cannot be chosen.

false
textValuestring

The text the typeahead matches, when the content is not plain text.

—

Plus every attribute of a <div>.