Listbox
A list of options supporting single and multiple selection with keyboard navigation and typeahead.
Listbox follows the Listbox WAI-ARIA design pattern using aria-activedescendant to manage focus on the root container.
Selected: none
import { component$, useSignal, useStyles$ } from '@builder.io/qwik';
import { Listbox } from '@leve-ui/listbox';
import { LuCheck } from '@qwikest/icons/lucide';
import styles from './default.css?inline';
const FRUITS = [
{ value: 'apple', label: 'Apple' },
{ value: 'apricot', label: 'Apricot' },
{ value: 'banana', label: 'Banana' },
{ value: 'blueberry', label: 'Blueberry', disabled: true },
{ value: 'blackberry', label: 'Blackberry' },
{ value: 'cherry', label: 'Cherry' },
{ value: 'grape', label: 'Grape' },
{ value: 'peach', label: 'Peach' },
];
export default component$(() => {
useStyles$(styles);
const value = useSignal<string[]>([]);
return (
<div class="listbox-default">
<span id="listbox-default-label" class="listbox-default-label">
Fruit
</span>
<Listbox.Root
aria-labelledby="listbox-default-label"
bind:value={value}
class="listbox-default-list"
>
{FRUITS.map((fruit) => (
<Listbox.Item
key={fruit.value}
value={fruit.value}
disabled={fruit.disabled}
class="listbox-default-item"
>
<Listbox.ItemText>{fruit.label}</Listbox.ItemText>
<Listbox.ItemIndicator class="listbox-default-indicator">
<LuCheck aria-hidden="true" class="listbox-default-icon" />
</Listbox.ItemIndicator>
</Listbox.Item>
))}
</Listbox.Root>
<p class="listbox-default-status">Selected: {value.value[0] ?? 'none'}</p>
</div>
);
});.listbox-default {
display: flex;
flex-direction: column;
gap: 8px;
width: 240px;
font-family: inherit;
}
.listbox-default-label {
font-size: 14px;
font-weight: 500;
color: light-dark(#0f172a, #ffffff);
}
.listbox-default-list {
display: flex;
flex-direction: column;
gap: 2px;
max-height: 200px;
overflow-y: auto;
padding: 4px;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
color: light-dark(#0f172a, #ffffff);
}
.listbox-default-list:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}
.listbox-default-item {
display: flex;
align-items: center;
justify-content: space-between;
padding: 6px 8px;
border-radius: 4px;
font-size: 14px;
cursor: default;
}
.listbox-default-item[data-state="checked"] {
color: light-dark(#006ce9, #4cb8ff);
font-weight: 500;
}
.listbox-default-item[data-disabled] {
opacity: 0.5;
cursor: not-allowed;
}
@media (hover: hover) {
.listbox-default-item:hover:not([data-disabled]) {
background: light-dark(#f1f5f9, #ffffff14);
}
}
/* Active descendant highlight while navigating with the keyboard */
.listbox-default-list:focus-visible .listbox-default-item[data-highlighted] {
background: light-dark(#f1f5f9, #ffffff14);
}
.listbox-default-indicator {
display: inline-flex;
align-items: center;
}
.listbox-default-indicator[hidden] {
display: none;
}
.listbox-default-icon {
width: 16px;
height: 16px;
}
.listbox-default-status {
margin: 0;
font-size: 14px;
color: light-dark(#475569, #c9ddf0);
}Installation#
Install the individual package, or use @leve-ui/core to get every component at once:
pnpm add @leve-ui/listboxnpm install @leve-ui/listboxyarn add @leve-ui/listboxbun add @leve-ui/listboxpnpm add @leve-ui/corenpm install @leve-ui/coreyarn add @leve-ui/corebun add @leve-ui/coreAnatomy#
import { Listbox } from '@leve-ui/listbox'; // or '@leve-ui/core'
<Listbox.Root aria-label="Options">
<Listbox.Item value="one">
<Listbox.ItemText>Option 1</Listbox.ItemText>
<Listbox.ItemIndicator />
</Listbox.Item>
<Listbox.Group>
<Listbox.GroupLabel>Group</Listbox.GroupLabel>
<Listbox.Item value="two">
<Listbox.ItemText>Option 2</Listbox.ItemText>
<Listbox.ItemIndicator />
</Listbox.Item>
</Listbox.Group>
</Listbox.Root>
| Part | Element | Description |
|---|---|---|
Listbox.Root | <div> | The listbox (role="listbox"). Holds the selected values and active descendant highlight. |
Listbox.Item | <div> | An option (role="option"). |
Listbox.ItemText | <span> | The text label of the option, used for typeahead when textValue is not set. |
Listbox.ItemIndicator | <span> | Visual check mark shown when the option is selected, hidden otherwise. |
Listbox.Group | <div> | A group of options (role="group"), named by its GroupLabel. |
Listbox.GroupLabel | <div> | The heading of a group. Skipped by keyboard navigation. |
Selection#
The value is always an array of the selected options' values, also in single-select mode (empty or one value). Use defaultValue for uncontrolled usage or bind:value for controlled usage, and listen to changes with onValueChange$.
Set multiple on Listbox.Root to allow selecting multiple options, toggling with Space / Enter or clicks, extending ranges with Shift + arrows / Home / End or Shift + click, and selecting all enabled options with Ctrl+A / Cmd+A.
Examples#
Multiple Selection#
Set multiple to enable multi-selection with aria-multiselectable="true", range selection, and Ctrl+A / Cmd+A.
Selected: none
import { component$, useSignal, useStyles$ } from '@builder.io/qwik';
import { Listbox } from '@leve-ui/listbox';
import { LuCheck } from '@qwikest/icons/lucide';
import styles from './multiple.css?inline';
const TOPPINGS = [
{ value: 'avocado', label: 'Avocado' },
{ value: 'bacon', label: 'Bacon' },
{ value: 'cheese', label: 'Cheese' },
{ value: 'lettuce', label: 'Lettuce' },
{ value: 'onion', label: 'Onion' },
{ value: 'truffle', label: 'Truffle', disabled: true },
{ value: 'tomato', label: 'Tomato' },
];
export default component$(() => {
useStyles$(styles);
const value = useSignal<string[]>([]);
return (
<div class="listbox-multiple">
<span id="listbox-multiple-label" class="listbox-multiple-label">
Toppings
</span>
<Listbox.Root
multiple
aria-labelledby="listbox-multiple-label"
bind:value={value}
class="listbox-multiple-list"
>
{TOPPINGS.map((topping) => (
<Listbox.Item
key={topping.value}
value={topping.value}
disabled={topping.disabled}
class="listbox-multiple-item"
>
<Listbox.ItemText>{topping.label}</Listbox.ItemText>
<Listbox.ItemIndicator class="listbox-multiple-indicator">
<LuCheck aria-hidden="true" class="listbox-multiple-icon" />
</Listbox.ItemIndicator>
</Listbox.Item>
))}
</Listbox.Root>
<p class="listbox-multiple-status">
Selected: {value.value.length > 0 ? value.value.join(', ') : 'none'}
</p>
</div>
);
});.listbox-multiple {
display: flex;
flex-direction: column;
gap: 8px;
width: 240px;
font-family: inherit;
}
.listbox-multiple-label {
font-size: 14px;
font-weight: 500;
color: light-dark(#0f172a, #ffffff);
}
.listbox-multiple-list {
display: flex;
flex-direction: column;
gap: 2px;
padding: 4px;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
color: light-dark(#0f172a, #ffffff);
}
.listbox-multiple-list:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}
.listbox-multiple-item {
display: flex;
align-items: center;
justify-content: space-between;
padding: 6px 8px;
border-radius: 4px;
font-size: 14px;
cursor: default;
}
.listbox-multiple-item[data-state="checked"] {
color: light-dark(#006ce9, #4cb8ff);
font-weight: 500;
}
.listbox-multiple-item[data-disabled] {
opacity: 0.5;
cursor: not-allowed;
}
@media (hover: hover) {
.listbox-multiple-item:hover:not([data-disabled]) {
background: light-dark(#f1f5f9, #ffffff14);
}
}
/* Active descendant highlight while navigating with the keyboard */
.listbox-multiple-list:focus-visible .listbox-multiple-item[data-highlighted] {
background: light-dark(#f1f5f9, #ffffff14);
}
.listbox-multiple-indicator {
display: inline-flex;
align-items: center;
}
.listbox-multiple-indicator[hidden] {
display: none;
}
.listbox-multiple-icon {
width: 16px;
height: 16px;
}
.listbox-multiple-status {
margin: 0;
font-size: 14px;
color: light-dark(#475569, #c9ddf0);
}Groups and Loop#
Listbox.Group and Listbox.GroupLabel organize options into labeled groups. With loop, arrow keys wrap between the first and last enabled options.
Selected: none
import { component$, useSignal, useStyles$ } from '@builder.io/qwik';
import { Listbox } from '@leve-ui/listbox';
import { LuCheck } from '@qwikest/icons/lucide';
import styles from './grouped.css?inline';
const TIMEZONES = [
{ region: 'Americas', zones: ['New York', 'Chicago', 'Los Angeles'] },
{ region: 'Europe', zones: ['London', 'Paris', 'Berlin'] },
{ region: 'Asia', zones: ['Tokyo', 'Seoul', 'Singapore'] },
];
export default component$(() => {
useStyles$(styles);
const value = useSignal<string[]>([]);
return (
<div class="listbox-grouped">
<span id="listbox-grouped-label" class="listbox-grouped-label">
Time zone
</span>
{/* loop wraps arrow navigation from the last option back to the first */}
<Listbox.Root
loop
aria-labelledby="listbox-grouped-label"
bind:value={value}
class="listbox-grouped-list"
>
{TIMEZONES.map((group) => (
<Listbox.Group key={group.region} class="listbox-grouped-group">
<Listbox.GroupLabel class="listbox-grouped-group-label">
{group.region}
</Listbox.GroupLabel>
{group.zones.map((zone) => (
<Listbox.Item key={zone} value={zone} class="listbox-grouped-item">
<Listbox.ItemText>{zone}</Listbox.ItemText>
<Listbox.ItemIndicator class="listbox-grouped-indicator">
<LuCheck aria-hidden="true" class="listbox-grouped-icon" />
</Listbox.ItemIndicator>
</Listbox.Item>
))}
</Listbox.Group>
))}
</Listbox.Root>
<p class="listbox-grouped-status">Selected: {value.value[0] ?? 'none'}</p>
</div>
);
});.listbox-grouped {
display: flex;
flex-direction: column;
gap: 8px;
width: 240px;
font-family: inherit;
}
.listbox-grouped-label {
font-size: 14px;
font-weight: 500;
color: light-dark(#0f172a, #ffffff);
}
.listbox-grouped-list {
display: flex;
flex-direction: column;
gap: 4px;
max-height: 220px;
overflow-y: auto;
padding: 4px;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
color: light-dark(#0f172a, #ffffff);
}
.listbox-grouped-list:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}
.listbox-grouped-group {
display: flex;
flex-direction: column;
gap: 2px;
}
.listbox-grouped-group-label {
padding: 6px 8px 2px;
font-size: 12px;
font-weight: 600;
color: light-dark(#475569, #c9ddf0);
}
.listbox-grouped-item {
display: flex;
align-items: center;
justify-content: space-between;
padding: 6px 8px;
border-radius: 4px;
font-size: 14px;
cursor: default;
}
.listbox-grouped-item[data-state="checked"] {
color: light-dark(#006ce9, #4cb8ff);
font-weight: 500;
}
@media (hover: hover) {
.listbox-grouped-item:hover {
background: light-dark(#f1f5f9, #ffffff14);
}
}
/* Active descendant highlight while navigating with the keyboard */
.listbox-grouped-list:focus-visible .listbox-grouped-item[data-highlighted] {
background: light-dark(#f1f5f9, #ffffff14);
}
.listbox-grouped-indicator {
display: inline-flex;
align-items: center;
}
.listbox-grouped-indicator[hidden] {
display: none;
}
.listbox-grouped-icon {
width: 16px;
height: 16px;
}
.listbox-grouped-status {
margin: 0;
font-size: 14px;
color: light-dark(#475569, #c9ddf0);
}Disabled#
disabled on Listbox.Root removes it from the tab order (tabIndex={-1}) and disables every option. disabled on an individual Listbox.Item prevents selection and skips it during keyboard navigation and typeahead.
import { component$, useStyles$ } from '@builder.io/qwik';
import { Listbox } from '@leve-ui/listbox';
import { LuCheck } from '@qwikest/icons/lucide';
import styles from './disabled.css?inline';
export default component$(() => {
useStyles$(styles);
return (
<div class="listbox-disabled">
<span id="listbox-disabled-label" class="listbox-disabled-label">
Fruit (disabled)
</span>
<Listbox.Root
disabled
defaultValue={['apple']}
aria-labelledby="listbox-disabled-label"
class="listbox-disabled-list"
>
<Listbox.Item value="apple" class="listbox-disabled-item">
<Listbox.ItemText>Apple</Listbox.ItemText>
<Listbox.ItemIndicator class="listbox-disabled-indicator">
<LuCheck aria-hidden="true" class="listbox-disabled-icon" />
</Listbox.ItemIndicator>
</Listbox.Item>
<Listbox.Item value="banana" class="listbox-disabled-item">
<Listbox.ItemText>Banana</Listbox.ItemText>
<Listbox.ItemIndicator class="listbox-disabled-indicator">
<LuCheck aria-hidden="true" class="listbox-disabled-icon" />
</Listbox.ItemIndicator>
</Listbox.Item>
<Listbox.Item value="cherry" class="listbox-disabled-item">
<Listbox.ItemText>Cherry</Listbox.ItemText>
<Listbox.ItemIndicator class="listbox-disabled-indicator">
<LuCheck aria-hidden="true" class="listbox-disabled-icon" />
</Listbox.ItemIndicator>
</Listbox.Item>
</Listbox.Root>
</div>
);
});.listbox-disabled {
display: flex;
flex-direction: column;
gap: 8px;
width: 240px;
font-family: inherit;
}
.listbox-disabled-label {
font-size: 14px;
font-weight: 500;
color: light-dark(#0f172a, #ffffff);
opacity: 0.5;
}
.listbox-disabled-list {
display: flex;
flex-direction: column;
gap: 2px;
padding: 4px;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
color: light-dark(#0f172a, #ffffff);
}
.listbox-disabled-list[data-disabled] {
opacity: 0.5;
cursor: not-allowed;
}
.listbox-disabled-item {
display: flex;
align-items: center;
justify-content: space-between;
padding: 6px 8px;
border-radius: 4px;
font-size: 14px;
}
.listbox-disabled-item[data-state="checked"] {
color: light-dark(#006ce9, #4cb8ff);
font-weight: 500;
}
.listbox-disabled-indicator {
display: inline-flex;
align-items: center;
}
.listbox-disabled-indicator[hidden] {
display: none;
}
.listbox-disabled-icon {
width: 16px;
height: 16px;
}API Reference#
Root#
| Prop | Default | Type |
|---|---|---|
| defaultValue | [] | string[] The values of the selected items initially (uncontrolled). |
| bind:value | – | Signal<string[]> A signal with the values of the selected items (controlled). |
| onValueChange$ | – | QRL<(value: string[]) => void> Called when the selected items change. |
| multiple | false | boolean Whether multiple items can be selected at once. |
| disabled | false | boolean Whether the listbox is disabled. |
| loop | false | boolean Whether arrow keys wrap from the last item to the first and vice versa. |
| orientation | 'vertical' | 'vertical' | 'horizontal' The orientation of the listbox, which decides the arrow keys that move highlight. |
Also accepts all <div> attributes.
| Data attribute | Value |
|---|---|
data-leve-listbox | Always present |
data-orientation | "vertical" | "horizontal" |
data-disabled | Present when disabled |
Item#
| Prop | Default | Type |
|---|---|---|
| value (required) | – | string The value of the item. Unique in the listbox. |
| disabled | false | boolean Whether the item cannot be selected. Arrow keys and typeahead skip it. |
| textValue | – | string The text used by typeahead when it differs from the item's text content. |
Also accepts all <div> attributes.
| Data attribute | Value |
|---|---|
data-leve-listbox-item | Always present |
data-value | The value |
data-state | "checked" | "unchecked" |
data-highlighted | Present on the highlighted option |
data-disabled | Present when the item or the listbox is disabled |
data-orientation | "vertical" | "horizontal" |
data-text-value | The textValue, when set |
Focus stays on Listbox.Root, so style the highlighted option with [data-highlighted], not :focus.
ItemText#
No props of its own. Accepts all <span> attributes.
| Data attribute | Value |
|---|---|
data-leve-listbox-item-text | Always present |
ItemIndicator#
No props of its own. Accepts all <span> attributes.
| Data attribute | Value |
|---|---|
data-leve-listbox-item-indicator | Always present |
data-state | "checked" | "unchecked" |
It has the hidden attribute while the option is unselected. If you set display on it, hide it again with [hidden] { display: none; }.
Group#
No props of its own. Accepts all <div> attributes.
| Data attribute | Value |
|---|---|
data-leve-listbox-group | Always present |
GroupLabel#
No props of its own. Accepts all <div> attributes.
| Data attribute | Value |
|---|---|
data-leve-listbox-group-label | Always present |
Accessibility#
Complies with the Listbox WAI-ARIA design pattern.
Listbox.Rootrendersrole="listbox"withtabIndex={0}(or-1when disabled),aria-orientation, andaria-multiselectable="true"whenmultipleis enabled. Focus stays on the root element andaria-activedescendantpoints to the highlighted option.- Name the listbox with
aria-labeloraria-labelledbyonListbox.Root. - Each
Listbox.Itemrendersrole="option"andaria-selected. Disabled options renderaria-disabled="true"and are skipped by arrow keys,Home/End, and typeahead. Listbox.Grouprendersrole="group", named by itsListbox.GroupLabel.Listbox.ItemIndicatoris hidden from assistive technologies witharia-hidden="true", sincearia-selectedconveys the selection state.
Keyboard Interactions#
| Key | Description |
|---|---|
Tab | Moves focus into or out of the listbox. When focused, highlights the first selected enabled option or the first enabled option. |
ArrowDown / ArrowUp | Vertical only. Moves highlight to the next / previous enabled option. Wraps when loop is true. |
ArrowRight / ArrowLeft | Horizontal only. Moves highlight to the next / previous enabled option (reversed in RTL). Wraps when loop is true. |
Home / End | Moves highlight to the first / last enabled option. |
Space / Enter | Selects the highlighted option (or toggles its selection when multiple is true). |
Shift + ArrowDown / Shift + ArrowUp | Multiple only. Moves highlight and extends range selection to the next / previous enabled option. |
Shift + ArrowRight / Shift + ArrowLeft | Multiple + horizontal only. Moves highlight and extends range selection. |
Shift + Home / Shift + End | Multiple only. Moves highlight and extends range selection to the first / last enabled option. |
Ctrl+A / Cmd+A | Multiple only. Selects all enabled options. |
| Printable characters | Typeahead. Moves highlight to the matching enabled option starting with the typed characters. |