Select
A dropdown select control with multiple size variants.
Installation
$
npx shadcn@latest add https://ui.mattdowney.com/r/select.jsonUsage
import { Select, SelectTrigger, SelectValue, SelectContent, SelectItem } from "@/components/ui/select"Examples
Default
One choice from a short, fixed list. The placeholder belongs to SelectValue, not the trigger, and Field ties the label to the trigger, so there is no id to write.
Trailing value
trailing puts a short value, such as a count, on a row's right edge. The check moves to the left edge so the right side is free. The trigger shows only the label.
Trigger sized to its value
A w-auto trigger is as wide as its value, and the menu grows to its longest row, so longer options stay on one line. Side text such as a tag goes in trailing, never in the label.
API Reference
| Prop | Type | Default | Description |
|---|---|---|---|
| sizeSelectTrigger | "xs" | "sm" | "default" | "default" | Trigger height and text size. |
| positionSelectContent | "item-aligned" | "popper" | "popper" | Dropdown positioning strategy: item-aligned overlays the trigger like a native select, popper anchors against it like a popover. |
| alignSelectContent | "start" | "center" | "end" | "center" | Alignment against the trigger along the chosen side; only applies when position is popper. |
| value*SelectItem | string | None | The value submitted when the item is selected. |
| defaultValueSelect | string | None | The initially selected value for uncontrolled usage. |
| onValueChangeSelect | (value: string) => void | None | Called with the new value when the selection changes. Pair with value for controlled usage. |
| disabledSelect | boolean | false | Disables the entire select. |
| disabledSelectItem | boolean | false | Disables a single option. |
| trailingSelectItem | React.ReactNode | None | A short value at the row's right edge, such as a count. Moves the check to the left edge. Not part of the label, so the trigger and typeahead ignore it. |
| sizeSelectTrigger"xs" | "sm" | "default" = "default"Trigger height and text size. |
positionSelectContent"item-aligned" | "popper" = "popper"Dropdown positioning strategy: item-aligned overlays the trigger like a native select, popper anchors against it like a popover. |
alignSelectContent"start" | "center" | "end" = "center"Alignment against the trigger along the chosen side; only applies when position is popper. |
| value*SelectItemstringThe value submitted when the item is selected. |
| defaultValueSelectstringThe initially selected value for uncontrolled usage. |
onValueChangeSelect(value: string) => voidCalled with the new value when the selection changes. Pair with value for controlled usage. |
| disabledSelectboolean = falseDisables the entire select. |
| disabledSelectItemboolean = falseDisables a single option. |
| trailingSelectItemReact.ReactNodeA short value at the row's right edge, such as a count. Moves the check to the left edge. Not part of the label, so the trigger and typeahead ignore it. |
"use client"
import * as React from "react"
import { Select as SelectPrimitive } from "@base-ui/react/select"
import { IconCheck as CheckIcon, IconChevronDown as ChevronDownIcon, IconChevronUp as ChevronUpIcon } from "@tabler/icons-react"
import { cn } from "@/registry/lib/utils"
/**
* Tracks whether a label has a group to name. Base UI's `Select.GroupLabel`
* throws when it has no `Select.Group` ancestor, and because the popup only
* mounts on open, neither SSR nor a typecheck catches a bare `<SelectLabel>`:
* it crashes in the user's hands. Inside a group the label stays the real
* `GroupLabel` that carries the `aria-labelledby` wiring; standalone it degrades
* to a plain div. Mirrors the same guard in dropdown-menu.
*/
const SelectGroupContext = React.createContext(false)
// @use-when a form field picking one value from a list too long to show, opened
// from a trigger. Not RadioGroup, which shows every option; not Combobox, which
// filters by typing; not DropdownMenu, which runs actions.
function Select({
...props
}: React.ComponentProps<typeof SelectPrimitive.Root>) {
return <SelectPrimitive.Root data-slot="select" {...props} />
}
function SelectGroup({
...props
}: React.ComponentProps<typeof SelectPrimitive.Group>) {
return (
<SelectGroupContext.Provider value={true}>
<SelectPrimitive.Group data-slot="select-group" {...props} />
</SelectGroupContext.Provider>
)
}
function SelectValue({
...props
}: React.ComponentProps<typeof SelectPrimitive.Value>) {
return <SelectPrimitive.Value data-slot="select-value" {...props} />
}
function SelectTrigger({
className,
size = "default" as "xs" | "sm" | "default",
children,
...props
}: React.ComponentProps<typeof SelectPrimitive.Trigger> & {
size?: "xs" | "sm" | "default"
}) {
return (
<SelectPrimitive.Trigger
data-slot="select-trigger"
data-size={size}
className={cn(
// Touch policy: xs/sm grow to a 32px floor under a coarse pointer.
// Button's short sizes do not grow at all (a dense row is why they
// were picked), but a trigger opens a list a thumb must then hit, so
// it keeps a floor. default grows to the full 44px target.
"border-border hover:border-border-strong disabled:hover:border-border data-[placeholder]:text-muted-foreground [&_svg:not([class*='text-'])]:text-muted-foreground focus-visible:border-border-focus-strong aria-invalid:border-destructive flex w-fit items-center justify-between gap-2 data-[size=default]:rounded-action data-[size=sm]:rounded-action-sm data-[size=xs]:rounded-action-xs border bg-input-bg bg-clip-padding backdrop-blur-sm px-3 text-field md:text-sm font-medium normal-case tracking-normal whitespace-nowrap shadow-xs transition-[color,box-shadow,border-color] duration-small ease-small outline-none disabled:cursor-not-allowed disabled:opacity-70 disabled:bg-muted disabled:shadow-none data-[size=default]:h-8 data-[size=sm]:h-7 data-[size=xs]:h-6 data-[size=xs]:px-2 data-[size=xs]:text-xs data-[size=xs]:gap-1 pointer-coarse:data-[size=xs]:min-h-8 pointer-coarse:data-[size=sm]:min-h-8 pointer-coarse:data-[size=default]:min-h-11 *:data-[slot=select-value]:line-clamp-1 *:data-[slot=select-value]:flex *:data-[slot=select-value]:items-center *:data-[slot=select-value]:gap-2 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-3",
className
)}
{...props}
>
{children}
{/* 12px beside 14px text on purpose: the chevron only marks "opens a
list" and carries no meaning of its own, so it follows the menu-row
convention (12px) and not Button's 16px, which is for an icon that
names an action. At full opacity it reads muted-foreground, which
clears 3:1 on the field fill (50% opacity did not). */}
<SelectPrimitive.Icon
render={<ChevronDownIcon className="size-3" />}
/>
</SelectPrimitive.Trigger>
)
}
function SelectContent({
className,
children,
position = "popper",
align = "center",
side,
sideOffset = 4,
alignOffset,
...props
}: React.ComponentProps<typeof SelectPrimitive.Popup> &
Pick<
React.ComponentProps<typeof SelectPrimitive.Positioner>,
"align" | "side" | "sideOffset" | "alignOffset"
> & {
position?: "item-aligned" | "popper"
}) {
return (
<SelectPrimitive.Portal>
<SelectPrimitive.Positioner
// positionMethod="fixed": Base UI's Positioner defaults to
// "absolute", which positions the portaled popup in DOCUMENT
// coordinates. On open, Base UI moves focus into the popup before
// Floating UI has computed the transform, so the browser scrolls the
// focused element into view while it is still sitting at the document
// origin, and the whole page jumps to the top. With "fixed" the
// pre-position origin is the viewport, so there is nothing to scroll
// to. It also keeps the popup correct inside a scrolling or
// transformed ancestor.
positionMethod="fixed"
side={side}
align={align}
sideOffset={sideOffset}
alignOffset={alignOffset}
alignItemWithTrigger={position === "item-aligned"}
className="z-50"
>
{/* relative stays: Base UI's scroll arrows position against this
element. The layer's real z-index lives on the Positioner above,
so no z utility belongs here.
Width: as wide as the longest row, never narrower than the trigger,
never wider than the screen. It used to be pinned to the trigger
(w-(--anchor-width)), so a short value like "Share" squeezed
"Own workflow" onto two lines and every app patched it per call. */}
<SelectPrimitive.Popup
data-slot="select-content"
className={cn(
"bg-popover text-popover-foreground transition-[opacity,scale,translate] motion-reduce:transition-[opacity] ease-large duration-large data-[ending-style]:duration-small data-[ending-style]:ease-small data-[starting-style]:opacity-0 data-[ending-style]:opacity-0 data-[starting-style]:scale-97 data-[ending-style]:scale-97 data-[side=bottom]:data-[starting-style]:-translate-y-2 data-[side=bottom]:data-[ending-style]:-translate-y-2 data-[side=top]:data-[starting-style]:translate-y-2 data-[side=top]:data-[ending-style]:translate-y-2 data-[side=right]:data-[starting-style]:-translate-x-2 data-[side=right]:data-[ending-style]:-translate-x-2 data-[side=left]:data-[starting-style]:translate-x-2 data-[side=left]:data-[ending-style]:translate-x-2 relative max-h-(--available-height) w-max min-w-(--anchor-width) max-w-(--available-width) origin-(--transform-origin) overflow-x-hidden overflow-y-auto rounded-lg shadow-ring-md",
className
)}
{...props}
>
<SelectScrollUpButton />
<SelectPrimitive.List className="p-1 scroll-my-1">
{children}
</SelectPrimitive.List>
<SelectScrollDownButton />
</SelectPrimitive.Popup>
</SelectPrimitive.Positioner>
</SelectPrimitive.Portal>
)
}
function SelectLabel({
className,
...props
}: React.ComponentProps<typeof SelectPrimitive.GroupLabel>) {
// See SelectGroupContext: GroupLabel throws outside a group.
const inGroup = React.useContext(SelectGroupContext)
// The generic is pinned on purpose: a bare React.ElementType collapses the
// merged props to `never` under a stricter tsconfig than this repo's, which
// then rejects every prop spread below. Pinning it costs nothing here and
// keeps the component compiling in a consumer with strict settings.
const Component = (inGroup ? SelectPrimitive.GroupLabel : "div") as React.ElementType<
React.ComponentProps<typeof SelectPrimitive.GroupLabel>
>
return (
<Component
data-slot="select-label"
className={cn(
// A group title is a small caps label (Command and NavLabel share it); a
// bare label outside a group keeps the quiet title style.
inGroup ? "px-2 pt-2 pb-1 text-2xs font-semibold tracking-wide uppercase text-muted-foreground" : "text-muted-foreground px-2 py-1.5 text-xs font-medium",
className
)}
{...props}
/>
)
}
// `trailing` puts a short value on the row's right edge, such as a count. The
// check then moves to the left edge to free that side, as macOS menus do. It
// renders outside `ItemText`, so the trigger and typeahead read only the label.
// Muted, one size below the label, brightened with the row like CommandItemMeta.
function SelectItem({
className,
children,
trailing,
...props
}: React.ComponentProps<typeof SelectPrimitive.Item> & {
trailing?: React.ReactNode
}) {
const hasTrailing = trailing != null && trailing !== false
return (
<SelectPrimitive.Item
data-slot="select-item"
className={cn(
"group/select-item data-[highlighted]:bg-secondary data-[highlighted]:text-foreground [&_svg:not([class*='text-'])]:text-muted-foreground relative flex w-full cursor-default items-center gap-2 rounded-md py-1.5 pe-8 ps-2 text-sm outline-hidden select-none data-[disabled]:pointer-events-none data-[disabled]:opacity-50 pointer-coarse:py-3 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-3 *:[span]:last:flex *:[span]:last:items-center *:[span]:last:gap-2",
hasTrailing && "pe-2 ps-8",
className
)}
{...props}
>
<span
data-slot="select-item-indicator"
className={cn(
"absolute flex size-3.5 items-center justify-center",
hasTrailing ? "start-2" : "end-2"
)}
>
<SelectPrimitive.ItemIndicator>
<CheckIcon className="size-3" />
</SelectPrimitive.ItemIndicator>
</span>
<SelectPrimitive.ItemText
className={cn(hasTrailing && "flex min-w-0 flex-1 items-center gap-2")}
>
{children}
</SelectPrimitive.ItemText>
{hasTrailing && (
<span
data-slot="select-item-trailing"
className="text-muted-foreground group-data-[highlighted]/select-item:text-foreground ms-auto shrink-0 text-xs tabular-nums"
>
{trailing}
</span>
)}
</SelectPrimitive.Item>
)
}
function SelectSeparator({
className,
...props
}: React.ComponentProps<typeof SelectPrimitive.Separator>) {
return (
<SelectPrimitive.Separator
data-slot="select-separator"
className={cn("bg-border pointer-events-none -mx-1 my-1 h-px", className)}
{...props}
/>
)
}
function SelectScrollUpButton({
className,
...props
}: React.ComponentProps<typeof SelectPrimitive.ScrollUpArrow>) {
return (
<SelectPrimitive.ScrollUpArrow
data-slot="select-scroll-up-button"
className={cn(
"flex cursor-default items-center justify-center py-1",
className
)}
{...props}
>
<ChevronUpIcon className="size-3" />
</SelectPrimitive.ScrollUpArrow>
)
}
function SelectScrollDownButton({
className,
...props
}: React.ComponentProps<typeof SelectPrimitive.ScrollDownArrow>) {
return (
<SelectPrimitive.ScrollDownArrow
data-slot="select-scroll-down-button"
className={cn(
"flex cursor-default items-center justify-center py-1",
className
)}
{...props}
>
<ChevronDownIcon className="size-3" />
</SelectPrimitive.ScrollDownArrow>
)
}
export {
Select,
SelectContent,
SelectGroup,
SelectItem,
SelectLabel,
SelectScrollDownButton,
SelectScrollUpButton,
SelectSeparator,
SelectTrigger,
SelectValue,
}