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.jsonAdds 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
// 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 }[];const RANGES = [
{ value: "1h", label: "Last hour" },
{ value: "24h", label: "Last 24 hours" },
{ value: "30d", label: "Last 30 days" },
];Props
| Prop | Type | Default | Description |
|---|---|---|---|
Select: value | string | null | – | The selected value, when the parent holds it. Use it with onValueChange. |
Select: defaultValue | string | 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: items | readonly { 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: name | string | – | The field name when the select sits in a form. |
Select: disabled | boolean | Default false | The whole field is dimmed and cannot be opened. |
Select: sound | boolean | Default true | Play a soft sound when the list opens or closes and a click when an option is picked, if the visitor has sound on. |
SelectTrigger: placeholder | string | – | Shown in the muted text colour while nothing is selected. |
SelectTrigger: className | string | – | Sets the width. The field fills its container by default. |
SelectContent | popup props | – | The list. It is at least as wide as the field and scrolls when it is taller than the space around it. |
SelectItem: value | string | Required | What onValueChange reports. Unique within the select. |
SelectItem: disabled | boolean | Default false | Shown, but it cannot be picked and the arrow keys skip it. |
SelectGroup, SelectLabel, SelectSeparator | div 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.
| Keys | Action |
|---|---|
| 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 / End | Move the highlight to the first or last option. |
| A to Z | Move the highlight to the next option that starts with the typed letters. It works on the closed field too. |
| Enter / Space | Pick the highlighted option and close the list. |
| Esc | Close the list and keep the value. Focus returns to the field. |
Source
"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.0.0 · 2026-12-03First release.