ListBox
This is a headless list of items that the user can select. The keyboard operates the list. The list permits one selection or more than one selection. You can control the state, or you can let the component control it.
Selected: banana
Anatomy
ListBox.Root is the container. It holds the selection state and it controls the keyboard operation. Each option is a ListBox.Item with a unique id.
<script>
import { ListBox } from '@human-kit/ui';
</script>
<ListBox.Root aria-label="Options">
<ListBox.Item id="1">Option 1</ListBox.Item>
</ListBox.Root>More than one selection
Set selectionMode="multiple" to let the user select more than one item. The selectionBehavior prop controls the result when the user selects the same item again. The default is "toggle", and it removes the selection. With "replace", the new item always replaces the selection. Push Ctrl+A to select all of the items.
2 selected
Disabled items
Use disabled on a ListBox.Item, or disabledKeys on the root, to stop the operation of an option. The keyboard focus does not stop on a disabled item, and the item shows data-disabled for the styles.
Usage guidelines
- Use
ListBox.Rootas the container for the selection state and the keyboard operation. Make each option withListBox.Item. - Use
valueandonChangewhen your own code controls the selection. UsedefaultValuefor the initial selection when the component controls it. Each value is a set of item ids. - Use
selectionBehavior="replace"when a new option must always replace the selection. - Use
disabledon an item, ordisabledKeyson the root, to stop the selection of an option. - If there is no label that the user sees, give the root an
aria-labelattribute.
Accessibility
ListBox.Roothasrole="listbox". In the multiple mode, it also hasaria-multiselectable. EachListBox.Itemhasrole="option"witharia-selected.- The arrow keys move the focus between the items. The
Homekey and theEndkey move the focus to the first item and to the last item. - The
Spacekey and theEnterkey select the item with the focus. In the multiple mode,Ctrl+Aselects all of the items. - The component shows each visual state in a data attribute for the styles:
data-selected,data-focused,data-focus-visible,data-hovered,data-pressed, anddata-disabled.
API reference
Root
The listbox container. It controls the registration of the options, the selection state, the focus, and the keyboard operation.
* required. Native HTML attributes of the underlying element are also accepted.
Item
An option that the user can select. It registers itself with the root. It shows its selected, focused, hovered, pressed, and disabled states in data attributes.
* required. Native HTML attributes of the underlying element are also accepted.