Todae

Todae

useCombobox

useCombobox owns a combobox's open state, value, input, highlight and ids. Components own the chrome: Field for the input, Popover, BottomSheet or Dialog for the surface, OptionList for the list. There is no Combobox.* export. The Combobox recipe shows single, multi, sheet, async and create.

On this page

presentation defaults to 'auto', which picks a sheet on a coarse pointer or a narrow viewport through useSheetPresentation. Force popover or sheet to keep it fixed, as the demo below forces popover. dialog puts the input and list in a Dialog, as the Command palette does. Composition lists which getter goes on which part.

import { ChevronDown } from 'lucide-react';
import {
  Field,
  Label,
  OptionList,
  Popover,
  useCombobox,
  useHighlight,
  type ComboboxOption,
} from '@tounsoo/todae';

const FRAMEWORKS: ComboboxOption[] = [
  { value: 'react', label: 'React' },
  { value: 'redux', label: 'Redux' },
  { value: 'svelte', label: 'Svelte' },
];

function optionNodes(
  items: ReturnType<typeof useCombobox>['items'],
  getOptionProps: ReturnType<typeof useCombobox>['getOptionProps'],

Filtering

With options, the hook picks the rows to show. filterOptions(options, query) returns them in the order to show them, and gets the input text trimmed, or '' while nothing is typed or the input shows the committed label, so its answer to '' is the list that opens. By default it keeps the rows whose label holds the text, ignoring case, in their own order. Return a ranked or capped list to put better matches first, as the Command palette does: each change of the trimmed text makes the first enabled row it returns the active one, even after the arrow keys moved the highlight, and allowCreate checks only the rows it returns for a label match.

With loadOptions, the loader is called with the trimmed text and its rows show as it returns them, so the loader does the matching. filterOptions is ignored beside it, and development warns. options then only lends labels to values the loader hasn't returned, such as the starting value's, so the input shows its label and the first load asks for the full list.

Accessible composition

Todae handles: getInputProps() and getChipInputProps() make the input a role="combobox" with aria-expanded, aria-controls (the listbox id), aria-autocomplete="list", autoComplete="off", and aria-activedescendant on the active option while open, so focus stays in the input. ArrowDown, ArrowUp, Home and End open the list and move the active option, skipping disabled ones. With Shift, Ctrl or Meta held, those keys are left to the input, so Shift+Home selects the text, and so are Home and End with Alt. Enter selects or creates while the list is open. While the current query's list is still loading, or its create is pending, Enter does nothing, and the form does not get it. While the list is closed, or open with no row to pick and no create row, Enter is left to the form and creates nothing. Any other commitKeys you give Field.ChipInput commit the same way while the list is open. While it is closed, they are kept from typing their character only with text in the input. Escape closes, and Tab closes. On a closed list, Escape clears typed text in single mode with no value; with nothing to clear it is left to the page, so a dialog around the field closes. Keys that belong to an IME composition, such as the Enter that ends a Korean word, are left to the input. While an async onCreate is pending, creating is true and that label's create row stays the active row with aria-disabled, so another Enter or click does nothing instead of running it again or picking another row; a create for another label runs beside it. Labels match without regard to case. What onCreate throws or rejects with lands in createError, not in error, which is only for loads, and only while the text still asks for that label; the next text change, pick, removal, clear(), create or opening of the list clears it. A falsy failure, such as reject() or throw null, lands in createError or error as an Error with it as cause, so it still reads as one. A throw from onValueChange, onInputValueChange or onOpenChange is yours, not a create failure: the other callbacks still run, then the throw goes on, out of the event handler, or after an async create to reportError, as an uncaught error, or as an unhandled rejection where there is no reportError. A pick in single mode, clear(), reset() and unmounting drop every pending create, and so does an Activity that hides the field. creating turns false at once, and a dropped create neither commits nor reports a failure. It stays in flight until its promise settles, so onCreate does not run again for its label. Running that label again, with Enter or the create row, takes the create back, and it then commits when it settles as if it had never been dropped. onCreate gets an AbortSignal after the label, which aborts when the field unmounts or an Activity hides it, but not on a pick, clear() or reset(). Once its signal aborts, a create is not taken back: running its label again calls onCreate again. In single mode, a create that settles applies only if the input then reads its label and the value is the one it started from, or was taken back from, so other text or another value wins. In multiple mode the created chip is added whatever the user did since, unless the create was dropped. getLabelProps() gives the field's Label the id the listbox takes its name from, and a htmlFor that points at the input, or at the trigger in a sheet or dialog. getListboxProps() gives OptionList.Root the listbox id, tabIndex={-1} and aria-labelledby set to ids.label, or the aria-label or aria-labelledby you pass it instead. In development, the hook warns when the open list has no name. getTriggerProps(props) sets aria-haspopup="dialog", aria-expanded and aria-controls, which win over yours, as its id does. Unless you pass aria-label or aria-labelledby, it names the trigger by the field's Label and its own text, so it reads "City San Francisco". With no Label rendered, its own text names it. It merges your other props and ref, and runs your onClick first: calling preventDefault() there skips the toggle. reset() puts back the default value, text and open state through the change callbacks, with the reset reason, and skips those that do not change. It sends the defaultValue, defaultInputValue and defaultOpen props, or an empty value, empty text and a closed list where they are not set, so a controlled owner gets those, not the first value it rendered. Escape in a popover returns focus to the input; a sheet returns it to the trigger when it closes.

You supply: The input's name: spread getLabelProps() on the field's Label, and in a sheet or dialog, name the search field with aria-label on its Field.Root. The listbox's name: spread getLabelProps() on the field's Label, or with no visible label, pass aria-label to getListboxProps(). Text for a failed create, from createError, next to the create row. Trigger text that shows the value. A more specific name for the sheet or dialog when needed: getSheetProps() and getDialogProps() use the options string ("Options" by default), and an aria-label after the spread replaces it. Status text: the hook reports loading, error, creating, createError and isEmpty but renders and announces none of them.

Watch for: Unmounting OptionList.Root while it is empty or loading: aria-controls then points at nothing, so set hidden instead. Your own onKeyDown, onFocus or onBlur after the spread, which replaces the hook's handlers. A trigger's handlers go into getTriggerProps({ … }) instead. An inline callback ref passed to getTriggerProps, which attaches again on every render. Buttons or links inside an option.

When to use

When: Searchable select — typeahead + list. Hook owns open/value/input; Field + Popover/BottomSheet + OptionList own chrome, as the recipe shows. Auto sheet on coarse/narrow.

When not: Static on-page exclusive → Radio. Non-search list → usePopover + OptionList only. Action menu → Menu. Exporting Combobox.* chrome (forbidden).

Props

useCombobox props
NameTypeDescription
selectionMode"single" | "multiple"

Single uses TextInput. Multiple uses ChipInput.

Default: "single"

optionsreadonly ComboboxOption[]

Every row there is; filterOptions picks the ones to show. With loadOptions, only lends labels, such as the starting value's.

filterOptions(options: readonly ComboboxOption[], query: string) => readonly ComboboxOption[]

Returns the rows to show, in the order to show them, for the trimmed input text, which is "" while nothing is typed or the input shows the committed label. Rank or cap the list here. Ignored when loadOptions is set.

Default: label holds the query, ignoring case

loadOptions(query: string, signal: AbortSignal) => Promise<readonly ComboboxOption[]>

Async options for the trimmed input text, shown as the loader returns them, so the loader does the matching. Stale responses are discarded. An inline function is fine; it is read at call time.

loadKeystring | number

Reloads options when it changes. Use it when loadOptions depends on state outside the query, such as a category.

presentation"popover" | "sheet" | "dialog" | "auto"

auto uses useSheetPresentation. dialog is for Dialog.Root, through getDialogProps.

Default: "auto"

allowCreateboolean

Show a create row when the query has no case-insensitive label match. With loadOptions, the row waits until the load for that query settles.

Default: false

valuestring | null | readonly string[]

Controlled value. Null for empty single. Array for multiple.

openboolean

Controlled open.

inputValuestring

Controlled filter text.