# Listbox

> A persistent single- or multiple-selection list with APG keyboard support, checkboxes, select all, a selection limit, disabled items, form submission and styling parts.

Canonical: https://salmonui.dev/docs/dropdowns/listbox

`Listbox` is a list that is always on the page, not in a popup.
It follows the WAI-ARIA Authoring
Practices listbox pattern and ships from its own subpath, so an application
that does not use it pays nothing for it:

```tsx
import { Listbox } from '@salmonui/dropdowns/listbox';
import '@salmonui/dropdowns/listbox/styles.css';
```

### Live demo: Listbox basics

Live demo: https://salmonui.dev/demos/dropdowns/listbox-basic

A single-selection Listbox where selection follows focus, and a multiple-selection Listbox with checkboxes, a tri-state select all, a five-item limit and a disabled item.

`Demo.tsx`:

```tsx
'use client';

import { useState } from 'react';
import { Listbox, type ListboxKey } from '@salmonui/dropdowns/listbox';

interface Fruit {
  value: string;
  label: string;
  disabled?: boolean;
}

// Objects with `value` and `label` need no accessors; `disabled` items are
// skipped by the arrow keys and type-ahead.
const fruits: Fruit[] = [
  { value: 'apple', label: 'Apple' },
  { value: 'apricot', label: 'Apricot' },
  { value: 'banana', label: 'Banana' },
  { value: 'blueberry', label: 'Blueberry' },
  { value: 'cherry', label: 'Cherry', disabled: true },
  { value: 'date', label: 'Date' },
  { value: 'elderberry', label: 'Elderberry' },
  { value: 'fig', label: 'Fig' },
  { value: 'grape', label: 'Grape' },
  { value: 'kiwi', label: 'Kiwi' },
  { value: 'lemon', label: 'Lemon' },
  { value: 'mango', label: 'Mango' },
];

export default function ListboxBasicDemo() {
  // Single selection, controlled: selection follows focus on the arrow keys.
  const [fruit, setFruit] = useState<ListboxKey | null>('banana');
  // Multiple selection, controlled, with checkboxes, select all and a limit.
  const [basket, setBasket] = useState<readonly ListboxKey[]>(['apple', 'kiwi']);
  return (
    <div style={{ display: 'flex', flexWrap: 'wrap', gap: 24, alignItems: 'start' }}>
      <div style={{ minWidth: 200 }}>
        <p id="listbox-basic-single">Favourite fruit</p>
        <Listbox data={fruits} aria-labelledby="listbox-basic-single" value={fruit} onValueChange={(value) => setFruit(value)} maxVisibleItems={6} />
        <p>
          Value: <output>{String(fruit)}</output>
        </p>
      </div>
      <div style={{ minWidth: 220 }}>
        <p id="listbox-basic-multiple">Basket (at most 5)</p>
        <Listbox
          data={fruits}
          multiple
          indicator="checkbox"
          showSelectAll
          maxSelected={5}
          aria-labelledby="listbox-basic-multiple"
          value={basket}
          onValueChange={(value) => setBasket(value)}
          maxVisibleItems={6}
        />
        <p>
          Value: <output>{basket.join(', ') || 'none'}</output>
        </p>
      </div>
    </div>
  );
}
```

## Data and accessors

`data` is an array that `Listbox` never copies. Objects with `value` and
`label` fields need no configuration; a primitive array
(`string[]`, `number[]`, `boolean[]`) uses each primitive as both value and
label:

```tsx
<Listbox data={['Small', 'Medium', 'Large']} aria-label="Size" />
```

Other shapes are read through the `getItemValue` and `getItemLabel`
accessor functions. Nested fields need no string
paths: the accessor reads them directly:

```tsx
<Listbox
  data={countries}
  getItemValue={(c) => c.Country.Code}
  getItemLabel={(c) => c.Country.Name}
  isItemDisabled={(c) => !c.shipping}
  getItemIcon={(c) => <Flag code={c.Country.Code} />}
  aria-label="Country"
/>
```

Keep accessors stable (module scope or `useCallback`) so the list does not
re-index on every render.

- `isItemDisabled(item)` (default: the `disabled` field) is evaluated on
  render, so it is reactive. Disabled options carry `aria-disabled`, cannot
  be selected and are skipped by the arrow keys and type-ahead.
