Forms

Combobox

Text input with a filtered list of suggestions — type to filter, arrows to move, Enter to pick.

react-aria-componentslucide-react

Preview

Installation

Components are copied into your project — no runtime package.

  1. Install the following dependencies:

    terminal
    npm install react-aria-components lucide-react
  2. Create a combobox.tsx file and paste the following code into it.

    src/components/ui/combobox.tsx
    "use client";
    
    import { ChevronDown } from "lucide-react";
    import type { ReactNode } from "react";
    import { Button as AriaButton, ComboBox as AriaComboBox, type ComboBoxProps as AriaComboBoxProps, Group, Input, ListBox, Popover } from "react-aria-components";
    import { cx } from "@/utils/cx";
    import { Description, ErrorMessage, Label, fieldBase } from "./field";
    import { SelectItem } from "./select";
    
    export interface ComboboxProps<T extends object> extends Omit<AriaComboBoxProps<T>, "children" | "className"> {
      label?: string;
      description?: string;
      errorMessage?: string;
      placeholder?: string;
      icon?: ReactNode;
      className?: string;
      children: ReactNode | ((item: T) => ReactNode);
    }
    
    /** Text input with a filtered list of suggestions. Type to filter, arrows to move, Enter to pick. */
    export function Combobox<T extends object>({ label, description, errorMessage, placeholder, icon, className, children, ...props }: ComboboxProps<T>) {
      return (
        <AriaComboBox {...props} isInvalid={props.isInvalid ?? Boolean(errorMessage)} className={cx("group flex flex-col gap-2", className)}>
          {label && <Label>{label}</Label>}
          <Group className={cx(fieldBase, "flex h-10 items-center gap-2 pl-3 pr-1 group-data-open:border-accent group-data-open:shadow-focus [&>svg]:size-4 [&>svg]:shrink-0 [&>svg]:text-fg-muted")}>
            {icon}
            <Input placeholder={placeholder} className="h-full min-w-0 flex-1 bg-transparent outline-none placeholder:text-fg-muted" />
            <AriaButton className="inline-flex size-8 items-center justify-center rounded-sm text-fg-muted outline-none data-hovered:bg-muted data-hovered:text-fg">
              <ChevronDown className="size-4 transition-transform group-data-open:rotate-180" aria-hidden />
            </AriaButton>
          </Group>
          {description && !errorMessage && <Description>{description}</Description>}
          <ErrorMessage>{errorMessage}</ErrorMessage>
          <Popover offset={6} className="w-(--trigger-width) rounded-md bg-surface p-1 shadow-popover outline-none">
            <ListBox
              renderEmptyState={() => <div className="px-3 py-6 text-center text-base text-fg-muted">No matches</div>}
              className="max-h-72 overflow-auto outline-none"
            >
              {children}
            </ListBox>
          </Popover>
        </AriaComboBox>
      );
    }
    
    /** Option row — same look as SelectItem. */
    export const ComboboxItem = SelectItem;
  3. Create a select.tsx file and paste the following code into it.

    src/components/ui/select.tsx
    "use client";
    
    import { Check, ChevronDown } from "lucide-react";
    import type { ReactNode } from "react";
    import {
      Button as AriaButton,
      ListBox,
      ListBoxItem,
      type ListBoxItemProps,
      Popover,
      Select as AriaSelect,
      type SelectProps as AriaSelectProps,
      SelectValue,
    } from "react-aria-components";
    import { cx } from "@/utils/cx";
    import { Description, ErrorMessage, Label, fieldBase } from "./field";
    
    export interface SelectProps<T extends object> extends Omit<AriaSelectProps<T>, "children" | "className"> {
      label?: string;
      description?: string;
      errorMessage?: string;
      items?: Iterable<T>;
      className?: string;
      children: ReactNode | ((item: T) => ReactNode);
    }
    
    /** Single-value dropdown. Trailing chevron, accent border while open. */
    export function Select<T extends object>({
      label,
      description,
      errorMessage,
      items,
      className,
      children,
      placeholder = "Select an option",
      ...props
    }: SelectProps<T>) {
      return (
        <AriaSelect {...props} placeholder={placeholder} className={cx("group flex flex-col gap-2", className)}>
          {label && <Label>{label}</Label>}
          <AriaButton
            className={cx(
              fieldBase,
              "flex h-10 cursor-default items-center gap-2 pl-3 pr-3 text-left",
              "data-focus-visible:border-accent data-focus-visible:shadow-focus group-data-open:border-accent group-data-open:shadow-focus",
            )}
          >
            <SelectValue className="flex-1 truncate data-placeholder:text-fg-muted" />
            <ChevronDown aria-hidden className="size-4 text-fg-muted transition-transform group-data-open:rotate-180" />
          </AriaButton>
          {description && !errorMessage && <Description>{description}</Description>}
          <ErrorMessage>{errorMessage}</ErrorMessage>
          <Popover
            offset={6}
            className="w-(--trigger-width) rounded-md bg-surface p-1 shadow-popover outline-none"
          >
            <ListBox items={items} className="max-h-72 overflow-auto outline-none">
              {children}
            </ListBox>
          </Popover>
        </AriaSelect>
      );
    }
    
    export function SelectItem({ className, children, ...props }: ListBoxItemProps & { children: ReactNode; className?: string }) {
      const textValue = props.textValue ?? (typeof children === "string" ? children : undefined);
      return (
        <ListBoxItem
          {...props}
          textValue={textValue}
          className={cx(
            "group/item flex h-9 cursor-default items-center gap-2 rounded-sm px-2.5 text-base text-fg outline-none",
            "data-focused:bg-muted data-disabled:text-fg-disabled",
            className,
          )}
        >
          <span className="flex-1 truncate">{children}</span>
          <Check aria-hidden className="hidden size-4 text-accent group-data-selected/item:block" />
        </ListBoxItem>
      );
    }
  4. Create a field.tsx file and paste the following code into it.

    src/components/ui/field.tsx
    "use client";
    
    import type { ReactNode } from "react";
    import { FieldError, Label as AriaLabel, Text } from "react-aria-components";
    import { cx } from "@/utils/cx";
    
    /** Shared field chrome: 40px tall, radius 12, 1px border-strong, accent border + 3px ring on focus. */
    export const fieldBase =
      "w-full rounded-md border border-line-strong bg-surface text-base text-fg outline-none transition-[border,box-shadow] " +
      "placeholder:text-fg-muted data-hovered:border-fg-disabled " +
      "data-focused:border-accent data-focused:shadow-focus data-focus-within:border-accent data-focus-within:shadow-focus " +
      "data-invalid:border-error data-invalid:data-focused:shadow-focus-error " +
      "data-disabled:border-line data-disabled:bg-subtle data-disabled:text-fg-disabled data-disabled:placeholder:text-fg-disabled";
    
    export function Label({ className, children }: { className?: string; children: ReactNode }) {
      return <AriaLabel className={cx("text-sm font-medium text-fg", className)}>{children}</AriaLabel>;
    }
    
    export function Description({ children }: { children: ReactNode }) {
      return (
        <Text slot="description" className="text-sm text-fg-muted">
          {children}
        </Text>
      );
    }
    
    export function ErrorMessage({ children }: { children?: ReactNode }) {
      return <FieldError className="text-sm text-error-text">{children}</FieldError>;
    }
  5. Add the theme tokens and the cx helper from Installation if you haven’t yet, then update the import paths to match your project setup.

Props

PropTypeDefaultDescription
labelstring—Visible label.
iconReactNode—Leading icon.
allowsCustomValueboolean—Keep text that matches no option.
onSelectionChange(key: Key) => void—Called with the picked item id.