Select
@codeui/styles v0.3.0
A field that opens a list of options, in plain HTML.
Load @codeui/styles/select.css, or all.css. The Solid component is documented in Select.
Demo
<div role="group" class="cui-select cui-field" data-cui="select" data-field-size="sm" style="max-width: 280px; margin-inline: auto"> <span id="font-label" class="cui-field-label" data-cui="select-label">Font</span> <button type="button" id="font" aria-haspopup="listbox" aria-expanded="false" aria-labelledby="font-label font" class="cui-field-input cui-select-trigger" data-cui="select-trigger" data-size="sm" data-variant="filled"> <span data-cui="select-value">JetBrains Mono</span> <span aria-hidden="true" data-cui="select-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>Markup
| Part | Element | Class | data-cui |
|---|---|---|---|
| Root | div | cui-select cui-field | select |
| Label | span or label | cui-field-label | select-label |
| Trigger | button | cui-field-input cui-select-trigger | select-trigger |
| Value | span | None | select-value |
| Icon | span with an svg | None | select-icon |
| Description | div | cui-field-description | select-description |
| Error message | div | cui-field-error | select-error-message |
| Content | div | cui-select-content | select-content |
| Listbox | ul | None | select-listbox |
| Item | li | cui-select-item | select-item |
| Item label | div | None | select-item-label |
| Item indicator | svg | cui-select-item-indicator | select-item-indicator |
The trigger shares the field box with TextField. It lays out the value and the icon at both ends.
The content is a sibling of the root, not a child. The listbox inside it has no class, so reset the ul padding, margin, and bullets.
| Attribute | Values | Default |
|---|---|---|
data-field-size on the root | xs, sm, md, lg | sm |
data-size on the trigger | xs, sm, md, lg | sm |
data-variant on the trigger | filled, inline | filled |
data-invalid on the trigger | Presence | Off |
data-disabled on the trigger | Presence | Off |
data-expanded on the content | Presence | Off |
data-selected, 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-select cui-field" data-cui="select" data-field-size="xs"> <span id="size-xs-label" class="cui-field-label" data-cui="select-label">Size xs</span> <button type="button" id="size-xs" aria-haspopup="listbox" aria-expanded="false" aria-labelledby="size-xs-label size-xs" class="cui-field-input cui-select-trigger" data-cui="select-trigger" data-size="xs" data-variant="filled"> <span data-cui="select-value">JetBrains Mono</span> <span aria-hidden="true" data-cui="select-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 role="group" class="cui-select cui-field" data-cui="select" data-field-size="sm"> <span id="size-sm-label" class="cui-field-label" data-cui="select-label">Size sm</span> <button type="button" id="size-sm" aria-haspopup="listbox" aria-expanded="false" aria-labelledby="size-sm-label size-sm" class="cui-field-input cui-select-trigger" data-cui="select-trigger" data-size="sm" data-variant="filled"> <span data-cui="select-value">JetBrains Mono</span> <span aria-hidden="true" data-cui="select-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 role="group" class="cui-select cui-field" data-cui="select" data-field-size="md"> <span id="size-md-label" class="cui-field-label" data-cui="select-label">Size md</span> <button type="button" id="size-md" aria-haspopup="listbox" aria-expanded="false" aria-labelledby="size-md-label size-md" class="cui-field-input cui-select-trigger" data-cui="select-trigger" data-size="md" data-variant="filled"> <span data-cui="select-value">JetBrains Mono</span> <span aria-hidden="true" data-cui="select-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 role="group" class="cui-select cui-field" data-cui="select" data-field-size="lg"> <span id="size-lg-label" class="cui-field-label" data-cui="select-label">Size lg</span> <button type="button" id="size-lg" aria-haspopup="listbox" aria-expanded="false" aria-labelledby="size-lg-label size-lg" class="cui-field-input cui-select-trigger" data-cui="select-trigger" data-size="lg" data-variant="filled"> <span data-cui="select-value">JetBrains Mono</span> <span aria-hidden="true" data-cui="select-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>xs, sm, md, and lg are 28px, 36px, 44px, and 52px at comfortable density. Set the same size as data-size on the trigger and data-field-size on the root, so the label and the messages match.
Filled and inline
<div class="cui-stack" data-gap="4" style="max-width: 280px; margin-inline: auto"> <div role="group" class="cui-select cui-field" data-cui="select" data-field-size="sm"> <span id="theme-filled-label" class="cui-field-label" data-cui="select-label">Filled</span> <button type="button" id="theme-filled" aria-haspopup="listbox" aria-expanded="false" aria-labelledby="theme-filled-label theme-filled" class="cui-field-input cui-select-trigger" data-cui="select-trigger" data-size="sm" data-variant="filled"> <span data-cui="select-value">JetBrains Mono</span> <span aria-hidden="true" data-cui="select-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 role="group" class="cui-select cui-field" data-cui="select" data-field-size="sm"> <span id="theme-inline-label" class="cui-field-label" data-cui="select-label">Inline</span> <button type="button" id="theme-inline" aria-haspopup="listbox" aria-expanded="false" aria-labelledby="theme-inline-label theme-inline" class="cui-field-input cui-select-trigger" data-cui="select-trigger" data-size="sm" data-variant="inline"> <span data-cui="select-value">JetBrains Mono</span> <span aria-hidden="true" data-cui="select-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>filled, the default, draws the field box. inline drops the box and keeps a bottom border, for a select that sits inside a sentence or a toolbar.
States
Open
<div class="cui-stack" data-gap="1.5" style="max-width: 280px; margin-inline: auto"> <div role="group" class="cui-select cui-field" data-cui="select" data-field-size="sm" data-expanded> <span id="open-font-label" class="cui-field-label" data-cui="select-label">Font</span> <button type="button" id="open-font" aria-haspopup="listbox" aria-expanded="true" aria-controls="open-font-listbox" aria-labelledby="open-font-label open-font" class="cui-field-input cui-select-trigger" data-cui="select-trigger" data-size="sm" data-variant="filled" data-expanded> <span data-cui="select-value">JetBrains Mono</span> <span aria-hidden="true" data-cui="select-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 class="cui-select-content" data-cui="select-content" data-expanded> <ul id="open-font-listbox" role="listbox" aria-labelledby="open-font-label" data-cui="select-listbox" style="margin: 0; padding: 0; list-style: none"> <li role="option" aria-selected="true" class="cui-select-item" data-cui="select-item" data-selected> <div data-cui="select-item-label">JetBrains Mono</div> <svg class="cui-icon cui-select-item-indicator" data-cui="select-item-indicator" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M20 6 9 17l-5-5" /> </svg> </li> <li role="option" aria-selected="false" class="cui-select-item" data-cui="select-item" data-highlighted> <div data-cui="select-item-label">Fira Code</div> </li> <li role="option" aria-selected="false" class="cui-select-item" data-cui="select-item"> <div data-cui="select-item-label">Source Code Pro</div> </li> <li role="option" aria-selected="false" aria-disabled="true" class="cui-select-item" data-cui="select-item" data-disabled> <div data-cui="select-item-label">Comic Mono</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 270px tall at most, 400px from the sm breakpoint, and scrolls past that. It has no width of its own. Match the trigger width, as the demo does with the column around both.
In the list, data-selected marks the chosen item. Render the check mark only in the selected item: an unselected item reserves its space with right padding, so the labels stay aligned. data-highlighted gives an item the hover fill, and data-disabled dims it. Fira Code is highlighted and Comic Mono is disabled in this demo.
Description, invalid, and disabled
<div class="cui-stack" data-gap="4" style="max-width: 280px; margin-inline: auto"> <div role="group" class="cui-select cui-field" data-cui="select" data-field-size="sm"> <span id="desc-font-label" class="cui-field-label" data-cui="select-label">Font</span> <button type="button" id="desc-font" aria-haspopup="listbox" aria-expanded="false" aria-labelledby="desc-font-label desc-font" class="cui-field-input cui-select-trigger" data-cui="select-trigger" data-size="sm" data-variant="filled" aria-describedby="desc-font-hint"> <span data-cui="select-value">Fira Code</span> <span aria-hidden="true" data-cui="select-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 id="desc-font-hint" class="cui-field-description" data-cui="select-description">Used by every new snippet.</div> </div> <div role="group" class="cui-select cui-field" data-cui="select" data-field-size="sm" data-invalid> <span id="err-font-label" class="cui-field-label" data-cui="select-label">Fallback font</span> <button type="button" id="err-font" aria-haspopup="listbox" aria-expanded="false" aria-labelledby="err-font-label err-font" class="cui-field-input cui-select-trigger" data-cui="select-trigger" data-size="sm" data-variant="filled" aria-invalid="true" aria-describedby="err-font-error" data-invalid> <span data-cui="select-value">Comic Mono</span> <span aria-hidden="true" data-cui="select-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 id="err-font-error" class="cui-field-error" data-cui="select-error-message">This font has no ligatures.</div> </div> <div role="group" class="cui-select cui-field" data-cui="select" data-field-size="sm" data-disabled> <span id="dis-font-label" class="cui-field-label" data-cui="select-label">Workspace font</span> <button type="button" id="dis-font" aria-haspopup="listbox" aria-expanded="false" aria-labelledby="dis-font-label dis-font" class="cui-field-input cui-select-trigger" data-cui="select-trigger" data-size="sm" data-variant="filled" disabled data-disabled> <span data-cui="select-value">JetBrains Mono</span> <span aria-hidden="true" data-cui="select-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>A description goes under the trigger. Link it with aria-describedby.
data-invalid on the trigger turns its border and text to the danger color. Show the error message under it and set aria-invalid="true".
data-disabled dims the trigger. Set the native disabled too, so it leaves the tab order.
What you supply
- Opening and closing the content from the trigger, and
aria-expandedandaria-controlson it. role="listbox"on the list androle="option"witharia-selectedon each item.- Arrow keys, Home, End, and type-ahead move
data-highlighted. Enter and Space select. Escape closes the list and returns focus to the trigger. - The selected value text in the trigger, and a hidden native
selector input if the value goes into a form. - Placement under the trigger, the content width, and
--cui-popper-content-transform-origin.
Helper
import "@codeui/styles/select.css";
import { applyStyleProps } from "@codeui/styles";
import { selectStyleProps } from "@codeui/styles/select";
const root = document.querySelector<HTMLElement>("[data-select]")!;
const trigger = root.querySelector<HTMLElement>("button")!;
applyStyleProps(root, selectStyleProps.root({ fieldSize: "md" }));
applyStyleProps(trigger, selectStyleProps({ size: "md", variant: "inline" }));
// trigger: class="cui-field-input cui-select-trigger" data-cui="select-trigger" data-size="md" data-variant="inline"