Listbox
An always-visible list for selecting one or more options.
Usage guidelines
- Prefer Select for forms: Use Select instead of Listbox for selection in forms.
- Multi-select support: Set
multipleto allow selecting more than one item. The value is then an array. - Provide an accessible name: Prefer
<Listbox.Label>, or provide anaria-labelon<Listbox.List>when no visible label is rendered.
Anatomy
Import the component and assemble its parts:
Examples
Multiple selection
Add the multiple prop to let users select more than one item. The value is an array of the selected items.
Opt-in multiple selection
Use multiple="opt-in" when users opt in to selecting more than one item by holding a modifier key. Without one, a click or an arrow key moves the selection to a single item.
- Ctrl or Cmd+click adds or removes an item.
- Shift+click or Shift+arrow keys select a range.
- Ctrl+arrow keys move focus without changing the selection, and Ctrl+ Space adds or removes the focused item. On macOS, use Option instead of Ctrl.
On touch devices, which have no modifier keys, a tap adds or removes an item, the same as multiple.
Selection toggle
Add Listbox.ItemSelectionToggle to give each item a control, such as a checkbox, that adds or removes the item without changing the rest of the selection. It’s most useful with multiple="opt-in", where pressing the rest of the item replaces the selection.
Pressing a toggle and dragging over the toggles of other items selects or deselects them as well, depending on what the press did to the first item. Holding the pointer near an edge of the scrolling container scrolls it, so the drag can reach items that are out of view.
- Extend the pressable area: Only the toggle’s own area responds to presses and drags. Make it larger than the visible checkbox, such as with a pseudo-element that spans the height of the row, so a drag doesn’t slip between rows.
- Scrolling on touch devices: A drag that starts on a toggle selects items instead of scrolling the list. Leave enough of each item outside the toggle to scroll from.
- Keyboard and assistive technology: The toggle is hidden from assistive technology and can’t be focused. Items are selected from the keyboard in the same way with or without it.
Listbox.ItemIndicator can be placed inside the toggle to render the checkmark.
Grouped
Use Listbox.Group and Listbox.GroupLabel to group related items.
Object data
To describe items that are objects, create a collection with Listbox.createItems() and pass it to the items prop. Make sure getValue returns a unique primitive for each item, such as its ID, and give each Listbox.Item that value. The selection holds the same values, and items.get() returns the object for one.
When items is set, disabled items and the labels used for keyboard text navigation come from the getDisabled and getLabel options. The disabled and label props of Listbox.Item are ignored.
Virtualized
Render large lists with a virtualization library such as @tanstack/react-virtual. Because only some of the items are rendered, a virtualized listbox needs the following:
- Pass every item to
items: The listbox can then reach items that aren’t rendered with keyboard navigation, typeahead, and selection. Without it, arrow keys stop at the last rendered item. - Scroll the highlighted item into view: In
onItemHighlighted, scroll toeventDetails.indexunless the reason is'pointer'. The item receives focus once it renders. - Set
aria-setsizeandaria-posinseton each item: Assistive technology then announces an item’s position in the whole list, not among the rendered items. - Keep a focus indicator on the list:
<Listbox.List>holds focus while the highlighted item isn’t rendered, so don’t remove its focus style without showing one elsewhere.
API reference
Root
Groups all parts of the listbox. Doesn’t render its own HTML element.
defaultValueUnion—
- Name
- Description
The uncontrolled value of the listbox when it’s initially rendered.
To render a controlled listbox, use the
valueprop instead.- Type
valueUnion—
- Name
- Description
The value of the listbox. Use when controlled.
- Type
onValueChangefunction—
- Name
- Description
Event handler called when the value of the listbox changes.
- Type
highlightItemOnHoverbooleantrue
- Description
Whether moving the pointer over an item highlights it. While focus is in the listbox, the item also receives focus, except with
multiple="opt-in", where moving focus can change the selection. The pointer never takes focus from outside the listbox.- Type
- Default
true
itemsUnion—
- Name
- Description
Every item in the list, including those that aren’t rendered. Pass them when the list is virtualized, so keyboard navigation, typeahead and selection can reach items that aren’t rendered.
Pass an array of strings or numbers, each of which is an item’s value and label, or a collection created with
Listbox.createItems()for any other data. Give eachListbox.Itemits value, which with a collection is whatgetValuereturns for the item.- Type
multipleboolean | 'opt-in'false
- Name
- Description
Whether multiple items can be selected, and how selecting an item affects the current selection.
false: only one item can be selected.true: each item is toggled independently.'opt-in': selecting an item moves the current selection to it, and users hold a modifier key to select more than one item. On touch devices, where there are no modifier keys, it behaves liketrue.
- Type
- Default
false
onItemHighlightedfunction—
- Description
Callback fired when an item is highlighted or unhighlighted. Receives the highlighted item value (or
undefinedif no item is highlighted) and event details with areasonproperty describing why the highlight changed. Thereasoncan be:'keyboard': the highlight changed due to keyboard navigation or typeahead.'pointer': the highlight changed due to pointer hovering. The event may be aMouseEventrather than aPointerEvent.'none': the highlight changed for another reason, such as an item receiving or losing focus.
With
items, the highlighted item may not be rendered yet, so scroll it into view.- Type
disabledbooleanfalse
- Name
- Description
Whether the component should ignore user interaction.
- Type
- Default
false
orientation'vertical' | 'horizontal''vertical'
- Name
- Description
The orientation of the listbox for keyboard navigation.
- Type
- Default
'vertical'
childrenReact.ReactNode—
- Name
- Type
Listbox.Root.StateHide
Listbox.Root.ChangeEventReasonHide
Listbox.Root.ChangeEventDetailsHide
Listbox.Root.HighlightEventReasonHide
Listbox.Root.HighlightEventDetailsHide
Label
An accessible label that is automatically associated with the listbox.
Renders a <div> element.
classNamestring | function—
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
styleReact.CSSProperties | function—
- Name
- Description
Style applied to the element, or a function that returns a style object based on the component’s state.
- Type
renderReactElement | function—
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
Listbox.Label.StateHide
List
A container for the listbox items.
Renders a <div> element.
classNamestring | function—
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
styleReact.CSSProperties | function—
- Name
- Description
Style applied to the element, or a function that returns a style object based on the component’s state.
- Type
renderReactElement | function—
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
data-orientation
Indicates the orientation of the listbox.
data-disabled
Present when the listbox is disabled.
Attribute | Description | |
|---|---|---|
data-orientation | Indicates the orientation of the listbox. | |
data-disabled | Present when the listbox is disabled. | |
Listbox.List.StateHide
Item
An individual option in the listbox.
Renders a <div> element.
labelstring—
- Name
- Description
Specifies the text label to use when the item is matched during keyboard text navigation.
Defaults to the item text content if not provided.
Ignored when the root has
items. Use thegetLabeloption ofListbox.createItems()instead.- Type
valueanynull
- Name
- Description
A unique value that identifies this listbox item.
- Type
- Default
null
nativeButtonbooleanfalse
- Name
- Description
Whether the component renders a native
<button>element when replacing it via therenderprop. Set totrueif the rendered element is a native button.- Type
- Default
false
disabledbooleanfalse
- Name
- Description
Whether the component should ignore user interaction.
Ignored when the root has
items. Use thegetDisabledoption ofListbox.createItems()instead.- Type
- Default
false
childrenReact.ReactNode—
- Name
- Type
classNamestring | function—
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
styleReact.CSSProperties | function—
- Name
- Description
Style applied to the element, or a function that returns a style object based on the component’s state.
- Type
renderReactElement | function—
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
data-selected
Present when the listbox item is selected.
data-highlighted
Present when the listbox item is highlighted.
data-disabled
Present when the listbox item is disabled.
Attribute | Description | |
|---|---|---|
data-selected | Present when the listbox item is selected. | |
data-highlighted | Present when the listbox item is highlighted. | |
data-disabled | Present when the listbox item is disabled. | |
Listbox.Item.StateHide
ItemIndicator
Indicates whether the listbox item is selected.
Renders a <span> element.
childrenReact.ReactNode—
- Name
- Type
classNamestring | function—
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
styleReact.CSSProperties | function—
- Name
- Description
Style applied to the element, or a function that returns a style object based on the component’s state.
- Type
keepMountedboolean—
- Name
- Description
Whether to keep the HTML element in the DOM when the item is not selected.
- Type
renderReactElement | function—
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
data-starting-style
Present when the indicator begins animating in.
data-ending-style
Present when the indicator is animating out.
Attribute | Description | |
|---|---|---|
data-starting-style | Present when the indicator begins animating in. | |
data-ending-style | Present when the indicator is animating out. | |
Listbox.ItemIndicator.PropsHide
Re-Export of ItemIndicator props as ListboxItemIndicatorProps
Listbox.ItemIndicator.StateHide
ItemSelectionToggle
A control inside a listbox item, such as a checkbox, that selects or deselects the item without
changing the rest of the selection when multiple items can be selected. Pressing it and dragging
over the toggles of other items selects or deselects them too.
Renders a <span> element.
childrenReact.ReactNode—
- Name
- Type
classNamestring | function—
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
styleReact.CSSProperties | function—
- Name
- Description
Style applied to the element, or a function that returns a style object based on the component’s state.
- Type
renderReactElement | function—
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
data-selected
Present when the listbox item is selected.
data-disabled
Present when the listbox item is disabled.
Attribute | Description | |
|---|---|---|
data-selected | Present when the listbox item is selected. | |
data-disabled | Present when the listbox item is disabled. | |
Listbox.ItemSelectionToggle.PropsHide
Re-Export of ItemSelectionToggle props as ListboxItemSelectionToggleProps
Listbox.ItemSelectionToggle.StateHide
Group
Groups related listbox items with the corresponding label.
Renders a <div> element.
classNamestring | function—
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
styleReact.CSSProperties | function—
- Name
- Description
Style applied to the element, or a function that returns a style object based on the component’s state.
- Type
renderReactElement | function—
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
Listbox.Group.StateHide
GroupLabel
An accessible label that is automatically associated with its parent group.
Renders a <div> element.
classNamestring | function—
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
styleReact.CSSProperties | function—
- Name
- Description
Style applied to the element, or a function that returns a style object based on the component’s state.
- Type
renderReactElement | function—
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
Listbox.GroupLabel.PropsHide
Re-Export of GroupLabel props as ListboxGroupLabelProps
Listbox.GroupLabel.StateHide
createItems
Creates a collection for the items prop of <Listbox.Root> from data that isn’t strings or numbers.
Parameters
data*Item[]—
- Name
- Description
The items, or
undefinedwhile they are loading.- Type
options*CreateListboxItemsOptions<
Item,
ListboxPrimitiveValue
>—
- Name
- Description
Functions that derive each item’s value, label and disabled state.
- Type