# Command palette (命令面板)

Keyboard-first ⌘K palette on shadcn Command: grouped results, shortcut keys, loading, empty and error states.

- Ref: `component/command-palette` (component · command)
- Page: https://uifino.com/en/components/command/command-palette
- Markdown: https://uifino.com/md/components/command-palette.md
- Preview image: https://uifino.com/blocks/covers/command-palette-dark.webp

## Install

- shadcn: `npx shadcn@latest add "https://uifino.com/r/blocks/command-palette.json"`
- UIFino CLI: `npx uifino@latest add component/command-palette`

## Dependencies

`lucide-react`

## shadcn/ui components it builds on

- `button`, `command`, `kbd`, `separator`, `skeleton`, `spinner`. The shadcn CLI installs them together with the block.

## Layout borrowed from

- https://www.raycast.com: Footer with open / actions hints

## Theme

The block reads the project's shadcn theme variables, so it follows whatever DESIGN.md the project uses and needs no options of its own.
To give the project the look of a UIFino brand or style, install its theme: `npx shadcn@latest add "https://uifino.com/r/themes/vercel.json"`.

## Usage

The files under `examples/` show the block wired with content, including its loading, empty and error states where it has them.

## Files

| Path | Role | Size |
| :--- | :--- | ---: |
| components/blocks/command-palette/command-palette.tsx | source | 5.7 KB |
| components/blocks/command-palette/examples/content.ts | demo | 3.1 KB |
| components/blocks/command-palette/examples/default.tsx | demo | 414 B |
| components/blocks/command-palette/examples/empty.tsx | demo | 441 B |
| components/blocks/command-palette/examples/error.tsx | demo | 429 B |
| components/blocks/command-palette/examples/loading.tsx | demo | 431 B |

### components/blocks/command-palette/command-palette.tsx

```tsx
"use client";

import { Fragment, useState } from "react";
import { CornerDownLeft, RotateCw, type LucideIcon } from "lucide-react";
import { Button } from "@/components/ui/button";
import {
  Command,
  CommandEmpty,
  CommandGroup,
  CommandInput,
  CommandItem,
  CommandList,
  CommandSeparator
} from "@/components/ui/command";
import { Kbd, KbdGroup } from "@/components/ui/kbd";
import { Separator } from "@/components/ui/separator";
import { Skeleton } from "@/components/ui/skeleton";
import { Spinner } from "@/components/ui/spinner";
import { cn } from "@/lib/utils";

export interface CommandPaletteItem {
  readonly id: string;
  readonly label: string;
  readonly hint?: string;
  readonly icon: LucideIcon;
  readonly shortcut?: readonly string[];
  readonly keywords?: readonly string[];
}

export interface CommandPaletteGroup {
  readonly heading: string;
  readonly items: readonly CommandPaletteItem[];
}

export interface CommandPaletteLabels {
  readonly placeholder: string;
  readonly empty: (query: string) => string;
  readonly emptyAction: string;
  readonly open: string;
  readonly actions: string;
  readonly error: string;
  readonly retry: string;
}

export interface CommandPaletteProps {
  readonly groups: readonly CommandPaletteGroup[];
  readonly labels: CommandPaletteLabels;
  readonly context: string;
  readonly modKey?: string;
  readonly status?: "ready" | "loading" | "error";
  readonly defaultQuery?: string;
  readonly onSelect?: (id: string) => void;
  readonly onRetry?: () => void;
  readonly onEmptyAction?: (query: string) => void;
  readonly className?: string;
}

const LOADING_ROW_WIDTHS = ["w-2/5", "w-3/5", "w-1/3", "w-1/2", "w-2/5"];

export function CommandPalette({
  groups,
  labels,
  context,
  modKey = "⌘",
  status = "ready",
  defaultQuery = "",
  onSelect,
  onRetry,
  onEmptyAction,
  className
}: CommandPaletteProps) {
  const [query, setQuery] = useState(defaultQuery);

  return (
    <Command
      loop
      className={cn("@container h-auto w-full max-w-xl rounded-xl border shadow-lg", className)}
    >
      <div className="relative">
        <CommandInput
          placeholder={labels.placeholder}
          value={query}
          onValueChange={setQuery}
          disabled={status !== "ready"}
          className="h-12"
        />
        {status === "loading" && (
          <Spinner className="absolute top-1/2 right-3 -translate-y-1/2 text-muted-foreground" />
        )}
      </div>
      <CommandList className="max-h-96">
        {status === "loading" ? (
          <div aria-busy="true" className="flex flex-col gap-1 p-2">
            {LOADING_ROW_WIDTHS.map((width, index) => (
              <div key={index} className="flex items-center gap-2 px-2 py-2">
                <Skeleton className="size-4 rounded-sm bg-muted-foreground/15" />
                <Skeleton className={cn("h-3.5 bg-muted-foreground/15", width)} />
              </div>
            ))}
          </div>
        ) : status === "error" ? (
          <div role="alert" className="flex flex-col items-center gap-3 px-6 py-10 text-center">
            <p className="max-w-xs text-sm text-muted-foreground">{labels.error}</p>
            <Button variant="outline" size="sm" onClick={onRetry}>
              <RotateCw />
              {labels.retry}
            </Button>
          </div>
        ) : (
          <>
            <CommandEmpty className="flex flex-col items-center gap-3 px-6 py-10 text-center text-sm text-muted-foreground">
              <p className="max-w-xs break-words">{labels.empty(query)}</p>
              <Button variant="outline" size="sm" onClick={() => onEmptyAction?.(query)}>
                {labels.emptyAction}
              </Button>
            </CommandEmpty>
            {groups.map((group, index) => (
              <Fragment key={group.heading}>
                {index > 0 && <CommandSeparator />}
                <CommandGroup heading={group.heading}>
                  {group.items.map((item) => (
                    <CommandItem
                      key={item.id}
                      value={item.id}
                      keywords={[item.label, ...(item.keywords ?? [])]}
                      onSelect={() => onSelect?.(item.id)}
                      className="gap-3 py-2"
                    >
                      <item.icon className="text-muted-foreground" />
                      <span className="truncate">{item.label}</span>
                      {item.hint && (
                        <span className="hidden truncate text-muted-foreground @md:inline">{item.hint}</span>
                      )}
                      {item.shortcut && (
                        <KbdGroup className="ml-auto">
                          {item.shortcut.map((key) => (
                            <Kbd key={key}>{key}</Kbd>
                          ))}
                        </KbdGroup>
                      )}
                    </CommandItem>
                  ))}
                </CommandGroup>
              </Fragment>
            ))}
          </>
        )}
      </CommandList>
      <div className="flex items-center justify-between gap-3 border-t px-3 py-2 text-xs text-muted-foreground">
        <span className="truncate">{context}</span>
        <div className="flex shrink-0 items-center gap-3">
          <span className="flex items-center gap-1.5">
            {labels.open}
            <Kbd>
              <CornerDownLeft />
            </Kbd>
          </span>
          <Separator orientation="vertical" className="data-[orientation=vertical]:h-3" />
          <span className="flex items-center gap-1.5">
            {labels.actions}
            <KbdGroup>
              <Kbd>{modKey}</Kbd>
              <Kbd>K</Kbd>
            </KbdGroup>
          </span>
        </div>
      </div>
    </Command>
  );
}
```

Not inlined (5 files, 4.8 KB). Full sources: https://uifino.com/r/blocks/command-palette.json
