Combobox
An animated React combobox — a searchable select and an autocomplete multi-select built on shadcn/ui's Popover + Command recipe (Radix and cmdk) with Motion: the panel eases open, results filter with layout animations and highlighted matches, and selections become removable animated chips.
- shadcn Popover + Command
- cmdk
- Motion layout
- Multi-select chips
- Keyboard accessible
"use client";
import * as React from "react";
import { Combobox } from "@/components/dusk/combobox/combobox";
const frameworks = [
{ value: "next", label: "Next.js", keywords: ["react", "vercel"] },
{ value: "remix", label: "Remix", keywords: ["react router"] },
{ value: "astro", label: "Astro", keywords: ["islands"] },
{ value: "sveltekit", label: "SvelteKit", keywords: ["svelte"] },
{ value: "nuxt", label: "Nuxt", keywords: ["vue"] },
{ value: "solidstart", label: "SolidStart", keywords: ["solid"] },
{ value: "qwik", label: "Qwik City", keywords: ["resumable"] },
{ value: "gatsby", label: "Gatsby", disabled: true },
];
export default function ComboboxDemo() {
const [framework, setFramework] = React.useState("");
return (
<div className="grid w-full max-w-xs gap-2">
<label htmlFor="framework" className="text-sm font-medium">
Framework
</label>
<Combobox
id="framework"
options={frameworks}
value={framework}
onValueChange={setFramework}
placeholder="Select a framework…"
searchPlaceholder="Search frameworks…"
emptyMessage="No framework found."
name="framework"
/>
</div>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/combobox.json1. Install the dependencies.
npm install motion cmdk @radix-ui/react-popover lucide-react2. Copy the source into
components/dusk/combobox/combobox.tsx.combobox.tsx "use client"; import * as React from "react"; import * as PopoverPrimitive from "@radix-ui/react-popover"; import { Command as CommandPrimitive } from "cmdk"; import { CheckIcon, ChevronsUpDownIcon, SearchIcon, SearchXIcon } from "lucide-react"; import { AnimatePresence, motion, useReducedMotion, type Transition, type Variants } from "motion/react"; import { Button } from "@/components/ui/button"; import { Popover, PopoverTrigger } from "@/components/ui/popover"; import { cn } from "@/lib/utils"; export interface ComboboxOption { value: string; label: string; /** Secondary text on the right of the option. */ description?: React.ReactNode; /** Leading icon element. Also shown in the trigger when selected. */ icon?: React.ReactNode; /** Extra search terms that match this option. */ keywords?: string[]; disabled?: boolean; } export interface ComboboxProps extends Omit<React.ComponentProps<typeof Button>, "value" | "defaultValue" | "onChange" | "children"> { options: ComboboxOption[]; value?: string; defaultValue?: string; /** Called with the new value, or "" when the selection is cleared. */ onValueChange?: (value: string) => void; /** Trigger text when nothing is selected. */ placeholder?: string; searchPlaceholder?: string; /** Shown when the search matches nothing. */ emptyMessage?: React.ReactNode; /** Picking the selected option again clears the selection. */ allowDeselect?: boolean; /** Form field name. The value is submitted with a hidden input. */ name?: string; /** Classes for the floating panel. `className` styles the trigger. */ contentClassName?: string; } const pillSpring: Transition = { type: "spring", stiffness: 520, damping: 38, mass: 0.6 }; const layoutSpring: Transition = { type: "spring", bounce: 0.1, duration: 0.3 }; /** Options whose label starts with the query come first; the rest keep their order. */ function filterOptions(options: ComboboxOption[], query: string) { const q = query.trim().toLowerCase(); if (!q) return options; const starts: ComboboxOption[] = []; const contains: ComboboxOption[] = []; for (const option of options) { const label = option.label.toLowerCase(); if (label.startsWith(q)) starts.push(option); else if (label.includes(q) || option.keywords?.some((k) => k.toLowerCase().includes(q))) contains.push(option); } return [...starts, ...contains]; } /** Highlights the first match of `query` inside `label`. */ function Highlight({ label, query }: { label: string; query: string }) { const q = query.trim(); const index = q ? label.toLowerCase().indexOf(q.toLowerCase()) : -1; if (index < 0) return <>{label}</>; return ( <> {label.slice(0, index)} <mark className="bg-transparent font-semibold text-foreground">{label.slice(index, index + q.length)}</mark> {label.slice(index + q.length)} </> ); } /** * A searchable select (shadcn's Popover + Command recipe) with motion. The * panel scales out of the trigger, the options cascade in, and as you type * the list filters with layout animations: matches slide into their new * places, the list height eases to fit, matched text is bolded and a * highlight pill glides with the arrow keys. A friendly empty state appears * when nothing matches. cmdk handles Up/Down/Home/End/Enter and aria * (combobox + listbox), Radix handles focus, Escape and outside clicks. * Under reduced motion it fades only. */ export function Combobox({ options, value: valueProp, defaultValue = "", onValueChange, placeholder = "Select an option…", searchPlaceholder = "Search…", emptyMessage = "No results found.", allowDeselect = true, name, disabled, className, contentClassName, ...props }: ComboboxProps) { const id = React.useId(); const reduceMotion = useReducedMotion() ?? false; const [uncontrolled, setUncontrolled] = React.useState(defaultValue); const [open, setOpen] = React.useState(false); const [query, setQuery] = React.useState(""); const [highlight, setHighlight] = React.useState(""); const value = valueProp ?? uncontrolled; const selected = options.find((option) => option.value === value); const filtered = React.useMemo(() => filterOptions(options, query), [options, query]); const handleOpenChange = (next: boolean) => { setOpen(next); if (next) { setQuery(""); setHighlight(value); } }; const select = (next: string) => { const resolved = allowDeselect && next === value ? "" : next; if (valueProp === undefined) setUncontrolled(resolved); onValueChange?.(resolved); setOpen(false); }; const surface: Variants = reduceMotion ? { closed: { opacity: 0, transition: { duration: 0.1 } }, open: { opacity: 1, transition: { duration: 0.15 } } } : { closed: { opacity: 0, scale: 0.96, y: -4, transition: { duration: 0.12, ease: "easeIn" } }, open: { opacity: 1, scale: 1, y: 0, transition: { type: "spring", duration: 0.25, bounce: 0 } }, }; return ( <Popover open={open} onOpenChange={handleOpenChange}> <PopoverTrigger asChild> <Button type="button" variant="outline" role="combobox" aria-expanded={open} aria-haspopup="listbox" disabled={disabled} className={cn( "w-full min-w-48 justify-between px-3 font-normal hover:border-ring/40", open && "border-ring/60 ring-[3px] ring-ring/15", className )} {...props} > <span className="relative flex min-w-0 flex-1 items-center overflow-hidden"> <AnimatePresence mode="popLayout" initial={false}> <motion.span key={selected?.value ?? "placeholder"} className={cn("flex min-w-0 items-center gap-2", !selected && "text-muted-foreground")} initial={reduceMotion ? { opacity: 0 } : { opacity: 0, y: 6 }} animate={{ opacity: 1, y: 0 }} exit={reduceMotion ? { opacity: 0 } : { opacity: 0, y: -6 }} transition={{ type: "spring", duration: 0.25, bounce: 0 }} > {selected?.icon && ( <span aria-hidden className="flex text-muted-foreground"> {selected.icon} </span> )} <span className="truncate">{selected?.label ?? placeholder}</span> </motion.span> </AnimatePresence> </span> <ChevronsUpDownIcon aria-hidden className={cn("text-muted-foreground transition-colors duration-200", open && "text-foreground")} /> </Button> </PopoverTrigger> {name && <input type="hidden" name={name} value={value} />} <AnimatePresence> {open && ( // forceMount hands mounting to AnimatePresence so the exit animation can play. <PopoverPrimitive.Portal key="combobox" forceMount> <PopoverPrimitive.Content forceMount asChild align="start" sideOffset={6}> <motion.div variants={surface} initial="closed" animate="open" exit="closed" className={cn( "z-50 w-(--radix-popover-trigger-width) min-w-56 overflow-hidden rounded-xl border bg-popover text-popover-foreground outline-none", "origin-(--radix-popover-content-transform-origin)", "shadow-lg", contentClassName )} > <CommandPrimitive shouldFilter={false} value={highlight} onValueChange={setHighlight} loop label={placeholder} className="flex flex-col" > <div className="flex h-10 items-center gap-2 border-b px-3"> <SearchIcon aria-hidden className="size-4 shrink-0 text-muted-foreground" /> <CommandPrimitive.Input value={query} onValueChange={setQuery} placeholder={searchPlaceholder} className="h-full w-full bg-transparent text-sm outline-none placeholder:text-muted-foreground" /> </div> {/* The wrapper scrolls (layoutScroll keeps layout animations right while scrolled); the list eases its height. */} <motion.div layoutScroll className="max-h-72 scroll-py-1.5 overflow-x-hidden overflow-y-auto overscroll-contain"> <CommandPrimitive.List label={placeholder} className={cn( "h-(--cmdk-list-height) overflow-clip transition-[height] duration-200 ease-out motion-reduce:transition-none", filtered.length > 0 && "[&_[cmdk-list-sizer]]:p-1.5" )} > {filtered.map((option, index) => { const isSelected = option.value === value; const isHighlighted = option.value === highlight; return ( <CommandPrimitive.Item key={option.value} value={option.value} disabled={option.disabled} onSelect={() => select(option.value)} asChild > <motion.div layout={reduceMotion ? false : "position"} initial={reduceMotion ? { opacity: 0 } : { opacity: 0, y: -4 }} animate={{ opacity: 1, y: 0 }} transition={{ layout: layoutSpring, default: { type: "spring", duration: 0.3, bounce: 0.1, delay: Math.min(index, 8) * 0.02 }, }} className={cn( "relative flex cursor-default items-center gap-2.5 rounded-lg py-2 pr-9 pl-2.5 text-sm text-foreground/85 outline-none select-none", "transition-colors duration-150 data-[selected=true]:text-foreground", "data-[disabled=true]:pointer-events-none data-[disabled=true]:opacity-50", "[&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4" )} > {isHighlighted && ( <motion.span aria-hidden layoutId={`${id}-pill`} transition={reduceMotion ? { duration: 0 } : pillSpring} className="absolute inset-0 rounded-lg bg-accent" /> )} {option.icon && ( <span aria-hidden className={cn( "relative flex transition-colors duration-150", isHighlighted || isSelected ? "text-foreground" : "text-muted-foreground" )} > {option.icon} </span> )} <span className="relative min-w-0 flex-1 truncate"> <Highlight label={option.label} query={query} /> </span> {option.description && ( <span className="relative shrink-0 text-xs text-muted-foreground">{option.description}</span> )} <span className="absolute right-2.5 flex size-4 items-center justify-center"> <AnimatePresence initial={false}> {isSelected && ( <motion.span key="check" initial={reduceMotion ? { opacity: 0 } : { scale: 0.6, opacity: 0 }} animate={{ scale: 1, opacity: 1 }} exit={{ opacity: 0, scale: reduceMotion ? 1 : 0.6 }} transition={reduceMotion ? { duration: 0.15 } : { type: "spring", bounce: 0.15, duration: 0.25 }} className="flex text-primary" > <CheckIcon className="size-4" /> </motion.span> )} </AnimatePresence> </span> </motion.div> </CommandPrimitive.Item> ); })} </CommandPrimitive.List> </motion.div> <AnimatePresence initial={false}> {filtered.length === 0 && ( <motion.div key="empty" role="status" initial={reduceMotion ? { opacity: 0 } : { opacity: 0, scale: 0.98, y: 4 }} animate={{ opacity: 1, scale: 1, y: 0 }} transition={{ duration: 0.2, ease: "easeOut" }} className="flex flex-col items-center gap-2 px-4 py-6 text-center text-sm text-muted-foreground" > <motion.span aria-hidden className="grid size-9 place-items-center rounded-full border border-white/10 bg-white/[0.03]" > <SearchXIcon className="size-4" /> </motion.span> <span>{emptyMessage}</span> </motion.div> )} </AnimatePresence> </CommandPrimitive> </motion.div> </PopoverPrimitive.Content> </PopoverPrimitive.Portal> )} </AnimatePresence> </Popover> ); }
shadcn/ui has no Combobox primitive — it is a recipe composed from Popover and Command. DuskUI ships it as a ready-made component that installs the DuskUI Button and Popover with it. Filtering is done in the component (cmdk's shouldFilter is off) so results can animate.
Usage
"use client"
import * as React from "react"
import { Combobox } from "@/components/dusk/combobox/combobox"
const frameworks = [
{ value: "next", label: "Next.js" },
{ value: "remix", label: "Remix" },
{ value: "astro", label: "Astro" },
]
export function FrameworkPicker() {
const [value, setValue] = React.useState("")
return (
<Combobox
aria-label="Framework"
options={frameworks}
value={value}
onValueChange={setValue}
placeholder="Select a framework…"
/>
)
}Examples
Default
The panel scales out of the trigger and options cascade in. As you type, matches that start with the query move to the top and every option slides to its new place, the list height eases to fit, matched text is bolded and a highlight pill glides with the arrow keys. When nothing matches, an empty state fades in. The trigger crossfades to the new label.
"use client";
import * as React from "react";
import { Combobox } from "@/components/dusk/combobox/combobox";
const frameworks = [
{ value: "next", label: "Next.js", keywords: ["react", "vercel"] },
{ value: "remix", label: "Remix", keywords: ["react router"] },
{ value: "astro", label: "Astro", keywords: ["islands"] },
{ value: "sveltekit", label: "SvelteKit", keywords: ["svelte"] },
{ value: "nuxt", label: "Nuxt", keywords: ["vue"] },
{ value: "solidstart", label: "SolidStart", keywords: ["solid"] },
{ value: "qwik", label: "Qwik City", keywords: ["resumable"] },
{ value: "gatsby", label: "Gatsby", disabled: true },
];
export default function ComboboxDemo() {
const [framework, setFramework] = React.useState("");
return (
<div className="grid w-full max-w-xs gap-2">
<label htmlFor="framework" className="text-sm font-medium">
Framework
</label>
<Combobox
id="framework"
options={frameworks}
value={framework}
onValueChange={setFramework}
placeholder="Select a framework…"
searchPlaceholder="Search frameworks…"
emptyMessage="No framework found."
name="framework"
/>
</div>
);
}Icons & descriptions
Options can carry an icon (shown in the trigger too), a description and extra search keywords — try “golang”. allowDeselect={false} keeps a value selected.
import { Braces, Coffee, FileCode2, Gem, Hash, Terminal } from "lucide-react";
import { Combobox } from "@/components/dusk/combobox/combobox";
const languages = [
{ value: "ts", label: "TypeScript", description: ".ts", icon: <FileCode2 />, keywords: ["javascript"] },
{ value: "py", label: "Python", description: ".py", icon: <Terminal /> },
{ value: "rs", label: "Rust", description: ".rs", icon: <Braces /> },
{ value: "go", label: "Go", description: ".go", icon: <Hash />, keywords: ["golang"] },
{ value: "rb", label: "Ruby", description: ".rb", icon: <Gem /> },
{ value: "java", label: "Java", description: ".java", icon: <Coffee /> },
];
export default function ComboboxIconsDemo() {
return (
<Combobox
aria-label="Language"
options={languages}
defaultValue="ts"
allowDeselect={false}
placeholder="Language"
searchPlaceholder="Filter languages…"
className="w-64"
/>
);
}Blocks
Ready-made variations composed from the base component with Motion. Each installs on its own and pulls in its dependencies automatically.
Multi-select Combobox
Type right in the field to filter, pick as many as you like and each becomes a chip that scales in. Chips slide over to make room and shrink away when removed with their ✕ or Backspace. Options show checkboxes that draw themselves, max caps the selection with a counter, and every change is announced to screen readers.
Skills
Pick up to 5. Backspace removes the last one.
"use client";
import * as React from "react";
import { MultiCombobox } from "@/components/dusk/combobox/multi-combobox";
const skills = [
"React",
"TypeScript",
"Next.js",
"Tailwind CSS",
"Motion",
"Node.js",
"GraphQL",
"PostgreSQL",
"Rust",
"Figma",
].map((label) => ({ value: label.toLowerCase().replace(/[^a-z0-9]+/g, "-"), label }));
export default function MultiComboboxDemo() {
const [value, setValue] = React.useState(["react", "motion"]);
return (
<div className="grid w-full max-w-sm gap-2">
<p className="text-sm font-medium">
Skills
</p>
<MultiCombobox
aria-label="Skills"
options={skills}
value={value}
onValueChange={setValue}
placeholder="Search skills…"
max={5}
name="skills"
/>
<p className="text-xs text-muted-foreground">Pick up to 5. Backspace removes the last one.</p>
</div>
);
}$ npx shadcn@latest add https://duskui.com/r/multi-combobox.json1. Install the dependencies.
npm install motion cmdk @radix-ui/react-popover lucide-react2. Copy the source into
components/dusk/combobox/multi-combobox.tsx.multi-combobox.tsx "use client"; import * as React from "react"; import * as PopoverPrimitive from "@radix-ui/react-popover"; import { Command as CommandPrimitive } from "cmdk"; import { ChevronDownIcon, SearchXIcon, XIcon } from "lucide-react"; import { AnimatePresence, motion, useReducedMotion, type Transition, type Variants } from "motion/react"; import { Popover, PopoverAnchor } from "@/components/ui/popover"; import { cn } from "@/lib/utils"; export interface MultiComboboxOption { value: string; label: string; /** Leading icon element. Also shown on the chip. */ icon?: React.ReactNode; /** Extra search terms that match this option. */ keywords?: string[]; disabled?: boolean; } export interface MultiComboboxProps { options: MultiComboboxOption[]; value?: string[]; defaultValue?: string[]; onValueChange?: (value: string[]) => void; /** Input placeholder while nothing is selected. */ placeholder?: string; /** Shown when the search matches nothing. */ emptyMessage?: React.ReactNode; /** Stop adding once this many are selected. */ max?: number; /** Form field name. Each value is submitted with its own hidden input. */ name?: string; disabled?: boolean; /** Accessible name for the search input and list (cmdk wires it to a hidden label). */ "aria-label"?: string; /** Classes for the field. */ className?: string; /** Classes for the floating panel. */ contentClassName?: string; } const pillSpring: Transition = { type: "spring", stiffness: 520, damping: 38, mass: 0.6 }; const layoutSpring: Transition = { type: "spring", bounce: 0.1, duration: 0.3 }; function filterOptions(options: MultiComboboxOption[], query: string) { const q = query.trim().toLowerCase(); if (!q) return options; const starts: MultiComboboxOption[] = []; const contains: MultiComboboxOption[] = []; for (const option of options) { const label = option.label.toLowerCase(); if (label.startsWith(q)) starts.push(option); else if (label.includes(q) || option.keywords?.some((k) => k.toLowerCase().includes(q))) contains.push(option); } return [...starts, ...contains]; } function Highlight({ label, query }: { label: string; query: string }) { const q = query.trim(); const index = q ? label.toLowerCase().indexOf(q.toLowerCase()) : -1; if (index < 0) return <>{label}</>; return ( <> {label.slice(0, index)} <mark className="bg-transparent font-semibold text-foreground">{label.slice(index, index + q.length)}</mark> {label.slice(index + q.length)} </> ); } /** * A multi-select combobox: type to filter, pick as many options as you like * and each one becomes a chip that scales into the field. Chips slide over to * make room and shrink away when removed (with their ✕, or Backspace in the * empty input). The list keeps its layout animations, a gliding highlight and * checkboxes that draw themselves. cmdk handles Up/Down/Home/End/Enter and * aria; Radix positions the panel and closes it on Escape, Tab or an outside * click. Changes are announced to screen readers. Under reduced motion it fades. */ export function MultiCombobox({ options, value: valueProp, defaultValue = [], onValueChange, placeholder = "Search…", emptyMessage = "No results found.", max, name, disabled = false, "aria-label": ariaLabel = "Options", className, contentClassName, }: MultiComboboxProps) { const autoId = React.useId(); const reduceMotion = useReducedMotion() ?? false; const fieldRef = React.useRef<HTMLDivElement>(null); const inputRef = React.useRef<HTMLInputElement>(null); const [uncontrolled, setUncontrolled] = React.useState<string[]>(defaultValue); const [open, setOpen] = React.useState(false); const [query, setQuery] = React.useState(""); const [highlight, setHighlight] = React.useState(""); const [announcement, setAnnouncement] = React.useState(""); const selected = valueProp ?? uncontrolled; const filtered = React.useMemo(() => filterOptions(options, query), [options, query]); const byValue = React.useMemo(() => new Map(options.map((o) => [o.value, o])), [options]); const full = max !== undefined && selected.length >= max; const commit = (next: string[]) => { if (valueProp === undefined) setUncontrolled(next); onValueChange?.(next); }; const toggle = (value: string) => { const label = byValue.get(value)?.label ?? value; if (selected.includes(value)) { commit(selected.filter((v) => v !== value)); setAnnouncement(`${label} removed`); } else if (!full) { commit([...selected, value]); setAnnouncement(`${label} added, ${selected.length + 1} selected`); } setQuery(""); }; const remove = (value: string) => { commit(selected.filter((v) => v !== value)); setAnnouncement(`${byValue.get(value)?.label ?? value} removed`); inputRef.current?.focus(); }; const surface: Variants = reduceMotion ? { closed: { opacity: 0, transition: { duration: 0.1 } }, open: { opacity: 1, transition: { duration: 0.15 } } } : { closed: { opacity: 0, scale: 0.96, y: -4, transition: { duration: 0.12, ease: "easeIn" } }, open: { opacity: 1, scale: 1, y: 0, transition: { type: "spring", duration: 0.25, bounce: 0 } }, }; const chip = reduceMotion ? { initial: { opacity: 0 }, animate: { opacity: 1 }, exit: { opacity: 0 } } : { initial: { opacity: 0, scale: 0.9 }, animate: { opacity: 1, scale: 1 }, exit: { opacity: 0, scale: 0.9, transition: { duration: 0.15 } }, }; return ( <Popover open={open && !disabled} onOpenChange={setOpen}> <CommandPrimitive shouldFilter={false} value={highlight} onValueChange={setHighlight} loop label={ariaLabel} className="relative w-full" > <PopoverAnchor asChild> <div ref={fieldRef} data-slot="multi-combobox" data-state={open ? "open" : "closed"} data-disabled={disabled || undefined} onPointerDown={(event) => { if (disabled || (event.target as HTMLElement).closest("button")) return; if (event.target !== inputRef.current) event.preventDefault(); inputRef.current?.focus(); setOpen(true); }} className={cn( "relative flex min-h-9 w-full cursor-text flex-wrap items-center gap-1 rounded-md border border-input bg-transparent py-1 pr-8 pl-1.5 text-sm shadow-xs dark:bg-input/30", "transition-[border-color,box-shadow,background-color] duration-200 ease-out motion-reduce:transition-none", "hover:border-ring/40 dark:hover:bg-input/45", "focus-within:border-ring focus-within:ring-[3px] focus-within:ring-ring/30", "data-disabled:pointer-events-none data-disabled:opacity-50", className )} > <AnimatePresence initial={false} mode="popLayout"> {selected.map((value) => { const option = byValue.get(value); const label = option?.label ?? value; return ( <motion.span key={value} layout={reduceMotion ? false : "position"} {...chip} transition={{ type: "spring", bounce: 0.1, duration: 0.25 }} className="inline-flex h-6 max-w-full items-center gap-1 rounded-md border border-white/10 bg-white/[0.06] pr-0.5 pl-1.5 text-xs font-medium text-foreground [&_svg:not([class*='size-'])]:size-3.5" > {option?.icon && ( <span aria-hidden className="flex"> {option.icon} </span> )} <span className="truncate">{label}</span> <button type="button" aria-label={`Remove ${label}`} disabled={disabled} onClick={() => remove(value)} className="grid size-4.5 place-items-center rounded-sm text-muted-foreground outline-none transition-colors hover:bg-white/10 hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring/60" > <XIcon className="size-3" /> </button> </motion.span> ); })} </AnimatePresence> <CommandPrimitive.Input ref={inputRef} value={query} disabled={disabled} placeholder={selected.length ? undefined : placeholder} onValueChange={(next) => { setQuery(next); setOpen(true); }} onKeyDown={(event) => { if (event.key === "Backspace" && !query && selected.length) { event.preventDefault(); remove(selected[selected.length - 1]); } else if ((event.key === "ArrowDown" || event.key === "ArrowUp") && !open) { event.preventDefault(); setOpen(true); } else if (event.key === "Escape" && !open && query) { setQuery(""); } }} className="h-6 min-w-16 flex-1 bg-transparent px-1 outline-none placeholder:text-muted-foreground" /> <ChevronDownIcon aria-hidden className={cn( "pointer-events-none absolute top-2.5 right-2.5 size-4 text-muted-foreground transition-[rotate,color] duration-200 ease-out motion-reduce:transition-none", open && "rotate-180 text-foreground" )} /> </div> </PopoverAnchor> {name && selected.map((value) => <input key={value} type="hidden" name={name} value={value} />)} <span className="sr-only" aria-live="polite"> {announcement} </span> <AnimatePresence> {open && !disabled && ( <PopoverPrimitive.Portal key="multi-combobox" forceMount> <PopoverPrimitive.Content forceMount asChild align="start" sideOffset={6} // Focus stays in the input while the list is open. onOpenAutoFocus={(event) => event.preventDefault()} onCloseAutoFocus={(event) => event.preventDefault()} onInteractOutside={(event) => { if (fieldRef.current?.contains(event.target as Node)) event.preventDefault(); }} > <motion.div variants={surface} initial="closed" animate="open" exit="closed" // Clicking an option must not steal focus from the input. onMouseDown={(event) => event.preventDefault()} className={cn( "z-50 w-(--radix-popover-trigger-width) min-w-56 overflow-hidden rounded-xl border bg-popover text-popover-foreground outline-none", "origin-(--radix-popover-content-transform-origin)", "shadow-lg", contentClassName )} > {/* The wrapper scrolls (layoutScroll keeps layout animations right while scrolled); the list eases its height. */} <motion.div layoutScroll className="max-h-72 scroll-py-1.5 overflow-x-hidden overflow-y-auto overscroll-contain"> <CommandPrimitive.List label={ariaLabel} aria-multiselectable className={cn( "h-(--cmdk-list-height) overflow-clip transition-[height] duration-200 ease-out motion-reduce:transition-none", filtered.length > 0 && "[&_[cmdk-list-sizer]]:p-1.5" )} > {filtered.map((option, index) => { const isSelected = selected.includes(option.value); const isHighlighted = option.value === highlight; const isDisabled = option.disabled || (full && !isSelected); return ( <CommandPrimitive.Item key={option.value} value={option.value} disabled={isDisabled} onSelect={() => toggle(option.value)} aria-checked={isSelected} asChild > <motion.div layout={reduceMotion ? false : "position"} initial={reduceMotion ? { opacity: 0 } : { opacity: 0, y: -4 }} animate={{ opacity: 1, y: 0 }} transition={{ layout: layoutSpring, default: { type: "spring", duration: 0.3, bounce: 0.1, delay: Math.min(index, 8) * 0.02 }, }} className={cn( "relative flex cursor-default items-center gap-2.5 rounded-lg px-2.5 py-2 text-sm text-foreground/85 outline-none select-none", "transition-colors duration-150 data-[selected=true]:text-foreground", "data-[disabled=true]:pointer-events-none data-[disabled=true]:opacity-50", "[&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4" )} > {isHighlighted && ( <motion.span aria-hidden layoutId={`${autoId}-pill`} transition={reduceMotion ? { duration: 0 } : pillSpring} className="absolute inset-0 rounded-lg bg-accent" /> )} <span aria-hidden className={cn( "relative grid size-4 shrink-0 place-items-center rounded-[4px] border transition-[background-color,border-color] duration-150", isSelected ? "border-primary bg-primary text-primary-foreground" : "border-input bg-input/30" )} > <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth={2.25} strokeLinecap="round" strokeLinejoin="round" className="size-3.5"> <motion.path d="M3.5 8.5l3 3 6-7" initial={false} animate={{ pathLength: isSelected ? 1 : 0, opacity: isSelected ? 1 : 0 }} transition={reduceMotion ? { duration: 0 } : { duration: isSelected ? 0.25 : 0.1, ease: "easeOut" }} /> </svg> </span> {option.icon && ( <span aria-hidden className={cn( "relative flex transition-colors duration-150", isHighlighted || isSelected ? "text-foreground" : "text-muted-foreground" )} > {option.icon} </span> )} <span className="relative min-w-0 flex-1 truncate"> <Highlight label={option.label} query={query} /> </span> </motion.div> </CommandPrimitive.Item> ); })} </CommandPrimitive.List> </motion.div> {filtered.length === 0 && ( <motion.div role="status" initial={reduceMotion ? { opacity: 0 } : { opacity: 0, scale: 0.98, y: 4 }} animate={{ opacity: 1, scale: 1, y: 0 }} transition={{ duration: 0.2, ease: "easeOut" }} className="flex flex-col items-center gap-2 px-4 py-6 text-center text-sm text-muted-foreground" > <span aria-hidden className="grid size-9 place-items-center rounded-full border border-white/10 bg-white/[0.03]" > <SearchXIcon className="size-4" /> </span> <span>{emptyMessage}</span> </motion.div> )} {max !== undefined && ( <div className="flex items-center justify-between border-t px-3 py-2 text-xs text-muted-foreground"> <span> <span className={cn("tabular-nums", full && "text-foreground")}>{selected.length}</span> of {max} selected </span> {selected.length > 0 && ( <button type="button" onClick={() => { commit([]); setAnnouncement("All selections cleared"); }} className="rounded-sm text-foreground/80 outline-none hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring/60" > Clear </button> )} </div> )} </motion.div> </PopoverPrimitive.Content> </PopoverPrimitive.Portal> )} </AnimatePresence> </CommandPrimitive> </Popover> ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| options | { value, label, icon?, keywords?, disabled? }[] | — | The choices. |
| value / onValueChange | string[] / (value: string[]) => void | — | Controlled selection. |
| defaultValue | string[] | [] | Uncontrolled initial selection. |
| max | number | — | Stop adding at this many; shows a counter and Clear button. |
| placeholder | string | "Search…" | Shown while nothing is selected. |
| emptyMessage | ReactNode | "No results found." | Shown when nothing matches. |
| name | string | — | Submits each value with its own hidden input. |
| aria-label | string | "Options" | Names the input and list. |
| disabled | boolean | false | Disable the whole field. |
| className / contentClassName | string | — | Classes for the field and the panel. |
API Reference
Combobox props. Anything else (id, aria-label, className, disabled…) is forwarded to the trigger Button.
| Prop | Type | Default | Description |
|---|---|---|---|
| options | { value, label, description?, icon?, keywords?, disabled? }[] | — | The choices. `label` is searched; `keywords` add extra matches. |
| value / onValueChange | string / (value: string) => void | — | Controlled selection; "" when cleared. |
| defaultValue | string | "" | Uncontrolled initial selection. |
| placeholder / searchPlaceholder | string | — | Trigger text when empty, and the search input placeholder. |
| emptyMessage | ReactNode | "No results found." | Shown when nothing matches. |
| allowDeselect | boolean | true | Picking the selected option again clears it. |
| name | string | — | Submits the value with a hidden input. |
| contentClassName | string | — | Classes for the panel. Other props go to the trigger Button. |
Accessibility
- The Combobox trigger is a button with role="combobox", aria-expanded and aria-haspopup="listbox"; name it with a <label htmlFor> or aria-label.
- Inside, cmdk wires the search input (role="combobox", aria-activedescendant) to the listbox, so Up, Down, Home, End and Enter work without leaving the input.
- Radix Popover returns focus to the trigger on close and closes on Escape or an outside click.
- The multi-select keeps focus in its input, marks the list aria-multiselectable and options aria-checked, gives every chip a labelled remove button, and announces additions and removals in a polite live region.
- Match highlighting uses <mark>; the gliding highlight pill is decorative (aria-hidden).
- Under prefers-reduced-motion the panel and chips only fade, the list stops sliding and the highlight jumps.