nqui
Components

Select

A dropdown select component built on Radix UI primitives

Select

The Select component provides a dropdown menu for selecting a single option from a list. Built on Radix UI primitives for full accessibility and keyboard navigation.

Installation

npm install @nqlib/nqui

Import

import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from '@nqlib/nqui';

Basic Usage

import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from '@nqlib/nqui';

export default function Example() {
  return (
    <Select>
      <SelectTrigger className="w-[180px]">
        <SelectValue placeholder="Select an option" />
      </SelectTrigger>
      <SelectContent>
        <SelectItem value="option1">Option 1</SelectItem>
        <SelectItem value="option2">Option 2</SelectItem>
        <SelectItem value="option3">Option 3</SelectItem>
      </SelectContent>
    </Select>
  );
}

Examples

Basic Select

With Label

Controlled Select

Disabled State

Sizes

Controlled Select

Use controlled state for programmatic control:

const [value, setValue] = useState('');

<Select value={value} onValueChange={setValue}>
  <SelectTrigger>
    <SelectValue placeholder="Select an option" />
  </SelectTrigger>
  <SelectContent>
    <SelectItem value="option1">Option 1</SelectItem>
    <SelectItem value="option2">Option 2</SelectItem>
  </SelectContent>
</Select>

With Label

Always pair selects with labels for accessibility:

import { Label } from '@nqlib/nqui';

<Label htmlFor="fruit-select">Favorite Fruit</Label>
<Select>
  <SelectTrigger id="fruit-select">
    <SelectValue placeholder="Select a fruit" />
  </SelectTrigger>
  <SelectContent>
    <SelectItem value="apple">Apple</SelectItem>
    <SelectItem value="banana">Banana</SelectItem>
  </SelectContent>
</Select>

Sizes

The SelectTrigger supports size variants:

<SelectTrigger size="sm">...</SelectTrigger>
<SelectTrigger size="default">...</SelectTrigger>

Disabled State

<Select disabled>
  <SelectTrigger>
    <SelectValue placeholder="Disabled select" />
  </SelectTrigger>
  <SelectContent>
    <SelectItem value="option1">Option 1</SelectItem>
  </SelectContent>
</Select>

Component Structure

The Select component consists of several sub-components:

  • Select - Root component that manages state
  • SelectTrigger - The button that opens the dropdown
  • SelectValue - Displays the selected value or placeholder
  • SelectContent - The dropdown menu container
  • SelectItem - Individual option items
  • SelectLabel - Optional group labels
  • SelectSeparator - Visual separator between groups
  • SelectGroup - Groups related items together

Props

Select

PropTypeDefaultDescription
valuestring-Controlled value
defaultValuestring-Uncontrolled default value
onValueChange(value: string) => void-Callback when value changes
disabledbooleanfalseWhether the select is disabled
namestring-Form field name
requiredbooleanfalseWhether the field is required

SelectTrigger

PropTypeDefaultDescription
size"sm" | "default""default"Size variant
classNamestring-Additional CSS classes
All standard button props--Accepts all HTML button attributes

SelectValue

PropTypeDefaultDescription
placeholderstring-Placeholder when nothing selected
classNamestring-Additional CSS classes

SelectContent

PropTypeDefaultDescription
position"popper" | "item-aligned""item-aligned"Positioning mode
align"start" | "center" | "end""center"Content alignment
sideOffsetnumber-Offset from trigger
classNamestring-Additional CSS classes
All standard div props--Accepts HTML div attributes

SelectItem

PropTypeDefaultDescription
valuestring-Item value (required)
disabledbooleanfalseWhether the item is disabled
classNamestring-Additional CSS classes

Styling

Selects use the following design tokens:

  • Border: border-input
  • Background: bg-input/20 (dark: bg-input/30)
  • Text: text-foreground
  • Placeholder: text-muted-foreground
  • Focus ring: ring-ring with ring-offset-background
  • Disabled: opacity-50 with cursor-not-allowed

Accessibility

  • Full keyboard navigation support
  • ARIA attributes for screen readers
  • Focus management
  • Proper label association
  • Escape key to close
  • Arrow keys to navigate options

Best Practices

  1. Always use labels: Pair every select with a label for accessibility
  2. Provide placeholders: Use helpful placeholder text
  3. Group related items: Use SelectGroup and SelectLabel for organization
  4. Handle disabled states: Disable options that aren't available
  5. Consider search: For long lists, consider using Combobox instead

Next Steps