Combobox
A text field that filters a list as you type. Pick one value, or many as chips. Use it when a list is too long to scan, such as teammates, channels, or time zones. For a short fixed list, use Select.
Installation
pnpm dlx shadcn@latest add https://sprout.tulipfarm.ai/r/combobox.jsonCopies Combobox and the components it uses (Button and Input Group) into your app, installs the packages, and adds the styles. Then import it from @/components/ui/combobox. Needs Tailwind CSS 4 and a components.json from shadcn init.
Install the following dependencies:
pnpm add clsx tailwind-merge @base-ui/react @hugeicons/core-free-icons @hugeicons/reactAdd Sprout base once per app: the
cn()helper and the design tokens.lib/sprout/classes.ts- Design tokens, for your global CSS
Add the components it uses: Button and Input Group.
Copy and paste the following code into your project.
'use client'; import { useRef } from 'react'; import { Combobox as ComboboxPrimitive } from '@base-ui/react/combobox'; import { CheckIcon } from '@/lib/sprout/icons/check-icon'; import { ChevronDownIcon } from '@/lib/sprout/icons/chevron-down-icon'; import { CloseIcon } from '@/lib/sprout/icons/close-icon'; import { cn, stateClassName } from '@/lib/sprout/classes'; import { Button } from '@/components/ui/button'; import { InputGroup, InputGroupAddon, InputGroupButton, InputGroupInput, type InputGroupSize } from '@/components/ui/input-group'; export type ComboboxProps<Value, Multiple extends boolean | undefined = false> = ComboboxPrimitive.Root.Props< Value, Multiple >; export const Combobox = ComboboxPrimitive.Root; export type ComboboxValueProps = ComboboxPrimitive.Value.Props; export function ComboboxValue(props: ComboboxValueProps) { return <ComboboxPrimitive.Value data-slot="combobox-value" {...props} />; } export type ComboboxTriggerProps = ComboboxPrimitive.Trigger.Props; export function ComboboxTrigger({ className, children, ...props }: ComboboxTriggerProps) { return ( <ComboboxPrimitive.Trigger data-slot="combobox-trigger" aria-label={children == null ? 'Show options' : undefined} {...props} className={stateClassName<ComboboxPrimitive.Trigger.State>('sprout-combobox-trigger', className)} > {children} <ChevronDownIcon aria-hidden="true" className="sprout-combobox-trigger-icon" /> </ComboboxPrimitive.Trigger> ); } function ComboboxClear({ className, ...props }: ComboboxPrimitive.Clear.Props) { return ( <ComboboxPrimitive.Clear data-slot="combobox-clear" aria-label="Clear selection" render={<InputGroupButton variant="ghost" size="icon" />} {...props} className={stateClassName<ComboboxPrimitive.Clear.State>('sprout-combobox-clear', className)} > <CloseIcon aria-hidden="true" /> </ComboboxPrimitive.Clear> ); } export interface ComboboxInputProps extends Omit<ComboboxPrimitive.Input.Props, 'className' | 'size'> { className?: string; size?: InputGroupSize | null; showTrigger?: boolean; showClear?: boolean; } export function ComboboxInput({ className, children, disabled = false, showTrigger = true, showClear = false, size, ...props }: ComboboxInputProps) { return ( <InputGroup size={size} className={cn('sprout-combobox-input-group', className)}> <ComboboxPrimitive.Input render={<InputGroupInput />} disabled={disabled} {...props} /> <InputGroupAddon align="inline-end"> {showTrigger && ( <InputGroupButton size="icon" variant="ghost" render={<ComboboxTrigger />} data-slot="input-group-button" className="sprout-combobox-input-trigger" disabled={disabled} /> )} {showClear && <ComboboxClear disabled={disabled} />} </InputGroupAddon> {children} </InputGroup> ); } type PositionerProps = Pick< ComboboxPrimitive.Positioner.Props, 'side' | 'align' | 'sideOffset' | 'alignOffset' | 'anchor' >; export interface ComboboxContentProps extends ComboboxPrimitive.Popup.Props, PositionerProps {} export function ComboboxContent({ className, side = 'bottom', sideOffset = 6, align = 'start', alignOffset = 0, anchor, ...props }: ComboboxContentProps) { return ( <ComboboxPrimitive.Portal> <ComboboxPrimitive.Positioner className="sprout-combobox-positioner" side={side} sideOffset={sideOffset} align={align} alignOffset={alignOffset} anchor={anchor} collisionPadding={8} > <ComboboxPrimitive.Popup data-slot="combobox-content" data-chips={!!anchor} {...props} className={stateClassName<ComboboxPrimitive.Popup.State>('sprout-combobox-content', className)} /> </ComboboxPrimitive.Positioner> </ComboboxPrimitive.Portal> ); } export type ComboboxListProps = ComboboxPrimitive.List.Props; export function ComboboxList({ className, ...props }: ComboboxListProps) { return ( <ComboboxPrimitive.List data-slot="combobox-list" {...props} className={stateClassName<ComboboxPrimitive.List.State>('sprout-combobox-list', className)} /> ); } export type ComboboxItemProps = ComboboxPrimitive.Item.Props; export function ComboboxItem({ className, children, ...props }: ComboboxItemProps) { return ( <ComboboxPrimitive.Item data-slot="combobox-item" {...props} className={stateClassName<ComboboxPrimitive.Item.State>('sprout-combobox-item', className)} > {children} <ComboboxPrimitive.ItemIndicator className="sprout-combobox-item-indicator" render={<span />}> <CheckIcon aria-hidden="true" /> </ComboboxPrimitive.ItemIndicator> </ComboboxPrimitive.Item> ); } export type ComboboxGroupProps = ComboboxPrimitive.Group.Props; export function ComboboxGroup({ className, ...props }: ComboboxGroupProps) { return ( <ComboboxPrimitive.Group data-slot="combobox-group" {...props} className={stateClassName<ComboboxPrimitive.Group.State>('sprout-combobox-group', className)} /> ); } export type ComboboxLabelProps = ComboboxPrimitive.GroupLabel.Props; export function ComboboxLabel({ className, ...props }: ComboboxLabelProps) { return ( <ComboboxPrimitive.GroupLabel data-slot="combobox-label" {...props} className={stateClassName<ComboboxPrimitive.GroupLabel.State>('sprout-combobox-label', className)} /> ); } export type ComboboxCollectionProps = ComboboxPrimitive.Collection.Props; export function ComboboxCollection(props: ComboboxCollectionProps) { return <ComboboxPrimitive.Collection data-slot="combobox-collection" {...props} />; } export type ComboboxEmptyProps = ComboboxPrimitive.Empty.Props; export function ComboboxEmpty({ className, ...props }: ComboboxEmptyProps) { return ( <ComboboxPrimitive.Empty data-slot="combobox-empty" {...props} className={stateClassName<ComboboxPrimitive.Empty.State>('sprout-combobox-empty', className)} /> ); } export type ComboboxSeparatorProps = ComboboxPrimitive.Separator.Props; export function ComboboxSeparator({ className, ...props }: ComboboxSeparatorProps) { return ( <ComboboxPrimitive.Separator data-slot="combobox-separator" {...props} className={stateClassName<ComboboxPrimitive.Separator.State>('sprout-combobox-separator', className)} /> ); } export interface ComboboxChipsProps extends ComboboxPrimitive.Chips.Props { size?: InputGroupSize | null; } export function ComboboxChips({ className, size, ...props }: ComboboxChipsProps) { const scale = size ?? 'md'; return ( <ComboboxPrimitive.Chips data-slot="combobox-chips" data-size={scale} {...props} className={stateClassName<ComboboxPrimitive.Chips.State>( cn('sprout-combobox-chips', `sprout-combobox-chips-size-${scale}`), className, )} /> ); } export interface ComboboxChipProps extends ComboboxPrimitive.Chip.Props { showRemove?: boolean; } export function ComboboxChip({ className, children, showRemove = true, ...props }: ComboboxChipProps) { return ( <ComboboxPrimitive.Chip data-slot="combobox-chip" {...props} className={stateClassName<ComboboxPrimitive.Chip.State>('sprout-combobox-chip', className)} > {children} {showRemove && ( <ComboboxPrimitive.ChipRemove aria-label={typeof children === 'string' ? `Remove ${children}` : 'Remove item'} render={<Button variant="ghost" size="icon-xs" />} className="sprout-combobox-chip-remove" data-slot="combobox-chip-remove" > <CloseIcon aria-hidden="true" /> </ComboboxPrimitive.ChipRemove> )} </ComboboxPrimitive.Chip> ); } export type ComboboxChipsInputProps = ComboboxPrimitive.Input.Props; export function ComboboxChipsInput({ className, ...props }: ComboboxChipsInputProps) { return ( <ComboboxPrimitive.Input data-slot="combobox-chip-input" {...props} className={stateClassName<ComboboxPrimitive.Input.State>('sprout-combobox-chips-input', className)} /> ); } export function useComboboxAnchor() { return useRef<HTMLDivElement | null>(null); }And the files it imports:
lib/sprout/icons/check-icon.tsxlib/sprout/icons/chevron-down-icon.tsxlib/sprout/icons/close-icon.tsxlib/sprout/icons/create-icon.tsx
Add the styles to your global CSS, after
@import "tailwindcss".@layer components { .sprout-combobox-trigger { font-family: inherit; } .sprout-combobox-trigger svg { pointer-events: none; flex-shrink: 0; } .sprout-combobox-trigger svg:not([class*="size-"]) { width: 1rem; height: 1rem; } .sprout-combobox-trigger-icon { color: var(--sprout-muted); } .sprout-combobox-trigger:has( > :not(svg)) .sprout-combobox-trigger-icon { margin-inline-start: auto; } .sprout-combobox-input-group .sprout-button.sprout-combobox-input-trigger[data-pressed], .sprout-combobox-input-group .sprout-button.sprout-combobox-input-trigger[data-popup-open] { background-color: #0000; } .sprout-combobox-input-group:has([data-slot="combobox-clear"]) .sprout-combobox-input-trigger { display: none; } .sprout-combobox-positioner { z-index: 50; isolation: isolate; outline: none; } .sprout-combobox-content { box-sizing: border-box; width: var(--anchor-width); min-width: calc(var(--anchor-width) + 28px); max-width: var(--available-width); max-height: var(--available-height); border: 1px solid var(--sprout-line); border-radius: var(--sprout-radius-panel); background: var(--sprout-popover); color: var(--sprout-text); box-shadow: var(--sprout-shadow-overlay); font-family: var(--sprout-font); font-size: var(--sprout-text-sm); transform-origin: var(--transform-origin); outline: none; line-height: 1.25rem; position: relative; overflow: hidden; } .sprout-combobox-content[data-chips="true"] { min-width: var(--anchor-width); } .sprout-combobox-content > .sprout-input-group { background-color: var(--sprout-canvas); width: auto; height: 32px; box-shadow: none; margin: 4px 4px 0; } .sprout-combobox-list { box-sizing: border-box; max-height: min(252px, calc(var(--available-height) - 36px)); overscroll-behavior: contain; scrollbar-width: thin; outline: none; padding: 4px; scroll-padding-block: 4px; overflow-y: auto; } .sprout-combobox-list[data-empty] { padding: 0; } .sprout-combobox-group { scroll-margin-block: 4px; } .sprout-combobox-label { color: var(--sprout-muted); font-size: var(--sprout-text-xs); overflow-wrap: break-word; padding: 6px 8px 4px; font-weight: 500; line-height: 1rem; } .sprout-combobox-item { box-sizing: border-box; width: 100%; min-width: 0; min-height: calc(var(--sprout-control-height) - 4px); border-radius: var(--sprout-radius-control); color: var(--sprout-text); overflow-wrap: anywhere; text-align: start; cursor: pointer; user-select: none; -webkit-tap-highlight-color: transparent; outline: none; align-items: center; gap: 8px; padding: 4px 32px 4px 6px; scroll-margin-block: 4px; display: flex; position: relative; } .sprout-combobox-item[data-highlighted] { background: var(--sprout-hover); } .sprout-combobox-item[data-selected] { font-weight: 500; } .sprout-combobox-item[data-disabled] { opacity: .5; cursor: not-allowed; pointer-events: none; } .sprout-combobox-item svg { pointer-events: none; flex-shrink: 0; } .sprout-combobox-item svg:not([class*="size-"]) { width: 16px; height: 16px; } .sprout-combobox-item > svg { color: var(--sprout-muted); } .sprout-combobox-item-indicator { width: 16px; height: 16px; color: var(--sprout-text); pointer-events: none; justify-content: center; align-items: center; display: flex; position: absolute; inset-inline-end: 8px; } .sprout-combobox-empty { color: var(--sprout-muted); font-size: var(--sprout-text-sm); text-align: center; justify-content: center; padding: 8px 12px; display: flex; } .sprout-combobox-empty:empty { padding: 0; } .sprout-combobox-separator { background: var(--sprout-line); pointer-events: none; height: 1px; margin: 4px -4px; } .sprout-combobox-chips { box-sizing: border-box; width: 100%; min-width: 0; min-height: var(--sprout-control-height); border: 1px solid var(--sprout-input); border-radius: var(--sprout-radius-control); background-color: var(--sprout-surface); color: var(--sprout-text); font-size: var(--sprout-text-sm); outline-offset: 0; cursor: text; outline: 0 solid #0000; flex-wrap: wrap; align-items: center; gap: 4px; padding: 4px 10px; line-height: 20px; display: flex; } .sprout-combobox-chips-size-sm { min-height: calc(var(--sprout-control-height) - 4px); border-radius: min(var(--sprout-radius-control), 12px); font-size: var(--sprout-text-xs); padding-block: 2px; line-height: 16px; } .sprout-combobox-chips-size-lg { min-height: calc(var(--sprout-control-height) + 4px); padding-block: 6px; } .sprout-combobox-chips:has([data-slot="combobox-chip"]) { padding-inline: 4px; } .sprout-combobox-chips:focus-within { border-color: var(--sprout-border); outline: var(--sprout-focus-width) solid transparent; } .sprout-combobox-chips:focus-within:has(:where(.sprout-combobox-chips-input:focus-visible)) { outline-color: var(--sprout-focus-ring); } .sprout-combobox-chips:has([aria-invalid="true"]), .sprout-combobox-chips:has([data-invalid]) { border-color: var(--sprout-danger); outline-color: var(--sprout-danger); } .sprout-combobox-chips:has(.sprout-combobox-chips-input:disabled) { background-color: var(--sprout-hover); opacity: .5; cursor: not-allowed; } .sprout-combobox-chip { box-sizing: border-box; border-radius: max(calc(var(--sprout-radius-control) - 2px), 2px); background: var(--sprout-secondary); max-width: 100%; height: 22px; color: var(--sprout-text); font-size: var(--sprout-text-xs); white-space: nowrap; outline: none; align-items: center; gap: 2px; padding: 0 6px; font-weight: 500; line-height: 16px; display: inline-flex; } .sprout-combobox-chip:has(.sprout-combobox-chip-remove) { padding-inline-end: 1px; } .sprout-combobox-chip:focus-visible, .sprout-combobox-chip[data-highlighted] { outline: var(--sprout-focus-width) solid var(--sprout-focus-ring); outline-offset: 0; } .sprout-combobox-chip:has(:disabled), .sprout-combobox-chip[data-disabled] { opacity: .5; cursor: not-allowed; pointer-events: none; } .sprout-combobox-chips:has(.sprout-combobox-chips-input:disabled) .sprout-combobox-chip { opacity: 1; } .sprout-button.sprout-combobox-chip-remove { --sprout-button-icon: .75rem; width: 20px; min-width: 20px; height: 20px; min-height: 20px; color: var(--sprout-text); opacity: .55; } .sprout-button.sprout-combobox-chip-remove:focus-visible { opacity: 1; } @media (hover: hover) { .sprout-button.sprout-combobox-chip-remove:hover { opacity: 1; } } .sprout-combobox-chips-input { min-width: min(6rem, 100%); max-width: 100%; height: 22px; color: var(--sprout-text); font: inherit; background: none; border: 0; outline: none; flex: 10rem; margin: 0; padding: 0 2px; } @supports (field-sizing: content) { .sprout-combobox-chips-input { field-sizing: content; flex-basis: auto; } } .sprout-combobox-chips-input::placeholder { color: var(--sprout-muted); opacity: 1; } .sprout-combobox-chips-input:disabled { cursor: not-allowed; } @media (pointer: coarse) { .sprout-combobox-item { min-height: 2.75rem; } .sprout-combobox-input-group { height: 2.75rem; } .sprout-combobox-chips { min-height: 2.75rem; } .sprout-combobox-input-group .sprout-button { min-width: 2.25rem; min-height: 2.25rem; } .sprout-combobox-chip { height: 28px; } .sprout-button.sprout-combobox-chip-remove { width: 26px; min-width: 26px; height: 26px; min-height: 26px; } } @media (forced-colors: active) { .sprout-combobox-item[data-highlighted] { outline-offset: -1px; outline: 2px solid highlight; } .sprout-combobox-chips:focus-within { outline-color: highlight; } .sprout-combobox-chip { border: 1px solid canvastext; } } @media (prefers-reduced-motion: no-preference) { .sprout-combobox-content { transition: opacity var(--sprout-motion-fast) var(--sprout-motion-ease), scale var(--sprout-motion-fast) var(--sprout-motion-ease), translate var(--sprout-motion-fast) var(--sprout-motion-ease); } .sprout-combobox-content[data-starting-style], .sprout-combobox-content[data-ending-style] { opacity: 0; scale: .97; } .sprout-combobox-content[data-side="bottom"][data-starting-style] { translate: 0 -4px; } .sprout-combobox-content[data-side="top"][data-starting-style] { translate: 0 4px; } .sprout-combobox-item { transition: background-color var(--sprout-motion-fast) ease-out; } .sprout-combobox-chips { transition: border-color var(--sprout-motion-fast) ease-out, outline-color var(--sprout-motion-fast) ease-out; } .sprout-combobox-chip, .sprout-button.sprout-combobox-chip-remove { transition: opacity var(--sprout-motion-fast) ease-out, background-color var(--sprout-motion-fast) ease-out; } } }Import it from
@/components/ui/combobox. Update the paths if your@/alias points elsewhere.
pnpm add @tulipfarm/sprout@import "tailwindcss";
@import "@tulipfarm/sprout/styles.css";The package keeps the source and ships fixes with each release. Import from @tulipfarm/sprout/combobox.
Priya Raman · #launches, #support-escalations
Usage
import {
Combobox,
ComboboxContent,
ComboboxEmpty,
ComboboxInput,
ComboboxItem,
ComboboxList,
} from "@tulipfarm/sprout/combobox";
const fruits = ["Apple", "Apricot", "Blueberry", "Cherry", "Pear"];
<Combobox items={fruits}>
<ComboboxInput aria-label="Choose a fruit" placeholder="Choose a fruit" showClear />
<ComboboxContent>
<ComboboxEmpty>No fruit found.</ComboboxEmpty>
<ComboboxList>
{(item) => (
<ComboboxItem key={item} value={item}>
{item}
</ComboboxItem>
)}
</ComboboxList>
</ComboboxContent>
</Combobox>Pass items to Combobox and a function to ComboboxList. The list filters as you type, and ComboboxEmpty shows when nothing matches. For objects, pass itemToStringLabel andisItemEqualToValue.
Single, multiple, and disabled
These are the three forms TulipFarm uses: a single choice with a clear button, many choices as chips with a starting value, and a disabled field.
Input options
ComboboxInput shows a chevron by default. Turn it off with showTrigger={false} so the list opens on typing or ↓. showClear adds a clear button that replaces the chevron once there is a value. Children are added to the group, such as a leading icon.
Chips
With multiple, render ComboboxChips from ComboboxValue inside ComboboxChips, then a ComboboxChipsInput. Pass a ref from useComboboxAnchor to the chips and to ComboboxContent’s anchor so the list matches the field. Set showRemove={false} on a chip that must stay. Chips wrap when they don’t fit.
const anchor = useComboboxAnchor();
<Combobox items={channels} multiple defaultValue={["launches"]}>
<ComboboxChips ref={anchor}>
<ComboboxValue>
{(values: string[]) =>
values.map((value) => <ComboboxChip key={value}>{value}</ComboboxChip>)
}
</ComboboxValue>
<ComboboxChipsInput aria-label="Alert channels" placeholder="Add channel" />
</ComboboxChips>
<ComboboxContent anchor={anchor}>
<ComboboxEmpty>No channel found.</ComboboxEmpty>
<ComboboxList>
{(channel) => <ComboboxItem key={channel} value={channel}>{channel}</ComboboxItem>}
</ComboboxList>
</ComboboxContent>
</Combobox>Trigger button
ComboboxTrigger can be the whole control. Render it as a Button, show ComboboxValue inside, and put a ComboboxInput with showTrigger={false} at the top of the list. With no children it is an icon button named “Show options”.
Groups and separators
Pass groups as items. Render each one as a ComboboxGroup with its own items, a ComboboxLabel, and aComboboxCollection. Put a ComboboxSeparatorbetween groups.
States
While items load, pass an empty list, set aria-busy on the input, and say so in ComboboxEmpty. Set aria-invalid and point aria-describedby at the error. Disabled items can be reached with the arrow keys but can’t be chosen. A disabled combobox can’t be focused.
Loading tables…
No tables yet. Create one in Tables first.
Locked while the workflow runs.
API
Combobox is the Base UI combobox root. It takes items, multiple, value, defaultValue,onValueChange, itemToStringLabel, disabled, name, and every other root prop.
ComboboxInput takes showTrigger (true), showClear (false), disabled, and every input prop. Its className goes on the InputGroup.
ComboboxContent renders its own portal and positioner. It takesside (bottom), align (start), sideOffset (6), alignOffset (0), and anchor.
ComboboxChip takes showRemove (true). ComboboxTrigger, ComboboxValue, ComboboxList, ComboboxItem, ComboboxGroup, ComboboxLabel, ComboboxCollection, ComboboxEmpty, ComboboxSeparator, ComboboxChips, and ComboboxChipsInput wrap the Base UI parts and take their props. useComboboxAnchor returns a ref for the chips. Every part sets a data-slot.
Accessibility
The input is a combobox with aria-expanded and aria-autocomplete. Typing or ↓ opens the list. Arrow keys move, Enter picks, and Escape closes and keeps focus in the input. In a chips field, ← and → move between chips and Backspace removes one. Each remove button is named “Remove” plus the chip text. ComboboxEmpty is a polite live region.
The list fades and scales in over 120ms. Motion turns off when reduced motion is requested. On touch screens items are at least 44px tall. In forced-colors mode the highlighted item gets an outline.
Theme tokens
Comboboxes use --sprout-surface, --sprout-canvas, --sprout-input, --sprout-line, --sprout-hover, --sprout-secondary, --sprout-text, --sprout-muted, --sprout-focus, --sprout-danger, --sprout-shadow-overlay, and the control and panel radii. Only the focus ring follows the brand, so they work with the neutral and TulipFarm themes in light and dark mode.