Command palette
⌘K search & actions. 2 states.
import * as React from "react";
import { Button } from "@/components/ui/button";
import { CommandPalette } from "@/components/ui/command-palette";
import { Inbox, Search, Zap } from "@/components/ui/icons";
import { Kbd } from "@/components/ui/kbd";
export default function Example() {
const [open, setOpen] = React.useState(false);
const [last, setLast] = React.useState("");
return (
<div style={{ display: "grid", gap: 10, justifyItems: "center" }}>
<Button variant="soft" onClick={() => setOpen(true)}>Search <Kbd size="sm">⌘K</Kbd></Button>
{last ? <span className="text-body-small text-fg-secondary">You picked: {last}</span> : null}
<CommandPalette
open={open}
onClose={() => setOpen(false)}
groups={[
{ heading: "Go to", items: [{ id: "orders", label: "Orders", icon: <Inbox />, onSelect: () => setLast("Orders") }, { id: "search", label: "Find a tea", icon: <Search />, shortcut: ["⌘", "F"], onSelect: () => setLast("Find a tea") }] },
{ heading: "Actions", items: [{ id: "new", label: "New order", icon: <Zap />, shortcut: ["⌘", "N"], onSelect: () => setLast("New order") }] },
]}
/>
</div>
);
}
Installation
$ npx uix-ui@latest add command-paletteThis copies the file into components/ui/command-palette.tsx in your project. npx shadcn@latest add @uix/command-palette works too.
Usage
import { CommandPalette } from "@/components/ui/command-palette";
<div style={{ display: "grid", gap: 10, justifyItems: "center" }}>
<Button variant="soft" onClick={() => setOpen(true)}>Search <Kbd size="sm">⌘K</Kbd></Button>
{last ? <span className="text-body-small text-fg-secondary">You picked: {last}</span> : null}
<CommandPalette
open={open}
onClose={() => setOpen(false)}
groups={[
{ heading: "Go to", items: [{ id: "orders", label: "Orders", icon: <Inbox />, onSelect: () => setLast("Orders") }, { id: "search", label: "Find a tea", icon: <Search />, shortcut: ["⌘", "F"], onSelect: () => setLast("Find a tea") }] },
{ heading: "Actions", items: [{ id: "new", label: "New order", icon: <Zap />, shortcut: ["⌘", "N"], onSelect: () => setLast("New order") }] },
]}
/>
</div>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
open | boolean | – | |
onClose | () => void | – | |
groups | CommandGroup[] | – | |
placeholder | string | "Type a command or search…" | |
emptyText | string | "No results found." | Shown when nothing matches. |
showFooter | boolean | true | Hint bar under the list. |
Everything else is passed to the root element, so className, aria-* and event handlers work as usual.
Dependencies
Installed for you by the CLI.
class-variance-authorityclsxtailwind-merge
lib/utils.tscomponents/ui/icons.tsx