Select
A form field with one or more values from a list of options.
Installation
pnpm dlx @human-kit/design add selectnpx @human-kit/design add selectyarn dlx @human-kit/design add selectbunx @human-kit/design add selectThe 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
itemsto 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
variantandsizeof 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>withrole="combobox"andaria-haspopup="listbox". Its name is the label and then the value: "Currency, Colombian peso". Select.Labelis a<label>for the trigger: a click on it focuses the trigger, as the label of a native select does. Without one, givearia-labelon the root.- On the trigger,
ArrowDown,ArrowUp,EnterandSpaceopen 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,
HomeandEndjump to the ends, typing searches, andEnterorSpacechooses.Escapecloses it with no change, and the focus goes back to the trigger. required,readonlyandinvalidreach the trigger asaria-required,aria-readonlyandaria-invalid.
API reference
Root
| Prop | Type | Default |
|---|---|---|
value | SelectKey | null | SelectKey[] The chosen key, or the keys in the multiple mode. Bindable. | — |
defaultValue | SelectKey | null | SelectKey[] The choice at the start, when you give no | — |
onChange | (value: SelectKey | null | SelectKey[]) => void Called when the choice changes. | — |
items | T[] The options, each with a key in | — |
placeholder | string The text of the trigger while nothing is chosen. | — |
selectionMode | 'single' | 'multiple' One option, or more than one. | 'single' |
closeOnSelect | boolean Closes the list after a choice. | — |
open | boolean Whether the list is open. Bindable. | — |
onOpenChange | (open: boolean, details: SelectOpenChangeDetails) => void Called when the list opens or closes, with the reason. | — |
disabledKeys | Iterable<SelectKey> The options the user cannot choose. | — |
loop | boolean The arrows go on from the last option to the first, and back. | — |
disabled | boolean Turns the select off. | — |
readonly | boolean Keeps the trigger in the tab order, but the list does not open. | — |
required | boolean The form needs a value. | — |
invalid | boolean Marks the value as wrong: the ring turns red. | — |
name | string The name the form submits the value with. Without it, the form does not send it. | — |
aria-label | string The name of the select, when there is no | — |
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
| Prop | Type | Default |
|---|---|---|
variant | 'default' | 'filled' | 'ghost' | 'underline' The look of the field, the same as an input. With | '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' |
children | Snippet 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
| Prop | Type | Default |
|---|---|---|
offset | number The gap between the trigger and the list, in px. | 4 |
listClass | string The class of the list inside the popover. | — |
Plus the props of the popover, as placement.
Item
| Prop | Type | Default |
|---|---|---|
id | string | number The key of the option, the one | — |
textValue | string The text that typing matches, when the content is not plain text. | — |
disabled | boolean The user cannot choose it. | — |
Plus every attribute of a <div>.
Value
| Prop | Type | Default |
|---|---|---|
placeholder | string The text while nothing is chosen, in place of the one of the root. | — |
children | Snippet<[SelectValueRenderState]> Your own content, given | — |
Plus every attribute of a <span>. The trigger draws one when you give it no content.