Search docs

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.

Fruit
Apple
Apricot
Banana
Blueberry
Blackberry
Cherry
Grape
Peach

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>
  );
});

Installation#

Install the individual package, or use @leve-ui/core to get every component at once:

pnpm add @leve-ui/listbox

Anatomy#

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>
PartElementDescription
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.

Toppings
Avocado
Bacon
Cheese
Lettuce
Onion
Truffle
Tomato

Selected: none

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.

Time zone
Americas
New York
Chicago
Los Angeles
Europe
London
Paris
Berlin
Asia
Tokyo
Seoul
Singapore

Selected: none

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.

Fruit (disabled)
Apple
Banana
Cherry

API Reference#

Root#

PropDefaultType
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.

multiplefalseboolean

Whether multiple items can be selected at once.

disabledfalseboolean

Whether the listbox is disabled.

loopfalseboolean

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 attributeValue
data-leve-listboxAlways present
data-orientation"vertical" | "horizontal"
data-disabledPresent when disabled

Item#

PropDefaultType
value (required)–string

The value of the item. Unique in the listbox.

disabledfalseboolean

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 attributeValue
data-leve-listbox-itemAlways present
data-valueThe value
data-state"checked" | "unchecked"
data-highlightedPresent on the highlighted option
data-disabledPresent when the item or the listbox is disabled
data-orientation"vertical" | "horizontal"
data-text-valueThe 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 attributeValue
data-leve-listbox-item-textAlways present

ItemIndicator#

No props of its own. Accepts all <span> attributes.

Data attributeValue
data-leve-listbox-item-indicatorAlways 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 attributeValue
data-leve-listbox-groupAlways present

GroupLabel#

No props of its own. Accepts all <div> attributes.

Data attributeValue
data-leve-listbox-group-labelAlways present

Accessibility#

Complies with the Listbox WAI-ARIA design pattern.

  • Listbox.Root renders role="listbox" with tabIndex={0} (or -1 when disabled), aria-orientation, and aria-multiselectable="true" when multiple is enabled. Focus stays on the root element and aria-activedescendant points to the highlighted option.
  • Name the listbox with aria-label or aria-labelledby on Listbox.Root.
  • Each Listbox.Item renders role="option" and aria-selected. Disabled options render aria-disabled="true" and are skipped by arrow keys, Home/End, and typeahead.
  • Listbox.Group renders role="group", named by its Listbox.GroupLabel. Listbox.ItemIndicator is hidden from assistive technologies with aria-hidden="true", since aria-selected conveys the selection state.

Keyboard Interactions#

KeyDescription
TabMoves focus into or out of the listbox. When focused, highlights the first selected enabled option or the first enabled option.
ArrowDown / ArrowUpVertical only. Moves highlight to the next / previous enabled option. Wraps when loop is true.
ArrowRight / ArrowLeftHorizontal only. Moves highlight to the next / previous enabled option (reversed in RTL). Wraps when loop is true.
Home / EndMoves highlight to the first / last enabled option.
Space / EnterSelects the highlighted option (or toggles its selection when multiple is true).
Shift + ArrowDown / Shift + ArrowUpMultiple only. Moves highlight and extends range selection to the next / previous enabled option.
Shift + ArrowRight / Shift + ArrowLeftMultiple + horizontal only. Moves highlight and extends range selection.
Shift + Home / Shift + EndMultiple only. Moves highlight and extends range selection to the first / last enabled option.
Ctrl+A / Cmd+AMultiple only. Selects all enabled options.
Printable charactersTypeahead. Moves highlight to the matching enabled option starting with the typed characters.