# Listbox

A listbox presents a list of selectable options using the ARIA listbox role, with full keyboard navigation support. It is used when users need to select one or more items from a visible list, as opposed to a dropdown select. Common use cases include settings panels, filter selections, multi-select interfaces, and any scenario where all options should be visible simultaneously.

**Status:** stable — exercised in composed page flows under e2e and axe, beyond the per-component checks.

The component manages focus movement between options via arrow keys, with Home and End keys to jump to the first and last items. The consumer provides option elements as children and handles selection state externally.

## Implementation Notes

- Renders a `<div>` with `role="listbox"` and an accessible label.
- Uses reactive state to track the `listRef` element reference for querying child options.
- Keyboard navigation queries all child elements with `[role='option']` to build the navigable item list.
- Arrow keys wrap around: pressing ArrowDown on the last item moves focus to the first, and vice versa.
- Home and End keys jump to the first and last option respectively.
- Spreads `restProps` onto the container for consumer customization.

## Props

- `label`: string (required) -- accessible name applied via `aria-label`.
- `children`: slot (required) -- option elements to render inside the listbox (should have `role="option"` and `tabindex="-1"`).
- `as`: string (default: `"div"`) -- the root element. The pickers set `"ul"` for their `<ul class="{helper}-list">`; Blazor and Nunjucks always render `<ul>`.
- `baseClass`: string (default: `"listbox"`) -- the base class token, replaced outright (not appended), for a helper whose contract needs its own class.
- `navigation`: `"roving-focus"` | `"active-descendant"` (default: `"roving-focus"`) -- options take focus in turn, or focus stays on the listbox and `aria-activedescendant` names the active option.
- `activeIndex`: number (default: `-1`; bindable) -- the active option in active-descendant mode; -1 is none.
- `clamp`: boolean (default: `false`) -- arrow keys stop at the first and last option instead of wrapping (active-descendant mode).
- `typeahead`: boolean (default: `false`) -- printable characters move to the next option whose label starts with them (active-descendant mode).
- `pageSize`: number (default: `10`) -- how far PageUp and PageDown move (active-descendant mode).
- `onActivate`: (index: number) => void -- Enter or Space on the active option (active-descendant mode).
- `onEscape`: () => void -- Escape pressed (active-descendant mode).
- `onTabOut`: (event: KeyboardEvent) => void -- Tab pressed, before focus moves; Tab is never prevented (active-descendant mode).
- `ref`: HTMLElement (bindable) -- the rendered root element.
- `...restProps`: unknown -- additional attributes spread onto the root element.

## Usage

Priority level selector:

```html
<Listbox label="Select priority">
  <div
    role="option"
    tabindex="-1"
    aria-selected="true"
  >
    High
  </div>
  <div
    role="option"
    tabindex="-1"
    aria-selected="false"
  >
    Medium
  </div>
  <div
    role="option"
    tabindex="-1"
    aria-selected="false"
  >
    Low
  </div>
</Listbox>
```

Department filter in a settings panel:

```html
<Listbox label="Filter by department">
  {#each departments as dept}
  <div
    role="option"
    tabindex="-1"
    aria-selected="{selectedDept"
    =""
    =""
    ="dept}"
    onclick="{()"
    =""
  >
    selectedDept = dept} > {dept}
  </div>
  {/each}
</Listbox>
```

Theme selection with visible options:

```html
<Listbox label="Choose a theme">
  <div
    role="option"
    tabindex="-1"
    aria-selected="false"
  >
    Light
  </div>
  <div
    role="option"
    tabindex="-1"
    aria-selected="false"
  >
    Dark
  </div>
  <div
    role="option"
    tabindex="-1"
    aria-selected="true"
  >
    System
  </div>
</Listbox>
```

## Option groups

Long lists group their options under headings, as the WAI-ARIA APG grouped listbox does (tasks.md P40-T4). Groups are the
consumer's markup; no prop is needed:

