select
select is a button that opens a list of options. A person picks one, and the list closes.
onChange called 0 time(s)
value is ''
import { computed, state } from '@markless/core';import { lucide, select } from '@markless/ui';export default function SelectBasic() @{ const names: Readonly<Record<string, string>> = { apple: 'Apple', banana: 'Banana', cherry: 'Cherry', }; let fruit = state(''); const shown = computed(() => (fruit === '' ? 'Choose a fruit' : names[fruit])); const written = computed(() => (fruit === '' ? "''" : `'${fruit}'`)); <select.root class="picker" name="fruit" value={fruit} onChange={(next: string) => { fruit = next; }} > <select.label class="label">Favourite fruit</select.label> <div class="box"> <select.trigger class="trigger"> <span>{shown}</span> <lucide.chevrondown class="caret" aria-hidden="true" /> </select.trigger> <select.content class="list"> <select.item class="option" value="apple"> <select.itemlabel>Apple</select.itemlabel> <select.itemindicator class="mark"> <lucide.check class="icon" aria-hidden="true" /> </select.itemindicator> </select.item> <select.item class="option" value="banana"> <select.itemlabel>Banana</select.itemlabel> <select.itemindicator class="mark"> <lucide.check class="icon" aria-hidden="true" /> </select.itemindicator> </select.item> <select.item class="option" value="cherry" disabled> <select.itemlabel>Cherry (sold out)</select.itemlabel> <select.itemindicator class="mark"> <lucide.check class="icon" aria-hidden="true" /> </select.itemindicator> </select.item> </select.content> </div> <p class="note"> value is <span class="code">{written}</span> </p> <select.field /> </select.root>;}
.picker { display: grid; gap: 0.4em; box-sizing: border-box; width: min(17rem, 100%); padding-block-end: 4em; 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;}.picker[ui-required] .label::after { content: ' (required)'; color: var(--muted-ink);}.box { position: relative;}.trigger { display: flex; gap: 0.75em; align-items: center; justify-content: space-between; box-sizing: border-box; width: 100%; padding: 0.55em 0.9em; border: 1px solid var(--ink); border-radius: 6px; background: var(--paper); color: var(--ink); font: inherit; font-family: 'Shantell Sans', system-ui, sans-serif; text-align: start; cursor: pointer; transition: background 140ms ease;}.trigger:hover { background: color-mix(in srgb, var(--purple) 16%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.trigger[ui-open] { background: color-mix(in srgb, var(--purple) 26%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.trigger:disabled { opacity: 0.55; cursor: not-allowed;}.trigger:focus-visible { outline: 2px solid var(--ink); outline-offset: 4px;}.caret { width: 1em; height: 1em; flex: none; opacity: 0.65; transition: rotate 140ms ease;}.trigger[ui-open] .caret { rotate: 180deg;}.list { position: absolute; inset-block-start: calc(100% + 0.3em); inset-inline: 0; z-index: 5; display: grid; padding: 0.25em; border: 1px solid var(--ink); border-radius: 6px; background: var(--raised); box-shadow: 3px 3px 0 color-mix(in srgb, var(--purple) 45%, var(--cream));}.list[hidden] { display: none;}.option { display: flex; gap: 0.75em; align-items: center; justify-content: space-between; padding: 0.4em 0.7em; border-radius: 4px; font-family: 'Shantell Sans', system-ui, sans-serif; cursor: pointer;}.option:hover { 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:focus-visible { outline: 2px solid var(--ink); outline-offset: -2px;}.option[ui-disabled] { opacity: 0.55; cursor: not-allowed;}.option[ui-disabled]:hover { background: transparent;}.mark { display: inline-flex;}.mark[ui-hidden] { visibility: hidden;}.icon { width: 1em; height: 1em;}.note { margin: 0; color: var(--muted-ink); font-family: 'Shantell Sans', system-ui, sans-serif;}.code { color: var(--ink); font-family: var(--font-mono);}@media (prefers-reduced-motion: reduce) { .trigger, .caret { transition: none; }}
Open the list and pick a fruit. Cherry is disabled. The line under the button shows the root's
value.
Now try it with the keyboard. Tab to the button and type b. The list opens on Banana. Press
Enter to pick it.
Each option has a value, and the family tracks the choice by that value. It never uses the
option's position. So you can reorder the options and the choice stays the same.
When to use it
Use select when a person picks one option from a fixed list. If they need to type to filter
the list, use combobox instead. select has no multiple prop. For several
choices, use checklist.
Parts
Holds the selection and connects the label, input or trigger, and options.
select.rootholds the chosen value, whether the list is open, and the form settings.select.labelnames the button and the list. Both point at it witharia-labelledby.select.triggeris the button that opens the list. It is the only Tab stop in the family.select.contentis the list, withrole="listbox". A closed list getshidden. It stays in the page.select.itemis one option. Itsvalueis required.select.itemlabelholds the option's words. Typing a letter matches against this text only.select.itemindicatoris the mark on the chosen option. It isaria-hidden, so a screen reader does not read it.select.fieldis a hidden native<select>. It sends the choice to a form undername. Tab never reaches it.
Styling hooks
The family writes these attributes. Each one is present while its state holds:
| Attribute | On | Present when |
|---|---|---|
ui-open, ui-closed |
root, trigger, content | The list is open, or closed. |
ui-disabled |
root | disabled is set on the root. |
ui-required |
root | required is set on the root. |
ui-selected |
item | This option is the chosen one. |
ui-disabled |
item | This option or the whole select is disabled. |
ui-hidden |
itemindicator | Its option is not the chosen one. |
Keyboard
The family's tests check each of these rows.
| Key | What it does |
|---|---|
| Down arrow, Home, Enter or Space on the button | Opens the list. Focus goes to the chosen option, or else to the first one. |
| Up arrow or End on the button | Opens the list. Focus goes to the chosen option, or else to the last one. |
| Alt and Down arrow on the button | Opens the list. Focus stays on the button. |
| A letter on the button | Opens the list on the first option that starts with that letter. |
| Down arrow or Up arrow in the list | Moves focus one option. It stops at the ends. |
| Home or End in the list | Moves focus to the first or the last option. |
| Page Down or Page Up in the list | Moves focus ten options. It stops at the ends. |
| Letters in the list | Moves focus to the first option that starts with what you typed. |
| Enter or Space in the list | Picks the focused option, closes the list, and puts focus on the button. |
| Escape | Closes the list without a change. Focus goes back to the button. |
| Tab in the list | Closes the list without a change. |
The arrows and typed letters skip a disabled option. A click outside the open list closes it and picks nothing.
Gotchas
API reference
select.root
valuestringThe value of the chosen option. Omit it and nothing is chosen.Default''.openbooleanThe popup is showing. Omit it and it starts closed.Defaultfalse.disabledbooleanNobody can change the choice.Defaultfalse.requiredbooleanA choice is needed before the form submits.Defaultfalse.namestringSubmitted under this name byselect.field.Default''.onChange(value: string) => voidIntended to be called with the new value when a person chooses a different option. Omit it and choosing still works; the call site simply does nothing.onOpenChange(open: boolean) => voidCalled when the popup opens or closes.
select.label
No props of its own.
select.trigger
No props of its own.
select.content
No props of its own.
select.item
valuestringRequired.Submitted when this option is the chosen one. Required: no index stands in for it.disabledbooleanNobody can choose this option, and the arrow keys walk past it.Defaultfalse.
select.itemlabel
No props of its own.
select.itemindicator
No props of its own.
select.field
No props of its own.