Skip to contents

Listbox

An always-visible list for selecting one or more options.

Playlist
1 of 16 selected
Billie JeanMichael Jackson4:54
Dancing QueenABBA3:51
Like a PrayerMadonna5:41
Running Up That HillKate Bush5:00
Hotel CaliforniaEagles6:30
London CallingThe Clash3:19
HeroesDavid Bowie6:07
Bohemian RhapsodyQueen5:55
SuperstitionStevie Wonder4:26
RespectAretha Franklin2:27
What's Going OnMarvin Gaye3:53
Let's Stay TogetherAl Green3:18
Blue MondayNew Order7:29
Around the WorldDaft Punk7:09
TeardropMassive Attack5:29
WindowlickerAphex Twin6:07

Usage guidelines

  • Prefer Select for forms: Use Select instead of Listbox for selection in forms.
  • Multi-select support: Set multiple to allow selecting more than one item. The value is then an array.
  • Provide an accessible name: Prefer <Listbox.Label>, or provide an aria-label on <Listbox.List> when no visible label is rendered.

Anatomy

Import the component and assemble its parts:

Anatomy

Examples

Multiple selection

Add the multiple prop to let users select more than one item. The value is an array of the selected items.

Labels
2 of 9 selected
Bug
Design
Docs
Feature
Good first issue
Performance
Question
Security
Tests

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.

Songs
1 of 8 selected
Billie Jean
Dancing Queen
Like a Prayer
Running Up That Hill
Hotel California
London Calling
Heroes
Bohemian Rhapsody

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.

Songs
1 of 16 selected
Billie Jean
Dancing Queen
Like a Prayer
Running Up That Hill
Hotel California
London Calling
Heroes
Bohemian Rhapsody
Purple Rain
Superstition
Go Your Own Way
Once in a Lifetime
Take On Me
Blue Monday
Under Pressure
Just Like Heaven

Grouped

Use Listbox.Group and Listbox.GroupLabel to group related items.

Move to
Inbox
Archive
Drafts
Projects
Receipts
Travel

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.

Object data

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 to eventDetails.index unless the reason is 'pointer'. The item receives focus once it renders.
  • Set aria-setsize and aria-posinset on 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.
Tracks

API reference

Root

Groups all parts of the listbox. Doesn’t render its own HTML element.

defaultValueUnion—
Description

The uncontrolled value of the listbox when it’s initially rendered.

To render a controlled listbox, use the value prop instead.

Type
valueUnion—
Name
Description

The value of the listbox. Use when controlled.

Type
onValueChangefunction—
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 each Listbox.Item its value, which with a collection is what getValue returns for the item.

Type
multipleboolean | 'opt-in'false
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 like true.
Type
Default
false
onItemHighlightedfunction—
Description

Callback fired when an item is highlighted or unhighlighted. Receives the highlighted item value (or undefined if no item is highlighted) and event details with a reason property describing why the highlight changed. The reason can be:

  • 'keyboard': the highlight changed due to keyboard navigation or typeahead.
  • 'pointer': the highlight changed due to pointer hovering. The event may be a MouseEvent rather than a PointerEvent.
  • '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
Description

Whether the component should ignore user interaction.

Type
Default
false
orientation'vertical' | 'horizontal''vertical'
Description

The orientation of the listbox for keyboard navigation.

Type
Default
'vertical'
childrenReact.ReactNode—
Type

Listbox.Root.PropsHide

Re-Export of Root props as ListboxRootProps

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—
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 ReactElement or a function that returns the element to render.

Type

Listbox.Label.PropsHide

Re-Export of Label props as ListboxLabelProps

Listbox.Label.StateHide

List

A container for the listbox items. Renders a <div> element.

classNamestring | function—
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 ReactElement or 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.PropsHide

Re-Export of List props as ListboxListProps

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 the getLabel option of Listbox.createItems() instead.

Type
valueanynull
Name
Description

A unique value that identifies this listbox item.

Type
Default
null
nativeButtonbooleanfalse
Description

Whether the component renders a native <button> element when replacing it via the render prop. Set to true if the rendered element is a native button.

Type
Default
false
disabledbooleanfalse
Description

Whether the component should ignore user interaction.

Ignored when the root has items. Use the getDisabled option of Listbox.createItems() instead.

Type
Default
false
childrenReact.ReactNode—
Type
classNamestring | function—
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 ReactElement or 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.PropsHide

Re-Export of Item props as ListboxItemProps

Listbox.Item.StateHide

ItemIndicator

Indicates whether the listbox item is selected. Renders a <span> element.

childrenReact.ReactNode—
Type
classNamestring | function—
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—
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 ReactElement or 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—
Type
classNamestring | function—
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 ReactElement or 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—
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 ReactElement or a function that returns the element to render.

Type

Listbox.Group.PropsHide

Re-Export of Group props as ListboxGroupProps

Listbox.Group.StateHide

GroupLabel

An accessible label that is automatically associated with its parent group. Renders a <div> element.

classNamestring | function—
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 ReactElement or 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 undefined while they are loading.

Type
options*CreateListboxItemsOptions< Item, ListboxPrimitiveValue >—
Description

Functions that derive each item’s value, label and disabled state.