Skip to Content
CodeUI

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

combobox/basic.html
<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

PartElementClassdata-cui
Rootdivcui-comboboxcombobox
Labellabelcui-combobox-labelcombobox-label
Controldivcui-combobox-controlcombobox-control
Inputinputcui-combobox-inputcombobox-input
Triggerbuttoncui-combobox-triggercombobox-trigger
Iconspan with an svgcui-combobox-iconcombobox-icon
Descriptiondivcui-combobox-descriptioncombobox-description
Error messagedivcui-combobox-error-messagecombobox-error-message
Contentdivcui-combobox-contentcombobox-content
Listboxulcui-combobox-listboxcombobox-listbox
Sectionlicui-combobox-sectioncombobox-section
Itemlicui-combobox-itemcombobox-item
Item labeldivcui-combobox-item-labelcombobox-item-label
Item descriptiondivcui-combobox-item-descriptioncombobox-item-description
Item indicatordiv with an svgcui-combobox-item-indicatorcombobox-item-indicator
Empty messagedivcui-combobox-emptyNone

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.

AttributeValuesDefault
data-size on the controlsm, mdsm
data-invalid on the controlPresenceOff
data-disabled on the controlPresenceOff
data-expanded on the contentPresenceOff
data-motion on the contentnoneNone
data-highlighted, data-disabled on an itemPresenceOff

Variants

Sizes

combobox/sizes.html
<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

combobox/open.html
<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

combobox/empty.html
<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

combobox/states.html
<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, and aria-autocomplete="list" on the input. Move aria-activedescendant with the highlight.
  • role="listbox" on the list and role="option" with aria-selected on 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 select or 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.

On this page