Combobox
@codeui/styles v0.3.0
A text input that filters a list of options, in plain HTML.
Load @codeui/styles/combobox.css, or all.css. The Solid component is documented in Combobox.
Demo
<div class="cui-stack" data-gap="4" style="max-width: 280px; margin-inline: auto"> <div role="group" class="cui-combobox" data-cui="combobox"> <label for="language" class="cui-combobox-label" data-cui="combobox-label">Language</label> <div class="cui-combobox-control" data-cui="combobox-control" data-size="sm"> <input id="language" type="text" role="combobox" value="TypeScript" placeholder="Search languages" autocomplete="off" aria-autocomplete="list" aria-expanded="false" class="cui-combobox-input" data-cui="combobox-input" /> <button type="button" tabindex="-1" aria-label="Show languages" aria-expanded="false" class="cui-combobox-trigger" data-cui="combobox-trigger"> <span aria-hidden="true" class="cui-combobox-icon" data-cui="combobox-icon"> <svg class="cui-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <path d="M8 9l4-4 4 4m0 6l-4 4-4-4" /> </svg> </span> </button> </div> </div></div>Markup
| Part | Element | Class | data-cui |
|---|---|---|---|
| Root | div | cui-combobox | combobox |
| Label | label | cui-combobox-label | combobox-label |
| Control | div | cui-combobox-control | combobox-control |
| Input | input | cui-combobox-input | combobox-input |
| Trigger | button | cui-combobox-trigger | combobox-trigger |
| Icon | span with an svg | cui-combobox-icon | combobox-icon |
| Description | div | cui-combobox-description | combobox-description |
| Error message | div | cui-combobox-error-message | combobox-error-message |
| Content | div | cui-combobox-content | combobox-content |
| Listbox | ul | cui-combobox-listbox | combobox-listbox |
| Section | li | cui-combobox-section | combobox-section |
| Item | li | cui-combobox-item | combobox-item |
| Item label | div | cui-combobox-item-label | combobox-item-label |
| Item description | div | cui-combobox-item-description | combobox-item-description |
| Item indicator | div with an svg | cui-combobox-item-indicator | combobox-item-indicator |
| Empty message | div | cui-combobox-empty | None |
The control is the field box. It holds the input and the trigger, and shows the focus ring when the input has focus. The content is a sibling of the root, not a child.
Unlike Select, the combobox carries its own label and message classes, and the listbox class resets the ul.
| Attribute | Values | Default |
|---|---|---|
data-size on the control | sm, md | sm |
data-invalid on the control | Presence | Off |
data-disabled on the control | Presence | Off |
data-expanded on the content | Presence | Off |
data-motion on the content | none | None |
data-highlighted, data-disabled on an item | Presence | Off |
Variants
Sizes
<div class="cui-stack" data-gap="4" style="max-width: 280px; margin-inline: auto"> <div role="group" class="cui-combobox" data-cui="combobox"> <label for="size-sm" class="cui-combobox-label" data-cui="combobox-label">Size sm</label> <div class="cui-combobox-control" data-cui="combobox-control" data-size="sm"> <input id="size-sm" type="text" role="combobox" value="TypeScript" placeholder="Search languages" autocomplete="off" aria-autocomplete="list" aria-expanded="false" class="cui-combobox-input" data-cui="combobox-input" /> <button type="button" tabindex="-1" aria-label="Show languages" aria-expanded="false" class="cui-combobox-trigger" data-cui="combobox-trigger"> <span aria-hidden="true" class="cui-combobox-icon" data-cui="combobox-icon"> <svg class="cui-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <path d="M8 9l4-4 4 4m0 6l-4 4-4-4" /> </svg> </span> </button> </div> </div> <div role="group" class="cui-combobox" data-cui="combobox"> <label for="size-md" class="cui-combobox-label" data-cui="combobox-label">Size md</label> <div class="cui-combobox-control" data-cui="combobox-control" data-size="md"> <input id="size-md" type="text" role="combobox" value="TypeScript" placeholder="Search languages" autocomplete="off" aria-autocomplete="list" aria-expanded="false" class="cui-combobox-input" data-cui="combobox-input" /> <button type="button" tabindex="-1" aria-label="Show languages" aria-expanded="false" class="cui-combobox-trigger" data-cui="combobox-trigger"> <span aria-hidden="true" class="cui-combobox-icon" data-cui="combobox-icon"> <svg class="cui-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <path d="M8 9l4-4 4 4m0 6l-4 4-4-4" /> </svg> </span> </button> </div> </div></div>The default sm control is 36px at comfortable density. md is 44px with larger text. Both follow the shared density scale. Only the control reads data-size.
States
Open
<div class="cui-stack" data-gap="1.5" style="max-width: 280px; margin-inline: auto"> <div role="group" class="cui-combobox" data-cui="combobox"> <label for="open-lang" class="cui-combobox-label" data-cui="combobox-label">Language</label> <div class="cui-combobox-control" data-cui="combobox-control" data-size="sm" data-expanded> <input id="open-lang" type="text" role="combobox" value="TypeScript" placeholder="Search languages" autocomplete="off" aria-autocomplete="list" aria-expanded="true" aria-controls="open-lang-listbox" class="cui-combobox-input" data-cui="combobox-input" data-expanded /> <button type="button" tabindex="-1" aria-label="Show languages" aria-expanded="true" class="cui-combobox-trigger" data-cui="combobox-trigger" data-expanded> <span aria-hidden="true" class="cui-combobox-icon" data-cui="combobox-icon"> <svg class="cui-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <path d="M8 9l4-4 4 4m0 6l-4 4-4-4" /> </svg> </span> </button> </div> </div> <div class="cui-combobox-content" data-cui="combobox-content" data-expanded> <ul id="open-lang-listbox" role="listbox" aria-label="Suggestions" class="cui-combobox-listbox" data-cui="combobox-listbox"> <li role="presentation" class="cui-combobox-section" data-cui="combobox-section">Web</li> <li role="option" aria-selected="true" class="cui-combobox-item" data-cui="combobox-item" data-selected> <div> <div class="cui-combobox-item-label" data-cui="combobox-item-label">TypeScript</div> <div class="cui-combobox-item-description" data-cui="combobox-item-description">.ts and .mts files</div> </div> <div aria-hidden="true" class="cui-combobox-item-indicator" data-cui="combobox-item-indicator"> <svg class="cui-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <path d="M20 6 9 17l-5-5" /> </svg> </div> </li> <li role="option" aria-selected="false" class="cui-combobox-item" data-cui="combobox-item" data-highlighted> <div> <div class="cui-combobox-item-label" data-cui="combobox-item-label">JavaScript</div> <div class="cui-combobox-item-description" data-cui="combobox-item-description">.js and .mjs files</div> </div> </li> <li role="presentation" class="cui-combobox-section" data-cui="combobox-section">Systems</li> <li role="option" aria-selected="false" class="cui-combobox-item" data-cui="combobox-item"> <div> <div class="cui-combobox-item-label" data-cui="combobox-item-label">Rust</div> <div class="cui-combobox-item-description" data-cui="combobox-item-description">.rs files</div> </div> </li> <li role="option" aria-selected="false" class="cui-combobox-item" data-cui="combobox-item" aria-disabled="true" data-disabled> <div> <div class="cui-combobox-item-label" data-cui="combobox-item-label">Zig</div> <div class="cui-combobox-item-description" data-cui="combobox-item-description">.zig files</div> </div> </li> </ul> </div></div>data-expanded on the content plays the pop-in from --cui-popper-content-transform-origin. Without it, the content plays its exit animation and stays transparent. data-motion="none" turns off both animations.
The list is at least as wide as --cui-popper-anchor-width and at most 400px tall, or --cui-popper-content-available-height when that is smaller. It scrolls past that. The demo matches the control width with the column around both.
A section is a small heading row with role="presentation". data-highlighted gives an item the menu hover fill and lifts its description to the item color. data-disabled dims an item. Render the check mark only in the selected item.
JavaScript is highlighted and Zig is disabled in this demo.
Empty
<div class="cui-stack" data-gap="1.5" style="max-width: 280px; margin-inline: auto"> <div role="group" class="cui-combobox" data-cui="combobox"> <label for="empty-lang" class="cui-combobox-label" data-cui="combobox-label">Language</label> <div class="cui-combobox-control" data-cui="combobox-control" data-size="sm" data-expanded> <input id="empty-lang" type="text" role="combobox" value="Haskel" placeholder="Search languages" autocomplete="off" aria-autocomplete="list" aria-expanded="true" aria-controls="empty-lang-listbox" class="cui-combobox-input" data-cui="combobox-input" data-expanded /> <button type="button" tabindex="-1" aria-label="Show languages" aria-expanded="true" class="cui-combobox-trigger" data-cui="combobox-trigger" data-expanded> <span aria-hidden="true" class="cui-combobox-icon" data-cui="combobox-icon"> <svg class="cui-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <path d="M8 9l4-4 4 4m0 6l-4 4-4-4" /> </svg> </span> </button> </div> </div> <div class="cui-combobox-content" data-cui="combobox-content" data-expanded> <ul id="empty-lang-listbox" role="listbox" aria-label="Suggestions" class="cui-combobox-listbox" data-cui="combobox-listbox"></ul> <div class="cui-combobox-empty" role="presentation">No language matches "Haskel".</div> </div></div>Put a div.cui-combobox-empty in the content, after the listbox, when no option matches the text.
Description, invalid, and disabled
<div class="cui-stack" data-gap="4" style="max-width: 280px; margin-inline: auto"> <div role="group" class="cui-combobox" data-cui="combobox"> <label for="desc-lang" class="cui-combobox-label" data-cui="combobox-label">Language</label> <div class="cui-combobox-control" data-cui="combobox-control" data-size="sm"> <input id="desc-lang" type="text" role="combobox" value="" placeholder="Search languages" autocomplete="off" aria-autocomplete="list" aria-expanded="false" class="cui-combobox-input" data-cui="combobox-input" aria-describedby="desc-lang-hint" /> <button type="button" tabindex="-1" aria-label="Show languages" aria-expanded="false" class="cui-combobox-trigger" data-cui="combobox-trigger"> <span aria-hidden="true" class="cui-combobox-icon" data-cui="combobox-icon"> <svg class="cui-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <path d="M8 9l4-4 4 4m0 6l-4 4-4-4" /> </svg> </span> </button> </div> <div id="desc-lang-hint" class="cui-combobox-description" data-cui="combobox-description">Sets the syntax highlighting.</div> </div> <div role="group" class="cui-combobox" data-cui="combobox" data-invalid> <label for="err-lang" class="cui-combobox-label" data-cui="combobox-label">Grammar</label> <div class="cui-combobox-control" data-cui="combobox-control" data-size="sm" data-invalid> <input id="err-lang" type="text" role="combobox" value="COBOL" placeholder="Search languages" autocomplete="off" aria-autocomplete="list" aria-expanded="false" class="cui-combobox-input" data-cui="combobox-input" aria-invalid="true" aria-describedby="err-lang-error" data-invalid /> <button type="button" tabindex="-1" aria-label="Show languages" aria-expanded="false" class="cui-combobox-trigger" data-cui="combobox-trigger"> <span aria-hidden="true" class="cui-combobox-icon" data-cui="combobox-icon"> <svg class="cui-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <path d="M8 9l4-4 4 4m0 6l-4 4-4-4" /> </svg> </span> </button> </div> <div id="err-lang-error" class="cui-combobox-error-message" data-cui="combobox-error-message">This language has no grammar.</div> </div> <div role="group" class="cui-combobox" data-cui="combobox" data-disabled> <label for="dis-lang" class="cui-combobox-label" data-cui="combobox-label">Workspace language</label> <div class="cui-combobox-control" data-cui="combobox-control" data-size="sm" data-disabled> <input id="dis-lang" type="text" role="combobox" value="TypeScript" placeholder="Search languages" autocomplete="off" aria-autocomplete="list" aria-expanded="false" class="cui-combobox-input" data-cui="combobox-input" disabled data-disabled /> <button type="button" tabindex="-1" aria-label="Show languages" aria-expanded="false" class="cui-combobox-trigger" data-cui="combobox-trigger" disabled> <span aria-hidden="true" class="cui-combobox-icon" data-cui="combobox-icon"> <svg class="cui-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <path d="M8 9l4-4 4 4m0 6l-4 4-4-4" /> </svg> </span> </button> </div> </div></div>A description goes under the control. Link it to the input with aria-describedby.
data-invalid on the control turns its border to the danger color. Show the error message and set aria-invalid="true" on the input.
data-disabled on the control dims it. Set the native disabled on the input and the trigger too.
What you supply
- Filtering the options as the user types, and opening the list on typing, ArrowDown, or a trigger click.
role="combobox",aria-expanded,aria-controls, andaria-autocomplete="list"on the input. Movearia-activedescendantwith the highlight.role="listbox"on the list androle="option"witharia-selectedon each item.- Up and Down Arrow move
data-highlighted. Enter selects. Escape closes the list and restores the selected label, and a second Escape clears the text. - The trigger stays out of the tab order with
tabindex="-1". Give it an accessible name. - Placement under the control,
--cui-popper-anchor-width,--cui-popper-content-available-height, and--cui-popper-content-transform-origin. - A hidden native
selector input if the value goes into a form.
Helper
import "@codeui/styles/combobox.css";
import { applyStyleProps } from "@codeui/styles";
import { comboboxStyleProps } from "@codeui/styles/combobox";
const control = document.querySelector<HTMLElement>("[data-combobox-control]")!;
applyStyleProps(control, comboboxStyleProps({ size: "md" }));
// class="cui-combobox-control" data-cui="combobox-control" data-size="md"comboboxStyleProps styles the control only. Use the comboboxStyles class helpers from the same entry for the other parts.