Full answer
An autocomplete is a combobox. Keyboard and screen reader users need the same behaviour as mouse users.
Structure. The input has role="combobox", aria-expanded (true when the list is shown), aria-controls (the listbox id), aria-autocomplete="list" and aria-activedescendant (the id of the highlighted option). The popup is role="listbox" and each item is role="option" with an id and aria-selected for the highlighted one. Focus never leaves the input. aria-activedescendant tells assistive technology which option is current.
State: the query text, an activeIndex (-1 for none) and open. Filtered results are derived during render from the query, not stored in state.
Keyboard: ArrowDown moves to the next option and wraps or stops at the end, ArrowUp moves back, Enter selects the active option and closes the list, and Escape closes it (and may clear the text). Call preventDefault on arrow keys so the cursor in the input does not move. When the query changes, reset activeIndex and open the list.
Mouse: use onMouseDown on options with preventDefault to keep focus in the input, or onClick with care about blur order. Selecting sets the query to the chosen label.
For remote data, debounce the query, abort stale requests (see useFetch), and announce the number of results in a live region. Scroll the active option into view. Consider an existing accessible library such as Downshift, React Aria or Radix for production, because the details are easy to get wrong.
The example filters a local list.
import { useId, useState } from 'react';
const COURSES = ['Maths', 'Machine learning', 'Marketing', 'Physics', 'Python'];
export default function CourseSearch() {
const listId = useId();
const [query, setQuery] = useState('');
const [open, setOpen] = useState(false);
const [activeIndex, setActiveIndex] = useState(-1);
const results = query ? COURSES.filter((c) => c.toLowerCase().startsWith(query.toLowerCase())) : [];
const showList = open && results.length > 0;
function choose(value) {
setQuery(value);
setOpen(false);
setActiveIndex(-1);
}
function handleKeyDown(event) {
if (event.key === 'ArrowDown') {
event.preventDefault();
setOpen(true);
setActiveIndex((i) => Math.min(i + 1, results.length - 1));
} else if (event.key === 'ArrowUp') {
event.preventDefault();
setActiveIndex((i) => Math.max(i - 1, 0));
} else if (event.key === 'Enter' && showList && activeIndex >= 0) {
event.preventDefault();
choose(results[activeIndex]);
} else if (event.key === 'Escape') {
setOpen(false);
setActiveIndex(-1);
}
}
return (
<div>
<input
role="combobox"
aria-expanded={showList}
aria-controls={listId}
aria-autocomplete="list"
aria-activedescendant={showList && activeIndex >= 0 ? listId + '-' + activeIndex : undefined}
value={query}
onChange={(e) => {
setQuery(e.target.value);
setOpen(true);
setActiveIndex(-1);
}}
onKeyDown={handleKeyDown}
/>
<ul id={listId} role="listbox" hidden={!showList}>
{results.map((course, index) => (
<li
key={course}
id={listId + '-' + index}
role="option"
aria-selected={index === activeIndex}
onMouseDown={(e) => {
e.preventDefault();
choose(course);
}}
>
{course}
</li>
))}
</ul>
</div>
);
}The check typed "ma" and then pressed ArrowDown twice and Enter. An asterisk marks the option with aria-selected set to true. Output of the run: typed ma: options=[Maths, Machine learning, Marketing] value="ma" expanded=true ArrowDown: options=[Maths*, Machine learning, Marketing] value="ma" expanded=true ArrowDown again: options=[Maths, Machine learning*, Marketing] value="ma" expanded=true Enter: options=[Machine learning] value="Machine learning" expanded=false
Key points
- The input is a combobox with aria-expanded, aria-controls and aria-activedescendant. Focus stays in the input.
- The popup is a listbox of role="option" items with aria-selected.
- Handle ArrowUp, ArrowDown, Enter and Escape, and reset the active index when the query changes.
- Derive the filtered list during render.
- For remote data add debouncing, aborting and a live region announcement.
Common mistakes
- Moving real focus into options, so typing stops working.
- Forgetting preventDefault on arrow keys.
- Not resetting the active index when the query changes.
- Storing the filtered list in state instead of deriving it.
- Missing ARIA attributes, so screen readers cannot follow the highlight.
Follow-ups
- How would you load suggestions from a server with debounce and cancellation?
- How would you announce the number of results to screen reader users?
- Why might you use a library such as Downshift or React Aria here?
Tips
- Name the combobox pattern and the aria-activedescendant technique.
- Keep focus in the input. It is the central design decision.
- Mention that a library is safer for production.