Select
A drop-down list with its own popup, built from scratch.
import { Select } from '@/design-system';Examples
Basics
import { Field, Select } from '@/design-system';
const PLANS = [
{ value: 'starter', label: 'Starter' },
{ value: 'team', label: 'Team' },
{ value: 'business', label: 'Business' },
{ value: 'enterprise', label: 'Enterprise (ask us)', disabled: true },
];
export default function SelectBasic() {
return (
<div className="grid gap-4 sm:grid-cols-2">
<Field label="Plan">
<Select name="plan" options={PLANS} defaultValue="team" />
</Field>
<Field label="With a placeholder" hint="Type a letter to jump to it.">
<Select options={PLANS} defaultValue="" placeholder="Choose a plan" />
</Field>
</div>
);
}Long lists and positioning
Scroll until this is near the bottom of the window and open it: the list moves above.
import { Field, Select } from '@/design-system';
const HOURS = Array.from({ length: 24 }, (_, hour) => ({
value: String(hour),
label: `${String(hour).padStart(2, '0')}:00`,
}));
export default function SelectLong() {
return (
<Field label="Hour" className="max-w-40">
<Select options={HOURS} defaultValue="12" />
</Field>
);
}How it works
- Keyboard: arrows, Enter or Space open the list. Arrows, Home, End and Page keys move. Enter, Space or Tab choose. Escape closes without choosing.
- Typing letters jumps to a matching option. Pressing one letter again moves to the next match.
- The popup opens below when it fits, otherwise on the side with more room. It stays inside the window, scrolls when too tall, and follows the control while the page scrolls or resizes.
- It is drawn above everything, including inside a
Dialog, and keeps the theme around it. It uses the browser’s popover support. - With a
name, a hidden input sends the value with a form.requiredis announced, but the browser does not enforce it, so check the value yourself. Resetting a form does not reset it.
Props
Select takes only these props.
| Prop | Type | Default | Description |
|---|---|---|---|
options | SelectOption[] | required | Each has a value, a label and optionally disabled. Values must differ. |
value | string | — | The chosen value, when you keep the state. |
defaultValue | string | first option | The starting value. Use an empty string to start on the placeholder. |
onChange | (value: string) => void | — | Called with the new value, not an event. |
name | string | — | Sends the value with a form. |
placeholder | string | 'Select…' | Shown while no option matches the value. |
disabled | boolean | false | Stops it being used. |
required | boolean | false | Marks it as required. |
id | string | from Field | The id of the control. |
aria-label | string | — | A name, when it is used without a Field. |
className | string | — | Extra classes, for spacing or width. |
Tokens
| Token | Default | Sets |
|---|---|---|
--field-radius | --radius-control | Corner radius of the closed control |
--field-background | --surface | Fill |
--field-border | --border | Border colour |
--field-font-size | 0.875rem | Text size |
--popup-radius | --radius-control | Corner radius of the popup |
--popup-background | --surface | Popup fill |
--popup-border | --border | Popup border |
--popup-shadow | a soft shadow | Popup shadow |
--option-active-background | a faint accent | The highlighted option |
--listbox-max-height | 20rem | Tallest the list gets before scrolling |