Combobox
A field you type in to filter a list, boxed with the pen: pick one, or several as circled chips.
"use client";
import { Combobox, ComboboxContent, ComboboxEmpty, ComboboxInput, ComboboxItem, ComboboxList } from "@/components/ui/combobox";
const frameworks = ["Next.js", "SvelteKit", "Nuxt.js", "Remix", "Astro"];
export default function ComboboxDemo() {
return (
<div className="w-full max-w-xs">
<Combobox items={frameworks}>
<ComboboxInput placeholder="Select a framework" aria-label="Framework" seed="cb-demo" />
<ComboboxContent seed="cb-demo-list">
<ComboboxEmpty>No framework found.</ComboboxEmpty>
<ComboboxList>
{(item: string) => (
<ComboboxItem key={item} value={item}>
{item}
</ComboboxItem>
)}
</ComboboxList>
</ComboboxContent>
</Combobox>
</div>
);
}Installation
pnpm dlx shadcn@latest add @ballpoint/ combobox"use client";
import { useRef } from "react";
import { Combobox as ComboboxPrimitive } from "@base-ui/react/combobox";
import { cn } from "@/lib/utils";
import { usePen, type Pen } from "@/hooks/use-ink-box";
import { inkClassName, inkRules, inkWash } from "@/lib/ink";
import { InkGlyph } from "@/lib/ink-glyphs";
import { InkOutline } from "@/lib/ink-outline";
import { InkPanel } from "@/lib/ink-panel";
/** Root: pass `items`, and `value` / `onValueChange` for a controlled choice. Add `multiple` for chips. */
const Combobox = ComboboxPrimitive.Root;
function ComboboxValue({ ...props }: ComboboxPrimitive.Value.Props) {
return <ComboboxPrimitive.Value data-slot="combobox-value" {...props} />;
}
function ComboboxTrigger({ className, children, ...props }: ComboboxPrimitive.Trigger.Props) {
return (
<ComboboxPrimitive.Trigger
data-slot="combobox-trigger"
aria-label="Open the list"
className={inkClassName(
"relative flex size-7 cursor-pointer items-center justify-center text-ink-3 transition-colors outline-none hover:text-ink focus-visible:text-ink [&_svg:not(.ink-sketch):not([class*='size-'])]:size-3.5",
className,
)}
{...props}
>
{children ?? <InkGlyph name="chevron-down" />}
</ComboboxPrimitive.Trigger>
);
}
function ComboboxClear({ className, ...props }: ComboboxPrimitive.Clear.Props) {
return (
<ComboboxPrimitive.Clear
data-slot="combobox-clear"
aria-label="Clear"
className={inkClassName("relative flex size-7 cursor-pointer items-center justify-center text-ink-3 transition-colors outline-none hover:text-ink focus-visible:text-ink", className)}
{...props}
>
<InkGlyph name="close" className="size-3.5" />
</ComboboxPrimitive.Clear>
);
}
/**
* The field you type in, boxed with the pen, with a chevron to open the list
* and (`showClear`) a cross to empty it.
*/
function ComboboxInput({
className,
children,
disabled = false,
showTrigger = true,
showClear = false,
seed,
roughness,
passes,
radius,
corners,
draw,
weight,
speed,
...props
}: ComboboxPrimitive.Input.Props &
Pick<Pen, "roughness" | "passes" | "radius" | "corners" | "draw" | "weight" | "speed"> & { showTrigger?: boolean; showClear?: boolean; seed?: string | number }) {
const pen = usePen({ roughness, passes, radius, corners, draw, weight, speed });
return (
<ComboboxPrimitive.InputGroup data-slot="combobox-input-group" className={cn("group/combobox ink-within relative inline-flex h-10 w-full min-w-0 items-center", disabled && "opacity-50")}>
<InkOutline
pen={pen}
seed={seed}
estimate={[224, 40]}
focusPass
className={cn(
"text-ink-3 transition-colors duration-(--dur-hover) group-hover/combobox:text-ink group-focus-within/combobox:text-ink",
"group-has-[[aria-invalid=true]]/combobox:text-destructive group-has-[[data-invalid]]/combobox:text-destructive",
)}
/>
<ComboboxPrimitive.Input
data-slot="combobox-input"
disabled={disabled}
className={inkClassName("relative h-full min-w-0 flex-1 bg-transparent pl-3 text-base text-foreground outline-none placeholder:text-ink-3 disabled:cursor-not-allowed", className)}
{...props}
/>
<div className="relative flex items-center pr-1">
{showClear && <ComboboxClear disabled={disabled} />}
{showTrigger && <ComboboxTrigger disabled={disabled} />}
</div>
{children}
</ComboboxPrimitive.InputGroup>
);
}
/** The list, on a slip of paper pencilled round and lifted off its shadow. */
function ComboboxContent({
className,
style,
side = "bottom",
sideOffset = 6,
align = "start",
alignOffset = 0,
anchor,
seed,
roughness,
passes,
radius,
corners,
shadow,
draw,
weight,
speed,
...props
}: ComboboxPrimitive.Popup.Props &
Pick<ComboboxPrimitive.Positioner.Props, "side" | "align" | "sideOffset" | "alignOffset" | "anchor"> &
Omit<Pen, "fill"> & { seed?: string | number }) {
const pen = usePen({ roughness, passes, radius, corners, shadow, draw, weight, speed });
const marks = { ...inkWash, ...inkRules };
return (
<ComboboxPrimitive.Portal>
<ComboboxPrimitive.Positioner side={side} sideOffset={sideOffset} align={align} alignOffset={alignOffset} anchor={anchor} className="isolate z-50 outline-none">
<ComboboxPrimitive.Popup
data-slot="combobox-content"
className={inkClassName(
"ink-paper relative isolate flex w-(--anchor-width) max-w-(--available-width) min-w-48 origin-(--transform-origin) flex-col text-popover-foreground outline-none",
"transition-[opacity,scale] duration-(--dur-hover) ease-out data-starting-style:opacity-0 data-ending-style:opacity-0 data-ending-style:ease-in",
"motion-safe:data-starting-style:scale-[0.97] motion-safe:data-ending-style:scale-[0.98]",
className,
)}
style={typeof style === "function" ? (state) => ({ ...marks, ...style(state) }) : { ...marks, ...style }}
{...props}
>
<InkPanel pen={pen} seed={seed} estimate={[224, 200]} offset={5} maxRadius={12} className="text-ink-3" />
{props.children}
</ComboboxPrimitive.Popup>
</ComboboxPrimitive.Positioner>
</ComboboxPrimitive.Portal>
);
}
function ComboboxList({ className, ...props }: ComboboxPrimitive.List.Props) {
return (
<ComboboxPrimitive.List
data-slot="combobox-list"
className={inkClassName("relative max-h-[min(22rem,var(--available-height))] scroll-py-1.5 overflow-y-auto overscroll-contain p-1.5 outline-none data-empty:p-0", className)}
{...props}
/>
);
}
/** The highlighted item is shaded in with a quick pass of the pen; the chosen one is ticked. */
function ComboboxItem({ className, children, ...props }: ComboboxPrimitive.Item.Props) {
return (
<ComboboxPrimitive.Item
data-slot="combobox-item"
className={inkClassName(
"relative isolate flex w-full cursor-default items-center gap-2 py-1 pr-9 pl-2.5 text-base outline-none select-none",
"before:pointer-events-none before:absolute before:inset-x-0.5 before:inset-y-0 before:-z-10 before:bg-current before:opacity-0 before:transition-opacity before:duration-(--dur-press)",
"before:[mask-image:var(--ink-wash-1)] before:[mask-size:100%_100%] before:[mask-repeat:no-repeat] nth-[3n+2]:before:[mask-image:var(--ink-wash-2)] nth-[3n]:before:[mask-image:var(--ink-wash-3)]",
"data-highlighted:before:opacity-13 data-disabled:pointer-events-none data-disabled:opacity-50",
"[&_svg:not(.ink-sketch)]:pointer-events-none [&_svg:not(.ink-sketch)]:shrink-0 [&_svg:not(.ink-sketch):not([class*='size-'])]:size-4",
className,
)}
{...props}
>
{children}
<ComboboxPrimitive.ItemIndicator data-slot="combobox-item-indicator" className="pointer-events-none absolute right-2.5 flex size-4 items-center justify-center">
<InkGlyph name="check" draw="mount" />
</ComboboxPrimitive.ItemIndicator>
</ComboboxPrimitive.Item>
);
}
function ComboboxGroup({ className, ...props }: ComboboxPrimitive.Group.Props) {
return <ComboboxPrimitive.Group data-slot="combobox-group" className={inkClassName(className)} {...props} />;
}
function ComboboxLabel({ className, ...props }: ComboboxPrimitive.GroupLabel.Props) {
return <ComboboxPrimitive.GroupLabel data-slot="combobox-label" className={inkClassName("px-2.5 pt-1.5 pb-1 text-sm text-ink-3", className)} {...props} />;
}
function ComboboxCollection({ ...props }: ComboboxPrimitive.Collection.Props) {
return <ComboboxPrimitive.Collection data-slot="combobox-collection" {...props} />;
}
/** Shown, in place of the list, when nothing matches. */
function ComboboxEmpty({ className, ...props }: ComboboxPrimitive.Empty.Props) {
return <ComboboxPrimitive.Empty data-slot="combobox-empty" className={inkClassName("p-3 text-center text-base text-ink-3 empty:hidden", className)} {...props} />;
}
/** A short rule drawn across the slip (see inkRules). */
function ComboboxSeparator({ className, ...props }: ComboboxPrimitive.Separator.Props) {
return (
<ComboboxPrimitive.Separator
data-slot="combobox-separator"
className={inkClassName("mx-1 my-1.5 h-[5px] bg-ink-4 [mask-image:var(--ink-rule-2)] [mask-size:100%_100%] [mask-repeat:no-repeat]", className)}
{...props}
/>
);
}
/** The box for a multiple combobox: chosen items sit in it as chips, with the field after them. */
function ComboboxChips({
className,
children,
seed,
roughness,
passes,
radius,
corners,
draw,
weight,
speed,
...props
}: ComboboxPrimitive.Chips.Props & Pick<Pen, "roughness" | "passes" | "radius" | "corners" | "draw" | "weight" | "speed"> & { seed?: string | number }) {
const pen = usePen({ roughness, passes, radius, corners, draw, weight, speed });
return (
<ComboboxPrimitive.Chips
data-slot="combobox-chips"
className={inkClassName("group/chips ink-within relative flex min-h-10 w-full flex-wrap items-center gap-1.5 px-2 py-1.5", className)}
{...props}
>
<InkOutline
pen={pen}
seed={seed}
estimate={[320, 40]}
focusPass
maxRadius={18}
className="text-ink-3 transition-colors duration-(--dur-hover) group-hover/chips:text-ink group-focus-within/chips:text-ink group-has-[[aria-invalid=true]]/chips:text-destructive"
/>
{children}
</ComboboxPrimitive.Chips>
);
}
/** A chosen item: a word circled with the pen, with a cross to take it back. */
function ComboboxChip({
className,
children,
showRemove = true,
seed,
...props
}: ComboboxPrimitive.Chip.Props & { showRemove?: boolean; seed?: string | number }) {
const pen = usePen({ passes: 1, corners: "joined" });
return (
<ComboboxPrimitive.Chip
data-slot="combobox-chip"
className={inkClassName("relative flex h-7 w-fit items-center gap-1 px-2.5 text-sm whitespace-nowrap outline-none has-disabled:opacity-50 data-highlighted:text-ink", className)}
{...props}
>
<InkOutline pen={pen} seed={seed} estimate={[64, 28]} pad={5} className="text-ink-3" />
<span className="relative">{children}</span>
{showRemove && (
<ComboboxPrimitive.ChipRemove
data-slot="combobox-chip-remove"
aria-label="Remove"
className="relative -mr-1.5 flex size-5 cursor-pointer items-center justify-center text-ink-3 transition-colors outline-none hover:text-ink focus-visible:text-ink"
>
<InkGlyph name="close" className="size-3" />
</ComboboxPrimitive.ChipRemove>
)}
</ComboboxPrimitive.Chip>
);
}
function ComboboxChipsInput({ className, ...props }: ComboboxPrimitive.Input.Props) {
return (
<ComboboxPrimitive.Input
data-slot="combobox-chip-input"
className={inkClassName("relative min-w-16 flex-1 bg-transparent px-1 text-base text-foreground outline-none placeholder:text-ink-3", className)}
{...props}
/>
);
}
/** A ref for the chips box, to anchor the list under it: <ComboboxContent anchor={anchor} />. */
function useComboboxAnchor() {
return useRef<HTMLDivElement | null>(null);
}
export {
Combobox,
ComboboxInput,
ComboboxContent,
ComboboxList,
ComboboxItem,
ComboboxGroup,
ComboboxLabel,
ComboboxCollection,
ComboboxEmpty,
ComboboxSeparator,
ComboboxChips,
ComboboxChip,
ComboboxChipsInput,
ComboboxTrigger,
ComboboxValue,
ComboboxClear,
useComboboxAnchor,
};Usage
import { Combobox, ComboboxContent, ComboboxEmpty, ComboboxInput, ComboboxItem, ComboboxList } from "@/components/ui/combobox"
const frameworks = ["Next.js", "SvelteKit", "Nuxt.js"]
<Combobox items={frameworks}>
<ComboboxInput placeholder="Select a framework" />
<ComboboxContent>
<ComboboxEmpty>No framework found.</ComboboxEmpty>
<ComboboxList>
{(item) => <ComboboxItem key={item} value={item}>{item}</ComboboxItem>}
</ComboboxList>
</ComboboxContent>
</Combobox>Examples
With a clear button
A cross that empties the field.
"use client";
import { Combobox, ComboboxContent, ComboboxEmpty, ComboboxInput, ComboboxItem, ComboboxList } from "@/components/ui/combobox";
const pens = ["Blue ballpoint", "Black gel", "Red felt tip", "Pencil", "Fountain pen"];
export default function ComboboxClear() {
return (
<div className="w-full max-w-xs">
<Combobox items={pens} defaultValue={pens[0]}>
<ComboboxInput placeholder="Choose a pen" aria-label="Pen" showClear seed="cbc" />
<ComboboxContent seed="cbc-list">
<ComboboxEmpty>No pen found.</ComboboxEmpty>
<ComboboxList>
{(item: string) => (
<ComboboxItem key={item} value={item}>
{item}
</ComboboxItem>
)}
</ComboboxList>
</ComboboxContent>
</Combobox>
</div>
);
}Groups
Items under headings, ruled apart.
"use client";
import {
Combobox,
ComboboxCollection,
ComboboxContent,
ComboboxEmpty,
ComboboxGroup,
ComboboxInput,
ComboboxItem,
ComboboxLabel,
ComboboxList,
ComboboxSeparator,
} from "@/components/ui/combobox";
const timezones = [
{ value: "Americas", items: ["(GMT-5) New York", "(GMT-8) Los Angeles", "(GMT-6) Chicago", "(GMT-3) São Paulo"] },
{ value: "Europe", items: ["(GMT+0) London", "(GMT+1) Paris", "(GMT+1) Berlin", "(GMT+2) Athens"] },
{ value: "Asia / Pacific", items: ["(GMT+9) Tokyo", "(GMT+8) Singapore", "(GMT+5:30) Mumbai", "(GMT+11) Sydney"] },
];
export default function ComboboxGroups() {
return (
<div className="w-full max-w-xs">
<Combobox items={timezones}>
<ComboboxInput placeholder="Select a timezone" aria-label="Timezone" seed="cbg" />
<ComboboxContent seed="cbg-list">
<ComboboxEmpty>No timezone found.</ComboboxEmpty>
<ComboboxList>
{(group: (typeof timezones)[number], index: number) => (
<ComboboxGroup key={group.value} items={group.items}>
<ComboboxLabel>{group.value}</ComboboxLabel>
<ComboboxCollection>
{(item: string) => (
<ComboboxItem key={item} value={item}>
{item}
</ComboboxItem>
)}
</ComboboxCollection>
{index < timezones.length - 1 && <ComboboxSeparator />}
</ComboboxGroup>
)}
</ComboboxList>
</ComboboxContent>
</Combobox>
</div>
);
}Multiple
Chosen items sit in the box as chips, with a cross each; the list opens under the box.
"use client";
import {
Combobox,
ComboboxChip,
ComboboxChips,
ComboboxChipsInput,
ComboboxContent,
ComboboxEmpty,
ComboboxItem,
ComboboxList,
ComboboxValue,
useComboboxAnchor,
} from "@/components/ui/combobox";
const topics = ["Letters", "Sketches", "Lists", "Recipes", "Margins", "Maps"];
export default function ComboboxMultiple() {
const anchor = useComboboxAnchor();
return (
<div className="w-full max-w-sm">
<Combobox multiple items={topics} defaultValue={[topics[0], topics[1]]}>
<ComboboxChips ref={anchor} seed="cbm">
<ComboboxValue>
{(values: string[]) => (
<>
{values.map((value) => (
<ComboboxChip key={value} seed={`cbm-${value}`}>
{value}
</ComboboxChip>
))}
<ComboboxChipsInput aria-label="Topics" placeholder={values.length ? "" : "Pick some topics"} />
</>
)}
</ComboboxValue>
</ComboboxChips>
<ComboboxContent anchor={anchor} seed="cbm-list">
<ComboboxEmpty>No topics found.</ComboboxEmpty>
<ComboboxList>
{(item: string) => (
<ComboboxItem key={item} value={item}>
{item}
</ComboboxItem>
)}
</ComboboxList>
</ComboboxContent>
</Combobox>
</div>
);
}API reference
- items
T[]What to choose from: strings, or objects with a value and label. Grouped items are { value, items }.
- value / defaultValue
TThe chosen item (an array with `multiple`), controlled or not.
- multipledefault
false booleanChoose several; use ComboboxChips and ComboboxChip for the box.
- ComboboxInput showTrigger / showCleardefault
true / false booleanA chevron to open the list; a cross to empty the field.
- ComboboxContent anchor
RefObjectWhat the list lines up under. Use useComboboxAnchor() for the chips box.
- seed
string | numberPins the drawing.
Every other prop goes to Base UI's Combobox.
Pen settings
The drawing also takes draw, roughness, passes, radius, corners, shadow, weight and speed, as props or from the nearest InkProvider. What each one does.