Select

The Select component provides a dropdown menu for selecting a single value from a list of options. It’s designed with an accessible and customizable interface that supports groups, search, and keyboard navigation.

Usage

<script>
  import { 
    Select,
    SelectTrigger,
    SelectContent, 
    SelectItem 
  } from "$lib/components/ui/select";
</script>

<Select>
  <SelectTrigger>
    <span>Select an option</span>
  </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

A simple select component with a few options.

With Placeholder

A select component with a placeholder.

Disabled

A disabled select component.

With Grouped Items

A select component with grouped options.

With Default Value

A select component with a preset value.

With Form Label

A select component with a form label for accessibility.

Select your preferred browser.

With Disabled Items

A select component with disabled options.

API Reference

Select Components

ComponentDescription
SelectThe root component that manages selection state.
SelectTriggerThe button that toggles the select dropdown.
SelectContentThe floating content that contains the select items.
SelectItemThe selectable option.
SelectGroupA group of related select items.
SelectGroupHeadingA label for a group of select items.
SelectSeparatorA visual separator between items or groups.
SelectScrollUpButtonA button to scroll up through the select content.
SelectScrollDownButtonA button to scroll down through the select content.

Select Properties

PropertyTypeDefaultDescription
valuestringundefinedThe selected value.
openbooleanfalseWhether the select is open.
disabledbooleanfalseWhether the select is disabled.
requiredbooleanfalseWhether a value is required.
onSelectedChangefunctionundefinedCallback fired when selection changes.
onOpenChangefunctionundefinedCallback fired when the open state changes.

SelectItem Properties

PropertyTypeDefaultDescription
valuestringundefinedThe value of the item.
labelstringundefinedThe label to display if no children are provided.
disabledbooleanfalseWhether the item is disabled.

SelectTrigger Properties

PropertyTypeDefaultDescription
placeholderstringundefinedPlaceholder text to display.
classstringundefinedCustom CSS classes.

SelectContent Properties

PropertyTypeDefaultDescription
sideOffsetnumber4Distance from the trigger.
classstringundefinedCustom CSS classes.
portalPropsobjectundefinedProps for the portal component.

Implementation

The Select component is built on top of bits-ui primitives. It uses CSS transitions for smooth animations and includes styling for hover, focus, disabled states, and more:

// SelectTrigger styling
"border-input ring-offset-background data-[placeholder]:text-muted-foreground focus:ring-ring flex h-9 w-full items-center justify-between whitespace-nowrap rounded-md border bg-transparent px-3 py-2 text-sm shadow-sm focus:outline-none focus:ring-1 disabled:cursor-not-allowed disabled:opacity-50 [&>span]:line-clamp-1"

// SelectContent styling
"data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 bg-popover text-popover-foreground relative z-50 max-h-96 min-w-[8rem] overflow-hidden rounded-md border shadow-md"

// SelectItem styling
"data-[highlighted]:bg-accent data-[highlighted]:text-accent-foreground relative flex w-full cursor-default select-none items-center rounded-sm py-1.5 pl-2 pr-8 text-sm outline-none data-[disabled]:pointer-events-none data-[disabled]:opacity-50"

The component is built with accessibility in mind, ensuring keyboard navigation, proper ARIA attributes, and focus management for a great user experience.