Listbox
An always-visible list for selecting one or more options.
Note: Listbox is currently in preview. Its API may change before it becomes stable.
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. - Keep items free of interactive content: Don’t place buttons, links, inputs, or other controls inside
<Listbox.Item>. Assistive technology treats an option’s content as plain text, and the listbox handles key presses on its items, so a control inside one can’t be reached or typed into. Put actions next to the listbox instead.
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 moving focus with the keyboard 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.
Items
Without the items prop, the rendered Listbox.Item parts are the whole list. Pass items to Listbox.Root to describe every item up front, which lets the listbox render them for you and reach items that aren’t rendered, as in a virtualized list. It accepts:
- An array of strings or numbers. Each one is an item’s value and label.
- A collection created with
Listbox.createItems()for any other data, such as objects or groups of items.
Give each Listbox.Item its value. The selection holds the same values.
Pass the same array or collection on every render, by defining it outside the component or memoizing it. A new one makes the listbox read the items again and render every item again.
To render the items, pass a function to Listbox.List. It’s called with each item and its index:
A function can’t be passed from a Server Component, so a list rendered this way must be in a Client Component. You can also map over the data yourself and render Listbox.Item parts as children, which works in either. To render other content next to the items, place the function in Listbox.Collection instead of Listbox.List.
When items is set, disabled items and the labels used for keyboard text navigation come from the data. The disabled and label props of Listbox.Item are ignored. An array of strings or numbers has no disabled items, so to disable some, create a collection with Listbox.createItems() and pass it isDisabled.
Filter the data before you pass it to items. Selecting everything with Ctrl or Cmd+A, or a range with Shift, replaces the selection with items from the current items, so selected items that a filter has removed are deselected.
Grouped
Use Listbox.Group and Listbox.GroupLabel to group related items.
To describe grouped items with the items prop, pass an array of groups, each with an items array, to Listbox.createItems(). The function passed to Listbox.List then receives each group. Pass the group’s items to Listbox.Group and render them with Listbox.Collection.
Custom data
For data that isn’t an array of strings or numbers, 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.
items.map() calls a function with each item in order, and its argument is typed from your data. createItems() also accepts an array of groups, each with an items array; an item can’t have an items array property of its own. The collection holds the functions you pass, so create it in a Client Component.
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', or the index is-1, which means that no item is highlighted. 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.
Keyboard interactions
In every selection mode:
- Arrow keys move focus to the next or previous item. Focus stops at the first and last items. A horizontal listbox uses the left and right arrow keys.
- Home and End move focus to the first and last items.
- Typing characters moves focus to the next item whose label starts with them.
- Space or Enter selects the focused item.
- Tab moves focus into the listbox, to the selected item or the first item, and out of it.
With multiple, each item is added or removed on its own:
- Space or Enter adds or removes the focused item.
With multiple="opt-in", the selection follows focus:
- Arrow keys, Home, End and typing move the selection to the item they move focus to. Disabled items can receive focus, and the selection stays where it was while focus is on one.
- Space or Enter makes the focused item the only selected item.
- See Opt-in multiple selection for the keys that add or remove single items.
In both multiple modes:
- Shift+arrow keys replace the selection with a range that starts at the focused item. Right after a Shift+press or another Shift+arrow key, they grow or shrink that range instead.
- Shift+Space replaces the selection with the range from the anchor to the
focused item. The anchor is the item that was last pressed without Shift, or the start
of the last range made with Shift+arrow keys. With
multiple="opt-in", moving the selection with the keyboard moves the anchor too. When there’s no anchor yet, the range starts at the first selected item, or at the first item. - Ctrl+Shift+Home or End adds the items from the focused item to the first or last item to the selection. On macOS, use Cmd instead of Ctrl.
- Ctrl or Cmd+A selects every item.
None of these change a disabled item: a selected one stays selected, and the others are skipped.
The listbox doesn’t handle Esc, so a popup or dialog around it can close.
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 one of the following:
- An array of primitives, such as strings or numbers, each of which is an item’s value and label.
- A collection created with
Listbox.createItems()for any other data, such as objects or groups of items.
Give each
Listbox.Itemits value, which with a collection is whatgetValuereturns for the item.Pass the same array or collection on every render, by defining it outside the component or memoizing it, since a new one makes every item render again.
- 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.eventDetails.indexis-1when no item is highlighted.- 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
ListboxPreview.Root.StateHide
ListboxPreview.Root.ChangeEventReasonHide
ListboxPreview.Root.ChangeEventDetailsHide
ListboxPreview.Root.HighlightEventReasonHide
ListboxPreview.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
ListboxPreview.Label.StateHide
List
A container for the listbox items.
Renders a <div> element.
children| React.ReactNode
| ((item: any, index: number) => React.ReactNode)—
| ((item: any, index: number) => React.ReactNode)
- Name
- Description
The items of the list. A function renders each of the root’s
items: it receives an item, or a group when the items are grouped, and its index.- 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-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. | |
ListboxPreview.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 the item’slabelfield or thegetLabeloption ofListbox.createItems()instead.- Type
value*any—
- Name
- Description
A unique value that identifies this listbox item.
- Type
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 the item’sdisabledfield or theisDisabledoption 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. | |
ListboxPreview.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
keepMountedbooleanfalse
- Name
- Description
Whether to keep the HTML element in the DOM when the item is not selected.
- Type
- Default
false
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-starting-style
Present when the indicator begins animating in.
data-ending-style
Present when the indicator is animating out.
Attribute | Description | |
|---|---|---|
data-selected | Present when the listbox item is selected. | |
data-starting-style | Present when the indicator begins animating in. | |
data-ending-style | Present when the indicator is animating out. | |
ListboxPreview.ItemIndicator.PropsHide
Re-Export of ItemIndicator props as ListboxPreviewItemIndicatorProps
ListboxPreview.ItemIndicator.StateHide
Group
Groups related listbox items with the corresponding label.
Renders a <div> element.
itemsany[]—
- Name
- Description
The items of this group. When provided, a
Listbox.Collectioninside the group renders these items.- 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
ListboxPreview.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
ListboxPreview.GroupLabel.PropsHide
Re-Export of GroupLabel props as ListboxPreviewGroupLabelProps
ListboxPreview.GroupLabel.StateHide
Collection
Renders the items passed to the root’s items prop, or the items of the group it’s in.
Doesn’t render its own HTML element.
To render nothing but the items in the list, pass a function child to the List component
instead, which implicitly wraps it.
children*(item: any, index: number) => React.ReactNode—
- Name
- Description
A function that renders an item. Outside a group, with grouped
items, it receives each group instead.- Type
ListboxPreview.Collection.PropsHide
Re-Export of Collection props as ListboxPreviewCollectionProps
ListboxPreview.Collection.StateHide
createItems
Creates a collection for the items prop of <Listbox.Root> from data of any shape.
Parameters
data*Union—
- Name
- Description
The items or groups of 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