Listbox
A list of options that holds focus itself (APG listbox): one <ul role="listbox"> of Options, whose aria-activedescendant points at the active option while the list has focus. It is the picker option model shown in the page — typeahead, groups, disabled options and the option look of Dropdown and Combobox. Use List with selectable (selectionMode) instead when each item needs real focus, actions of its own or interactive content.
- **Focus.** One tab stop. The active option exists only while the list has focus, and a switch to another window keeps it: the selected option (with multiselect, the first selected one in list order), else the first. While no option can be active (an empty list, every option hidden, or every option disabled without disabledOptionsFocusable), and while the active option is scrolled out of the list's view, the list draws its own focus ring. A press on an option with the primary button focuses the list and makes that option active without scrolling, so the click selects it even while the selected option is scrolled out of view; a right or middle press is left to the browser. autoFocus focuses the list when it mounts.
- **Keys.** ArrowDown and ArrowUp move without wrapping, Home and End go to the first and last option, PageUp and PageDown move ten options, and typing moves to the matching option (typeahead). Space and Enter select the active option: the selection does not follow the active option. Tab, Escape and Alt+Arrow keys are left to the page, so an enclosing Dialog closes on Escape.
- **Selection.** value, defaultValue and onValueChange are Fluent's selectedOptions, defaultSelectedOptions and onOptionSelect under WaveUI's value naming. A single-select Listbox keeps the selected option when it is selected again (no deselection). multiselect turns the value into an array: options draw a checkbox, Space, Enter and a click toggle an option, the value keeps selection order, and every toggle the user makes is announced (labels.added/labels.removed; never a controlled change or a form reset). A multi-select list also has the range keys of the APG pattern: Shift+ArrowDown and Shift+ArrowUp move and toggle the next or previous option, Shift+Space selects the options from the most recently selected one to the active one, Ctrl+Shift+Home and Ctrl+Shift+End select up to the first or last option and move there, and Ctrl+A (Cmd+A) selects every enabled option, or deselects them when they all are selected (labels.rangeAdded/labels.rangeRemoved announce them). A non-literal multiselect={flag} is a type error: render two elements, one for each mode.
- **Disabled.** disabled takes the list out of the tab order (a click does not focus it either), dims it, takes pointer input off its options, selects nothing and submits nothing. Disabled options are skipped by the keys unless disabledOptionsFocusable, and are never selected.
- **Data mode.** items passes the options as data instead of children: the list renders an option per item (renderOption draws it, an Option with the item's label by default), the keys, typeahead and the selection follow the data, and each option carries aria-posinset and aria-setsize. A long list takes a virtualWindow (from useVirtualWindow({ itemSize })): the list then renders only the options in view, plus an overscan, with spacers for the others, every option itemSize tall, and the active option stays rendered. Items with a group are drawn under group headers (renderGroup), also in a window: each group is a role="group" named by its name, and the header is a row that no key reaches.
- **Forms and Field.** With name the value is submitted with its form (one entry, or one per value with multiselect), and required needs a value (native validation; a missing value moves focus to the list). A reset of its form restores defaultValue, with or without a name (a multiselect reset is compared by content, so restoring the same values reports nothing). Inside a Field the list is labelled by its label, described by its message and hint, and takes its invalid and required state; otherwise name it with aria-label or aria-labelledby (development warns once when it has no name).
- It has no built-in height: give it one through className and it scrolls.
Every prop, the ref and the handlers go to the <ul>. Sub-components: Listbox.Option, Listbox.OptionGroup. React Server Components import the flat names ListboxOption / ListboxOptionGroup (dotted access needs a client file).
Examples
Default
- Apple
- Banana
- Cherry
- Date
export const Default: Story = {
args: {
defaultValue: 'banana',
},
render: (args) => (
<Listbox {...args}>
<Listbox.Option value="apple">Apple</Listbox.Option>
<Listbox.Option value="banana">Banana</Listbox.Option>
<Listbox.Option value="cherry">Cherry</Listbox.Option>
<Listbox.Option value="date">Date</Listbox.Option>
</Listbox>
),
};Multiselect
- Apple
- Banana
- Cherry
- Date
export const Multiselect: StoryObj<ListboxProps<true>> = {
args: {
'aria-label': 'Fruits',
multiselect: true,
defaultValue: ['banana'],
},
// The spread alone leaves `multiselect` optional (StoryObj widens `args`), which satisfies
// neither call signature (D9): the literal attribute after it forces the multi-select one.
render: (args) => (
<Listbox {...args} multiselect>
<Listbox.Option value="apple">Apple</Listbox.Option>
<Listbox.Option value="banana">Banana</Listbox.Option>
<Listbox.Option value="cherry">Cherry</Listbox.Option>
<Listbox.Option value="date">Date</Listbox.Option>
</Listbox>
),
};Groups
- Mammals
- Cat
- Dog
- Horse
- Fish
- Goldfish
- Salmon
- Shark (not available)
export const Groups: Story = {
args: {
'aria-label': 'Animal',
},
render: (args) => (
<Listbox {...args} className="max-h-56">
<Listbox.OptionGroup label="Mammals">
<Listbox.Option value="cat">Cat</Listbox.Option>
<Listbox.Option value="dog">Dog</Listbox.Option>
<Listbox.Option value="horse">Horse</Listbox.Option>
</Listbox.OptionGroup>
<Listbox.OptionGroup label="Fish">
<Listbox.Option value="goldfish">Goldfish</Listbox.Option>
<Listbox.Option value="salmon">Salmon</Listbox.Option>
<Listbox.Option value="shark" disabled>
Shark (not available)
</Listbox.Option>
</Listbox.OptionGroup>
</Listbox>
),
};Disabled Options
- Cat
- Dog (not available)
- Fish
export const DisabledOptions: Story = {
args: {
'aria-label': 'Animal',
disabledOptionsFocusable: true,
},
render: (args) => (
<Listbox {...args}>
<Listbox.Option value="cat">Cat</Listbox.Option>
<Listbox.Option value="dog" disabled>
Dog (not available)
</Listbox.Option>
<Listbox.Option value="fish">Fish</Listbox.Option>
</Listbox>
),
};Data Mode
- Ada Andersen
- Ada Berg
- Ada Costa
- Ada Dahl
- Ada Eriksen
- Ada Fischer
- Ada Garcia
- Ada Haugen
- Ada Ito
- Ada Johansen
- Ada Kim
- Ada Larsen
- Ada Moreau
- Ada Nilsen
- Ada Okafor
- Ada Pedersen
- Ada Rossi
- Ada Silva
- Ada Tanaka
- Ada Weber
- Ben Andersen
- Ben Berg
- Ben Costa
- Ben Dahl
- Ben Eriksen
- Ben Fischer
- Ben Garcia
- Ben Haugen
- Ben Ito
- Ben Johansen
- Ben Kim
- Ben Larsen
- Ben Moreau
- Ben Nilsen
- Ben Okafor
- Ben Pedersen
- Ben Rossi
- Ben Silva
- Ben Tanaka
- Ben Weber
- Chloe Andersen
- Chloe Berg
- Chloe Costa
- Chloe Dahl
- Chloe Eriksen
- Chloe Fischer
- Chloe Garcia
- Chloe Haugen
- Chloe Ito
- Chloe Johansen
export const DataMode: Story = {
args: {
'aria-label': 'Person',
defaultValue: 'person-32',
items: PEOPLE.slice(0, 50),
className: 'max-h-72',
},
argTypes: {
items: { control: false },
renderOption: { control: false },
virtualWindow: { control: false },
},
};Ten Thousand Options
- Ada Andersen (Accra)
- Ada Andersen (Austin)
- Ada Andersen (Bergen)
- Ada Andersen (Berlin)
- Ada Andersen (Bogota)
- Ada Andersen (Cairo)
- Ada Andersen (Dakar)
- Ada Andersen (Dublin)
- Ada Andersen (Hanoi)
- Ada Andersen (Kyoto)
- Ada Andersen (Lagos)
- Ada Andersen (Lima)
- Ada Andersen (Lisbon)
- Ada Andersen (Madrid)
- Ada Andersen (Nairobi)
- Ada Andersen (Oslo)
- Ada Andersen (Paris)
- Ada Andersen (Quito)
- Ada Andersen (Seoul)
- Ada Andersen (Vienna)
- Ada Berg (Accra)
- Ada Berg (Austin)
- Ada Berg (Bergen)
Mounted options: 0 of 10,000
export const TenThousandOptions: StoryObj<TenThousandOptionsArgs> = {
args: {
'aria-label': 'Person',
defaultValue: 'person-5000',
itemSize: 32,
className: 'h-72',
style: { width: 260 },
},
argTypes: {
items: { control: false },
renderOption: { control: false },
virtualWindow: { control: false },
// Heights an option fits in; a smaller one would overlap the options.
itemSize: { control: 'select', options: [32, 40, 48] },
},
render: (args) => <TenThousandListbox {...args} />,
};Ten Thousand Grouped Options
- Ada Andersen (Accra)
- Ada Andersen (Austin)
- Ada Andersen (Bergen)
- Ada Andersen (Berlin)
- Ada Andersen (Bogota)
- Ada Andersen (Cairo)
- Ada Andersen (Dakar)
- Ada Andersen (Dublin)
- Ada Andersen (Hanoi)
- Ada Andersen (Kyoto)
- Ada Andersen (Lagos)
- Ada Andersen (Lima)
- Ada Andersen (Lisbon)
- Ada Andersen (Madrid)
- Ada Andersen (Nairobi)
- Ada Andersen (Oslo)
- Ada Andersen (Paris)
- Ada Andersen (Quito)
- Ada Andersen (Seoul)
- Ada Andersen (Vienna)
- Ada Berg (Accra)
- Ada Berg (Austin)
Mounted options: 0 of 10,000
export const TenThousandGroupedOptions: StoryObj<TenThousandOptionsArgs> = {
args: {
'aria-label': 'Person',
defaultValue: 'person-5000',
itemSize: 32,
className: 'h-72',
style: { width: 260 },
},
argTypes: {
items: { control: false },
renderOption: { control: false },
renderGroup: { control: false },
virtualWindow: { control: false },
itemSize: { control: 'select', options: [32, 40, 48] },
},
render: (args) => <TenThousandGroupedListbox {...args} />,
};In Field
- Apple
- Banana
- Cherry
Pick your favorite.
export const InField: Story = {
args: {
'aria-label': undefined,
},
render: (args) => (
<Field label="Fruit" hint="Pick your favorite." required>
<Listbox {...args}>
<Listbox.Option value="apple">Apple</Listbox.Option>
<Listbox.Option value="banana">Banana</Listbox.Option>
<Listbox.Option value="cherry">Cherry</Listbox.Option>
</Listbox>
</Field>
),
};Custom Picker
export const CustomPicker: Story = {
render: () => <FontPicker />,
};Command Palette
- New File
- Open File…
- Save File
- Close Tab
- Run Tests
Last command: none
export const CommandPalette: Story = {
render: () => <CommandPaletteWidget />,
};Props
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
multiselect | boolean | No | false | Several options can be selected (Fluent's |
value | readonly string[] | string | No | Controlled selected value ( | |
defaultValue | readonly string[] | string | No | '' (`[]` with `multiselect`) | Initial selected value for uncontrolled usage, which a form reset restores. |
onValueChange | ((value: string[]) => void) | ((value: string) => void) | No | Called with the new value when it changes (a new array with | |
onActiveOptionChange | (value: string | null) => void | No | Called after the active (highlighted) option changes — focus, arrow keys, typeahead, the pointer — and with | |
disabled | boolean | No | false | Takes the list out of the tab order (a click does not focus it either, as a disabled native |
autoFocus | boolean | No | false | Focuses the list when it mounts in the browser (React focuses only form controls itself), not while |
disabledOptionsFocusable | boolean | No | false | Keeps disabled options in the arrow-key, Home/End, PageUp/PageDown and typeahead order (the option that is active on focus may then be a disabled one too); they still cannot be selected (Space, Enter and a click do nothing). Unlike Fluent, which always keeps disabled options reachable, WaveUI skips them by default. |
labels | ListboxLabels | No | The | |
items | readonly ListboxItem[] | No | Data mode: the options as data, in list order ( | |
renderOption | (item: ListboxItem, state: ListboxOptionRenderState) => React.ReactNode | No | an `Option` with the item's `value`, `label` (also its text), `textValue` and `disabled` | Data mode: renders the option of an item, given its state ( |
renderGroup | (group: string, state: ListboxGroupRenderState) => React.ReactNode | No | the group's name, as text | Data mode: renders the header of a group of |
virtualWindow | VirtualWindow | No | Data mode: renders only the options in view of a long list, plus an overscan, and spacers ( The list element is the window's scroller: give it a bounded height (a height or a maximum height, through The window counts the shown options (the items that are not | |
name | string | No | Name of the value in form submissions (renders a hidden input inside the list; one per value with | |
form | string | No | Id of the form the value belongs to, when the Listbox is outside it. | |
required | boolean | No | A value is required to submit the form (native constraint validation); when validation finds it empty, focus moves to the list. | |
onKeyDown | React.KeyboardEventHandler<HTMLUListElement> | No | Called on a key press on the list, before the built-in keys; | |
ref | React.Ref<HTMLUListElement> | No | Ref to the |
Any other prop is passed to the element it renders (HTMLUListElement).
Parts
Listbox.OptionListbox.OptionGroup
Stable class names
| Part | Class |
|---|---|
root | wave-listbox |
spacer | wave-listbox__spacer |