Combobox
Also called autocomplete, typeahead, or searchable select
A text field with a dropdown list that filters as you type, so people can pick one option from a long list quickly.
import { useState } from 'react';
import { Combobox } from '@base-ui/react/combobox';
const CITIES = ['Berlin', 'Boston', 'Lisbon', 'London', 'Paris', 'Tokyo'];
export function CityCombobox() {
const [query, setQuery] = useState('');
// Base UI wires up role='combobox', aria-expanded, and aria-activedescendant.
return (
<Combobox.Root
items={CITIES}
limit={8}
inputValue={query}
onInputValueChange={setQuery}
>
<label className='flex w-64 flex-col gap-1.5 text-sm font-medium'>
City
<Combobox.Input
placeholder='Start typing a city'
className='h-10 rounded-lg border bg-background px-3 font-normal outline-none focus:ring-2 focus:ring-ring'
/>
</label>
<Combobox.Portal>
<Combobox.Positioner sideOffset={4}>
<Combobox.Popup className='w-(--anchor-width) origin-(--transform-origin) rounded-lg border bg-background p-1 shadow-lg transition-[opacity,scale] duration-100 data-ending-style:scale-95 data-ending-style:opacity-0 data-starting-style:scale-95 data-starting-style:opacity-0'>
<Combobox.Empty className='px-3 py-2 text-sm text-muted-foreground empty:p-0'>
No matches
</Combobox.Empty>
<Combobox.List>
{(city: string) => (
<Combobox.Item
key={city}
value={city}
className='cursor-default rounded-md px-3 py-2 text-sm outline-none select-none data-highlighted:bg-muted'
>
<Highlight text={city} query={query} />
</Combobox.Item>
)}
</Combobox.List>
</Combobox.Popup>
</Combobox.Positioner>
</Combobox.Portal>
</Combobox.Root>
);
}
// Bolds the part of the city name that matches what was typed.
function Highlight({ text, query }: { text: string; query: string }) {
const start = query ? text.toLowerCase().indexOf(query.toLowerCase()) : -1;
if (start < 0) return text;
const end = start + query.length;
return (
<>
{text.slice(0, start)}
<strong>{text.slice(start, end)}</strong>
{text.slice(end)}
</>
);
}Turn the City field into a combobox: as people type, show up to 8 matching cities below the input with the matching letters in bold. Support Up, Down, Enter to choose, and Escape to close, and close the list when the field loses focus. Use role='combobox' with aria-expanded and aria-activedescendant, and show 'No matches' when nothing fits.
Paste into Layerwise or any AI coding tool.
What is Combobox?
A combobox pairs an input with a list of options. As you type, the list narrows to the matches, often with the matching letters in bold. Arrow keys move through the results, Enter or a click picks one, and the field then shows the chosen value.
Use it when a plain select would be too long to scroll, such as countries, cities, time zones, or teammates. With fewer than about seven options, a select or radio buttons are quicker. Decide whether people may enter a value that is not in the list, like a new tag, and make that clear.
Filter on every keystroke for short local lists. When results come from a server, debounce requests by about 150 to 300ms and show a loading state. Follow the ARIA combobox pattern, with role='combobox', aria-expanded, and aria-activedescendant, so screen readers announce the highlighted option. The native <datalist> element gives a basic version with no script, though its list can't be styled.
Often confused with
- Dropdown menu
- A dropdown menu runs an action when you pick an item. A combobox fills in a value for a field and lets you type to filter.
- Command palette
- A command palette searches every action in the app. A combobox searches the options for one field.
Related terms
All termsDebounce
A technique that waits for a short pause in typing or other repeated events, then runs the work once instead of on every keystroke.
Tag input
A text field where each word you type becomes a removable chip when you press Enter or a comma, for adding labels, emails, or keywords.
Command palette
A search box that opens with a shortcut like ⌘K and lets you find and run any command or page by typing a few letters.
Inline validation
Form checking that happens right next to each field as you fill it in, showing an error or a checkmark before you press Submit.
Build it with Layerwise
Describe your app, watch it run in a live preview, and publish it with a database, storage, and sign-in built in.