Select

Web/Mobile

Displays a list of options for the user to pick from, triggered by a button.

Installation

pnpm dlx cubix-ui@latest add select

Usage

Import
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/cubix/select"
Example
<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
    └── SelectSeparator

Accessibility

  • 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 Label with id and htmlFor, or pass aria-label.
  • Link helper and error text with aria-describedby, and set aria-invalid while 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

PropTypeDefaultDescription
valuestring | null-Controlled selected value. null shows the placeholder. Use with onValueChange.
defaultValuestring | null-Initial selected value for uncontrolled usage.
onValueChange(value: string | null) => void-Called when the user picks an item.
openboolean-Controlled open state of the popup. Use with onOpenChange.
defaultOpenbooleanfalseInitial open state for uncontrolled usage.
onOpenChange(open: boolean) => void-Called when the popup opens or closes.
disabledbooleanfalseDisables the select and prevents interaction.
requiredbooleanfalseMarks the select as required in a form.
namestring-Name submitted with the parent form.
itemsRecord<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.
langstring-Language of the popup. Defaults to the closest lang on the page.

SelectTrigger

PropTypeDefaultDescription
size"sm" | "default" | "lg""default"Trigger height: sm 32px, default 40px (same as Cubix fields), lg 48px.
aria-labelstring-Accessible name when there is no visible label linked with id and htmlFor.
aria-invalidboolean-Shows the destructive border. Pair it with an error message.
classNamestring-Additional Tailwind classes merged with the component styles (last one wins).

SelectValue

PropTypeDefaultDescription
placeholderReact.ReactNode-Shown in the trigger while no item is selected.
classNamestring-Additional Tailwind classes merged with the component styles (last one wins).

SelectContent

PropTypeDefaultDescription
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.
sideOffsetnumber4Distance from the trigger in pixels.
alignOffsetnumber0Offset from the aligned edge in pixels.
alignItemWithTriggerbooleanfalseBase UI and Radix: overlap the selected item with the trigger. Ignored on React Aria, which always opens below.

SelectItem

PropTypeDefaultDescription
valuestring-Value of the item. Must be unique and not empty.
disabledbooleanfalsePrevents the item from being selected.
childrenReact.ReactNode-Label of the item, also shown in the trigger when selected.