Select

Slant / Forms

A button that opens a list of options, with typeahead and a form value.

Demo

Usage

@tuttn/slant
import { Field, Select } from '@tuttn/slant'

const durations = [
  { value: '15', label: '15 minutes' },
  { value: '30', label: '30 minutes' },
  { value: '60', label: '1 hour' },
]

// Uncontrolled, in a Field: the label names it, and a hidden input submits
// the chosen value under `name`.
<Field>
  <Field.Label>Duration</Field.Label>
  <Select options={durations} defaultValue="30" name="duration" />
</Field>

// Controlled: null means nothing is chosen, and the placeholder shows.
const [reviewer, setReviewer] = useState<string | null>(null)
<Select options={people} value={reviewer} onValueChange={setReviewer}
  label="Reviewer" placeholder="Pick a reviewer" />

// Rich options: textValue is what typeahead matches and the closed select shows.
<Select label="Owner" options={[
  { value: 'ada', label: <><strong>Ada</strong> Lovelace</>, textValue: 'Ada Lovelace', description: 'Design systems' },
  { value: 'alan', label: 'Alan Turing', description: 'Away this week', disabled: true },
]} />

// A disabled option can say why: the reason shows under it and describes it.
<Select label="Runner" options={[
  { value: 'cpu', label: 'CPU runner' },
  { value: 'gpu', label: 'GPU runner', disabled: true, reason: 'No GPU minutes left this month' },
]} />

// The Field's invalid, required, disabled and readOnly reach the select.
<Field invalid={!reviewer} required>
  <Field.Label>Reviewer</Field.Label>
  <Select options={people} value={reviewer} onValueChange={setReviewer} />
  <Field.Error>Choose who reviews the change.</Field.Error>
</Field>

Props

PropTypeDefaultDescription
optionsreadonly SelectOption[]—The options, in order: `{ value, label, textValue?, description?, disabled?, reason? }`. The closed select shows the chosen option's `textValue`, else its label; typeahead matches `textValue`, else a string label.
valuestring | null—The chosen option's value, or null for none. Pass it with onValueChange to control the select; leave it out to let it keep its own.
defaultValuestring—The value an uncontrolled select starts with. Without one nothing is chosen and the placeholder shows.
onValueChange(value: string) => void—Called with the option's value when the reader chooses a different one.
openboolean—Whether the list is open. Pass it with onOpenChange to control it; leave it out and the select keeps its own state.
defaultOpenbooleanfalseWhether the list starts open, when uncontrolled.
onOpenChange(open: boolean) => void—Called with the new state whenever the list opens or closes.
placeholderstring'Choose…'What the button shows while nothing is chosen. From the root's `messages.selectPlaceholder`.
labelstring—The accessible name, unless a Field's label or `aria-labelledby` names the select. The button's text is its value, never its name.
namestring—Submits the select with a form: a hidden input with this name carries the chosen value (`''` while none is).
formstring—The id of the form the hidden input belongs to, when the select sits outside it.
size'sm' | 'md' | 'lg''md'Height on the control scale: 28, 34 or 40px at regular density.
invalidboolean—Sets aria-invalid and a danger border. Inside a Field, the field's `invalid` does this.
readOnlyboolean—Focusable and showing its value, but never opens. Inside a Field, the field's `readOnly` does this.
requiredboolean—Sets aria-required. Inside a Field, the field's `required` does this.
disabledboolean—Dims the select and ignores input; a disabled select is not submitted. Inside a Field, the field's `disabled` does this.
placementPlacement'bottom-start'The side of the button the list hangs from, and the edge it lines up with. Start and end follow the text direction, and the list flips to the other side when this one lacks room.
…restButtonHTMLAttributes<HTMLButtonElement>—The ref, className, style and every other attribute (id, autoFocus, aria-*, data-*, handlers) reach the <button role="combobox">. Your onClick and onKeyDown run first and can call preventDefault to stop the select’s own handling. `name` and `form` go on the hidden input that carries the value.

Keyboard

  • On the closed select, ArrowDown, Enter, Space and Alt+ArrowDown open the list on the chosen option, else the first enabled one; ArrowUp opens it on the chosen option, else the last. Home and End open it on the first and last options, and typing a letter opens it on the next option that starts with it.
  • In the open list, ArrowDown and ArrowUp move one option, skipping disabled ones and stopping at the ends; Home and End jump to them, and Page Up and Page Down move ten. Typing jumps too: letters typed within half a second build one search, and a repeated letter cycles through the options that start with it.
  • Enter, Space and Alt+ArrowUp choose the active option and close the list. A Space typed in the middle of a search is part of the search, so a name with a space in it can be typed whole.
  • Escape closes the list without choosing, and goes no further: a Dialog around the select stays open. Tab closes it without choosing and moves on as usual.

Accessibility

It is the WAI-ARIA select-only combobox: a button with role="combobox", aria-haspopup="listbox" and aria-expanded. Focus never leaves the button; while the list is open, aria-controls names the list and aria-activedescendant the active option, so a screen reader follows the keys without focus moving. The options carry aria-selected and aria-disabled.

A disabled option with a reason shows it as a line under its label, after any description, and the reason stays readable while the rest of the option dims. It is the option’s accessible description (aria-describedby) and is kept out of its name, so a screen reader reads it once. The arrows and typeahead skip the option and a click on it does nothing either way; the reason is what tells the reader why. Keep it short: what is missing, or what would make the option available.

A Field’s label names the button (through for), else label or aria-labelledby; the button’s text is its value, never its name. The list is named the same way. The field’s description and error describe it, and required, readOnly and invalid become aria-required, aria-readonly and aria-invalid.

Forms

Given a name, the select renders a hidden input carrying the chosen value, or an empty string while none is, so it submits with a plain form; form ties it to a form elsewhere on the page. A disabled select is not submitted. The button itself is type="button", so Enter on it never submits the form.

Behaviour

A press on the button opens and closes the list. Hovering lights an option, and a click chooses it and closes the list, leaving focus on the button. A click on the Field’s label only focuses the select, as it does a native one. A press outside the list, or focus moving anywhere else, closes it without choosing.

The list renders in a portal, at least as wide as the button, so no overflow or stacking context clips it; it hangs from the side placement names, flips above when there is no room below, and inside a Dialog or a Popover it renders inside that layer. A read-only select is focusable and shows its value but never opens; a disabled one is a disabled button. Without value or open, it keeps its own state, starting from defaultValue and defaultOpen.

To filter a long list by typing, use a Combobox. For two to five choices worth seeing at once, a Radio group or Segmented control; for actions rather than a value, a Menu.