# Typeahead

A text field that suggests values as you type: `<datalist>` first, an APG combobox where it falls short.

_Diagram: A typeahead is a text field in a form whose list of suggestions opens right under it as you type._

## Live example

### Typeahead

```html
<dui-combobox>
  <label for="city">City</label>
  <input type="text" id="city" name="city" list="city-options">
  <datalist id="city-options">
    <option value="Amsterdam"></option>
    <option value="Athens"></option>
    <option value="Berlin"></option>
    <option value="Bern"></option>
    <option value="Brussels"></option>
    <option value="Copenhagen"></option>
    <option value="Dublin"></option>
    <option value="Lisbon"></option>
    <option value="Madrid"></option>
    <option value="Oslo"></option>
    <option value="Paris"></option>
    <option value="Rome"></option>
    <option value="Vienna"></option>
  </datalist>
</dui-combobox>
```

### Typeahead in a form

```html
<form id="ship-form" class="stack">
  <dui-combobox>
    <label for="carrier">Carrier</label>
    <input type="text" id="carrier" name="carrier" list="carrier-options" required>
    <datalist id="carrier-options">
      <option value="DHL Express" label="Next day"></option>
      <option value="DPD Classic" label="2 to 3 days"></option>
      <option value="FedEx International" label="Next day"></option>
      <option value="PostNL Parcel" label="1 to 2 days"></option>
      <option value="UPS Standard" label="2 to 5 days"></option>
    </datalist>
  </dui-combobox>
  <p><button type="submit">Ship order</button></p>
  <p role="status" id="ship-result"></p>
</form>
```

### Options loaded by the page

```html
<dui-combobox id="country-combobox">
  <label for="country">Country</label>
  <input type="text" id="country" name="country" list="country-options">
  <datalist id="country-options"></datalist>
</dui-combobox>
```

## Attributes

| Attribute or value | Effect |
| --- | --- |
| `<dui-combobox>` | wraps a `<label>`, an `<input list>` and its `<datalist>` |
| `<option value="…" label="…">` | the suggestions; an optional label shows as a hint |
| `dui-combobox-filter` | event, cancelable, detail { query }: cancel it to load options yourself |
| `element.setOptions([...])` | replaces the options: strings or { value, label } objects |

### Keyboard

| Key | Action |
| --- | --- |
| <kbd>Down Arrow</kbd> | opens the list and moves to the next option (the first if none) |
| <kbd>Up Arrow</kbd> | opens the list and moves to the previous option (the last if none) |
| <kbd>Alt+Down Arrow</kbd> | opens the list without moving to an option |
| <kbd>Enter</kbd> | accepts the active option and closes the list |
| <kbd>Escape</kbd> | closes the list; if it is already closed, clears the input |
| <kbd>Home</kbd> / <kbd>End</kbd> | moves the caret to the start or end of the input; the list loses its active option |
| <kbd>Typing</kbd> | filters the options |
| <kbd>Tab</kbd> | closes the list without accepting an option |

## Accessibility

- A text field that suggests values as you type, following the APG Combobox pattern, list autocomplete variant: the input keeps focus, a listbox of matching options opens below it, and the arrow keys move an active option shown with aria-activedescendant.
- The listbox is exactly as wide as the combobox, so it never overflows the viewport sideways (WCAG 1.4.10), and it needs neither anchor positioning nor the popover fallback that centers a panel at the bottom of the viewport.
- The input has role="combobox", aria-autocomplete="list", aria-expanded and aria-controls; the list is role="listbox", named by the label, with role="option" items.
- Focus never leaves the input; the active option is aria-activedescendant and has aria-selected="true", and it is drawn with an outline in the focus color (3:1, WCAG 1.4.11) and a tint, so it does not rely on color alone (WCAG 1.4.1).
- A polite role="status" region, in the page from the upgrade on, announces how many options match after typing pauses (WCAG 4.1.3).
- Options are at least --dui-size-row tall (32px, 28px compact, 44px on touch; WCAG 2.5.8).
- In forced colors mode the list keeps its border and the active option its outline, in system colors.
- The list fades and grows from --dui-motion-zoom over --dui-motion-duration-fast, entering with --dui-motion-easing-enter and leaving with --dui-motion-easing-exit; the zoom runs for that duration times --dui-motion-movement, so reduced motion keeps only the fade (WCAG 2.3.3).

## Specifications

- [HTML `<datalist>` element](https://html.spec.whatwg.org/multipage/form-elements.html#the-datalist-element)
- [APG Combobox pattern](https://www.w3.org/WAI/ARIA/apg/patterns/combobox/)
- [MDN `<datalist>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/datalist)
- [APG combobox autocomplete list](https://www.w3.org/WAI/ARIA/apg/patterns/combobox/examples/combobox-autocomplete-list/)
- [MDN `combobox` role](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Roles/combobox_role)
- [MDN `listbox` role](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Roles/listbox_role)
- [WCAG Status messages](https://www.w3.org/WAI/WCAG22/Understanding/status-messages.html)
- [WCAG Target size minimum](https://www.w3.org/WAI/WCAG22/Understanding/target-size-minimum.html)

## See also

- [Molecules](https://domestic-ui.odonckers.dev/molecules/index.md)
- [Search dialog](https://domestic-ui.odonckers.dev/patterns/search-dialog.md): a typeahead in a modal dialog, to search a whole site
- Source: [`css/components/molecules/combobox.css`](https://github.com/odonckers/domestic-ui/blob/main/css/components/molecules/combobox.css), [`examples/molecules/combobox.html`](https://github.com/odonckers/domestic-ui/blob/main/examples/molecules/combobox.html)
- Demos: Orders view ([Plain HTML](https://github.com/odonckers/domestic-ui/blob/main/demos/vanilla/orders.html), [React](https://github.com/odonckers/domestic-ui/blob/main/demos/react/src/OrdersPage.tsx), [Angular](https://github.com/odonckers/domestic-ui/tree/main/demos/angular/src/app/orders), [Astro](https://github.com/odonckers/domestic-ui/blob/main/demos/astro/src/pages/orders.astro), [Vue](https://github.com/odonckers/domestic-ui/blob/main/demos/vue/src/OrdersPage.vue), [Svelte](https://github.com/odonckers/domestic-ui/blob/main/demos/svelte/src/OrdersPage.svelte))

## How other systems do it

For comparison only; these are not the standard this system follows.

- [Apple Human Interface Guidelines: Combo boxes](https://developer.apple.com/design/human-interface-guidelines/combo-boxes)
- Material Design 3: No equivalent
- [Vercel Geist: Combobox](https://vercel.com/geist/combobox)
