#686 Accessible Combobox

medium
javascript
accessibility
dom

Search boxes that suggest matches as you type ("autocomplete") are everywhere: country pickers, tag inputs, command palettes. Drawing a list under an input is the easy part. A screen reader user also needs to hear that suggestions exist, which one is highlighted, and when the list closes. A keyboard user needs to move through the suggestions without focus ever leaving the input. The WAI-ARIA combobox pattern describes how this works with aria-activedescendant.

Write a function createCombobox(input, listbox, config) that turns a text input and an empty list into an accessible combobox:

<label for="fruit">Fruit</label>
<input id="fruit" />
<ul></ul>

config.options is an array of strings. config.onSelect(value) is optional and is called when the user picks one. The function returns an object with a destroy() method.

Roles and relationships

  • The input gets role="combobox", aria-autocomplete="list", aria-expanded="false" and autocomplete="off" (so the browser's own suggestions don't cover yours).
  • The list gets role="listbox". If it has no id, give it <input id>-listbox. The input's aria-controls points at the list's id.
  • The list starts closed.

Open and closed

  • Open means: the list has no hidden attribute, the input has aria-expanded="true", and the list contains one <li role="option"> per match.
  • Closed means: the list has the hidden attribute and no children, the input has aria-expanded="false", and the input has no aria-activedescendant.
  • Each option has the id <listbox id>-option-<index>, where index is its position in the shown list, starting at 0. Its text is the matching string, and it has aria-selected="false" unless it is the active option.

Typing

On every input event, take the input's value without surrounding spaces:

  • If it is empty, close the list.
  • Otherwise, show every option that contains the text, ignoring case, in the original order. If nothing matches, close the list.
  • After typing, no option is active.
createCombobox(input, listbox, {
  options: ["Apple", "Apricot", "Banana", "Pineapple"],
  onSelect: (value) => console.log("picked", value),
});

// user types "ap"  -> list shows Apple, Apricot, Pineapple
// user types "zz"  -> list closes

Keyboard (all on keydown of the input; focus always stays on the input)

  • ArrowDown makes the next option active, or the first one when none is active. From the last one it wraps to the first.
  • ArrowUp makes the previous option active, or the last one when none is active. From the first one it wraps to the last.
  • If the list is closed, either arrow key first opens it using the current text (empty text shows every option), then makes the first (ArrowDown) or last (ArrowUp) option active. If nothing matches, the list stays closed.
  • Both arrow keys always call preventDefault(), so the text cursor doesn't jump.
  • Making an option active sets aria-selected="true" on it, "false" on the others, and points the input's aria-activedescendant at its id.
  • Enter with an active option picks it and calls preventDefault(). With no active option, or a closed list, do nothing.
  • Escape on an open list closes it without changing the text. On a closed list with text, it clears the text. Call preventDefault() in both cases, and not when it does nothing.
  • Leave every other key alone.

Picking an option

Picking an option, with Enter or by clicking it, sets the input's value to that option's text, closes the list, and then calls onSelect(value).

Cleaning up

destroy() closes the list and removes every listener the combobox added.