combobox
onChange called 0 time(s)
import { combobox, lucide } from '@markless/ui';export default function ComboboxBasic() @{ <combobox.root class="combobox" inline placeholder="Pick a fruit"> <combobox.label class="label">Favourite fruit</combobox.label> <div class="field"> <combobox.input class="input" /> <combobox.trigger class="show"> <lucide.chevrondown class="show-icon" aria-hidden="true" /> </combobox.trigger> </div> <combobox.content class="list"> <combobox.item class="option" value="apple" label="Apple"> <combobox.itemlabel>Apple</combobox.itemlabel> <combobox.itemindicator class="tick"> <lucide.check class="tick-icon" aria-hidden="true" /> </combobox.itemindicator> </combobox.item> <combobox.item class="option" value="banana" label="Banana"> <combobox.itemlabel>Banana</combobox.itemlabel> <combobox.itemindicator class="tick"> <lucide.check class="tick-icon" aria-hidden="true" /> </combobox.itemindicator> </combobox.item> <combobox.item class="option" value="cherry" label="Cherry"> <combobox.itemlabel>Cherry</combobox.itemlabel> <combobox.itemindicator class="tick"> <lucide.check class="tick-icon" aria-hidden="true" /> </combobox.itemindicator> </combobox.item> <combobox.item class="option" value="mango" label="Mango"> <combobox.itemlabel>Mango</combobox.itemlabel> <combobox.itemindicator class="tick"> <lucide.check class="tick-icon" aria-hidden="true" /> </combobox.itemindicator> </combobox.item> <combobox.item class="option" value="orange" label="Orange"> <combobox.itemlabel>Orange</combobox.itemlabel> <combobox.itemindicator class="tick"> <lucide.check class="tick-icon" aria-hidden="true" /> </combobox.itemindicator> </combobox.item> </combobox.content> </combobox.root>;}
.combobox { display: grid; align-content: start; gap: 0.35em; box-sizing: border-box; width: min(18rem, 100%); min-height: 15.5rem; font-family: 'Shantell Sans', system-ui, sans-serif; font-size: var(--step--1); line-height: 1.5; color: var(--ink);}.label { font-family: 'Shantell Sans', system-ui, sans-serif;}.field { display: flex; border: 1px solid var(--ink); border-radius: 6px; background: var(--paper);}.field:focus-within { outline: 2px solid var(--ink); outline-offset: 3px;}.input { flex: 1; min-width: 0; padding: 0.5em 0.75em; border: 0; border-radius: 6px 0 0 6px; background: transparent; color: var(--ink); font: inherit; font-family: 'Shantell Sans', system-ui, sans-serif; outline: none;}.input::placeholder { color: var(--muted-ink);}.show { display: grid; place-items: center; width: 2.4em; padding: 0; border: 0; border-inline-start: 1px solid var(--ink); border-radius: 0 6px 6px 0; background: transparent; color: var(--ink); cursor: pointer;}.show:hover { background: color-mix(in srgb, var(--purple) 16%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.show-icon { width: 1em; height: 1em; transition: transform 140ms ease;}.show[ui-open] .show-icon { transform: rotate(180deg);}/* In flow, not absolute: the root's min-height already reserves the room the list opens into. */.list { box-sizing: border-box; margin-block-start: 0.2em; padding: 0.3em; border: 1px solid var(--ink); border-radius: 6px; background: var(--paper); box-shadow: 3px 3px 0 var(--muted-ink);}.list[hidden] { display: none;}.combobox[ui-inline] .list { box-shadow: none;}.option { display: flex; align-items: center; justify-content: space-between; padding: 0.35em 0.6em; border-radius: 4px; font-family: 'Shantell Sans', system-ui, sans-serif; cursor: pointer;}.option[ui-highlighted] { background: color-mix(in srgb, var(--purple) 16%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.option[ui-selected] { background: color-mix(in srgb, var(--purple) 26%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.option[ui-disabled] { cursor: not-allowed; opacity: 0.55;}.tick[ui-hidden] { visibility: hidden;}.tick-icon { display: block; width: 1em; height: 1em;}.combobox[ui-disabled] .field { cursor: not-allowed; opacity: 0.55;}.field:has(.input[aria-invalid='true']) { border-color: var(--pink); background: color-mix(in srgb, var(--pink) 14%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}@media (prefers-reduced-motion: reduce) { .show-icon { transition: none; }}
A combobox is a text field with a list of options under it. Use it when the list is long enough that people want to type to find an option. For a short list with no typing, use select.
Focus stays in the text field the whole time. The arrow keys move a highlight through the options, but the caret never leaves the field. Even a click on an option puts focus back in the field.
When a person picks an option, the field shows that option's label. If you leave label out,
the field shows the value.
Anatomy
Holds the selection and connects the label, input or trigger, and options.
combobox.input is the only Tab stop. combobox.trigger opens and closes the list with a click,
but Tab skips it. Screen readers hear it as "Show options".
Filtering is yours
The family does not filter the list. It keeps the typed text, and you render the options you want.
onInput on the root gets the text on every keystroke. A component inside the root can also read
it as combobox.state().input.
Keyboard
| Key | What it does |
|---|---|
| Down arrow | Open the list on the first option, or move the highlight down. |
| Up arrow | Open the list on the last option, or move the highlight up. |
| Home or End | While the list shows, highlight the first or last option. Otherwise, move the caret. |
| Page Down or Page Up | While the list shows, move the highlight ten options. |
| Enter | While the list shows, pick the highlighted option. Otherwise, submit the form. |
| Escape | Close the list. The choice stays as it was. |
| Tab | Close the list and move on. |
| Backspace | With multiple and removeOnBackspace, in an empty field, remove the last choice. |
The highlight stops at the ends. Set loop to make it wrap. It skips disabled options.
Choosing and unchoosing
A click on an option picks it and closes the list. A click on the option that is already picked
unpicks it and leaves the list open. Without multiple, an empty field also clears the choice.
With multiple, the root's value is a list, and the list stays open after each pick. With
inline, the list is always on the page. Nothing opens or closes it.
Help and errors
combobox.description is a hint and combobox.error is an error message. The input lists both in
aria-describedby, the error first. The error has role="alert", so a screen reader reads it
when it appears.
Forms
combobox.field renders a hidden native <select> for the form. It reads name, required and
disabled from the root, and submits the chosen value.
Styling
The family ships no CSS. It writes these attributes:
| Attribute | On | When |
|---|---|---|
ui-open, ui-closed |
root, input, trigger, content |
The list is open or closed. |
ui-highlighted |
item |
The keyboard or the pointer has this option highlighted. |
ui-selected |
item |
This option is picked. |
ui-disabled |
root, item |
The combobox or this option is disabled. |
ui-value |
item |
Always. It holds the option's value. |
ui-hidden |
itemindicator |
Its option is not picked. |
ui-required |
root |
required is set. |
ui-inline |
root |
inline is set. |
combobox.itemindicator stays in the page while its option is not picked. Hide it with
[ui-hidden] { visibility: hidden }.
Props on combobox.root
| Prop | Type | What it does |
|---|---|---|
value |
string | readonly string[] |
The picked option. With multiple, the list of picked options. |
open |
boolean |
Whether the list starts open. The default is false. |
multiple |
boolean |
More than one option can be picked, and value becomes a list. |
inline |
boolean |
The list is part of the page and always shows. |
loop |
boolean |
The highlight wraps at the ends instead of stopping. |
removeOnBackspace |
boolean |
Backspace in an empty field removes the last choice. Needs multiple. |
disabled |
boolean |
Nobody can type or pick. |
required |
boolean |
The form will not submit without a choice. |
invalid |
boolean |
Sets aria-invalid="true" on the input. |
name |
string |
The name combobox.field submits under. |
placeholder |
string |
The field's placeholder. It replaces any placeholder on the input. |
onChange |
(value: string | readonly string[]) => void |
Called with the new choice. |
onInput |
(value: string) => void |
Called with the field's text on every keystroke. |
onOpenChange |
(open: boolean) => void |
Called when the list opens or closes. |
Every other prop goes on the root <div>, which has role="group". combobox.item takes
value, which it needs, plus label and disabled.