- `getItemIcon(item)` returns a `ReactNode` shown before the label;
use `renderItem`
  ([Listbox data and templates](https://salmonui.dev/docs/dropdowns/listbox-data#templates)) for
  anything richer.
- Item values must be unique. In development a
  duplicate value logs one warning naming the duplicates.

### Live demo: Listbox data shapes and options

Live demo: https://salmonui.dev/demos/dropdowns/listbox-options

A primitive array, nested objects read through accessors, unique values behind equal labels, a checkbox at the inline end, a fixed height and per-option props.

`Demo.tsx`:

```tsx
'use client';

import { useState } from 'react';
import { Listbox, type ListboxKey } from '@salmonui/dropdowns/listbox';

interface Office {
  readonly office: { readonly code: string; readonly city: string; readonly state: string };
  readonly closed: boolean;
}

const SIZES = ['XS', 'S', 'M', 'L', 'XL'];

// Two offices share the city name "Springfield": the label may repeat, the value (the code) may not.
const OFFICES: Office[] = [
  { office: { code: 'BOS', city: 'Boston', state: 'MA' }, closed: false },
  { office: { code: 'CHI', city: 'Chicago', state: 'IL' }, closed: false },
  { office: { code: 'DEN', city: 'Denver', state: 'CO' }, closed: true },
  { office: { code: 'SPI', city: 'Springfield', state: 'IL' }, closed: false },
  { office: { code: 'SGF', city: 'Springfield', state: 'MO' }, closed: false },
  { office: { code: 'SEA', city: 'Seattle', state: 'WA' }, closed: false },
  { office: { code: 'AUS', city: 'Austin', state: 'TX' }, closed: true },
];

// Accessors at module scope stay stable, so the list never re-indexes on render.
const officeValue = (item: Office) => item.office.code;
const officeLabel = (item: Office) => `${item.office.city}, ${item.office.state}`;
const officeProps = (item: Office) => (item.closed ? { title: 'Closed for renovation', 'data-closed': true } : { title: `Office code ${item.office.code}` });

export default function ListboxOptionsDemo() {
  const [size, setSize] = useState<ListboxKey | null>('M');
  const [offices, setOffices] = useState<readonly ListboxKey[]>(['SPI']);
  return (
    <div style={{ display: 'flex', flexWrap: 'wrap', gap: 24, alignItems: 'start' }}>
      <div style={{ minWidth: 160 }}>
        <p id="listbox-options-size">Size</p>
        <Listbox data={SIZES} aria-labelledby="listbox-options-size" value={size} onValueChange={(next) => setSize(next)} />
        <p>
          Value: <output>{String(size)}</output>
        </p>
      </div>
      <div style={{ minWidth: 240 }}>
        <p id="listbox-options-offices">Offices</p>
        <Listbox
          data={OFFICES}
          multiple
          getItemValue={officeValue}
          getItemLabel={officeLabel}
          getItemProps={officeProps}
          indicator="checkbox"
          indicatorPosition="end"
          maxVisibleItems={4}
          aria-labelledby="listbox-options-offices"
          value={offices}
          onValueChange={(next) => setOffices(next)}
        />
        <p>
          Values: <output>{offices.join(', ') || '(none)'}</output>
        </p>
      </div>
    </div>
  );
}
```

## Single and multiple selection

Single selection is the default; `multiple` turns on multiple selection.

The value holds item values, never the item objects: one `ListboxKey` (or
`null`) in a single list, an array of them with `multiple`. It is
controlled with `value` plus `onValueChange`, or uncontrolled with
`defaultValue` (switching between the two logs a development warning):

```tsx
const [value, setValue] = useState<ListboxKey | null>(null);
<Listbox data={fruits} value={value} onValueChange={(next) => setValue(next)} aria-label="Fruit" />;

const [basket, setBasket] = useState<readonly ListboxKey[]>([]);
<Listbox data={fruits} multiple value={basket} onValueChange={(next) => setBasket(next)} aria-label="Basket" />;
```

`onValueChange(value, event)` receives a frozen event with `reason`
(`'select'`, `'deselect'`, `'selectAll'`, `'clear'` or `'reset'`), `source`
(`'pointer'`, `'keyboard'`, `'touch'` or `'api'`), the loaded `items` of the
new value and the `previousValue`.

### Checkboxes, select all and the selection limit

- `indicator` picks the selected-option marker: `'check'` (default) or
  `'checkbox'`.
  `indicatorPosition` (`'start'` default, or `'end'`) places it at the
  inline start or end; both mirror in right-to-left layouts.
- `showSelectAll` adds a tri-state "Select all" checkbox above a multiple
  list. It selects every enabled item, or clears the selection
  when all are selected. To select all from code, set `value`.
- `maxSelected` caps the number of values. Once the limit is reached the unselected
  options become unavailable, and an attempt to select past it (click,
  Space, a range or select all) announces `localeText.selectionLimit` in a
  polite live region.

The other events live on the feature pages: `onSearchChange` and
`onLoadError` on [Listbox search, grouping and remote
data](https://salmonui.dev/docs/dropdowns/listbox-data), and `onReorder`, `onItemDragStart`,
`onItemDrag` and `onItemDrop` on [Dual list and
reordering](https://salmonui.dev/docs/dropdowns/dual-list).

## Height

The list shows `maxVisibleItems` items (default 8) and scrolls beyond that;
`style.height` (or a `className`) sets an explicit height instead.
Page Up and Page Down move by
`maxVisibleItems`.

## Keyboard

`Listbox` uses the APG recommended multi-select model. On macOS,
Cmd replaces Ctrl.

| Key | Single | Multiple |
| --- | --- | --- |
| Down / Up | Move focus and select (selection follows focus) | Move focus |
| Ctrl+Down / Ctrl+Up | Move focus without selecting | Move focus |
| Home / End | First / last enabled option, selected | First / last enabled option |
| Page Down / Page Up | Move by `maxVisibleItems`, selected | Move by `maxVisibleItems` |
| Space / Enter | Select | Toggle the focused option |
| Shift+Down / Shift+Up | | Move focus and toggle the option it lands on |
| Shift+Space | | Select the range from the anchor (the last option toggled by click, Space or Shift+arrow; plain focus moves keep it) to the focused option |
| Ctrl+Shift+Home / End | | Select from the focused option to the first / last option |
| Ctrl+A | | Select all enabled options; when all are selected, clear the selection |
| Printable characters | Type-ahead: focus and select the next matching option | Type-ahead: focus the next matching option |
| Alt+Down / Alt+Up | Reorder ([Dual list](https://salmonui.dev/docs/dropdowns/dual-list)) | Reorder the selected options |

Type-ahead is a locale-aware prefix match (`Intl.Collator`) with
a 500 ms buffer; repeating one letter cycles through the options that start
with it. It skips disabled options and works on virtual lists, where the
matching option is scrolled into view.

Pointer: a click selects in a single list and toggles in a multiple list;
Shift+click in a multiple list selects the range from the anchor.

## Grid layout: controls inside items and tile galleries

An APG listbox option may not contain buttons or links. When the items of a
selectable list hold their own controls (item templates with buttons),
or when the items are tiles laid out several per
row (a gallery in a popover), set `layout="grid"`. The list then
follows the APG Grid pattern: the focusable element is `role="grid"`, each
row of `columns` items (default 1) is a `role="row"`, and each item is a
`role="gridcell"` with `aria-selected`.

### Live demo: Listbox grid layout

Live demo: https://salmonui.dev/demos/dropdowns/listbox-grid

A selectable list whose rows hold their own buttons, and a four-column tile gallery, both on the APG Grid pattern of Listbox layout="grid".

`Demo.tsx`:

```tsx
'use client';

import { useState } from 'react';
import { Button } from '@salmonui/buttons/button';
import { Listbox, type ListboxKey } from '@salmonui/dropdowns/listbox';

interface Message {
  value: string;
  label: string;
}

const messages: Message[] = [
  { value: 'm1', label: 'Quarterly report' },
  { value: 'm2', label: 'Team lunch on Friday' },
  { value: 'm3', label: 'Release checklist' },
  { value: 'm4', label: 'Design review notes' },
  { value: 'm5', label: 'Travel booking' },
];

// An SVG glyph at the `sm` icon size (14px), filled when starred; never a font character.
function StarIcon({ filled }: { filled: boolean }) {
  return (
    <svg width={14} height={14} viewBox="0 0 24 24" aria-hidden="true" focusable="false" fill={filled ? 'currentColor' : 'none'} stroke="currentColor" strokeWidth={2} strokeLinejoin="round">
      <path d="M12 2.5l2.9 6.1 6.6.8-4.9 4.6 1.3 6.6L12 17.3l-5.9 3.3 1.3-6.6-4.9-4.6 6.6-.8z" />
    </svg>
  );
}

const swatches = ['Coral', 'Amber', 'Lime', 'Teal', 'Sky', 'Indigo', 'Violet', 'Rose', 'Slate', 'Sand', 'Moss', 'Ink'].map((label) => ({ value: label.toLowerCase(), label }));

export default function ListboxGridDemo() {
  // Rows that hold their own buttons: Tab or Right Arrow reaches them, Up / Down move between rows.
  const [selected, setSelected] = useState<ListboxKey | null>('m2');
  const [starred, setStarred] = useState<readonly string[]>(['m3']);
  // A gallery of tiles, four per row: arrows move in two dimensions, Enter or Space picks.
  const [swatch, setSwatch] = useState<ListboxKey | null>('teal');
  return (
    <div style={{ display: 'flex', flexWrap: 'wrap', gap: 24, alignItems: 'start' }}>
      <div style={{ minWidth: 280 }}>
        <p id="listbox-grid-rows">Messages</p>
        <Listbox
          layout="grid"
          data={messages}
          value={selected}
          onValueChange={(value) => setSelected(value)}
          aria-labelledby="listbox-grid-rows"
          styles={{ option: { blockSize: 40 } }}
          renderItem={({ item, label }) => (
            <span style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
              <span style={{ flex: 1 }}>{label}</span>
              <Button
                variant="ghost"
                iconOnly
                aria-pressed={starred.includes(item.value)}
                aria-label={`Star ${label}`}
                onClick={() => setStarred((s) => (s.includes(item.value) ? s.filter((v) => v !== item.value) : [...s, item.value]))}
              >
                <StarIcon filled={starred.includes(item.value)} />
              </Button>
            </span>
          )}
        />
        <p>
          Selected: <output>{String(selected)}</output>; starred: <output>{starred.join(', ') || 'none'}</output>
        </p>
      </div>
      <div style={{ minWidth: 280 }}>
        <p id="listbox-grid-gallery">Accent swatch</p>
        <Listbox
          layout="grid"
          columns={4}
          data={swatches}
          value={swatch}
          onValueChange={(value) => setSwatch(value)}
          maxVisibleItems={3}
          aria-labelledby="listbox-grid-gallery"
          styles={{ option: { blockSize: 56, justifyContent: 'center' } }}
        />
        <p>
          Swatch: <output>{String(swatch)}</output>
        </p>
      </div>
    </div>
  );
}
```

```tsx
<Listbox
  layout="grid"
  data={messages}
  value={selected}
  onValueChange={setSelected}
  aria-label="Messages"
  renderItem={({ item, label }) => (
    <span className="message-row">
      <span>{label}</span>
      <button type="button" aria-label={`Star ${label}`} onClick={() => star(item.value)}>☆</button>
    </span>
  )}
/>

<Listbox layout="grid" columns={4} data={swatches} maxVisibleItems={3} aria-label="Accent swatch" />
```

| Key | On the grid | On a control inside a cell |
| --- | --- | --- |
| Down / Up | The same column of the next / previous row (a shorter last row clamps) | Back to the grid, next / previous row |
| Right / Left | Right: the cell's first control, else the next cell of the row; Left: the previous cell. Mirrored in right-to-left layouts | Next / previous control; Left on the first control returns to the cell, Right on the last moves to the next cell |
| Home / End | First / last cell of the row | |
| Ctrl+Home / Ctrl+End | First / last cell | |
| Page Down / Page Up | Move by `maxVisibleItems` rows | |
| Space / Enter | Select (single) or toggle (multiple) the active cell | Belong to the control |
| Shift+Space, Ctrl+A | Range from the anchor, select all (multiple) | |
| Tab | The active cell's controls, then out of the list | The next control of the active cell, then out of the list |
| Escape | | Back to the grid on the same cell |
| Printable characters | Type-ahead moves the active cell | Belong to the control |

- Selection does not follow focus in the grid layout: the arrow keys only
  move the active cell, so browsing a gallery does not apply every tile.
- Only the active cell's controls are in the Tab order; the controls of the
  other cells get `tabindex="-1"` until their cell becomes active. A click
  on a control makes its cell active without changing the selection; a
  click elsewhere in the cell selects it.
- Text fields inside a cell keep every key except Escape.
- Rows are one fixed-height unit for the virtual list (above 100 items by
  default), so `maxVisibleItems` counts rows. Size tiles with
  `styles.option` or `classNames.option`; the row is a CSS grid of
  `columns` equal tracks (`.salmon-listbox-row`).
- The grid layout has no group headers and no reordering: `getItemGroup`,
  `reorderable` and `dragGroup` are list-layout features and are ignored,
  with a development warning.

## Accessibility

The focusable element has `role="listbox"`, with `aria-multiselectable` on
a multiple list, and each option has `role="option"` with `aria-selected`
and `aria-disabled`. Name the list with `aria-label` or
`aria-labelledby`.

Focus stays on the listbox element; the active option is announced through
`aria-activedescendant`. Under virtualization the active option is always
mounted, even when it is scrolled out of view, so the reference never
dangles. Options carry `aria-setsize` and `aria-posinset`, so a
screen reader announces "12 of 10,000" while only a window of options is in
the DOM. A remote list sets `aria-busy` while it loads.

`disabled` disables the whole list and `readOnly` keeps it focusable while
blocking changes (`aria-readonly`); `required` sets `aria-required`. With
`layout="grid"` the roles are those of the grid layout above.

## Styling and parts

`className` and `style` go to the root element. `classNames` and `styles`
target the parts: `root`, `search`, `selectAll`, `list`, `option`, `group`,
`groupHeader` and `empty`:

```tsx
<Listbox
  data={fruits}
  className="fruit-list"
  classNames={{ option: 'fruit-option' }}
  styles={{ list: { maxBlockSize: 320 } }}
  getItemProps={(item, { selected }) => ({ title: item.label, 'data-selected': selected })}
  aria-label="Fruit"
/>
```

`getItemProps(item, { selected, disabled })` returns per-option
`className`, `style`, `title` and `data-*` attributes. It is pure and runs
for rendered options only.

The `ref` receives the `role="listbox"` element (`role="grid"` with
`layout="grid"`), so `ref.current.focus()` focuses the list. Colors come
from the theme
([Appearance](https://salmonui.dev/docs/data-grid/appearance)).

## Forms

With `name`, `Listbox` renders a hidden native `<select>` that submits the
value with its form (one entry per value in a multiple list); `required`
and `form` work as on native fields. Resetting the owning form restores
`defaultValue` and fires `onValueChange` with reason `'reset'`:

```tsx
<form action="/order">
  <Listbox data={fruits} name="fruit" required defaultValue="banana" aria-label="Fruit" />
  <Listbox data={fruits} name="basket" multiple defaultValue={['apple', 'kiwi']} aria-label="Basket" />
  <button type="reset">Reset</button>
  <button type="submit">Order</button>
</form>
```

## Localization

Static texts come from `localeText`, a partial `ListboxLocaleText` merged
over `LISTBOX_DEFAULT_LOCALE_TEXT`: `noData`, `noResults`, `loading`,
`loadError`, `retry`, `searchLabel`, `selectAll`, `resultCount` (`{count}`,
plural forms by `Intl.PluralRules`), `selectionLimit` (`{max}`) and `moved`
(`{label}`, `{position}`, `{count}`). It is plain JSON, safe to pass from a
server component:

```tsx
<Listbox
  data={fruits}
  multiple
  showSelectAll
  localeText={{ selectAll: 'Alle auswählen', noData: 'Keine Einträge', resultCount: { one: '1 Treffer', other: '{count} Treffer' } }}
  aria-label="Obst"
/>
```

## Server rendering

`Listbox` is a client component that renders on the server (Next App
Router, Vite SSR): the options, the selected state, the ids (`useId`) and
the hidden `<select>` are deterministic, so hydration matches. The search
index, the grid-layout keyboard code and the drag code are separate chunks
loaded after hydration. A remote source renders its loading state on the
server and starts reading on the client.

## Changing data and selection from code

`Listbox` has no imperative data or selection methods. The
application owns `data` and `value`; a copy mutated inside the component
would disagree with them, and lookups by value are plain array work. Every
task goes through the controlled props:

| Task | How |
| --- | --- |
| Add items | `setData([...data, ...items])` |
| Remove items | Filter `data` |
| Disable items | `isItemDisabled` |
| Select values | `setValue(values)` |
| Read the items | The `data` array |
| Read the items sorted | Sort `data` yourself |
| Find items by value | Look up in `data`, or read `event.items` |
| Filter the list | Filter `data`, or use `searchable` |

Moving items is two pure functions from `@salmonui/dropdowns/listbox`,
since list order lives in your array; pure functions are
testable without a component and work for any number of lists:
`reorderItems(list, values, delta)` returns the reordered array and
`moveItems(source, target, values, index?)` returns `[nextSource,
nextTarget]` (both take an optional last `getItemValue`). Moving the
selected items up one place:

```tsx
setItems(reorderItems(items, selected, -1));
```

A large `delta` moves them to the top or bottom. The
[dual list](https://salmonui.dev/docs/dropdowns/dual-list) recipe uses `moveItems`. Every prop
and type is listed in the [API reference](https://salmonui.dev/docs/reference/dropdowns/listbox).
