Select
An animated React select built on shadcn/ui and Radix Select: the drop-in Select grows out of its trigger, flips its chevron and fades in the check mark — plus a Motion block with a list that scales open, cascading options and a highlight that glides between items.
- Drop-in shadcn API
- Radix Select
- Motion layoutId
- Keyboard & typeahead
- prefers-reduced-motion
Deploying to fra1
"use client";
import * as React from "react";
import { Cloud, Database, Globe, HardDrive, Server, Zap } from "lucide-react";
import {
AnimatedSelect,
AnimatedSelectContent,
AnimatedSelectGroup,
AnimatedSelectItem,
AnimatedSelectLabel,
AnimatedSelectSeparator,
AnimatedSelectTrigger,
AnimatedSelectValue,
} from "@/components/dusk/select/animated-select";
export default function AnimatedSelectDemo() {
const [region, setRegion] = React.useState("fra1");
return (
<div className="grid w-full max-w-xs gap-2">
<label htmlFor="deploy-region" className="text-sm font-medium">
Deploy region
</label>
<AnimatedSelect value={region} onValueChange={setRegion} name="region">
<AnimatedSelectTrigger id="deploy-region">
<AnimatedSelectValue placeholder="Choose a region" />
</AnimatedSelectTrigger>
<AnimatedSelectContent>
<AnimatedSelectGroup>
<AnimatedSelectLabel>Edge</AnimatedSelectLabel>
<AnimatedSelectItem value="iad1" icon={<Zap />} description="12ms">
Washington, D.C.
</AnimatedSelectItem>
<AnimatedSelectItem value="fra1" icon={<Globe />} description="18ms">
Frankfurt
</AnimatedSelectItem>
<AnimatedSelectItem value="hnd1" icon={<Cloud />} description="41ms">
Tokyo
</AnimatedSelectItem>
</AnimatedSelectGroup>
<AnimatedSelectSeparator />
<AnimatedSelectGroup>
<AnimatedSelectLabel>Dedicated</AnimatedSelectLabel>
<AnimatedSelectItem value="db1" icon={<Database />}>
Database cluster
</AnimatedSelectItem>
<AnimatedSelectItem value="vm1" icon={<Server />}>
Bare metal
</AnimatedSelectItem>
<AnimatedSelectItem value="local" icon={<HardDrive />} disabled>
On-premise (soon)
</AnimatedSelectItem>
</AnimatedSelectGroup>
</AnimatedSelectContent>
</AnimatedSelect>
<p className="text-xs text-muted-foreground" aria-live="polite">
Deploying to <span className="font-mono text-foreground">{region}</span>
</p>
</div>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/select.json1. Install the dependencies.
npm install @radix-ui/react-select lucide-react2. Copy the source into
components/ui/select.tsx.select.tsx "use client"; import * as React from "react"; import * as SelectPrimitive from "@radix-ui/react-select"; import { CheckIcon, ChevronDownIcon, ChevronUpIcon } from "lucide-react"; import { cn } from "@/lib/utils"; /** * DuskUI Select — a drop-in upgrade of the shadcn/ui Select. * * Same parts, props and data-slot attributes as shadcn (Select, SelectGroup, * SelectValue, SelectTrigger, SelectContent, SelectLabel, SelectItem, * SelectSeparator, SelectScrollUpButton, SelectScrollDownButton) plus: * - The chevron flips while open. * - The list grows out of the trigger (150ms ease-out) and closes faster * than it opens (popper mode shifts in slightly from the side it opens on). * - Items highlight with a smooth color transition; the check mark fades in. * - A subtle hover background on the trigger and a clear invalid state. * - Under `prefers-reduced-motion` the list only fades. * CSS only (tw-animate-css). Radix handles typeahead, keyboard, focus, * form submission (`name`) and aria. */ function Select({ ...props }: React.ComponentProps<typeof SelectPrimitive.Root>) { return <SelectPrimitive.Root data-slot="select" {...props} />; } /** Groups related items under a SelectLabel (role="group"). */ function SelectGroup({ ...props }: React.ComponentProps<typeof SelectPrimitive.Group>) { return <SelectPrimitive.Group data-slot="select-group" {...props} />; } /** Shows the selected item's text, or `placeholder` when nothing is selected. */ function SelectValue({ ...props }: React.ComponentProps<typeof SelectPrimitive.Value>) { return <SelectPrimitive.Value data-slot="select-value" {...props} />; } /** The button that opens the list. `size` is `sm` or `default`. */ function SelectTrigger({ className, size = "default", children, ...props }: React.ComponentProps<typeof SelectPrimitive.Trigger> & { size?: "sm" | "default"; }) { return ( <SelectPrimitive.Trigger data-slot="select-trigger" data-size={size} className={cn( "group/select-trigger flex w-fit items-center justify-between gap-2 rounded-md border border-input bg-transparent px-3 py-2 text-sm whitespace-nowrap shadow-xs outline-none select-none dark:bg-input/30", "transition-[color,background-color,border-color,box-shadow] duration-150 ease-out motion-reduce:transition-none", "dark:hover:bg-input/45", "data-[placeholder]:text-muted-foreground", "focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50", "aria-invalid:border-destructive aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40", "disabled:cursor-not-allowed disabled:opacity-50", "data-[size=default]:h-9 data-[size=sm]:h-8", "*: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-4 [&_svg:not([class*='text-'])]:text-muted-foreground", className )} {...props} > {children} <SelectPrimitive.Icon asChild> <ChevronDownIcon className={cn( "size-4 opacity-60 transition-[rotate,opacity] duration-200 ease-out motion-reduce:transition-none", "group-hover/select-trigger:opacity-90 group-data-[state=open]/select-trigger:rotate-180 group-data-[state=open]/select-trigger:opacity-100" )} /> </SelectPrimitive.Icon> </SelectPrimitive.Trigger> ); } /** * The floating list. Portalled. `position="item-aligned"` (default, like * shadcn) lines the selected item up with the trigger; `position="popper"` * drops it below the trigger at the trigger's width. */ function SelectContent({ className, children, position = "item-aligned", align = "center", ...props }: React.ComponentProps<typeof SelectPrimitive.Content>) { return ( <SelectPrimitive.Portal> <SelectPrimitive.Content data-slot="select-content" position={position} align={align} className={cn( "relative z-50 max-h-(--radix-select-content-available-height) min-w-[8rem] overflow-x-hidden overflow-y-auto rounded-lg border bg-popover text-popover-foreground", "origin-(--radix-select-content-transform-origin)", "shadow-md ease-out", "data-[state=open]:animate-in data-[state=open]:fade-in-0 data-[state=open]:zoom-in-95 data-[state=open]:duration-150", "data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[state=closed]:duration-100", "motion-reduce:data-[state=open]:zoom-in-100 motion-reduce:data-[state=closed]:zoom-out-100", position === "popper" && [ "data-[side=bottom]:translate-y-1 data-[side=left]:-translate-x-1 data-[side=right]:translate-x-1 data-[side=top]:-translate-y-1", "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", "motion-reduce:data-[side=bottom]:slide-in-from-top-0 motion-reduce:data-[side=left]:slide-in-from-right-0 motion-reduce:data-[side=right]:slide-in-from-left-0 motion-reduce:data-[side=top]:slide-in-from-bottom-0", ], className )} {...props} > <SelectScrollUpButton /> <SelectPrimitive.Viewport className={cn( "p-1", position === "popper" && "h-[var(--radix-select-trigger-height)] w-full min-w-[var(--radix-select-trigger-width)] scroll-my-1" )} > {children} </SelectPrimitive.Viewport> <SelectScrollDownButton /> </SelectPrimitive.Content> </SelectPrimitive.Portal> ); } /** A small heading for a SelectGroup. */ function SelectLabel({ className, ...props }: React.ComponentProps<typeof SelectPrimitive.Label>) { return ( <SelectPrimitive.Label data-slot="select-label" className={cn("px-2 py-1.5 text-xs font-medium text-muted-foreground", className)} {...props} /> ); } /** An option. Its children become the trigger's value text when selected. */ function SelectItem({ className, children, ...props }: React.ComponentProps<typeof SelectPrimitive.Item>) { return ( <SelectPrimitive.Item data-slot="select-item" className={cn( "relative flex w-full cursor-default items-center gap-2 rounded-md py-1.5 pr-8 pl-2 text-sm text-foreground/90 outline-hidden select-none", "transition-colors duration-150 ease-out motion-reduce:transition-none", "focus:bg-accent focus:text-accent-foreground data-[state=checked]:text-foreground", "data-[disabled]:pointer-events-none data-[disabled]:opacity-50", "[&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 [&_svg:not([class*='text-'])]:text-muted-foreground", "*:[span]:last:flex *:[span]:last:items-center *:[span]:last:gap-2", className )} {...props} > <span className="absolute right-2 flex size-3.5 items-center justify-center"> <SelectPrimitive.ItemIndicator className="animate-in fade-in-0 duration-150 ease-out motion-reduce:animate-none"> <CheckIcon className="size-4" /> </SelectPrimitive.ItemIndicator> </span> <SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText> </SelectPrimitive.Item> ); } /** A divider between groups. */ function SelectSeparator({ className, ...props }: React.ComponentProps<typeof SelectPrimitive.Separator>) { return ( <SelectPrimitive.Separator data-slot="select-separator" className={cn("pointer-events-none -mx-1 my-1 h-px bg-border", className)} {...props} /> ); } /** Scrolls the list up when it overflows. Rendered automatically by SelectContent. */ function SelectScrollUpButton({ className, ...props }: React.ComponentProps<typeof SelectPrimitive.ScrollUpButton>) { return ( <SelectPrimitive.ScrollUpButton data-slot="select-scroll-up-button" className={cn("flex cursor-default items-center justify-center py-1 text-muted-foreground", className)} {...props} > <ChevronUpIcon className="size-4" /> </SelectPrimitive.ScrollUpButton> ); } /** Scrolls the list down when it overflows. Rendered automatically by SelectContent. */ function SelectScrollDownButton({ className, ...props }: React.ComponentProps<typeof SelectPrimitive.ScrollDownButton>) { return ( <SelectPrimitive.ScrollDownButton data-slot="select-scroll-down-button" className={cn("flex cursor-default items-center justify-center py-1 text-muted-foreground", className)} {...props} > <ChevronDownIcon className="size-4" /> </SelectPrimitive.ScrollDownButton> ); } export { Select, SelectContent, SelectGroup, SelectItem, SelectLabel, SelectScrollDownButton, SelectScrollUpButton, SelectSeparator, SelectTrigger, SelectValue, };
Replaces components/ui/select.tsx with the same parts and data-slot attributes, so existing selects keep working. The base Select animates with CSS only (tw-animate-css).
Usage
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select"
export function ThemeSelect() {
return (
<Select defaultValue="system" name="theme">
<SelectTrigger className="w-44" aria-label="Theme">
<SelectValue placeholder="Theme" />
</SelectTrigger>
<SelectContent>
<SelectItem value="light">Light</SelectItem>
<SelectItem value="dark">Dark</SelectItem>
<SelectItem value="system">System</SelectItem>
</SelectContent>
</Select>
)
}Examples
Default
A labelled select with a group heading and a disabled option. In the default item-aligned mode the selected option lines up with the trigger, like a native select.
import {
Select,
SelectContent,
SelectGroup,
SelectItem,
SelectLabel,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
export default function SelectBasicDemo() {
return (
<div className="grid w-full max-w-xs gap-2">
<label htmlFor="fruit" className="text-sm font-medium">
Favorite fruit
</label>
<Select defaultValue="mango">
<SelectTrigger id="fruit" className="w-full">
<SelectValue placeholder="Pick a fruit" />
</SelectTrigger>
<SelectContent>
<SelectGroup>
<SelectLabel>Fruits</SelectLabel>
<SelectItem value="apple">Apple</SelectItem>
<SelectItem value="banana">Banana</SelectItem>
<SelectItem value="mango">Mango</SelectItem>
<SelectItem value="kiwi">Kiwi</SelectItem>
<SelectItem value="dragonfruit" disabled>
Dragonfruit
</SelectItem>
</SelectGroup>
</SelectContent>
</Select>
</div>
);
}Groups & scrolling
position="popper" drops the list below the trigger at its width and slides it in from the side it opens on. Long lists scroll, with scroll buttons at the edges, and name submits the value with a form.
import {
Select,
SelectContent,
SelectGroup,
SelectItem,
SelectLabel,
SelectSeparator,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
const zones = [
{
label: "Americas",
items: [
["america/los_angeles", "Pacific Time (PT)"],
["america/denver", "Mountain Time (MT)"],
["america/chicago", "Central Time (CT)"],
["america/new_york", "Eastern Time (ET)"],
["america/sao_paulo", "Brasília Time (BRT)"],
],
},
{
label: "Europe & Africa",
items: [
["europe/london", "Greenwich Mean Time (GMT)"],
["europe/berlin", "Central European Time (CET)"],
["europe/athens", "Eastern European Time (EET)"],
["africa/nairobi", "East Africa Time (EAT)"],
],
},
{
label: "Asia & Pacific",
items: [
["asia/kolkata", "India Standard Time (IST)"],
["asia/shanghai", "China Standard Time (CST)"],
["asia/tokyo", "Japan Standard Time (JST)"],
["australia/sydney", "Australian Eastern Time (AET)"],
],
},
];
export default function SelectScrollableDemo() {
return (
<Select name="timezone">
<SelectTrigger className="w-72" aria-label="Timezone">
<SelectValue placeholder="Select a timezone" />
</SelectTrigger>
<SelectContent position="popper" className="max-h-72">
{zones.map((zone, i) => (
<SelectGroup key={zone.label}>
{i > 0 && <SelectSeparator />}
<SelectLabel>{zone.label}</SelectLabel>
{zone.items.map(([value, label]) => (
<SelectItem key={value} value={value}>
{label}
</SelectItem>
))}
</SelectGroup>
))}
</SelectContent>
</Select>
);
}Sizes & states
size="sm" on the trigger for dense UIs, aria-invalid for errors (link the message with aria-describedby) and disabled.
Pick a role to continue.
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
const roles = ["Viewer", "Editor", "Admin"];
function RoleItems() {
return roles.map((role) => (
<SelectItem key={role} value={role.toLowerCase()}>
{role}
</SelectItem>
));
}
export default function SelectStatesDemo() {
return (
<div className="grid w-full max-w-xs gap-4">
<Select defaultValue="editor">
<SelectTrigger size="sm" className="w-full" aria-label="Role (small)">
<SelectValue />
</SelectTrigger>
<SelectContent>
<RoleItems />
</SelectContent>
</Select>
<div className="grid gap-1.5">
<Select>
<SelectTrigger className="w-full" aria-label="Role" aria-invalid aria-describedby="role-error">
<SelectValue placeholder="Choose a role" />
</SelectTrigger>
<SelectContent>
<RoleItems />
</SelectContent>
</Select>
<p id="role-error" className="text-xs text-destructive">
Pick a role to continue.
</p>
</div>
<Select disabled defaultValue="viewer">
<SelectTrigger className="w-full" aria-label="Role (disabled)">
<SelectValue />
</SelectTrigger>
<SelectContent>
<RoleItems />
</SelectContent>
</Select>
</div>
);
}Blocks
Ready-made variations composed from the base component with Motion. Each installs on its own and pulls in its dependencies automatically.
Animated Select
The list scales out of the trigger and options cascade in quickly. A highlight pill glides between options as you hover or arrow through them (a shared Motion layoutId), the selected option's check mark fades in, and the active option's icon brightens. Options can carry an icon (shown in the trigger too) and a description. Same composition as the shadcn Select.
Deploying to fra1
"use client";
import * as React from "react";
import { Cloud, Database, Globe, HardDrive, Server, Zap } from "lucide-react";
import {
AnimatedSelect,
AnimatedSelectContent,
AnimatedSelectGroup,
AnimatedSelectItem,
AnimatedSelectLabel,
AnimatedSelectSeparator,
AnimatedSelectTrigger,
AnimatedSelectValue,
} from "@/components/dusk/select/animated-select";
export default function AnimatedSelectDemo() {
const [region, setRegion] = React.useState("fra1");
return (
<div className="grid w-full max-w-xs gap-2">
<label htmlFor="deploy-region" className="text-sm font-medium">
Deploy region
</label>
<AnimatedSelect value={region} onValueChange={setRegion} name="region">
<AnimatedSelectTrigger id="deploy-region">
<AnimatedSelectValue placeholder="Choose a region" />
</AnimatedSelectTrigger>
<AnimatedSelectContent>
<AnimatedSelectGroup>
<AnimatedSelectLabel>Edge</AnimatedSelectLabel>
<AnimatedSelectItem value="iad1" icon={<Zap />} description="12ms">
Washington, D.C.
</AnimatedSelectItem>
<AnimatedSelectItem value="fra1" icon={<Globe />} description="18ms">
Frankfurt
</AnimatedSelectItem>
<AnimatedSelectItem value="hnd1" icon={<Cloud />} description="41ms">
Tokyo
</AnimatedSelectItem>
</AnimatedSelectGroup>
<AnimatedSelectSeparator />
<AnimatedSelectGroup>
<AnimatedSelectLabel>Dedicated</AnimatedSelectLabel>
<AnimatedSelectItem value="db1" icon={<Database />}>
Database cluster
</AnimatedSelectItem>
<AnimatedSelectItem value="vm1" icon={<Server />}>
Bare metal
</AnimatedSelectItem>
<AnimatedSelectItem value="local" icon={<HardDrive />} disabled>
On-premise (soon)
</AnimatedSelectItem>
</AnimatedSelectGroup>
</AnimatedSelectContent>
</AnimatedSelect>
<p className="text-xs text-muted-foreground" aria-live="polite">
Deploying to <span className="font-mono text-foreground">{region}</span>
</p>
</div>
);
}$ npx shadcn@latest add https://duskui.com/r/animated-select.json1. Install the dependencies.
npm install motion @radix-ui/react-select lucide-react2. Copy the source into
components/dusk/select/animated-select.tsx.animated-select.tsx "use client"; import * as React from "react"; import * as SelectPrimitive from "@radix-ui/react-select"; import { CheckIcon, ChevronDownIcon, ChevronUpIcon } from "lucide-react"; import { motion, useReducedMotion, type Transition, type Variants } from "motion/react"; import { SelectTrigger, SelectValue } from "@/components/ui/select"; import { cn } from "@/lib/utils"; const pillSpring: Transition = { type: "spring", stiffness: 520, damping: 38, mass: 0.6 }; type AnimatedSelectContextValue = { /** Base for the shared highlight layoutId; changes every time the list opens. */ layoutId: string; value: string | undefined; /** The item under the highlight, by its value. */ active: string | null; setActive: (value: string | null) => void; reduceMotion: boolean; }; const AnimatedSelectContext = React.createContext<AnimatedSelectContextValue | null>(null); function useAnimatedSelect() { const context = React.useContext(AnimatedSelectContext); if (!context) throw new Error("AnimatedSelect parts must be used inside <AnimatedSelect>."); return context; } export interface AnimatedSelectProps extends Omit< React.ComponentProps<typeof SelectPrimitive.Root>, "value" | "defaultValue" | "onValueChange" | "open" | "defaultOpen" | "onOpenChange" > { value?: string; defaultValue?: string; onValueChange?: (value: string) => void; open?: boolean; defaultOpen?: boolean; onOpenChange?: (open: boolean) => void; } /** * A select with personality. The list springs out of the trigger, items * cascade in, a highlight pill glides between items as you hover or arrow * through them (a shared Motion `layoutId`) and the check mark pops in. * * Compose it like the shadcn Select: AnimatedSelect › AnimatedSelectTrigger › * AnimatedSelectValue, then AnimatedSelectContent › AnimatedSelectItem. * Built on Radix Select, so typeahead, arrow keys, Home/End, Escape, form * submission (`name`) and aria all work. Radix keeps closed content in a * detached fragment so the trigger can show the selected label, so the exit is * a quick CSS fade Radix waits for. Under reduced motion it only fades. */ export function AnimatedSelect({ value: valueProp, defaultValue, onValueChange, open: openProp, defaultOpen = false, onOpenChange, children, ...props }: AnimatedSelectProps) { const id = React.useId(); const reduceMotion = useReducedMotion() ?? false; const [uncontrolledValue, setUncontrolledValue] = React.useState(defaultValue); const [uncontrolledOpen, setUncontrolledOpen] = React.useState(defaultOpen); const [active, setActive] = React.useState<string | null>(null); const [session, setSession] = React.useState(0); const value = valueProp ?? uncontrolledValue; const open = openProp ?? uncontrolledOpen; const handleOpenChange = (next: boolean) => { if (openProp === undefined) setUncontrolledOpen(next); // A fresh layoutId per opening, so the pill never glides in from where it was last time. if (next) setSession((s) => s + 1); else setActive(null); onOpenChange?.(next); }; const handleValueChange = (next: string) => { if (valueProp === undefined) setUncontrolledValue(next); onValueChange?.(next); }; const context = React.useMemo( () => ({ layoutId: `${id}-pill-${session}`, value, active, setActive, reduceMotion }), [id, session, value, active, reduceMotion] ); return ( <AnimatedSelectContext.Provider value={context}> <SelectPrimitive.Root value={value} onValueChange={handleValueChange} open={open} onOpenChange={handleOpenChange} {...props} > {children} </SelectPrimitive.Root> </AnimatedSelectContext.Provider> ); } /** The button that opens the list. Same props as the DuskUI SelectTrigger. */ export function AnimatedSelectTrigger({ className, ...props }: React.ComponentProps<typeof SelectTrigger>) { return <SelectTrigger className={cn("w-full min-w-48", className)} {...props} />; } /** Shows the selected item's label, or `placeholder`. */ export const AnimatedSelectValue = SelectValue; export interface AnimatedSelectContentProps extends Omit<React.ComponentProps<typeof SelectPrimitive.Content>, "position" | "asChild" | "forceMount"> { /** Delay between items as they cascade in, in seconds. */ stagger?: number; } /** The list surface. Drops below the trigger at its width, scales open and staggers its items in. */ export function AnimatedSelectContent({ className, children, side = "bottom", sideOffset = 6, align = "start", stagger = 0.015, onPointerLeave, ...props }: AnimatedSelectContentProps) { const { reduceMotion, setActive } = useAnimatedSelect(); const offset = side === "top" ? 4 : side === "bottom" ? -4 : 0; const variants: Variants = reduceMotion ? { closed: { opacity: 0 }, open: { opacity: 1, transition: { duration: 0.15 } } } : { closed: { opacity: 0, scale: 0.96, y: offset }, open: { opacity: 1, scale: 1, y: 0, transition: { type: "spring", duration: 0.25, bounce: 0.1, staggerChildren: stagger, delayChildren: 0.02 }, }, }; return ( <SelectPrimitive.Portal> <SelectPrimitive.Content position="popper" side={side} sideOffset={sideOffset} align={align} asChild onPointerLeave={(event) => { onPointerLeave?.(event); setActive(null); }} {...props} > <motion.div data-slot="select-content" variants={variants} initial="closed" animate="open" className={cn( "relative z-50 max-h-[min(20rem,var(--radix-select-content-available-height))] min-w-(--radix-select-trigger-width) overflow-hidden rounded-md border bg-popover text-popover-foreground outline-none", "origin-(--radix-select-content-transform-origin)", "shadow-md", // Exit: a quick CSS fade that Radix waits for before unmounting. "data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[state=closed]:duration-100", "motion-reduce:data-[state=closed]:zoom-out-100", className )} > <SelectPrimitive.ScrollUpButton className="flex h-6 cursor-default items-center justify-center text-muted-foreground"> <ChevronUpIcon className="size-4" /> </SelectPrimitive.ScrollUpButton> <SelectPrimitive.Viewport className="scroll-my-1.5 p-1.5">{children}</SelectPrimitive.Viewport> <SelectPrimitive.ScrollDownButton className="flex h-6 cursor-default items-center justify-center text-muted-foreground"> <ChevronDownIcon className="size-4" /> </SelectPrimitive.ScrollDownButton> </motion.div> </SelectPrimitive.Content> </SelectPrimitive.Portal> ); } function useItemVariants(): Variants { const { reduceMotion } = useAnimatedSelect(); return reduceMotion ? { closed: { opacity: 0 }, open: { opacity: 1 } } : { closed: { opacity: 0, y: -2 }, open: { opacity: 1, y: 0, transition: { duration: 0.2, ease: "easeOut" }, }, }; } export interface AnimatedSelectItemProps extends Omit<React.ComponentProps<typeof SelectPrimitive.Item>, "asChild"> { /** Leading icon. Shown in the list and, when selected, in the trigger. */ icon?: React.ReactNode; /** Secondary line under the label. Shown in the list only. */ description?: React.ReactNode; } /** An option. Its children (and icon) become the trigger's value when selected. */ export function AnimatedSelectItem({ value, className, children, icon, description, onFocus, ...props }: AnimatedSelectItemProps) { const { layoutId, value: selected, active, setActive, reduceMotion } = useAnimatedSelect(); const variants = useItemVariants(); const isActive = active === value; const isSelected = selected === value; return ( <SelectPrimitive.Item value={value} asChild // Radix focuses items on hover and arrow keys, so focus drives the pill. onFocus={(event) => { onFocus?.(event); setActive(value); }} {...props} > <motion.div variants={variants} className={cn( "relative flex w-full cursor-default items-center gap-2.5 rounded-sm py-2 pr-9 pl-2.5 text-sm text-foreground/85 outline-none select-none", "transition-colors duration-150 data-[highlighted]:text-foreground data-[state=checked]:text-foreground", "data-[disabled]:pointer-events-none data-[disabled]:opacity-50", "[&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4", className )} > {isActive && ( <motion.span aria-hidden layoutId={layoutId} className="absolute inset-0 rounded-sm bg-accent" transition={reduceMotion ? { duration: 0 } : pillSpring} /> )} {/* ItemText's children are also portalled into the trigger when selected. */} <SelectPrimitive.ItemText asChild> <span className="relative flex min-w-0 flex-1 items-center gap-2.5"> {icon && ( <span aria-hidden className={cn( "flex transition-colors duration-150", isActive || isSelected ? "text-foreground" : "text-muted-foreground" )} > {icon} </span> )} <span className="truncate">{children}</span> </span> </SelectPrimitive.ItemText> {description && ( <span className="relative hidden shrink-0 text-xs text-muted-foreground sm:inline">{description}</span> )} <span className="absolute right-2.5 flex size-4 items-center justify-center"> <SelectPrimitive.ItemIndicator asChild> <motion.span initial={reduceMotion ? { opacity: 0 } : { scale: 0.8, opacity: 0 }} animate={{ scale: 1, opacity: 1 }} transition={reduceMotion ? { duration: 0.15 } : { type: "spring", bounce: 0.1, duration: 0.2 }} className="flex text-foreground" > <CheckIcon className="size-4" /> </motion.span> </SelectPrimitive.ItemIndicator> </span> </motion.div> </SelectPrimitive.Item> ); } /** Groups related items under an AnimatedSelectLabel. */ export const AnimatedSelectGroup = SelectPrimitive.Group; /** A small heading for a group. Cascades in with the items. */ export function AnimatedSelectLabel({ className, ...props }: Omit<React.ComponentProps<typeof motion.div>, "variants">) { const variants = useItemVariants(); return ( <SelectPrimitive.Label asChild> <motion.div variants={variants} className={cn("px-2.5 pt-1.5 pb-1 text-xs font-medium text-muted-foreground", className)} {...props} /> </SelectPrimitive.Label> ); } /** A divider between groups. Draws in from the left as the list opens. */ export function AnimatedSelectSeparator({ className, ...props }: Omit<React.ComponentProps<typeof motion.div>, "variants">) { const { reduceMotion } = useAnimatedSelect(); return ( <SelectPrimitive.Separator asChild> <motion.div variants={ reduceMotion ? { closed: { opacity: 0 }, open: { opacity: 1 } } : { closed: { scaleX: 0, opacity: 0 }, open: { scaleX: 1, opacity: 1, transition: { duration: 0.3, ease: "easeOut" } } } } className={cn("-mx-1.5 my-1.5 h-px origin-left bg-border", className)} {...props} /> </SelectPrimitive.Separator> ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| value / onValueChange | string / (value: string) => void | — | Controlled selection (on AnimatedSelect). |
| defaultValue | string | — | Uncontrolled initial selection. |
| open / onOpenChange | boolean / (open: boolean) => void | — | Controlled open state. |
| name / required / disabled | Radix Select props | — | Forwarded to the Radix root; `name` submits with forms. |
| AnimatedSelectTrigger | SelectTrigger props | — | The trigger button. Put AnimatedSelectValue inside. |
| AnimatedSelectContent › stagger | number | 0.015 | Delay between options as they cascade in, in seconds. |
| AnimatedSelectContent › side / align / sideOffset | Radix props | "bottom" / "start" / 6 | Placement next to the trigger. |
| AnimatedSelectItem › icon | ReactNode | — | Leading icon; also shown in the trigger when selected. |
| AnimatedSelectItem › description | ReactNode | — | Secondary text on the right (list only). |
| AnimatedSelectGroup / Label / Separator | — | — | Group options under a heading; the separator draws in. |
API Reference
Same parts as shadcn/ui — Select, SelectGroup, SelectValue, SelectTrigger, SelectContent, SelectLabel, SelectItem, SelectSeparator, SelectScrollUpButton and SelectScrollDownButton — each forwarding its Radix Select props.
| Prop | Type | Default | Description |
|---|---|---|---|
| value / onValueChange | string / (value: string) => void | — | Controlled selection (on Select). |
| defaultValue | string | — | Uncontrolled initial selection. |
| name / required / disabled | string / boolean / boolean | — | Form integration on Select. A hidden native select submits the value. |
| size | "sm" | "default" | "default" | On SelectTrigger: 32px or 36px tall. |
| position | "item-aligned" | "popper" | "item-aligned" | On SelectContent: align the selected item over the trigger, or drop below it. |
| placeholder | ReactNode | — | On SelectValue: shown until something is selected. |
| disabled | boolean | — | On SelectItem: skip it in keyboard navigation and typeahead. |
Accessibility
- Built on Radix Select: the trigger is a combobox button, the list a listbox of options with aria-selected; arrow keys, Home, End, Enter, Space, Escape and typeahead all work.
- Label the trigger with a <label htmlFor> pointing at its id, or aria-label. Mark errors with aria-invalid and link the message with aria-describedby.
- A hidden native select mirrors the value, so forms, autofill and form reset work.
- The gliding highlight is decorative (aria-hidden); the active option is still conveyed by focus and data-highlighted.
- Under prefers-reduced-motion the list only fades, options appear without movement and the highlight jumps.