Select
Pick one value from a list of five or more options.
SourceStyles source@monoline/react 0.4.0Markdown for AI toolsstable
- 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
| Prop | Type | Default | Description |
|---|---|---|---|
value | string | – | Controlled value. An empty string shows the placeholder. |
defaultValue | string | '' | Initial value when uncontrolled. |
onValueChange | ((value: string) => void) | – | Called with the chosen item's value. |
open | boolean | – | Controlled open state. |
defaultOpen | boolean | false | Initial open state when uncontrolled. Default false. |
onOpenChange | ((open: boolean) => void) | – | Called with the next open state. |
disabled | boolean | – | Disables the trigger. |
name | string | – | Form field name; the value is submitted through a hidden input. |
children | ReactNode | – | The parts of the component: a trigger and its content. |
SelectTriggerProps
| Prop | Type | Default | Description |
|---|---|---|---|
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. |
invalid | boolean | – | Marks the control invalid (`aria-invalid`). A surrounding Field sets this from its `error`. |
SelectContentProps
| Prop | Type | Default | Description |
|---|---|---|---|
position | "item-aligned" | "popper" | – | Accepted for compatibility with earlier versions; the list is always positioned against the trigger. |
SelectItemProps
| Prop | Type | Default | Description |
|---|---|---|---|
value required | string | – | The value reported by `onValueChange` when this item is chosen. Must be non-empty. |
disabled | boolean | – | 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.
| Attribute | Stylesheet |
|---|---|
[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
| Class | Stylesheet |
|---|---|
.input-lg | select.css |
.input-sm | select.css |
.select-content | select.css |
.select-item | select.css |
.select-label | select.css |
.select-trigger | select.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.
Apple
Pear
Plum
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
| Keys | Action |
|---|---|
| Enter | Selects |
| Escape | Closes |