Skip to the content
human-kit design
27 — Pickers

Select

A form field with one or more values from a list of options.

Installation

pnpm dlx @human-kit/design add select
npx @human-kit/design add select
yarn dlx @human-kit/design add select
bunx @human-kit/design add select

The command copies the files to src/lib/components/ui/select. It also installs the packages that the files import.

Usage

<script lang="ts">
	import * as Select from '$lib/components/ui/select';

	const currencies = [
		{ id: 'cop', label: 'Colombian peso' },
		{ id: 'usd', label: 'US dollar' }
	];

	let value = $state<string | number | null>('cop');
</script>

<Select.Root bind:value items={currencies}>
	<Select.Label>Currency</Select.Label>
	<Select.Trigger />
	<Select.Content>
		{#each currencies as currency (currency.id)}
			<Select.Item id={currency.id}>{currency.label}</Select.Item>
		{/each}
	</Select.Content>
</Select.Root>

Examples

Placeholder and disabled options

With no value, the trigger shows placeholder in a fainter ink. disabledKeys keeps an option in the list, but the user cannot choose it.

In a field

Inside a Field.Root, spread the control it hands you on Select.Root: the label of the field names the select, and its error describes it and turns the ring red. Leave out Select.Label then.

Choose the payment terms.

Choose the payment terms.

A long list

For thousands of options, give the array as items to the root and to Select.Content, a snippet that draws one option, and virtualizer: the list draws only the rows near its viewport. Rows are one height, 24px, the rowHeight it is told. It opens centred on the chosen option, and Home, End, Page Up, Page Down and typing a code reach every option.

Guidelines

  • Use a select to pick one value from a short, fixed list. For a long list, or one the user searches, use a combobox.
  • For two or three options the user should see at once, use a radio group or a toggle group.
  • Pass items to the root: the trigger reads the text of the value from it while the list is closed and its options are not on the page.
  • Give the trigger the variant and size of the inputs beside it, so the form reads as one.
  • Use look="button" in a toolbar or a dock, where the trigger sits among buttons, not fields.

Accessibility

  • The trigger is a native <button> with role="combobox" and aria-haspopup="listbox". Its name is the label and then the value: "Currency, Colombian peso".
  • Select.Label is a <label> for the trigger: a click on it focuses the trigger, as the label of a native select does. Without one, give aria-label on the root.
  • On the trigger, ArrowDown, ArrowUp, Enter and Space open the list on the chosen option. A typed letter opens it on the first option that starts with it.
  • In the list, the arrows move, Home and End jump to the ends, typing searches, and Enter or Space chooses. Escape closes it with no change, and the focus goes back to the trigger.
  • required, readonly and invalid reach the trigger as aria-required, aria-readonly and aria-invalid.

API reference

Root

PropTypeDefault
valueSelectKey | null | SelectKey[]

The chosen key, or the keys in the multiple mode. Bindable.

—
defaultValueSelectKey | null | SelectKey[]

The choice at the start, when you give no value.

—
onChange(value: SelectKey | null | SelectKey[]) => void

Called when the choice changes.

—
itemsT[]

The options, each with a key in id or value and a text in textValue, label or name. The trigger reads its text from them.

—
placeholderstring

The text of the trigger while nothing is chosen.

—
selectionMode'single' | 'multiple'

One option, or more than one.

'single'
closeOnSelectboolean

Closes the list after a choice. true in the single mode, false in the multiple one.

—
openboolean

Whether the list is open. Bindable.

—
onOpenChange(open: boolean, details: SelectOpenChangeDetails) => void

Called when the list opens or closes, with the reason. details.cancel() refuses it.

—
disabledKeysIterable<SelectKey>

The options the user cannot choose.

—
loopboolean

The arrows go on from the last option to the first, and back.

—
disabledboolean

Turns the select off.

—
readonlyboolean

Keeps the trigger in the tab order, but the list does not open.

—
requiredboolean

The form needs a value.

—
invalidboolean

Marks the value as wrong: the ring turns red.

—
namestring

The name the form submits the value with. Without it, the form does not send it.

—
aria-labelstring

The name of the select, when there is no Select.Label.

—

Plus every attribute of a <div>. The root holds the state and has no look of its own. Select.Label has no props of its own: it takes those of a <label> but for, which points at the trigger.

Trigger

PropTypeDefault
variant'default' | 'filled' | 'ghost' | 'underline'

The look of the field, the same as an input. With look="button", the button of the same weight: default is the outline button, filled the filled one, ghost and underline the ghost one.

'default'
size'sm' | 'default' | 'lg'

The height, the same as an input. A large one takes the width of its column.

'default'
look'field' | 'button'

A field among fields, or the ghost button for a toolbar.

'field'
childrenSnippet

The content of the trigger. The default is the chosen value.

—

Plus every attribute of a native <button>, but the ARIA ones the select sets.

Content

PropTypeDefault
offsetnumber

The gap between the trigger and the list, in px.

4
listClassstring

The class of the list inside the popover.

—

Plus the props of the popover, as placement.

Item

PropTypeDefault
idstring | number

The key of the option, the one value holds.

—
textValuestring

The text that typing matches, when the content is not plain text.

—
disabledboolean

The user cannot choose it.

—

Plus every attribute of a <div>.

Value

PropTypeDefault
placeholderstring

The text while nothing is chosen, in place of the one of the root.

—
childrenSnippet<[SelectValueRenderState]>

Your own content, given value, label and placeholder.

—

Plus every attribute of a <span>. The trigger draws one when you give it no content.