Playground

Select

Pick one value from a list of five or more options.

  • Built on native ARIA (shared positioning)
  • 7 parts, named exports
  • Trigger is role=combobox with aria-expanded; the list is role=listbox with option items.
  • Arrow keys, Home, End and type-ahead move; Enter selects; Escape closes.
  • Inside a Field it is labelled by the Field's label.
  • Styles in one file: select.css

Import

import '@monoline/css';
import {Select} from '@monoline/react';

Not installed yet? Pick a route.

Copies the source and its styles into your project.

npx monoline add select

Usage

import {Field, Select, SelectContent, SelectGroup, SelectItem, SelectLabel, SelectTrigger, SelectValue} from '@monoline/react';

<div style={{maxWidth: 320}}>
  <Field label="Sync every">
    <Select defaultValue="15">
      <SelectTrigger>
        <SelectValue placeholder="Choose" />
      </SelectTrigger>
      <SelectContent>
        <SelectGroup>
          <SelectLabel>Interval</SelectLabel>
          <SelectItem value="5">5 minutes</SelectItem>
          <SelectItem value="15">15 minutes</SelectItem>
          <SelectItem value="60">Hour</SelectItem>
        </SelectGroup>
      </SelectContent>
    </Select>
  </Field>
</div>

Anatomy

Import the parts you need and nest them like this.

import {Select, SelectGroup, SelectValue, SelectTrigger, SelectContent, SelectItem, SelectLabel} from '@monoline/react';

<Select>
  <SelectGroup />
  <SelectValue />
  <SelectTrigger />
  <SelectContent />
  <SelectItem />
  <SelectLabel />
</Select>

Examples

In a field

Props

SelectProps

PropTypeDefaultDescription
valuestring–Controlled value. An empty string shows the placeholder.
defaultValuestring''Initial value when uncontrolled.
onValueChange((value: string) => void)–Called with the chosen item's value.
openboolean–Controlled open state.
defaultOpenbooleanfalseInitial open state when uncontrolled. Default false.
onOpenChange((open: boolean) => void)–Called with the next open state.
disabledboolean–Disables the trigger.
namestring–Form field name; the value is submitted through a hidden input.
childrenReactNode–The parts of the component: a trigger and its content.

SelectTriggerProps

PropTypeDefaultDescription
size"sm" | "md" | "lg"'md'`md` (default), `sm` for dense layouts or `lg` for prominent forms. Heights match Button, Input and Select at each size.
invalidboolean–Marks the control invalid (`aria-invalid`). A surrounding Field sets this from its `error`.

SelectContentProps

PropTypeDefaultDescription
position"item-aligned" | "popper"–Accepted for compatibility with earlier versions; the list is always positioned against the trigger.

SelectItemProps

PropTypeDefaultDescription
value requiredstring–The value reported by `onValueChange` when this item is chosen. Must be non-empty.
disabledboolean–Makes the item unselectable and skipped by the keyboard.

Data Attributes

State is exposed through attributes. Style it with these selectors, not with class toggles.

AttributeStylesheet
[aria-invalid="true"]select.css
[data-disabled]select.css
[data-highlighted]select.css
[data-invalid]select.css
[data-placeholder]select.css
[data-state="open"]select.css

CSS classes

ClassStylesheet
.input-lgselect.css
.input-smselect.css
.select-contentselect.css
.select-itemselect.css
.select-labelselect.css
.select-triggerselect.css

Customization

Style it with the kit's tokens in your own CSS, loaded after @monoline/css. Prefer the attributes above over extra classes. See Theming for every token.

app.css
.input-lg {
  border-color: var(--accent);
}
.input-lg[aria-invalid="true"] {
  background: var(--wash);
}

Try the props

Change a prop and the component above updates; the code below is what you would write. To edit the code itself, use the Playground.

example.tsx
<Select>
  <SelectTrigger aria-label="Fruit">
    <SelectValue placeholder="Pick a fruit" />
  </SelectTrigger>
  <SelectContent>
    <SelectItem value="apple">Apple</SelectItem>
    <SelectItem value="pear">Pear</SelectItem>
    <SelectItem value="plum">Plum</SelectItem>
  </SelectContent>
</Select>

Accessibility

  • Trigger is role=combobox with aria-expanded; the list is role=listbox with option items.
  • Arrow keys, Home, End and type-ahead move; Enter selects; Escape closes.
  • Inside a Field it is labelled by the Field's label.

Keyboard

KeysAction
EnterSelects
EscapeCloses