shadcn.io is not affiliated with official shadcn/ui
Installation
bunx --bun shadcn@latest add https://kit.dev/r/base/combobox.jsonnpx shadcn@latest add https://kit.dev/r/base/combobox.jsonpnpm dlx shadcn@latest add https://kit.dev/r/base/combobox.jsonyarn shadcn@latest add https://kit.dev/r/base/combobox.jsonThis component depends on Button and Input Group. Install them first if you haven't already.
Install the following dependencies:
bun add @base-ui/react lucide-reactnpm install @base-ui/react lucide-reactpnpm add @base-ui/react lucide-reactyarn add @base-ui/react lucide-reactCopy and paste the following code into your project.
"use client";
import { Combobox as ComboboxPrimitive } from "@base-ui/react";
import { CheckIcon, ChevronDownIcon, XIcon } from "lucide-react";
import React from "react";
import { cn } from "@/lib/utils";
import { Button } from "@/registry/base/components/button";
import {
InputGroup,
InputGroupAddon,
InputGroupButton,
InputGroupInput,
} from "@/registry/base/components/input-group";
export const Combobox = ComboboxPrimitive.Root;
export const ComboboxValue = (props: ComboboxPrimitive.Value.Props) => (
<ComboboxPrimitive.Value data-slot="combobox-value" {...props} />Update the import paths to match your project setup.
Anatomy
ComboboxRoot
├── ComboboxClear
├── ComboboxContent
├── ComboboxControl
├── ComboboxFieldInput
├── ComboboxItem
│ ├── ComboboxGroup
│ │ └── ComboboxGroupLabel
│ ├── ComboboxItemIndicator
│ └── ComboboxItemText
├── ComboboxLabel
├── ComboboxList
├── ComboboxPositioner
└── ComboboxTriggerUsage
import { useListCollection } from "@ark-ui/react/collection";
import { useFilter } from "@ark-ui/react/locale";
import {
Combobox,
ComboboxContent,
ComboboxEmpty,
ComboboxInput,
ComboboxItem,
ComboboxList,
} from "@/components/ui/combobox";const frameworks = ["Next.js", "SvelteKit", "Nuxt.js", "Remix", "Astro"];
const ComboboxDemo = () => {
const { contains } = useFilter({ sensitivity: "base" });
const { collection, filter } = useListCollection({
filter: contains,
initialItems: frameworks,
});
const handleInputValueChange = ({ inputValue }: { inputValue: string }) => {
filter(inputValue);
};
return (
<Combobox
collection={collection}
onInputValueChange={handleInputValueChange}
>
<ComboboxInput placeholder="Select a framework" />
<ComboboxContent>
<ComboboxEmpty>No items found.</ComboboxEmpty>
<ComboboxList>
{collection.items.map((item) => (
<ComboboxItem item={item} key={item}>
{item}
</ComboboxItem>
))}
</ComboboxList>
</ComboboxContent>
</Combobox>
);
};Accessibility
| Key | Description |
|---|---|
ArrowDown | Move down. |
ArrowUp | Move up. |
Home | Go to the start. |
End | Go to the end. |
Enter | Activate the focused item. |
Escape | Dismiss. |
Space | Activate the focused control. |
Demos
Basic
A simple combobox with a list of frameworks.
Multiple
A combobox with multiple selection using multiple and ComboboxChips.
Clear
Use the showClear prop to show a clear button.
Groups
Use ComboboxGroup and ComboboxSeparator to group items.
Custom
You can render a custom component inside ComboboxItem.
Invalid
Use the aria-invalid prop to make the combobox invalid.
Disabled
Use the disabled prop to disable the combobox.
Auto Highlight
Use inputBehavior="autohighlight" to automatically highlight the first item on filter.
Popup
Trigger the combobox from a button. Move ComboboxInput inside ComboboxContent.
Input Group
Add an addon to the combobox with InputGroupAddon inside ComboboxInput.
Rtl
Right-to-left combobox. See the RTL configuration guide for document direction.
API Reference
Combobox
PropType
ComboboxValue
PropType
AttributeType
ComboboxTrigger
PropType
AttributeType
ComboboxClear
PropType
AttributeType
ComboboxInput
PropType
ComboboxContent
PropType
AttributeType
data-chips?stringCSS variableType
--anchor-widthstring--available-heightstring--available-widthstring--transform-originstringComboboxList
PropType
AttributeType
ComboboxItem
PropType
AttributeType
ComboboxGroup
PropType
AttributeType
ComboboxLabel
PropType
AttributeType
ComboboxCollection
PropType
AttributeType
ComboboxEmpty
PropType
AttributeType
ComboboxSeparator
PropType
AttributeType
ComboboxChips
PropType
AttributeType
ComboboxChip
PropType
AttributeType
ComboboxChipsInput
PropType
AttributeType