Skip to content

Select

A drop-down list with its own popup, built from scratch.

import { Select } from '@/design-system';

Examples

Basics

Type a letter to jump to it.

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. required is 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.

Select props
PropTypeDefaultDescription
optionsSelectOption[]requiredEach has a value, a label and optionally disabled. Values must differ.
valuestring—The chosen value, when you keep the state.
defaultValuestringfirst optionThe starting value. Use an empty string to start on the placeholder.
onChange(value: string) => void—Called with the new value, not an event.
namestring—Sends the value with a form.
placeholderstring'Select…'Shown while no option matches the value.
disabledbooleanfalseStops it being used.
requiredbooleanfalseMarks it as required.
idstringfrom FieldThe id of the control.
aria-labelstring—A name, when it is used without a Field.
classNamestring—Extra classes, for spacing or width.

Tokens

Tokens
TokenDefaultSets
--field-radius--radius-controlCorner radius of the closed control
--field-background--surfaceFill
--field-border--borderBorder colour
--field-font-size0.875remText size
--popup-radius--radius-controlCorner radius of the popup
--popup-background--surfacePopup fill
--popup-border--borderPopup border
--popup-shadowa soft shadowPopup shadow
--option-active-backgrounda faint accentThe highlighted option
--listbox-max-height20remTallest the list gets before scrolling