Select
Picks one value from a list. The list opens over the trigger with the current choice in place.
Usage
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from 'nyctoui/select'
const items = { light: 'Light', dark: 'Dark' }
<Select items={items} defaultValue="light">
<SelectTrigger aria-label="Theme">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="light">Light</SelectItem>
<SelectItem value="dark">Dark</SelectItem>
</SelectContent>
</Select>Pass items, a map of values to labels, so SelectValue can show the chosen label before the list has ever been opened.
Good for
- Country or time zone on a profile form, with search.
- A role picker when inviting a teammate: Admin, Member, Viewer.
- Sort order in a list toolbar.
- Choosing a currency or a language in settings.
Anatomy
<Select>
<SelectTrigger>
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectGroup>
<SelectLabel />
<SelectItem />
</SelectGroup>
<SelectSeparator />
<SelectItem />
</SelectContent>
</Select>Examples
With icons
Groups and placeholder
With no defaultValue, the trigger shows the placeholder of SelectValue.
Sizes
Below the trigger
Set alignItemWithTrigger={false} on SelectContent to drop the list below the trigger like a menu.
Search
For a longer list, set searchable on SelectContent and put a SelectSearch, a SelectEmpty and the items in a SelectList inside it. Typing narrows the items while the arrow keys keep moving through them.
Controlled
Disabled
Best practices
- Give the select a visible label.
- Write a placeholder that hints at the choice, like “Choose a time zone”, not “Select…”.
- Group options with
SelectGroupandSelectLabelwhen the list passes eight or so. - Turn on
searchablefor long lists such as countries.
- Use a select for actions. That’s a dropdown.
- Use a select for two or three options. A segmented control shows them all at once.
- Use a select for yes or no. That’s a switch or a checkbox.
API reference
Select
The ui-sdk Select root, with items as a label map. Renders no element.
| Prop | Type | Default |
|---|---|---|
itemsLabels for the values, so SelectValue shows one before the list first opens. | Record<string, string> | — |
valueThe selected value, when controlled. | Value | null | — |
defaultValueThe value selected at first, when uncontrolled. | Value | null | — |
onValueChangeCalled when the user picks a value. | (value, details) => void | — |
open / defaultOpen / onOpenChangeControl whether the list is open. | boolean · (open, details) => void | — |
disabledDisables the trigger. | boolean | false |
nameSubmits the value with a form under this name. | string | — |
SelectTrigger
The closed control. The chevron is built in.
| Prop | Type | Default |
|---|---|---|
sizeControl height; matches Button and Input. | 'sm' | 'md' | 'lg' | 'md' |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
SelectValue
The selected item’s label.
| Prop | Type | Default |
|---|---|---|
placeholderShown while nothing is selected. | string | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
SelectContent
The popup with the list of options, in the top layer.
| Prop | Type | Default |
|---|---|---|
searchableLeaves the children as they are, for a SelectSearch above a SelectList. | boolean | false |
alignItemWithTriggerOpen over the trigger with the selected item in place. | boolean | true |
sideSide of the trigger, when not aligned with it. | 'top' | 'bottom' | 'left' | 'right' | 'start' | 'end' | 'bottom' |
alignAlignment along that side. | 'start' | 'center' | 'end' | 'start' |
sideOffsetGap between trigger and popup, in pixels. | number | 6 |
collisionPaddingSpace kept from the viewport edges. | number | 8 |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
SelectItem
One option, with a check when selected.
| Prop | Type | Default |
|---|---|---|
valueThe value this option selects. | Value | — |
iconIcon shown before the label. | ReactNode | — |
disabledKeeps the option from being picked. | boolean | false |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
SelectGroup
Groups related options.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
SelectLabel
Heading for a SelectGroup.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
SelectSeparator
Hairline between options.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
SelectSearch
An <input> above the list of a searchable SelectContent. Give it an aria-label.
| Prop | Type | Default |
|---|---|---|
placeholderHint shown while empty. | string | 'Search…' |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
SelectList
The scrolling list of items under a SelectSearch.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
SelectEmpty
Shown when the search matches nothing.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |