Update the import paths to match your project setup.
Usage
Use ListBox to display a list of options and allow a user to select one or more of them.
ListBox Options
Orientation
By default, ListBox expects items to be arranged in a vertical stack, and implements keyboard navigation and drag and drop accordingly.
Use the orientation prop to change the layout to a horizontal stack.
When selectionBehavior is set to "replace", clicking a row with the mouse replaces the selection with only that row. Using the arrow keys moves both focus and selection. To select multiple rows, modifier keys such as Ctrl, Cmd, and Shift can be used. On touch screen devices, selection always behaves as toggle since modifier keys may not be available.
By default, items in a ListBox are labeled by their text contents for accessibility. Items also support the "label" and "description" props to separate primary and secondary content.
ListBox supports sections in order to group options. Sections can be used by wrapping groups of items in a Section element.
Sections without a title must provide an aria-label for accessibility.
If you need to customize things further, you can drop down to the composition level.
Next.jsReact-based SSR and static site framework.
RemixFull-stack framework with efficient data loading.
AstroLightweight static site builder for performance.
composition.tsx
<ListBox selectionMode="multiple"> <Item> <Text slot="label">Next.js</Text> <Text slot="description">React-based SSR and static site framework.</Text> </Item> <Item> <Text slot="label">Remix</Text> <Text slot="description">Full-stack framework with efficient data loading.</Text> </Item> <Item> <Text slot="label">Astro</Text> <Text slot="description">Lightweight static site builder for performance.</Text> </Item></ListBox>
Examples
Asynchronous loading
This example uses the useAsyncList hook to handle asynchronous loading of data from a server.
async-loading.tsx
const list = useAsyncList<Character>({ async load({ signal }) { const res = await fetch(`https://pokeapi.co/api/v2/pokemon`, { signal }) const json = (await res.json()) as { results: Character[] } return { items: json.results, } }})return ( <ListBox aria-label="Pick a Pokemon" items={list.items} isLoading={list.isLoading} selectionMode="single" > {(item) => <Item id={item.name}>{item.name}</Item>} </ListBox>)
API Reference
ListBox
Prop
Type
Default
Description
selectionBehavior
'toggle' | 'replace'
-
How multiple selection should behave in the collection.
dragAndDropHooks
DragAndDropHooks
-
The drag and drop hooks returned by useDragAndDrop used to enable drag and drop behavior for the ListBox.
renderEmptyState
(props: ListBoxRenderProps) => ReactNode
-
Provides content to display when there are no items in the list.
layout
'stack' | 'grid'
'stack'
Whether the items are arranged in a stack or grid.
orientation
'horizontal' | 'vertical'
'vertical'
The primary orientation of the items. Usually this is the direction that the collection scrolls.
autoFocus
boolean | 'first' | 'last'
-
Whether to auto focus the listbox or an option.
shouldFocusWrap
boolean
-
Whether focus should wrap around when the end/start is reached.
items
Iterable<T>
-
Item objects in the collection.
disabledKeys
Iterable<Key>
-
The item keys that are disabled. These items cannot be selected, focused, or otherwise interacted with.
selectionMode
'none' | 'single' | 'multiple'
-
The type of selection that is allowed in the collection.
disallowEmptySelection
boolean
-
Whether the collection allows empty selection.
selectedKeys
'all' | Iterable<Key>
-
The currently selected keys in the collection (controlled).
defaultSelectedKeys
'all' | Iterable<Key>
-
The initial selected keys in the collection (uncontrolled).
children
ReactNode | (item: object) => ReactNode
-
The contents of the collection.
dependencies
any[]
-
Values that should invalidate the item cache when using dynamic collections.