---
layout: ~/layouts/MdxDocLayout.astro
title: "CommandPalette"
description: "A composable command palette component for search and command dialogs."
sourceFile: "components/command-palette"
---

import ComponentExample from "~/components/docs/ComponentExample.astro";
import ComponentSection from "~/components/docs/ComponentSection.astro";
import KeyboardShortcuts from "~/components/docs/KeyboardShortcuts.astro";
import PropsTable from "~/components/docs/PropsTable.astro";
import {
  CommandPaletteBasicDemo,
  CommandPaletteSimpleDemo,
  CommandPaletteLoadingDemo,
  CommandPaletteNoAutocompleteDemo,
  CommandPaletteResultItemDemo,
} from "~/components/demos/CommandPaletteDemo";

{/* Hero Demo */}

<ComponentSection>
  <ComponentExample demo="CommandPaletteBasicDemo">
    <CommandPaletteBasicDemo client:visible />
  </ComponentExample>
</ComponentSection>

{/* Installation */}

<ComponentSection>

## Installation

### Barrel

```tsx
import { CommandPalette } from "@cloudflare/kumo";
```

### Granular

```tsx
import { CommandPalette } from "@cloudflare/kumo/components/command-palette";
```

</ComponentSection>

{/* Usage */}

<ComponentSection>

## Usage

  <p>
    CommandPalette is a compound component built on Base UI's Autocomplete
    primitive. It provides accessible keyboard navigation and customizable
    styling for command palette interfaces.
  </p>

```tsx
import { useState } from "react";
import { CommandPalette } from "@cloudflare/kumo";

interface Item {
  id: string;
  title: string;
}

const items: Item[] = [
  { id: "1", title: "Create Project" },
  { id: "2", title: "Open Settings" },
];

export default function Example() {
  const [open, setOpen] = useState(false);
  const [search, setSearch] = useState("");

  return (
    <>
      <button onClick={() => setOpen(true)}>Open</button>
      <CommandPalette.Root
        open={open}
        onOpenChange={setOpen}
        items={items}
        value={search}
        onValueChange={setSearch}
        itemToStringValue={(item) => item.title}
        getSelectableItems={(items) => items}
      >
        <CommandPalette.Input placeholder="Search..." />
        <CommandPalette.List>
          <CommandPalette.Results>
            {(item) => (
              <CommandPalette.Item
                key={item.id}
                value={item}
                onClick={() => setOpen(false)}
              >
                {item.title}
              </CommandPalette.Item>
            )}
          </CommandPalette.Results>
          <CommandPalette.Empty>No results</CommandPalette.Empty>
        </CommandPalette.List>
      </CommandPalette.Root>
    </>
  );
}
```

</ComponentSection>

{/* Keyboard Navigation */}

<ComponentSection>

## Keyboard Navigation

  <p>Built-in keyboard navigation is provided automatically:</p>
  <KeyboardShortcuts
    shortcuts={[
      { keys: ["↑", "↓"], description: "Move highlight between items" },
      { keys: ["Enter"], description: "Select highlighted item" },
      { keys: ["⌘/Ctrl", "Enter"], description: "Select with newTab: true" },
      { keys: ["Escape"], description: "Close the dialog" },
    ]}
  />
</ComponentSection>

{/* Examples */}

<ComponentSection>

## Examples

### With Grouped Items

<p>Group related commands together with labels.</p>
<ComponentExample demo="CommandPaletteBasicDemo">
  <CommandPaletteBasicDemo client:visible />
</ComponentExample>

### Simple Flat List

<p>For simpler use cases, use a flat array of items without grouping.</p>
<ComponentExample demo="CommandPaletteSimpleDemo">
  <CommandPaletteSimpleDemo client:visible />
</ComponentExample>

### Loading State

<p>Show a loading spinner while fetching results.</p>
<ComponentExample demo="CommandPaletteLoadingDemo">
  <CommandPaletteLoadingDemo client:visible />
</ComponentExample>

### Disabling Browser Autocomplete

<p>Pass standard HTML input attributes like `autoComplete`, `autoCorrect`, `spellCheck`, and `data-*` to suppress browser and password manager autocomplete overlays.</p>
<ComponentExample demo="CommandPaletteNoAutocompleteDemo">
  <CommandPaletteNoAutocompleteDemo client:visible />
</ComponentExample>

### ResultItem with Breadcrumbs

  <p>
    Use `ResultItem` for rich items with
    breadcrumbs, icons, and optional text highlighting.
  </p>
  <ComponentExample demo="CommandPaletteResultItemDemo">
    <CommandPaletteResultItemDemo client:visible />
  </ComponentExample>
</ComponentSection>

{/* Component Parts */}

<ComponentSection>

## Component Parts

### CommandPalette.Root

<p>
  The main wrapper that combines Dialog + Panel. Manages open state and
  Autocomplete functionality.
</p>

### CommandPalette.Dialog

<p>
  Modal dialog wrapper. Use with Panel for swappable content (e.g., drill-down
  navigation).
</p>

### CommandPalette.Panel

<p>
  Autocomplete panel without dialog. Use inside Dialog for content that can swap
  without re-mounting.
</p>

### CommandPalette.Input

<p>Search input field with auto-focus and keyboard handling.</p>

### CommandPalette.List

<p>Scrollable container for results.</p>

### CommandPalette.Results

<p>Render prop iterator for items/groups.</p>

### CommandPalette.Group

<p>Category grouping container.</p>

### CommandPalette.GroupLabel

<p>Section header text within a group.</p>

### CommandPalette.Items

<p>Render prop iterator for items within a group.</p>

### CommandPalette.Item

<p>Basic selectable item.</p>

### CommandPalette.ResultItem

<p>Rich item with breadcrumbs, icons, and text highlighting.</p>

### CommandPalette.HighlightedText

<p>Renders text with highlighted portions based on match indices.</p>

### CommandPalette.Empty

<p>Empty state when no results match.</p>

### CommandPalette.Loading

<p>Loading spinner state.</p>

### CommandPalette.Footer

  <p>
    Footer for keyboard hints or other content.
  </p>
</ComponentSection>

{/* API Reference */}

<ComponentSection>

## API Reference

### CommandPalette.Root Props

```tsx
interface CommandPaletteRootProps<TGroup, TItem> {
  // Dialog state
  open: boolean;
  onOpenChange: (open: boolean) => void;
  onBackdropClick?: (e: React.MouseEvent) => void;

  // Autocomplete
  items: TGroup[];
  value?: string;
  onValueChange?: (value: string) => void;
  itemToStringValue?: (item: TGroup) => string;
  filter?: (item: TGroup, query: string) => boolean;
  onItemHighlighted?: (item: TGroup | undefined, details: {...}) => void;

  // Selection
  onSelect?: (item: TItem, options: { newTab: boolean }) => void;
  getSelectableItems?: (items: TGroup[]) => TItem[];

  children: React.ReactNode;
}
```

### CommandPalette.ResultItem Props

```tsx
interface CommandPaletteResultItemProps<T> {
  value: T;
  title: string;
  breadcrumbs?: string[];
  titleHighlights?: [number, number][];
  breadcrumbHighlights?: [number, number][][];
  description?: string;
  icon?: React.ReactNode;
  onClick: (e: React.MouseEvent) => void;
  showArrow?: boolean; // default: true
  external?: boolean; // shows external link icon
  nonInteractive?: boolean;
}
```

</ComponentSection>
