Skip to content
Thrum

Select

A field that opens a list and holds one of its options: a time range, a region, a unit. Use it from about five options up. Two to five options that fit in a row read faster in a segmented control.

Install

npx shadcn@latest add https://thrumui.dev/r/select.json

Adds components/ui/select.tsx and installs @base-ui/react@1.9.0. Nothing else in your project changes.

import { Select } from "@/components/ui/select";

Automatically installs icons check and chevron-down from the icon set.

Data shape

Type
// Passed to Select as `items`, so the field can show a label before the list has ever opened.
type SelectItems = readonly { value: string; label: string }[];
Sample
const RANGES = [
  { value: "1h", label: "Last hour" },
  { value: "24h", label: "Last 24 hours" },
  { value: "30d", label: "Last 30 days" },
];

Props

PropTypeDefaultDescription
Select: valuestring | null–The selected value, when the parent holds it. Use it with onValueChange.
Select: defaultValuestring | null–The value selected at first, when the select holds it itself.
Select: onValueChange(value: string | null) => void–Called with the new value when an option is picked.
Select: itemsreadonly { value: string; label: string }[]–Every option's value and label. With it the field shows the label of the selected value at once, before the list is first opened.
Select: namestring–The field name when the select sits in a form.
Select: disabledbooleanDefault falseThe whole field is dimmed and cannot be opened.
Select: soundbooleanDefault truePlay a soft sound when the list opens or closes and a click when an option is picked, if the visitor has sound on.
SelectTrigger: placeholderstring–Shown in the muted text colour while nothing is selected.
SelectTrigger: classNamestring–Sets the width. The field fills its container by default.
SelectContentpopup props–The list. It is at least as wide as the field and scrolls when it is taller than the space around it.
SelectItem: valuestringRequiredWhat onValueChange reports. Unique within the select.
SelectItem: disabledbooleanDefault falseShown, but it cannot be picked and the arrow keys skip it.
SelectGroup, SelectLabel, SelectSeparatordiv props–A group of options, its heading, and a line between groups.

Motion and sound

Motion

  • The list fades in and grows from 96% to full size over 150 ms, from the edge that touches the field.
  • Closing plays the same change backwards, and a second press during it reverses it.
  • Reduced motion: the list appears and leaves at once.

Sound

  • A soft rising tone when the list opens and a falling one when it closes without a pick.
  • A click when an option is picked. That press makes no second sound for the closing list.

Sound is off until the visitor turns it on. Pass sound={false} to keep this component silent. How sound works

Accessibility

  • It follows the WAI-ARIA select-only combobox pattern: the field is a combobox that opens a listbox of options.
  • The field needs a name: a label element, or an aria-label on SelectTrigger as in the example.
  • The selected option carries a tick as well as aria-selected, so selection does not rest on the highlight colour.
  • A hidden native input carries the value in a form, and the browser's autofill can set it.
  • A long label is cut with an ellipsis in the field and wraps in the list. Nothing overflows at 160 pixels wide.
  • Under a glass material the list is translucent. Its tint keeps the text at 4.5 to 1 over any page.
  • Strict content security policy: the headless base adds a style element to hide the native scrollbar in the list. Wrap the app in <CSPProvider disableStyleElements> from @base-ui/react/csp-provider and add the .base-ui-disable-scrollbar rule (scrollbar-width: none, and display: none on its ::-webkit-scrollbar) to the global stylesheet.
KeysAction
Enter / Space / ↓ / ↑On the field: open the list, with the selected option highlighted.
↓ / ↑Move the highlight to the next or previous option. Disabled options are skipped.
Home / EndMove the highlight to the first or last option.
A to ZMove the highlight to the next option that starts with the typed letters. It works on the closed field too.
Enter / SpacePick the highlighted option and close the list.
EscClose the list and keep the value. Focus returns to the field.

Source

components/ui/select.tsx
"use client";

import { Select as SelectPrimitive } from "@base-ui/react/select";

import { cn } from "@/lib/utils";
import { play } from "@/lib/thrum-sound";
import { CheckIcon } from "@/components/icons/check";
import { ChevronDownIcon } from "@/components/icons/chevron-down";

export type SelectProps = SelectPrimitive.Root.Props<string> & {
  /** Play a soft sound when the list opens or closes and a click when a new option is picked, if the visitor has sound on. */
  sound?: boolean;
};

/**
 * A field that opens a list and holds one of its options. Use it for five or more choices; for fewer, radio buttons or
 * a segmented control show them all at once. Values are strings. Name it with a `<label>` or an `aria-label` on the trigger.
 */
export function Select({ sound = true, onOpenChange, onValueChange, ...props }: SelectProps) {
  return (
    <SelectPrimitive.Root
      onOpenChange={(open, details) => {
        // Picking an option also closes the list. That press gets the pick's click, not a second sound.
        if (sound && details.reason !== "item-press") play(open ? "open" : "close");
        onOpenChange?.(open, details);
      }}
      onValueChange={(value, details) => {
        if (sound) play("select");
        onValueChange?.(value, details);
      }}
      {...props}
    />
  );
}

