# List box

SvListBox: an assignee picker - inline multi-select with grouped options (departments), roving keyboard + type-ahead and a live summary.

Uses `@svgrid/grid` (MIT). Category: Selection. Live demo: https://svgrid.com/demos/312-listbox/

Install: `npm i @svgrid/grid` (Svelte 5).

## About this example

SvListBox in Svelte 5 as an assignee picker: an inline multi-select list with options grouped by department, a roving keyboard with type-ahead, a fixed number of visible rows and a live summary of the selection. Controlled through value and onChange with multiple on.

SvListBox - a production assignee picker: an inline multi-select list with grouped options (departments) and a live selection summary. Copy-paste ready.

## Imports, features and API used

- Imports: `@svgrid/grid`

## Frequently asked questions

### How are groups defined?

Options carry a group name; the list renders a heading per group and keeps keyboard navigation flowing across groups as one list.

### How does type-ahead work?

Typing letters while the list has focus jumps to the next option starting with them, following the WAI-ARIA listbox pattern; arrow keys move and Space toggles in multiple mode.

### How many rows are shown?

rows sets the visible height in options and the rest scroll; add virtual for very long lists.

## Related documentation

- [SvListBox](https://svgrid.com/docs/help/ui-components/sv-list-box.md)

## Related articles

- [A Tri-State Select-All Checkbox in SvGrid](https://svgrid.com/blog/tri-state-select-all/) - How SvGrid's rowSelectionFeature drives the header checkbox through unchecked, indeterminate, and checked states - and the pagination edge case that catches everyone.
- [Stable Row Identity in SvGrid (and why it matters)](https://svgrid.com/blog/stable-row-identity-getrowid/) - How SvGrid tracks rows across mutations, why object reference stability is the key to correct selection and editing, and the exact patterns to use with live data feeds.
- [A Fill Handle (Drag to Fill) in SvGrid](https://svgrid.com/blog/fill-handle-drag-to-fill/) - Build a working spreadsheet-style fill handle on top of SvGrid's cell selection and editing - pointer tracking, range highlighting, series fill, and undo/redo integration all covered.

## Source code (312-listbox.svelte)

```svelte
<script lang="ts">
  /**
   * SvListBox - a production assignee picker: an inline multi-select list with
   * grouped options (departments) and a live selection summary. Copy-paste ready.
   */
  import { SvListBox } from '@svgrid/grid'
  import type { ListOption } from '@svgrid/grid'

  const people: ListOption[] = [
    { value: 'amy', label: 'Amy Chen', group: 'Engineering' },
    { value: 'ben', label: 'Ben Ortiz', group: 'Engineering' },
    { value: 'cara', label: 'Cara Singh', group: 'Engineering' },
    { value: 'dan', label: 'Dan Weber', group: 'Design' },
    { value: 'eve', label: 'Eve Larsson', group: 'Design' },
    { value: 'finn', label: 'Finn Murphy', group: 'Product' },
    { value: 'gia', label: 'Gia Rossi', group: 'Product' },
  ]
  let assignees = $state<Array<string | number>>(['amy', 'dan'])
  const names = $derived(assignees.map((v) => people.find((p) => p.value === v)?.label).join(', '))
</script>

<div class="wrap">
  <header>
    <h2>List box</h2>
    <p>An inline single/multi-select list (WAI-ARIA listbox) with grouped options, roving keyboard and type-ahead. Assign work, build audiences, pick columns.</p>
  </header>

  <div class="card">
    <label class="ttl">Assignees</label>
    <SvListBox options={people} value={assignees} multiple rows={7} onChange={(v) => (assignees = v)} />
    <p class="summary">{assignees.length ? names : 'No one assigned'}</p>
  </div>
</div>

<style>
  .wrap { padding: 20px; max-width: 420px; display: flex; flex-direction: column; gap: 16px; }
  header h2 { margin: 0 0 4px; font-size: 20px; font-weight: 700; }
  header p { margin: 0; color: var(--sg-muted, #64748b); font-size: 13.5px; line-height: 1.5; }
  .card { display: flex; flex-direction: column; gap: 8px; }
  .ttl { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.03em; color: var(--sg-muted, #64748b); }
  .summary { margin: 0; font-size: 13px; color: var(--sg-fg, #0f172a); }
</style>
```

[View this example on GitHub](https://github.com/sv-grid/sv-grid/blob/main/examples/src/demos/312-listbox.svelte)
