Guides
An accessible combobox in React
For React developers who need a searchable select that works with a keyboard and a screen reader. What the pattern requires, how ReactLoom's Combobox meets it, server-side search without race conditions, and when to use something simpler.
What a combobox has to do
A combobox is a text input that controls a popup list. The WAI-ARIA pattern asks for a few things that are easy to miss when building one from a div and an input:
- Focus stays in the input. Arrow keys move a highlighted option, but the caret never leaves the text box. The highlighted option is exposed with
aria-activedescendant, so screen readers announce it while the user keeps typing. - The input says what it controls:
role="combobox",aria-expandedandaria-controlspointing at thelistbox. - Result counts are announced as the list changes, politely, so people know whether typing narrowed anything down.
- Escape closes, then clears. A second Escape empties the input, as users of native search fields expect.
- The rest of the page is hidden from assistive technology while the list is open, so a screen reader cannot wander behind it.
Combobox in @reactloom/ui is built on React Aria’s combobox hooks, which handle these details across browsers and screen readers, with ReactLoom’s styling, sizes and field wiring (label, description, error) on top.
A searchable select
Customer picker
Type to filter, use ↓ and ↑ to move, Enter to choose.
Selected: —
import { Combobox, Stack, Text } from '@reactloom/ui';
import { useState } from 'react';
const customers = [
{ value: 'acme', label: 'Acme Corporation' },
{ value: 'globex', label: 'Globex' },
{ value: 'initech', label: 'Initech' },
{ value: 'umbrella', label: 'Umbrella Health' },
{ value: 'wayne', label: 'Wayne Enterprises' },
];
export default function Example() {
const [value, setValue] = useState<string | null>(null);
return (
<Stack gap="2" style={{ width: '100%', maxWidth: 320 }}>
<Combobox label="Customer" options={customers} value={value} onValueChange={setValue} placeholder="Type to search" />
<Text size="sm" tone="muted">Selected: {value ?? '—'}</Text>
</Stack>
);
}| Key | Action |
|---|---|
| Type | Filter the options |
| ↓ / ↑ | Open the list and move the highlight |
| Enter | Choose the highlighted option |
| Escape | Close the list; press again to clear |
Searching on the server
For long lists, query your API and pass filter={false} so the component does not filter the results again. Two details matter: debounce the request so you are not querying on every keystroke, and abort the previous request so a slow, older response cannot replace a newer one.
Results from a (simulated) server
import { Combobox } from '@reactloom/ui';
import { useEffect, useState } from 'react';
const all = ['Amsterdam', 'Bangalore', 'Berlin', 'Boston', 'Chennai', 'Dublin', 'Lisbon', 'London', 'Mumbai', 'Toronto'];
export default function Example() {
const [query, setQuery] = useState('');
const [loading, setLoading] = useState(false);
const [results, setResults] = useState(all);
useEffect(() => {
// Stand-in for a server request; filter={false} tells Combobox not to filter again.
setLoading(true);
const id = setTimeout(() => {
setResults(all.filter((c) => c.toLowerCase().includes(query.toLowerCase())));
setLoading(false);
}, 400);
return () => clearTimeout(id);
}, [query]);
return (
<div style={{ width: '100%', maxWidth: 320 }}>
<Combobox
label="Office"
options={results.map((c) => ({ value: c.toLowerCase(), label: c }))}
filter={false}
loading={loading}
onInputChange={setQuery}
emptyMessage="No offices match"
/>
</div>
);
}const [query, setQuery] = useState('');
const [results, setResults] = useState<Option[]>([]);
const [loading, setLoading] = useState(false);
useEffect(() => {
if (!query) return setResults([]);
const controller = new AbortController(); // drop stale responses
const timer = setTimeout(async () => { // wait for a pause in typing
setLoading(true);
try {
const res = await fetch(`/api/customers?q=${encodeURIComponent(query)}`, { signal: controller.signal });
setResults(await res.json());
} catch (error) {
if ((error as Error).name !== 'AbortError') throw error;
} finally {
if (!controller.signal.aborted) setLoading(false);
}
}, 250);
return () => {
clearTimeout(timer);
controller.abort();
};
}, [query]);
<Combobox label="Customer" options={results} filter={false} loading={loading} onInputChange={setQuery} />While loading is true the list shows a loading row and the input a spinner instead of a stale “No results”.
Combobox, Select or a plain input?
- Fewer than about ten options: use Select. Typing is not needed and a button that opens a list is simpler for everyone.
- Free text with suggestions (a city that may not be in your list): Combobox with
allowsCustomValue. - Several values: show each chosen value as a removable Tag next to the Combobox; the filter bar recipe shows the same pattern for active filters.