Skip to content

Listbox

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

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

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

Submitted: nothing yet

export const CustomPicker: Story = {
  render: () => <FontPicker />,
};

Command Palette

  • New File
  • Open File…
  • Save File
  • Close Tab
  • Toggle Sidebar
  • Run Tests

Last command: none

export const CommandPalette: Story = {
  render: () => <CommandPaletteWidget />,
};

Props

NameTypeRequiredDefaultDescription
multiselectbooleanNofalse

Several options can be selected (Fluent's multiselect; List's selectionMode="multiple" is the other widget): the value is an array, options draw a checkbox, Space, Enter and a click toggle an option, and each toggle is announced. A non-literal multiselect={flag} is a type error: render two elements, one for each mode.

valuereadonly string[] | stringNo

Controlled selected value ('' for none), or with multiselect the selected values in selection order (Fluent's selectedOptions).

defaultValuereadonly string[] | stringNo'' (`[]` 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 multiselect; Fluent's onOptionSelect, under WaveUI's value/onValueChange naming). In single-select mode, selecting the selected option again changes nothing: the Listbox never deselects it.

onActiveOptionChange(value: string | null) => voidNo

Called after the active (highlighted) option changes — focus, arrow keys, typeahead, the pointer — and with null when focus leaves the list (a switch to another window keeps the active option). It reports what aria-activedescendant points at, from an effect once the change commits. Fluent's onActiveOptionChange reports neither the pointer (its options have no hover highlight) nor the loss of focus.

disabledbooleanNofalse

Takes the list out of the tab order (a click does not focus it either, as a disabled native <select>) and dims it: its options take no pointer input, nothing can be selected, and nothing is submitted or validated. Renders aria-disabled and data-disabled. An aria-disabled of your own without disabled is passed through as it is: the list stays interactive.

autoFocusbooleanNofalse

Focuses the list when it mounts in the browser (React focuses only form controls itself), not while disabled. Server-rendered HTML carries the autofocus attribute for the browser's own autofocus, so a hydrating list takes no focus, as React leaves a hydrated form control alone.

disabledOptionsFocusablebooleanNofalse

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.

labelsListboxLabelsNo

The multiselect toggle announcements, for localization. Unset members keep their English defaults.

itemsreadonly ListboxItem[]No

Data mode: the options as data, in list order (value, label, and optionally textValue, disabled, hidden and group; values unique). Adjacent items with the same group are drawn as a named group under a header (renderGroup), the options of the group counting aria-posinset and aria-setsize among themselves. The list renders one option per item that is not hidden (see renderOption), and the keys, typeahead and the selection follow items. The children are neither read nor rendered: passing both warns once in development. Each option gets aria-posinset and aria-setsize from its place among the shown items, and keeps one id per value. Without it, the options are the Option children.

renderOption(item: ListboxItem, state: ListboxOptionRenderState) => React.ReactNodeNoan `Option` with the item's `value`, `label` (also its text), `textValue` and `disabled`

Data mode: renders the option of an item, given its state (selected, active, and index among the shown options). Return an Option, or an option component of your own on useListboxOption, with the item's value (and its disabled); its position is set for you, and in a windowed list (virtualWindow) its height. It is called again only when the item, its state, its position (aria-posinset and aria-setsize, which new items change) or the function itself changes, so pass a stable function in a long list. Ignored without items, with a development warning.

renderGroup(group: string, state: ListboxGroupRenderState) => React.ReactNodeNothe group's name, as text

Data mode: renders the header of a group of items (those with the same group, next to each other), given the group's name and its state (index among the groups, size: its shown options). Return text or a few inline elements: the header is a row of the list, not an option, so nothing in it is reachable, and it is out of the accessibility tree (aria-hidden): the group's list is named by its name. In a list with a virtualWindow the header is a row of the window (as tall as an option, or measured with measureItems) and is not sticky: it scrolls away with its group. It is called again when the group's name, index or size changes or the function itself changes. Ignored without items, with a development warning.

virtualWindowVirtualWindowNo

Data mode: renders only the options in view of a long list, plus an overscan, and spacers (aria-hidden, data-virtual-spacer) as tall as the options they stand for: pass the result of useVirtualWindow({ itemSize }), or an object of your own that implements VirtualWindow. It needs items: without them, or beside OptionGroup children, it is ignored with a development warning (a window cannot read the options of children). Items with a group are windowed with their headers, which are rows of the window (itemSize tall, or measured with measureItems) and scroll away with their group: they are not sticky.

The list element is the window's scroller: give it a bounded height (a height or a maximum height, through className or style); without one the window measures the whole list and renders every option. While windowed the list has no block padding (add none through className), so the options start at its top, where the window counts them, and no scroll anchoring of the browser (the window lays the options out). Every option is exactly itemSize tall (the list sets its height, after the option's own style), so an option's content must fit in it: the default Option with one line of text is 32 pixels tall. With a window that measures its rows (useVirtualWindow({ itemSize, measureItems: true }), itemSize being the estimate) an option has the height of its content instead, and carries the window's itemProps.

The window counts the shown options (the items that are not hidden). The keys and typeahead move over all of them (Home and End to the first and the last, PageUp and PageDown by ten); the active option stays rendered while it is scrolled away, so aria-activedescendant always names an element, and when the list gains focus or a key makes it active the list and the window move to it in the same update (a pointer press moves neither). The server renders the first window.

namestringNo

Name of the value in form submissions (renders a hidden input inside the list; one per value with multiselect).

formstringNo

Id of the form the value belongs to, when the Listbox is outside it.

requiredbooleanNo

A value is required to submit the form (native constraint validation); when validation finds it empty, focus moves to the list.

onKeyDownReact.KeyboardEventHandler<HTMLUListElement>No

Called on a key press on the list, before the built-in keys; event.preventDefault() skips them.

refReact.Ref<HTMLUListElement>No

Ref to the <ul role="listbox">.

Any other prop is passed to the element it renders (HTMLUListElement).

Parts

  • Listbox.Option
  • Listbox.OptionGroup

Stable class names

PartClass
rootwave-listbox
spacerwave-listbox__spacer