export type SelectTriggerProps = SelectPrimitive.Trigger.Props & {
  /** Shown while nothing is selected. */
  placeholder?: string;
};

/** The field. It shows the selected option's text, or the placeholder. */
export function SelectTrigger({ placeholder, className, children, ...props }: SelectTriggerProps) {
  return (
    <SelectPrimitive.Trigger
      data-slot="select"
      data-surface="control"
      className={cn(
        "flex h-8 w-full min-w-0 cursor-pointer items-center justify-between gap-2 rounded-md border border-input bg-background px-2.5 text-left text-base text-foreground select-none md:text-sm",
        "transition-[border-color] duration-150 ease-out motion-reduce:transition-none",
        "focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-ring",
        "data-disabled:cursor-not-allowed data-disabled:opacity-50 aria-invalid:border-destructive",
        className,
      )}
      {...props}
    >
      {children ?? <SelectPrimitive.Value placeholder={placeholder} className="min-w-0 truncate data-placeholder:text-muted-foreground" />}
      <SelectPrimitive.Icon className="flex-none text-muted-foreground">
        <ChevronDownIcon size={16} />
      </SelectPrimitive.Icon>
    </SelectPrimitive.Trigger>
  );
}

/**
 * The list. It opens under the field, as wide as the field, and scrolls when it is taller than the space it has.
 * It is a floating surface, so it turns to glass when the page's material asks for it.
 */
export function SelectContent({ className, children, ...props }: SelectPrimitive.Popup.Props) {
  return (
    <SelectPrimitive.Portal>
      <SelectPrimitive.Positioner sideOffset={4} alignItemWithTrigger={false} className="z-50 outline-none">
        <SelectPrimitive.Popup
          data-slot="select-content"
          data-surface="floating"
          className={cn(
            "max-h-(--available-height) min-w-(--anchor-width) origin-(--transform-origin) overflow-y-auto overscroll-contain rounded-lg border bg-popover p-1 text-popover-foreground shadow-md outline-none",
            "transition-[opacity,scale] duration-150 ease-out data-ending-style:scale-[0.96] data-ending-style:opacity-0 data-starting-style:scale-[0.96] data-starting-style:opacity-0 motion-reduce:transition-none",
            className,
          )}
          {...props}
        >
          {children}
        </SelectPrimitive.Popup>
      </SelectPrimitive.Positioner>
    </SelectPrimitive.Portal>
  );
}

/** One option. Its text is what the field shows once it is picked. */
export function SelectItem({ className, children, ...props }: SelectPrimitive.Item.Props) {
  return (
    <SelectPrimitive.Item
      data-slot="select-item"
      className={cn(
        "grid min-h-8 cursor-default grid-cols-[1fr_1rem] items-center gap-2 rounded-sm py-1.5 pr-2 pl-2.5 text-sm outline-none select-none",
        "data-highlighted:bg-accent data-highlighted:text-accent-foreground data-disabled:opacity-50",
        className,
      )}
      {...props}
    >
      <SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText>
      {/* The tick marks the selected option, so selection never rests on the highlight colour. */}
      <SelectPrimitive.ItemIndicator>
        <CheckIcon size={16} />
      </SelectPrimitive.ItemIndicator>
    </SelectPrimitive.Item>
  );
}

/** Options that belong together, under a `SelectLabel`. */
export function SelectGroup(props: SelectPrimitive.Group.Props) {
  return <SelectPrimitive.Group data-slot="select-group" {...props} />;
}

export function SelectLabel({ className, ...props }: SelectPrimitive.GroupLabel.Props) {
  return <SelectPrimitive.GroupLabel data-slot="select-label" className={cn("px-2.5 py-1.5 text-xs font-medium text-muted-foreground", className)} {...props} />;
}

export function SelectSeparator({ className, ...props }: SelectPrimitive.Separator.Props) {
  return <SelectPrimitive.Separator data-slot="select-separator" className={cn("-mx-1 my-1 h-px bg-border", className)} {...props} />;
}

Files and dependencies

Adds
components/ui/select.tsx
Packages
@base-ui/react@1.9.0
Brings along
Sound engine
Icons
checkchevron-down

Changelog

  1. 1.0.0 · 2026-12-03First release.

Loading index…

Keyboard shortcuts

/Ctrl KSearch components, docs and actions
?Show this list
AAppearance studio
SSound on or off
MFull or reduced motion
TTheme: auto, light, dark
GSurface: solid or frosted
BBackdrop photograph (under glass)
CCorner radius: square to round
RReplay the demo, on a component page
DDraw new data, on a component page
←→HomeEndMove the cursor in a focused chart
EscClose a dialog, or leave a chart cursor