Listboxes
Interactive listboxes that maintain selection state.
import { ListBox, ListBoxItem } from '@brainandbones/skeleton';
Examples
Single Selection
-
Item 1
-
Item 2
-
Item 3
Selected: 1
Multi-Selection
-
Item A
-
Item B
-
Item C
Selected: A,B
Usage
Define a writable store to house the selection state, then add a value
prop to each child. You may optionally provide an a11y-friendly label
prop as well.
Single Value
Create a writable with a singular value (string, number, etc).
const storeSingle: Writable<number> = writable(1);
<ListBox selected="{storeSingle}" label="Single Selection">
<ListBoxItem value={1}>Selection Example 1</ListBoxItem>
<ListBoxItem value={2}>Selection Example 2</ListBoxItem>
</ListBox>
Multiple Values
Create a writable with a an array of Values.
let storeMultiple: Writable<any[]> = writable(['A', 'B']);
<ListBox selected={storeMultiple} label="Multi-Selection">
<ListBoxItem value={'A'}>Item A</ListBoxItem>
<ListBoxItem value={'B'}>Item B</ListBoxItem>
<ListBoxItem value={'C'}>Item C</ListBoxItem>
</ListBox>
Properties
Listbox
Prop | Type | Default | Values | Description |
---|---|---|---|---|
selected | string | writable(undefined) | any | any[] | Provide a writable store to maintain list selection. |
space | string | space-y-1 | class | Provide classes to set vertical item spacing. |
height | string | - | class | Provide to set scrollable listbox region height. |
regionLabel | string | - | class | Provide arbitrary classes to the label element. |
regionList | string | - | class | Provide arbitrary classes to the scrollable listbox element. |
Listbox Item
These properties can be set on the parent to affect all child items.
Prop | Type | Default | Description |
---|---|---|---|
accent | string | !bg-primary-500 | Provide classes to set the selected element background. |
padding | string | px-4 py-3 | Provide classes to set the padding styles. |
rounded | string | rounded | Provide classes to set the border radius styles. |
Slots
Name | Description |
---|---|
lead | Positioned on the left of each row item. |
trail | Positioned on the right of each row item. |
Accessibility
ARIA GuidelinesListbox
Prop | Type | Default | Description |
---|---|---|---|
label | string | - | Define a semantic ARIA label. |
labelId | string | - | Set automatically based on the label text, but can be overwritten. |
Listbox Item
Prop | Type | Default | Description |
---|---|---|---|
id | string | - | Define a unique and semantic identifier for the item. |
Keyboard Interactions
Keys | Description |
---|---|
Tab | Moves focus to the next option. |
Shift + Tab | Moves focus to the previous option. |
Space or Enter | Changes the selection state of the focused option. |