- Wrap each group's options in an element with `role="group"`, labelled by its heading through `aria-labelledby`.
- The heading has `role="presentation"` and an `id`; it is not an option and is never focused or selected.
- The listbox finds options in document order through the groups, so ArrowDown on a group's last option moves to the next
  group's first, Home and End reach the first and last option of the whole list, and type-ahead searches every group.
- In a `<ul>` listbox (Nunjucks), each group is `<li role="presentation"><ul role="group" aria-labelledby="…">`, with the
  heading as its first `<li role="presentation">`.
- In Blazor, `OptionCount`, `OptionId` and `OptionLabel` count options across all groups by index.
- Keep group headings short and in the consumer's words; a list of a few options needs no groups.

```html
<div
  class="listbox"
  role="listbox"
  aria-label="Diagnosis codes"
>
  <div
    role="group"
    aria-labelledby="codes-common"
  >
    <div
      role="presentation"
      id="codes-common"
    >
      Common
    </div>
    <div
      role="option"
      tabindex="0"
      aria-selected="false"
    >
      Asthma
    </div>
    <div
      role="option"
      tabindex="-1"
      aria-selected="false"
    >
      Hypertension
    </div>
  </div>
  <div
    role="group"
    aria-labelledby="codes-recent"
  >
    <div
      role="presentation"
      id="codes-recent"
    >
      Recently used
    </div>
    <div
      role="option"
      tabindex="-1"
      aria-selected="false"
    >
      Migraine
    </div>
  </div>
</div>
```

## Keyboard Interactions

- ArrowDown: Moves focus to the next option, wrapping to the first option from the last.
- ArrowUp: Moves focus to the previous option, wrapping to the last option from the first.
- Home: Moves focus to the first option.
- End: Moves focus to the last option.

## ARIA

- `role="listbox"` -- identifies the container as a listbox widget for selecting from a list of options.
- `aria-label` -- provides an accessible name describing the purpose of the listbox.
- Child elements should use `role="option"` and optionally `aria-selected` to indicate selection state.

## When to Use

- Use Listbox when users need to select one or more items from a visible list of options, such as filter panels or settings.
- Use Listbox instead of a Select when all options should be visible simultaneously without opening a dropdown.
- Use Listbox for preference or settings panels where the user chooses from a short to medium list.
- Use Listbox for multi-select interfaces where users need to see all available options at once.

## When Not to Use

- Do not use Listbox for dropdown selection where space is limited -- use Select or Combobox instead.
- Do not use Listbox for checkable items with independent on/off states -- use CheckboxGroup instead.
- Do not use Listbox for very long option lists that benefit from type-ahead filtering -- use Combobox instead.

## Headless

This headless component provides the ARIA listbox role, keyboard navigation (arrow keys, Home, End with wrapping), and focus management for child option elements. The consumer provides all visual styling, including option appearance, selected-state highlighting, scrolling behavior, and focus indicators.

## Styles

The consumer provides all CSS styling. The component renders with a `.listbox` class for targeting. No default styles are included — this is a fully headless component.

## Testing

- Verify the component renders a `<div>` element with class `listbox`
- Verify the root has `role="listbox"` and its `aria-label`
- Verify child options keep `role="option"` and their `aria-selected` state
- Verify that with option groups the arrows cross from one group to the next and type-ahead searches every group
- Verify keyboard interactions work correctly
- Verify pass-through attributes are applied

## Advice

- **Designers**: Provide clear visual distinction for the selected and focused option states, using both color and a non-color indicator to support color-blind users.
- **Developers**: Ensure each child option uses `role="option"` with `tabindex="-1"` and manage `aria-selected` on options to reflect the current selection state.

## Related components

- `select` — a dropdown select element for choosing one option
- `combobox` — a text input combined with a dropdown list for filtering options
- `option` — an option element within a select dropdown

## Known limitations

None known.

## References

- WAI-ARIA Listbox Pattern: https://www.w3.org/WAI/ARIA/apg/patterns/listbox/

---

Lily™ and Lily Design System™ are trademarks.
