Select
A select on Base UI Select.
Pass items to Select so the trigger shows an option's label, not its value.
With a label
import { Label } from "@/components/ui/label"
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"
const statuses = [
{ value: "backlog", label: "Backlog" },
{ value: "in-progress", label: "In progress" },
{ value: "in-review", label: "In review" },
{ value: "done", label: "Done" },
]
export default function SelectWithALabel() {
return (
<div className="grid w-48 gap-2">
<Label htmlFor="select-status">Status</Label>
<Select items={statuses}>
<SelectTrigger id="select-status" className="w-full">
<SelectValue placeholder="Choose a status" />
</SelectTrigger>
<SelectContent>
{statuses.map((status) => (
<SelectItem key={status.value} value={status.value}>
{status.label}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
)
}
Sizes
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"
const pageSizes = [
{ value: "10", label: "10 rows" },
{ value: "25", label: "25 rows" },
{ value: "50", label: "50 rows" },
]
export default function SelectSizes() {
return (
<div className="flex flex-wrap items-center gap-2">
<Select items={pageSizes} defaultValue="25">
<SelectTrigger size="sm" aria-label="Rows per page, small">
<SelectValue />
</SelectTrigger>
<SelectContent>
{pageSizes.map((size) => (
<SelectItem key={size.value} value={size.value}>
{size.label}
</SelectItem>
))}
</SelectContent>
</Select>
<Select items={pageSizes} defaultValue="25">
<SelectTrigger aria-label="Rows per page, default">
<SelectValue />
</SelectTrigger>
<SelectContent>
{pageSizes.map((size) => (
<SelectItem key={size.value} value={size.value}>
{size.label}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
)
}
With groups
import { Label } from "@/components/ui/label"
import {
Select,
SelectContent,
SelectGroup,
SelectItem,
SelectLabel,
SelectSeparator,
SelectTrigger,
SelectValue,
} from "@/components/ui/select"
const releases = [
{ value: "4.3", label: "4.3, in planning" },
{ value: "4.2", label: "4.2, in testing" },
{ value: "4.1", label: "4.1" },
{ value: "4.0", label: "4.0" },
]
export default function SelectWithGroups() {
return (
<div className="grid w-48 gap-2">
<Label htmlFor="select-release">Release</Label>
<Select items={releases} defaultValue="4.2">
<SelectTrigger id="select-release" className="w-full">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectGroup>
<SelectLabel>Upcoming</SelectLabel>
<SelectItem value="4.3">4.3, in planning</SelectItem>
<SelectItem value="4.2">4.2, in testing</SelectItem>
</SelectGroup>
<SelectSeparator />
<SelectGroup>
<SelectLabel>Shipped</SelectLabel>
<SelectItem value="4.1">4.1</SelectItem>
<SelectItem value="4.0">4.0</SelectItem>
</SelectGroup>
</SelectContent>
</Select>
</div>
)
}
Below the trigger
import { Label } from "@/components/ui/label"
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"
const priorities = [
{ value: "low", label: "Low" },
{ value: "normal", label: "Normal" },
{ value: "high", label: "High" },
{ value: "urgent", label: "Urgent" },
]
export default function SelectBelowTheTrigger() {
return (
<div className="grid w-48 gap-2">
<Label htmlFor="select-priority">Priority</Label>
<Select items={priorities} defaultValue="normal">
<SelectTrigger id="select-priority" className="w-full">
<SelectValue />
</SelectTrigger>
<SelectContent alignItemWithTrigger={false} align="start">
{priorities.map((priority) => (
<SelectItem key={priority.value} value={priority.value}>
{priority.label}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
)
}
Disabled
import { Label } from "@/components/ui/label"
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"
const plans = [
{ value: "starter", label: "Starter" },
{ value: "team", label: "Team" },
{ value: "enterprise", label: "Enterprise" },
]
export default function SelectDisabled() {
return (
<div className="group grid w-48 gap-2" data-disabled="true">
<Label htmlFor="select-plan-locked">Plan</Label>
<Select items={plans} defaultValue="team" disabled>
<SelectTrigger id="select-plan-locked" className="w-full">
<SelectValue />
</SelectTrigger>
<SelectContent>
{plans.map((plan) => (
<SelectItem key={plan.value} value={plan.value}>
{plan.label}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
)
}
With a disabled option
import { Label } from "@/components/ui/label"
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"
const plans = [
{ value: "starter", label: "Starter" },
{ value: "team", label: "Team" },
{ value: "enterprise", label: "Enterprise", disabled: true },
]
export default function SelectWithADisabledOption() {
return (
<div className="grid w-48 gap-2">
<Label htmlFor="select-plan">Plan</Label>
<Select items={plans} defaultValue="team">
<SelectTrigger id="select-plan" className="w-full">
<SelectValue />
</SelectTrigger>
<SelectContent>
{plans.map((plan) => (
<SelectItem key={plan.value} value={plan.value} disabled={plan.disabled}>
{plan.label}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
)
}
Invalid
Choose the team that owns this project.
import { Field, FieldError, FieldLabel } from "@/components/ui/field"
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"
const teams = [
{ value: "design", label: "Design" },
{ value: "engineering", label: "Engineering" },
{ value: "finance", label: "Finance" },
]
export default function SelectInvalid() {
return (
<Field data-invalid="true" className="w-56">
<FieldLabel htmlFor="select-owner">Owner</FieldLabel>
<Select items={teams} required>
<SelectTrigger id="select-owner" aria-invalid aria-describedby="select-owner-error">
<SelectValue placeholder="Choose a team" />
</SelectTrigger>
<SelectContent>
{teams.map((team) => (
<SelectItem key={team.value} value={team.value}>
{team.label}
</SelectItem>
))}
</SelectContent>
</Select>
<FieldError id="select-owner-error">Choose the team that owns this project.</FieldError>
</Field>
)
}
Installation
npx shadcn@latest add @khaeli/selectbunx --bun shadcn@latest add @khaeli/selectInstall @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.
Installs with
- Registry items
- npm packages
- @base-ui/react ^1.7.0
- @keyline-icons/react ^2.2.0
Keyboard
EnterSpaceArrowDownArrowUp- On the trigger, open the list. Focus lands on the chosen option, or on the first one.
ArrowDownArrowUp- Move through the options. A disabled option can be reached but not chosen.
HomeEnd- Move to the first and to the last option.
A to Z- In the list, moves to the option that starts with the letters typed. On the closed trigger, chooses it without opening the list.
EnterSpace- On an option, choose it and close the list. Focus returns to the trigger.
Escape- Closes the list and keeps the value. Focus returns to the trigger.
Tab- Closes the list and moves focus on.
Accessible name
- The trigger is a button with
role="combobox". GiveSelectTriggeranidand point a @khaeli/label at it withhtmlFor, or give it anaria-label. - The chosen option's label is read as the trigger's value, so the name is the question, not the answer: Status, not In progress.
SelectLabelnames aSelectGroup.- For an invalid select, put
aria-invalidandaria-describedbyonSelectTrigger.
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/select.json