Typeahead
A text field that suggests values as you type: <datalist> first, an APG combobox where it falls short.
Live example
Typeahead
<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
<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
<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 |
|---|---|
| Down Arrow | opens the list and moves to the next option (the first if none) |
| Up Arrow | opens the list and moves to the previous option (the last if none) |
| Alt+Down Arrow | opens the list without moving to an option |
| Enter | accepts the active option and closes the list |
| Escape | closes the list; if it is already closed, clears the input |
| Home / End | moves the caret to the start or end of the input; the list loses its active option |
| Typing | filters the options |
| Tab | 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 - APG Combobox pattern
- MDN
<datalist> - APG combobox autocomplete list
- MDN
comboboxrole - MDN
listboxrole - WCAG Status messages
- WCAG Target size minimum
See also
- Molecules
- Search dialog: a typeahead in a modal dialog, to search a whole site
- Source:
css/components/molecules/combobox.css,examples/molecules/combobox.html - Demos: Orders view (Plain HTML, React, Angular, Astro, Vue, Svelte)
How other systems do it
For comparison only; these are not the standard this system follows.
- Apple Human Interface Guidelines: Combo boxes
- Material Design 3: No equivalent
- Vercel Geist: Combobox