Playground

Combobox

Type to filter a list and pick one option.

  • Built on native elements
  • ARIA 1.2 combobox: the input has role="combobox" with aria-expanded, aria-controls and aria-activedescendant.
  • ArrowDown/ArrowUp move through options, Enter picks, Escape closes (and clears the typed text on a second press), Tab leaves.
  • Options are in a listbox with aria-selected; an empty result is announced as text.
  • Styles in one file: 03-form-fields.css

Import

import '@monoline/css';
import {Combobox} from '@monoline/react';

Not installed yet? Pick a route.

Copies the source and its styles into your project.

npx monoline add combobox

Usage

import {Combobox, Field} from '@monoline/react';

const options = ['Ada Lovelace', 'Grace Hopper', 'Alan Turing', 'Katherine Johnson', 'Linus Torvalds'].map((n) => ({value: n.toLowerCase().replace(/\W+/g, '-'), label: n}));

<div style={{maxWidth: '320px', minHeight: '260px'}}>
  <Field label="Person">
    <Combobox options={options} placeholder="Type a name" />
  </Field>
</div>

Examples

Search and pick

Props

ComboboxProps

PropTypeDefaultDescription
options requiredComboboxOption[]–The choices, each with a `value`, a `label` and an optional `disabled`. The user filters them by typing part of the label.
valuestring–The selected option's `value` when controlled; `''` means none. Pair with `onValueChange`.
defaultValuestring''The initially selected option's `value` when uncontrolled. Default none.
onValueChange((value: string) => void)–Called with the chosen option's value, or '' when cleared.
emptyTextstring'No matches'Shown when nothing matches.
size"sm" | "md" | "lg"'md'`md` (default), `sm` for dense layouts or `lg` for prominent forms. Heights match Button, Input and Select at each size.
invalidboolean–Marks the field as having an error: sets `aria-invalid` and shows the error border.

Data Attributes

State is exposed through attributes. Style it with these selectors, not with class toggles.

AttributeStylesheet
[aria-checked="true"]03-form-fields.css
[aria-invalid="true"]03-form-fields.css
[aria-pressed="true"]03-form-fields.css
[aria-disabled="true"]combobox.css
[aria-selected="true"]combobox.css
[data-active]combobox.css

CSS classes

ClassStylesheet
.field03-form-fields.css
.field-hint03-form-fields.css
.input03-form-fields.css
.input-bare03-form-fields.css
.input-end03-form-fields.css
.input-lg03-form-fields.css
.input-sm03-form-fields.css
.input-wrap03-form-fields.css
.muted03-form-fields.css
.seg03-form-fields.css
.seg-opt03-form-fields.css
.select03-form-fields.css
.setting03-form-fields.css
.switch03-form-fields.css
.textarea03-form-fields.css
.comboboxcombobox.css
.combobox-emptycombobox.css
.combobox-listcombobox.css
.combobox-optioncombobox.css

Customization

Style it with the kit's tokens in your own CSS, loaded after @monoline/css. Prefer the attributes above over extra classes. See Theming for every token.

app.css
.field {
  border-color: var(--accent);
}
.field[aria-checked="true"] {
  background: var(--wash);
}

Accessibility

  • ARIA 1.2 combobox: the input has role="combobox" with aria-expanded, aria-controls and aria-activedescendant.
  • ArrowDown/ArrowUp move through options, Enter picks, Escape closes (and clears the typed text on a second press), Tab leaves.
  • Options are in a listbox with aria-selected; an empty result is announced as text.

Keyboard

KeysAction
ArrowDown / ArrowUpMove through options
EnterPicks
EscapeCloses (and clears the typed text on a second press)
TabLeaves