Select
Web/MobileDisplays a list of options for the user to pick from, triggered by a button.
Installation
pnpm dlx cubix-ui@latest add select --base ariaUsage
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/cubix/select"
<Select>
<SelectTrigger aria-label="شهر" className="w-56">
<SelectValue placeholder="یک شهر انتخاب کنید" />
</SelectTrigger>
<SelectContent>
<SelectItem value="tehran">تهران</SelectItem>
<SelectItem value="mashhad">مشهد</SelectItem>
<SelectItem value="isfahan">اصفهان</SelectItem>
<SelectItem value="shiraz">شیراز</SelectItem>
<SelectItem value="tabriz">تبریز</SelectItem>
</SelectContent>
</Select>
Composition
Use the following composition to build a Select. Padding, the check indicator and the popup alignment use logical start and end, and the popup follows the closest dir on the page, so the same markup is correct in RTL and LTR.
Select
├── SelectTrigger
│ └── SelectValue
└── SelectContent
├── SelectItem
├── SelectGroup
│ ├── SelectLabel
│ └── SelectItem
└── SelectSeparatorAccessibility
- The trigger opens the list with Enter, Space or the arrow keys. Arrow keys move through items, typing jumps to a matching item, and Escape closes the list.
- Give the trigger a name: link a
LabelwithidandhtmlFor, or passaria-label. - Link helper and error text with
aria-describedby, and setaria-invalidwhile the value is invalid.
Examples
Sizes
Set size on the trigger: sm is 32px for toolbars, default is 40px like Cubix fields, and lg is 48px.
Label and description
Link a Label to the trigger and describe the field with aria-describedby.
استان محل سکونت خود را انتخاب کنید.
Groups
Group related items with SelectGroup and SelectLabel, and split groups with SelectSeparator.
Disabled
Set disabled on Select to disable the whole control, or on a SelectItem to disable one option.
Invalid
Set aria-invalid on the trigger for the destructive border and link the error message.
انتخاب شهر الزامی است.
Controlled
Pass value and onValueChange to own the selection. A null value shows the placeholder.
شهر انتخابشده: اصفهان
API Reference
Note: The same props work on Base UI, React Aria and Radix. The trigger shows the label of the selected item on every base.
Select
| Prop | Type | Default | Description |
|---|---|---|---|
| value | string | null | - | Controlled selected value. null shows the placeholder. Use with onValueChange. |
| defaultValue | string | null | - | Initial selected value for uncontrolled usage. |
| onValueChange | (value: string | null) => void | - | Called when the user picks an item. |
| open | boolean | - | Controlled open state of the popup. Use with onOpenChange. |
| defaultOpen | boolean | false | Initial open state for uncontrolled usage. |
| onOpenChange | (open: boolean) => void | - | Called when the popup opens or closes. |
| disabled | boolean | false | Disables the select and prevents interaction. |
| required | boolean | false | Marks the select as required in a form. |
| name | string | - | Name submitted with the parent form. |
| items | Record<string, ReactNode> | { value: string | null; label: ReactNode }[] | - | Optional value-to-label map. Labels are read from SelectItem by default; pass items when the items render inside your own component. |
| dir | "ltr" | "rtl" | - | Text direction of the trigger and popup. Defaults to the closest dir on the page. |
| lang | string | - | Language of the popup. Defaults to the closest lang on the page. |
SelectTrigger
| Prop | Type | Default | Description |
|---|---|---|---|
| size | "sm" | "default" | "lg" | "default" | Trigger height: sm 32px, default 40px (same as Cubix fields), lg 48px. |
| aria-label | string | - | Accessible name when there is no visible label linked with id and htmlFor. |
| aria-invalid | boolean | - | Shows the destructive border. Pair it with an error message. |
| className | string | - | Additional Tailwind classes merged with the component styles (last one wins). |
SelectValue
| Prop | Type | Default | Description |
|---|---|---|---|
| placeholder | React.ReactNode | - | Shown in the trigger while no item is selected. |
| className | string | - | Additional Tailwind classes merged with the component styles (last one wins). |
SelectContent
| Prop | Type | Default | Description |
|---|---|---|---|
| side | "top" | "right" | "bottom" | "left" | "bottom" | Side of the trigger the popup opens on. |
| align | "start" | "center" | "end" | "start" | Alignment against the trigger. start follows the reading direction. |
| sideOffset | number | 4 | Distance from the trigger in pixels. |
| alignOffset | number | 0 | Offset from the aligned edge in pixels. |
| alignItemWithTrigger | boolean | false | Base UI and Radix: overlap the selected item with the trigger. Ignored on React Aria, which always opens below. |
SelectItem
| Prop | Type | Default | Description |
|---|---|---|---|
| value | string | - | Value of the item. Must be unique and not empty. |
| disabled | boolean | false | Prevents the item from being selected. |
| children | React.ReactNode | - | Label of the item, also shown in the trigger when selected. |