Skip to content
Primitives

Combobox

Search and select one option with Combobox or toggle several with MultiCombobox.

MotionUses Kineto’s animated Popover.

Install

Install Kineto and follow the theme setup once in your app.

bash
npm install kineto
tsx
import { Combobox, MultiCombobox } from "kineto/combobox";

Preview

tsx
"use client";

import * as React from "react";
import { Check, ChevronsUpDown } from "lucide-react";

import { Button } from "@/components/ui/button";
import { Command, CommandEmpty, CommandInput, CommandItem, CommandList } from "@/components/ui/command";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
import { cn } from "@/lib/cn";

export interface ComboboxOption {
  value: string;
  label: string;
  keywords?: string[];
  disabled?: boolean;
}

interface ComboboxBaseProps extends React.AriaAttributes {
  options: readonly ComboboxOption[];
  id?: string;
  className?: string;
  disabled?: boolean;
  placeholder?: string;
  searchPlaceholder?: string;
  emptyMessage?: string;
}

export interface ComboboxProps extends ComboboxBaseProps {
  value: string | null;
  onValueChange: (value: string) => void;
}

export interface MultiComboboxProps extends ComboboxBaseProps {
  value: readonly string[];
  onValueChange: (value: string[]) => void;
}

interface ComboboxPickerProps extends ComboboxBaseProps {
  selectedValues: readonly string[];
  onSelect: (value: string) => void;
  multiple?: boolean;
}

function ComboboxPicker({
  options,
  selectedValues,
  onSelect,
  multiple = false,
  className,
  disabled,
  placeholder = "Select an option",
  searchPlaceholder = "Search options…",
  emptyMessage = "No options found.",
  ...triggerProps
}: ComboboxPickerProps) {
  const [open, setOpen] = React.useState(false);
  const searchRef = React.useRef<HTMLInputElement>(null);
  const selected = new Set(selectedValues);
  const summary = options
    .filter((option) => selected.has(option.value))
    .map((option) => option.label)
    .join(", ");

  return (
    <Popover open={open && !disabled} onOpenChange={setOpen}>
      <PopoverTrigger asChild>
        <Button
          {...triggerProps}
          type="button"
          variant="outline"
          disabled={disabled}
          className={cn("w-full justify-between px-3.5 text-left font-normal", className)}
        >
          <span className={cn("truncate", !summary && "text-muted-foreground")}>
            {summary || placeholder}
          </span>
          <ChevronsUpDown aria-hidden className="text-muted-foreground" />
        </Button>
      </PopoverTrigger>
      <PopoverContent
        align="start"
        aria-label={searchPlaceholder}
        className="w-[var(--radix-popover-trigger-width)] min-w-48 max-w-[var(--radix-popover-content-available-width)] p-0"
        onOpenAutoFocus={(event) => {
          event.preventDefault();
          searchRef.current?.focus();
        }}
      >
        <Command>
          <CommandInput ref={searchRef} placeholder={searchPlaceholder} aria-label={searchPlaceholder} />
          <CommandList className="max-h-[min(20rem,var(--radix-popover-content-available-height))]">
            <CommandEmpty>{emptyMessage}</CommandEmpty>
            {options.map(({ value, label, keywords, disabled: optionDisabled }) => {
              const checked = selected.has(value);
              return (
                <CommandItem
                  key={value}
                  value={value}
                  keywords={[label, ...(keywords ?? [])]}
                  disabled={optionDisabled}
                  aria-checked={checked}
                  onSelect={() => {
                    onSelect(value);
                    if (!multiple) setOpen(false);
                  }}
                >
                  <Check aria-hidden className={cn(!checked && "invisible")} />
                  <span className="min-w-0 break-words">{label}</span>
                </CommandItem>
              );
            })}
          </CommandList>
        </Command>
      </PopoverContent>
    </Popover>
  );
}

export function Combobox({ value, onValueChange, ...props }: ComboboxProps) {
  return (
    <ComboboxPicker
      {...props}
      selectedValues={value === null ? [] : [value]}
      onSelect={onValueChange}
    />
  );
}

export function MultiCombobox({ value, onValueChange, ...props }: MultiComboboxProps) {
  return (
    <ComboboxPicker
      {...props}
      multiple
      selectedValues={value}
      onSelect={(next) =>
        onValueChange(value.includes(next) ? value.filter((item) => item !== next) : [...value, next])
      }
    />
  );
}

src/components/ui/combobox.tsx

Props

PropTypeDefaultDescription
optionsreadonly ComboboxOption[]—Options with unique values, labels, optional search keywords, and a disabled state.
valuestring | null (Combobox), readonly string[] (MultiCombobox)—Controlled selection.
onValueChange(value) => void—Receives the selected value or the updated list.
disabledboolean—Disables the picker.
placeholderstring—Trigger text when the selection is empty.
searchPlaceholderstring—Search input placeholder and accessible name.
emptyMessagestring—Message shown when the search has no matches.