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>;}

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

select anatomy
Anatomy perspectiveFlat
Layers
Your workspaceDesign studioDesign studio✓EngineeringCustomer care
select.root

Holds the selection and connects the label, input or trigger, and options.

  • select.root holds the chosen value, whether the list is open, and the form settings.
  • select.label names the button and the list. Both point at it with aria-labelledby.
  • select.trigger is the button that opens the list. It is the only Tab stop in the family.
  • select.content is the list, with role="listbox". A closed list gets hidden. It stays in the page.
  • select.item is one option. Its value is required.
  • select.itemlabel holds the option's words. Typing a letter matches against this text only.
  • select.itemindicator is the mark on the chosen option. It is aria-hidden, so a screen reader does not read it.
  • select.field is a hidden native <select>. It sends the choice to a form under name. 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

valuestring
The value of the chosen option. Omit it and nothing is chosen. Default ''.
openboolean
The popup is showing. Omit it and it starts closed. Default false.
disabledboolean
Nobody can change the choice. Default false.
requiredboolean
A choice is needed before the form submits. Default false.
namestring
Submitted under this name by select.field. Default ''.
onChange(value: string) => void
Intended 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) => void
Called 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

valuestring
Required. Submitted when this option is the chosen one. Required: no index stands in for it.
disabledboolean
Nobody can choose this option, and the arrow keys walk past it. Default false.

select.itemlabel

No props of its own.

select.itemindicator

No props of its own.

select.field

No props of its own.