# Command palette

> ⌘K that grows out of the search pill: fuzzy search, nested lists, and commands that run in place.

Most command palettes appear from nowhere in the middle of the screen. This one grows out of the "Search… ⌘K" pill that was already on the page: its position, size and corners ease from pill to panel while the pill’s words fade into the field, and closing folds it back. Results narrow as you type with the matching letters marked, the panel’s height eases to fit them, and a highlight glides between rows. "Change theme…" opens its own list in place, sliding in from the right as a chip joins the field. A command that returns a promise runs on its row, its shortcut blurring into a spinner and then a check that draws itself, before the palette folds away.

## Install

```bash
npx shadcn@latest add https://hairlineui.com/r/command-palette.json
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: none
- Hairline UI components it needs (the CLI installs them too): `text-morph`
- Docs and live preview: https://hairlineui.com/ui/command-palette
- Registry item (source): https://hairlineui.com/r/command-palette.json

## Usage

```tsx
import { CommandPalette, type CommandItem } from "@/components/command-palette";

const items: CommandItem[] = [
  { id: "orders", group: "Go to", label: "Orders", href: "/orders", shortcut: ["G", "O"] },
  { id: "export", group: "Actions", label: "Export sales", onSelect: () => exportCsv() },
  { id: "theme", group: "Actions", label: "Change theme", items: [
    { id: "light", label: "Light", onSelect: () => setTheme("light") },
    { id: "dark", label: "Dark", onSelect: () => setTheme("dark") },
  ] },
];

<CommandPalette items={items} />
```

## In a navbar

Put the pill where people already look for search; ⌘K works from anywhere on the page.

```tsx
"use client";
import { useRouter } from "next/navigation";
import { CommandPalette, type CommandItem } from "@/components/command-palette";

export function NavSearch({ docs }: { docs: { slug: string; title: string; section: string }[] }) {
  const router = useRouter();
  const items: CommandItem[] = docs.map((d) => ({
    id: d.slug,
    group: d.section,
    label: d.title,
    onSelect: () => router.push(`/docs/${d.slug}`),
  }));

  return <CommandPalette items={items} placeholder="Search the docs…" />;
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `items` | `CommandItem[]` | – | id, label, and any of group, icon, shortcut, keywords, description, href, onSelect (return a promise to show progress) and items (a nested list). |
| `placeholder` | `string` | `"Search or jump to…"` | The field’s placeholder on the first page. |
| `hotkey` | `string` | `"k"` | The letter that toggles it with ⌘ or Ctrl; an empty string turns it off. |
| `open / defaultOpen / onOpenChange` | `boolean / boolean / (open) => void` | – | Control it, or let it manage itself. |
| `showTrigger / triggerLabel` | `boolean / string` | `true / "Search…"` | The pill it grows from, and its words. |

## States

- **closed**: A hairline pill with "Search…" and its shortcut, ⌘K on a Mac and Ctrl K elsewhere.
- **opening**: The pill grows into the panel over 440ms: left, top, width, height and corner radius ease together while a faint veil fades over the page. Opened by the hotkey with the pill off-screen, it rises in from just above.
- **search**: Fuzzy matching ranks whole words first, then word starts, then letters in order; matched letters are set in the full text colour. The list eases to its new height (300ms) and the highlight glides (200ms).
- **nested**: A row with items opens its list in place: the old list slides out to the left, the new one in from the right through a 4px blur, and a chip with its name opens in the field. Backspace on an empty field or Escape goes back the other way.
- **running**: A command that returns a promise keeps the palette open: the shortcut blurs into a spinner, then a check draws itself and the palette folds back into the pill 520ms later. A rejected promise shakes the row.
- **empty**: "No results for “xyz”", morphing letter by letter as the query changes.

## What it handles

Most command palettes appear from nowhere in the middle of the screen. This one grows out of the search pill already on the page, and handles the ranking, the nesting, the shortcuts and the commands that take time.

- **It grows from your search pill**: Position, size and corners ease from pill to panel, and closing folds it back.
- **⌘K from anywhere**: The hotkey toggles it: ⌘K on a Mac and Ctrl K elsewhere. Change the letter or turn it off.
- **Fuzzy search with marked letters**: Whole words rank first, then word starts, then letters in order, and the matching letters are marked.
- **Lists inside lists**: “Change theme…” opens its own list in place. Backspace or Escape goes back a level.
- **Commands that take time**: A command that returns a promise shows a spinner, then a drawn check, before the palette folds away.
- **Never clipped**: It is rendered into the page body, so no overflow or transform on a parent can cut it off.

## Replaces

a command menu library, a dialog, fuzzy search, a global hotkey, nested lists, loading states for commands

## Where it fits

- **SaaS dashboards**: Jump to any page, record or setting from the keyboard.
- **Documentation sites**: A ⌘K search that opens from the pill in the header.
- **Marketing site headers**: A search pill that grows into results for pages and links.
- **Editors and admin tools**: Run commands such as “Change theme” or “Create invoice” without hunting through menus.

## Notes

- A modal dialog: focus moves into the field and back to whatever had it before; the page behind stops scrolling. The field is a combobox with aria-activedescendant on a listbox of options grouped under their headings.
- Arrow keys move, Home and End jump (on an empty field), Enter runs, Escape clears the field, then goes back a level, then closes. Tab stays in the field.
- The palette is rendered into the body, so a transformed or overflow-hidden ancestor can’t clip it.
- Installs Text morph. With reduced motion it opens and closes in place and pages change without sliding.

License: MIT (https://hairlineui.com/license#components)
