useListBox

Provides the behavior and accessibility implementation for a listbox component. A listbox displays a list of options and allows a user to select one or more of them.

Left
Middle
Right
Example
ListBox.tsx
ListBox.css
import {ListBox, ListBoxItem} from './ListBox';

<ListBox aria-label="Alignment" selectionMode="single">
  <ListBoxItem id="left">Left</ListBoxItem>
  <ListBoxItem id="middle">Middle</ListBoxItem>
  <ListBoxItem id="right">Right</ListBoxItem>
</ListBox>

API

Option 1Option 2ItemItem labelDescriptionDescriptionOption 3DescriptionItem descriptionSECTION TITLESection headerSection
<ListBox>
  <ListBoxItem>
    <Text slot="label" />
    <Text slot="description" />
    <SelectionIndicator />
  </ListBoxItem>
  <ListBoxSection>
    <Header />
    <ListBoxItem />
  </ListBoxSection>
  <ListBoxLoadMoreItem />
</ListBox>
useListState<T>(props: <T>): <T>useListBox<T>( props: <T>, state: <T>, ref: <HTMLElementnull> ): useOption<T>( props: , state: <T>, ref: <FocusableElementnull> ): useListBoxSection(props: ):

ListState

NameType
collection<<T>>

A collection of items in the list.

selectionManager

A selection manager to read and update multiple selection state.

disabledKeysSet<Key>

A set of items that are disabled.

AriaListBoxOptions

NameTypeDefault
isVirtualizedbooleanDefault:

Whether the listbox uses virtual scrolling.

keyboardDelegateDefault:

An optional keyboard delegate implementation for type to select, to override the default.

layoutDelegateDefault:

A delegate object that provides layout information for items in the collection. By default this uses the DOM, but this can be overridden to implement things like virtualized scrolling.

shouldUseVirtualFocusbooleanDefault:

Whether the listbox items should use virtual focus instead of being focused directly.

linkBehavior'action''selection''override'Default: 'override'

The behavior of links in the collection. - 'action': link behaves like onAction. - 'selection': link follows selection interactions (e.g. if URL drives selection). - 'override': links override all other interactions (link items are not selectable).

orientationDefault: 'vertical'

The primary orientation of the items. Usually this is the direction that the collection scrolls.

itemsIterable<T>Default:

Item objects in the collection.

selectionModeDefault:

The type of selection that is allowed in the collection.

selectionBehaviorDefault:

How multiple selection should behave in the collection.

selectedKeys'all'Iterable<Key>Default:

The currently selected keys in the collection (controlled).

defaultSelectedKeys'all'Iterable<Key>Default:

The initial selected keys in the collection (uncontrolled).

onSelectionChange(keys: ) => voidDefault:

Handler that is called when the selection changes.

disabledKeysIterable<Key>Default:

The item keys that are disabled. These items cannot be selected, focused, or otherwise interacted with.

disallowEmptySelectionbooleanDefault:

Whether the collection allows empty selection.

shouldSelectOnPressUpbooleanDefault:

Whether selection should occur on press up instead of press down.

shouldFocusWrapbooleanDefault:

Whether focus should wrap around when the end/start is reached.

shouldFocusOnHoverbooleanDefault:

Whether options should be focused when the user hovers over them.

escapeKeyBehavior'clearSelection''none'Default: 'clearSelection'

Whether pressing the escape key should clear selection in the listbox or not.

Most experiences should not modify this option as it eliminates a keyboard user's ability to easily clear selection. Only use if the escape key is being handled externally or should not trigger selection clearing contextually.

ListBoxAria

NameType
labelPropsDOMAttributes
Props for the listbox's visual label element (if any).
listBoxPropsDOMAttributes
Props for the listbox element.

AriaOptionProps

NameType
keyKey

The unique key for the option.

OptionAria

NameType
hasActionboolean
Whether the item has an action, dependent on onAction, disabledKeys, and disabledBehavior. It may also change depending on the current selection state of the list (e.g. when selection is primary). This can be used to enable or disable hover styles or other visual indications of interactivity.
allowsSelectionboolean
Whether the item may be selected, dependent on selectionMode, disabledKeys, and disabledBehavior.
isDisabledboolean
Whether the item is non-interactive, i.e. both selection and actions are disabled and the item may not be focused. Dependent on disabledKeys and disabledBehavior.
isSelectedboolean
Whether the item is currently selected.
isPressedboolean
Whether the item is currently in a pressed state.
isFocusVisibleboolean
Whether the option is keyboard focused.
isFocusedboolean
Whether the option is currently focused.
descriptionPropsDOMAttributes
Props for the description text element inside the option, if any.
labelPropsDOMAttributes
Props for the main text element inside the option.
optionPropsDOMAttributes
Props for the option element.

AriaListBoxSectionProps

NameType
headingReactNode

The heading for the section.

ListBoxSectionAria

NameType
groupPropsDOMAttributes
Props for the group element.
headingPropsDOMAttributes
Props for the heading element, if any.
itemPropsDOMAttributes
Props for the wrapper list item.