# Command Palette
One search for every page, action and record, opened with Cmd+K (Ctrl+K off a Mac) and run by a person or an agent.
Source page: https://dinachi.dev/docs/components/command-palette

## Installation
```bash
npx @dinachi/cli@latest add command-palette
```
Adds to your project:
- `components/ui/command-palette/command-palette.tsx`
- `components/ui/command-palette/index.ts`
Installs:
- `@base-ui/react` ^1.9.0
- `lucide-react` ^0.552.0

## Usage
```tsx
import { FileText, Receipt } from "lucide-react"
import {
  CommandPalette,
  CommandPaletteBody,
  CommandPaletteContent,
  CommandPaletteInput,
  CommandPaletteList,
  CommandPalettePreview,
  CommandPaletteTrigger,
  type CommandItem,
} from "@/components/ui/command-palette"

const commands: CommandItem[] = [
  { id: "orders", label: "Orders", group: "Pages", kind: "Page", icon: <FileText />, shortcut: "G O" },
  {
    id: "order-4821",
    label: "Order #4821",
    description: "Ada Okafor",
    group: "Orders",
    kind: "Order",
    icon: <Receipt />,
    actions: [
      { id: "open", label: "Open order" },
      { id: "refund", label: "Refund" },
    ],
  },
]

export function Search({ run }: { run: (id: string, action?: string) => void }) {
  return (
    <CommandPalette items={commands} recent={["order-4821"]} onRun={(item, action) => run(item.id, action?.id)}>
      <CommandPaletteTrigger />
      <CommandPaletteContent>
        <CommandPaletteInput />
        <CommandPaletteBody>
          <CommandPaletteList />
          <CommandPalettePreview placeholder="Type to search pages and orders." />
        </CommandPaletteBody>
      </CommandPaletteContent>
    </CommandPalette>
  )
}
```

## Anatomy
```tsx
<CommandPalette>
  <CommandPaletteTrigger />
  <CommandPaletteContent>
    <CommandPaletteInput />
    <CommandPaletteBody>
      <CommandPaletteList>
        {(item) => <CommandPaletteItem item={item} />}
      </CommandPaletteList>
      <CommandPalettePreview />
    </CommandPaletteBody>
  </CommandPaletteContent>
</CommandPalette>
```
- `CommandPalette`: Holds the commands, the search, the open state and the sub-list you are in.
- `CommandPaletteTrigger`: A search-style button with the shortcut hint. The agent sees the closed palette through it.
- `CommandPaletteContent`: The dialog. 720px under the top of the screen on a desktop; full screen on a phone.
- `CommandPaletteInput`: The search field, with Esc on a desktop and Cancel on a phone. Shows the sub-list you are in.
- `CommandPaletteBody`: Lays the list and preview side by side, 380px tall on a desktop.
- `CommandPaletteList`: The grouped results and the empty state.
- `CommandPaletteItem`: One row: icon, label with the matched words in bold, description, meta and shortcut.
- `CommandPalettePreview`: The highlighted item and its actions beside the list; on a phone, the screen a record opens into.
- `CommandPaletteShortcut`: A key hint, as rows and the trigger use.

## Examples

### Default
A hotel back office: recent items, groups, a preview with each record's actions, and Switch property as a sub-list. On a phone it fills the screen with the search at the bottom.
```tsx
import { BedDouble, FileText, Receipt, Zap } from 'lucide-react';
import {
  CommandPalette,
  CommandPaletteBody,
  CommandPaletteContent,
  CommandPaletteInput,
  CommandPaletteList,
  CommandPalettePreview,
  CommandPaletteTrigger,
  type CommandItem,
} from '@/components/ui/command-palette';

const commands: CommandItem[] = [
  { id: 'orders', label: 'Orders', group: 'Pages', kind: 'Page', icon: <FileText />, shortcut: 'G O' },
  { id: 'refund', label: 'Refund an order', group: 'Actions', kind: 'Action', icon: <Zap /> },
  {
    id: 'property', label: 'Switch property', group: 'Actions', icon: <Zap />, shortcut: '⌘ P',
    items: [
      { id: 'saltmarsh', label: 'Saltmarsh Hotel', description: 'Whitstable' },
      { id: 'harbour', label: 'Harbour House', description: 'Deal' },
    ],
  },
  {
    id: 'order-4821', label: 'Order #4821', description: 'Ada Okafor', group: 'Orders', kind: 'Order',
    icon: <Receipt />, meta: '£204.00',
    actions: [
      { id: 'open', label: 'Open order' },
      { id: 'refund', label: 'Refund' },
      { id: 'email', label: 'Email guest' },
    ],
  },
  { id: 'room-214', label: 'Room 214', description: 'Sea View Double', group: 'Rooms', kind: 'Room', icon: <BedDouble /> },
];

export function Search({ onRun }: { onRun: (item: CommandItem, actionId?: string) => void }) {
  return (
    <CommandPalette items={commands} recent={['order-4821']} onRun={(item, action) => onRun(item, action?.id)}>
      <CommandPaletteTrigger />
      <CommandPaletteContent>
        <CommandPaletteInput />
        <CommandPaletteBody>
          <CommandPaletteList />
          <CommandPalettePreview placeholder="Type to search pages, actions, orders and rooms.">
            {(item) => <p className="text-muted-foreground">{item.description}</p>}
          </CommandPalettePreview>
        </CommandPaletteBody>
      </CommandPaletteContent>
    </CommandPalette>
  );
}
```

### List only
No preview: a narrower palette of plain commands, with its own empty text.
```tsx
import {
  CommandPalette,
  CommandPaletteBody,
  CommandPaletteContent,
  CommandPaletteInput,
  CommandPaletteList,
  CommandPaletteTrigger,
  type CommandItem,
} from '@/components/ui/command-palette';

const commands: CommandItem[] = [
  { id: 'inbox', label: 'Inbox', group: 'Go to', shortcut: 'G I' },
  { id: 'projects', label: 'Projects', group: 'Go to', shortcut: 'G P' },
  { id: 'new-issue', label: 'New issue', group: 'Create', shortcut: 'C' },
  { id: 'new-project', label: 'New project', group: 'Create' },
  { id: 'theme', label: 'Toggle theme', group: 'Preferences', keywords: ['dark', 'light'] },
];

export function Commands({ onRun }: { onRun: (id: string) => void }) {
  return (
    <CommandPalette items={commands} onRun={(item) => onRun(item.id)}>
      <CommandPaletteTrigger>Commands</CommandPaletteTrigger>
      <CommandPaletteContent className="sm:w-[520px]">
        <CommandPaletteInput placeholder="Type a command" />
        <CommandPaletteBody className="sm:h-auto sm:max-h-[320px]">
          <CommandPaletteList empty={(q) => `No command for “${q}”`} />
        </CommandPaletteBody>
      </CommandPaletteContent>
    </CommandPalette>
  );
}
```

## Notes

### The item shape
`CommandItem`, `CommandAction`, `CommandRunDetails` and `filterCommands` live in `command-items`, which `dinachi add command-palette` installs alongside, so other command surfaces can share them without the palette. An item has `id`, `label`, and optionally `description`, `group`, `kind`, `icon`, `shortcut`, `meta`, `keywords`, `disabled`, `actions` and `items`. All of it is plain data except `icon` and `meta`, which are React nodes: a list from a server or a model sends the rest and the app adds those two. The order of `actions` is part of the contract: `actions[0]` is what Enter runs, `actions[1]` what Mod+Enter runs, and the rest are reached from the preview. `items` makes it a sub-list that opens in place. Keep app logic in `onRun` rather than on the items.

### What stays in your app
The palette lays out a record's name and actions. Its fields, such as an order's status and amount, come from `CommandPalettePreview`'s children. What running does, and any toast after it, is `onRun`'s job. `shortcut` on an item is a hint; binding it is up to you.

### No motion
It is opened from the keyboard many times a day, so it never animates: no scale, fade or backdrop fade, and the highlight and sub-lists change at once. `CommandPaletteContent` uses Base UI's dialog parts directly, not the Dialog component, so it carries no transition. The phone screen appears at once too, as the keyboard already rises under it.

### While the search is empty
With `recent` or `suggested` set, an empty search shows the recent items, then the suggested ones, up to `idleLimit` rows, rather than the whole list. Typing searches everything.

### Long lists
Every search filters all the items, but at most `limit` rows render, 50 by default: about five screens of the list, and few enough that each keystroke stays quick with thousands of items. Past that, typing narrows faster than scrolling. The status says how many matched, such as 50 of 312 results, and the agent's `search` returns the same rows the list shows. Moving the highlight re-renders only the two rows it leaves and reaches, and the preview.

### On a phone
Below 640px it fills the screen with the field at the bottom, above the keyboard, and the best match just above the field. The arrows move the way they point, so Up goes away from the field. A record with more than one action opens a detail screen with the keyboard down, focus on Back and the list hidden from assistive tech; Back returns focus to the field. Rows, the sub-list chip, Back and Cancel are 44px or taller.

### In these docs
The site's search holds Cmd+K, so the demos here open from their button.

## API reference
Props marked **agent** change what an agent reads or may do.

### CommandPalette
Holds the commands, the search, the open state and the sub-list you are in.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `items` **agent** | `CommandItem[]` | - | Every command, in the order groups and rows are listed. The agent reads them while closed. |
| `recent` | `string[]` | - | Ids listed first, under Recent, while the search is empty. |
| `recentLabel` | `string` | `"Recent"` | The heading over recent items. |
| `suggested` | `string[]` | - | Ids listed after the recent ones, under their own groups, while the search is empty. |
| `idleLimit` | `number` | `8` | How many rows show while the search is empty. Applies once recent or suggested is set; without either, the list shows up to limit. |
| `limit` | `number` | `50` | The most rows a search, a sub-list or the full list shows. Typing narrows it. Infinity lifts it. |
| `onRun` **agent** | `(item, action, details) => void` | - | Runs a picked item with its first action, or the one asked for. details.reason is item-press, action-press or keyboard. `details.cancel()` keeps the palette open. |
| `open` | `boolean` | - | Controlled open state. |
| `defaultOpen` | `boolean` | `false` | Starts open when uncontrolled. |
| `onOpenChange` **agent** | `(open, details) => void` | - | Runs for people and agents. `details.cancel()` refuses the change. |
| `value` | `string` | - | Controlled search text. |
| `defaultValue` | `string` | `""` | Starting search text. |
| `onValueChange` **agent** | `(value, details) => void` | - | Runs as the search changes, from either side. |
| `shortcut` | `string \| false` | `"k"` | The key that toggles it with Cmd on a Mac and Ctrl elsewhere. It matches the physical key on other layouts. false turns it off and hides the trigger's hint. |
| `label` **agent** | `string` | `"Command palette"` | Names the dialog and the field. The agent finds the palette by it. |
| `filter` | `(item, query) => boolean` | `filterCommands` | Decides what matches. The default needs every word in the label, description or keywords. |
| `disabled` **agent** | `boolean` | `false` | Blocks the trigger, the shortcut and the agent. |

### CommandPaletteTrigger
A search-style button with the shortcut hint. The agent sees the closed palette through it.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `ReactNode` | - | Replaces the icon, Search and the shortcut hint, which reads ⌘ K on a Mac and Ctrl K elsewhere. |

### CommandPaletteContent
The dialog. 720px under the top of the screen on a desktop; full screen on a phone.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `className` | `string` | - | Sizes the panel, such as sm:w-[520px]. |

### CommandPaletteInput
The search field, with Esc on a desktop and Cancel on a phone. Shows the sub-list you are in.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `placeholder` | `string` | `"Search or run a command"` | Shown while empty, at the top level. |
| `cancelLabel` | `string` | `"Cancel"` | The phone's close button. |

### CommandPaletteBody
Lays the list and preview side by side, 380px tall on a desktop.

Takes the props of the element it renders.

### CommandPaletteList
The grouped results and the empty state.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `(item) => ReactNode` | `CommandPaletteItem` | Renders each row. |
| `empty` | `ReactNode \| (query) => ReactNode` | - | Shown when nothing matches. |

### CommandPaletteItem
One row: icon, label with the matched words in bold, description, meta and shortcut.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `item` | `CommandItem` | - | The command this row stands for. |
| `children` | `ReactNode` | - | Replaces the row's content. Highlight and picking stay. |

### CommandPalettePreview
The highlighted item and its actions beside the list; on a phone, the screen a record opens into.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `(item) => ReactNode` | - | The item's own fields, under its name. |
| `placeholder` | `ReactNode` | - | Shown while nothing is highlighted. |
| `actionLabel` | `string` | `"Open"` | The button for an item without actions. |
| `backLabel` | `string` | `"Search"` | The phone's way back to the results. |
| `backAriaLabel` | `string` | - | That button's accessible name. Defaults to Back to, then `backLabel` in lower case. |

### CommandPaletteShortcut
A key hint, as rows and the trigger use.

Takes the props of the element it renders.

## Agent
Inside an `AgentProvider`, Command Palette appears in `agent.snapshot()` as `combobox`.
Reads:
- `label`: The palette's label.
- `open`: True while the palette is showing.
- `value`: The search text, while open.
- `items`: Every command on the current level while closed, or the matches while open.
- `disabled`: True when the palette is blocked.
Actions:
- `open() / close()`: Opens or closes the palette.
- `search("refund")`: Opens it, types the text, returns the matches shown, up to limit, with their group and actions.
- `select("Order #4821", "Refund")`: Runs a command, with its first action or the one named, and closes if open. A sub-list opens and returns its commands.
- `back()`: Inside a sub-list, steps back out and returns the level above.
Opening and closing run through `onOpenChange`, the search through `onValueChange` and running through `onRun`. `isAgentChange(details)` is true for each; `details.cancel()` refuses it and the agent is told. disabled refuses everything. The agent sees a closed palette through its trigger, so render `CommandPaletteTrigger` for an agent to find it before it opens.
```ts
await agent.act({
  steps: [
    { ref: "Command palette", action: "search", args: ["4821"] },
    { ref: "Command palette", action: "select", args: ["Order #4821", "Refund"] },
  ],
})
```

## Accessibility
- The palette is a modal dialog named by label; focus moves to the search and returns to the trigger on close.
- The search has role combobox and the results are a listbox of options; aria-activedescendant keeps focus in the field, and the highlighted option has aria-selected.
- The listbox is named after the label, and a polite status counts the matches as you type, as 50 of 312 results when limit cuts the list.
- Opening a sub-list names the search after it and announces it, as Switch property, 3 options.
- On a phone, the detail screen's Back is named Back to search. Back and Cancel mix the info colour with the text colour so they pass 4.5:1 in both themes.
- Groups are labelled by their headings. On a phone they are reversed visually only, so reading order stays best match first.
- Matched words are marked with mark, and shortcuts are kbd.
### Keyboard
| Keys | Does |
| --- | --- |
| `⌘` / `K` | Opens or closes the palette. Ctrl+K off a Mac |
| `↓` / `↑` | Moves the highlight |
| `Enter` | Runs the highlighted item, or opens its sub-list |
| `⌘` / `Enter` | Runs the highlighted item's second action. Ctrl+Enter off a Mac |
| `Tab` | Completes the highlighted item into the search, or opens its sub-list. Pressed again, moves into the preview's actions |
| `Esc` | Leaves a sub-list, or closes the palette |
| `Backspace` | Leaves a sub-list when the search is empty |

## Source
`components/ui/command-palette/command-palette.tsx`
```tsx
"use client"

import * as React from "react"
import { Dialog as BaseDialog } from "@base-ui/react/dialog"
import { Autocomplete as AutocompletePrimitive } from "@base-ui/react/autocomplete"
import { mergeProps } from "@base-ui/react/merge-props"
import { ChevronLeft, ChevronRight, Search } from "lucide-react"
import { cn } from "@/lib/utils"
import {
  changeAsAgent,
  createAgentChangeDetails,
  isAgentChange,
  matchAgentChoice,
  useAgentControl,
  useAgentLatest,
  useHasAgent,
  useMergedRef,
  waitForAgent,
} from "@/components/ui/agent"
import { commandsHint, filterCommands, type CommandAction, type CommandItem, type CommandRunDetails, type CommandRunReason } from "@/components/ui/command-items"

export { filterCommands }
export type { CommandAction, CommandItem, CommandRunDetails, CommandRunReason }

/** @deprecated Kept for the first release's name; use `CommandRunDetails`. */
export type CommandPaletteRunDetails = CommandRunDetails

type OpenDetails = BaseDialog.Root.ChangeEventDetails
type ValueDetails = AutocompletePrimitive.Root.ChangeEventDetails
type Group = { key: string; label: string; items: CommandItem[] }

/** The Recent group's key, apart from any group the items name. */
const RECENT = "recent"

function groupsOf(items: CommandItem[]): Group[] {
  const groups: Group[] = []
  for (const item of items) {
    const label = item.group ?? ""
    const last = groups.find((g) => g.label === label)
    if (last) last.items.push(item)
    else groups.push({ key: `group:${label}`, label, items: [item] })
  }
  return groups
}

/** Change details for a person's change that Base UI did not make itself. */
function personDetails<D>(reason: string, event?: Event): D {
  const details = {
    reason,
    event,
    isCanceled: false,
    isPropagationAllowed: false,
    trigger: undefined,
    cancel() {
      details.isCanceled = true
    },
    allowPropagation() {},
    preventUnmountOnClose() {},
  }
  return details as unknown as D
}

const PHONE = "(max-width: 639px)"

function usePhone(): boolean {
  return React.useSyncExternalStore(
    (onChange) => {
      const query = window.matchMedia?.(PHONE)
      query?.addEventListener?.("change", onChange)
      return () => query?.removeEventListener?.("change", onChange)
    },
    () => Boolean(window.matchMedia?.(PHONE).matches),
    () => false
  )
}

const isMac = () => typeof navigator !== "undefined" && /Mac|iPhone|iPad/.test(navigator.platform || navigator.userAgent)
const never = () => () => {}

/** False on the server and through hydration, so the first client render matches. */
function useMac(): boolean {
  return React.useSyncExternalStore(never, isMac, () => false)
}

/** Cmd on a Mac, Ctrl elsewhere. Not both: Ctrl+K on a Mac is a text field's kill-line. */
function isMod(event: { metaKey: boolean; ctrlKey: boolean; altKey: boolean; shiftKey: boolean }): boolean {
  if (event.altKey || event.shiftKey) return false
  return isMac() ? event.metaKey && !event.ctrlKey : event.ctrlKey && !event.metaKey
}

/** Matches by the letter, or by the physical key when the layout types another script. */
function isKey(event: KeyboardEvent, key: string): boolean {
  if (event.key.toLowerCase() === key) return true
  if (/^[a-z0-9]$/i.test(event.key)) return false
  return event.code === `Key${key.toUpperCase()}` || event.code === `Digit${key}`
}

const reversed = new WeakSet<Event>()

interface PaletteContext {
  /** Inside an AgentProvider: marks the palette's own buttons so the agent sees one control. */
  part: "" | undefined
  label: string
  open: boolean
  disabled: boolean
  phone: boolean
  mac: boolean
  shortcut: string | false
  query: string
  setQuery: (query: string, details: ValueDetails) => void
  page: CommandItem | undefined
  pop: () => void
  shown: Group[]
  /** How many matched the search, past `limit` included. */
  total: number
  /** The search's words, built once per search for every row's highlight. */
  pattern: RegExp | undefined
  /** The highlighted item, read when a key needs it, so a moving highlight re-renders nothing here. */
  latest: React.MutableRefObject<{ highlighted: CommandItem | undefined }>
  setHighlighted: (item: CommandItem | undefined) => void
  detail: CommandItem | undefined
  setDetail: (item: CommandItem | undefined) => void
  pick: (item: CommandItem) => void
  run: (item: CommandItem, action: CommandAction | undefined, details: CommandRunDetails) => string | undefined
  close: (event?: Event) => void
  trigger: React.MutableRefObject<HTMLElement | null>
  input: React.MutableRefObject<HTMLInputElement | null>
}

const PaletteContext = React.createContext<PaletteContext | null>(null)
/** Apart from the rest, so only what shows the highlighted item re-renders as it moves. */
const HighlightContext = React.createContext<CommandItem | undefined>(undefined)

function usePalette(part: string): PaletteContext {
  const context = React.useContext(PaletteContext)
  if (!context) throw new Error(`${part} must be inside <CommandPalette>.`)
  return context
}

export interface CommandPaletteProps {
  /** Every command, in the order groups and items are listed. */
  items: CommandItem[]
  /** Ids listed first, under "Recent", while the search is empty. */
  recent?: string[]
  recentLabel?: string
  /** Ids listed after the recent ones while the search is empty, under their own groups. */
  suggested?: string[]
  /** How many rows show while the search is empty, when `recent` or `suggested` is set. */
  idleLimit?: number
  /** The most rows a search or a full list shows. Typing narrows it. */
  limit?: number
  /** Runs a picked item. `details.cancel()` keeps the palette open. */
  onRun?: (item: CommandItem, action: CommandAction | undefined, details: CommandRunDetails) => void
  open?: boolean
  defaultOpen?: boolean
  onOpenChange?: (open: boolean, details: OpenDetails) => void
  /** The search text. */
  value?: string
  defaultValue?: string
  onValueChange?: (value: string, details: ValueDetails) => void
  /** The key that opens it with Cmd on a Mac and Ctrl elsewhere. `false` turns the shortcut off. */
  shortcut?: string | false
  /** Names the dialog and the search field, and is what an agent finds it by. */
  label?: string
  filter?: (item: CommandItem, query: string) => boolean
  disabled?: boolean
  children?: React.ReactNode
}

/**
 * A Base UI dialog holding the search, list and preview. Opens on Cmd or Ctrl+K and
 * forgets the search, sub-list and highlight when it closes.
 */
function CommandPalette({
  items,
  recent,
  recentLabel = "Recent",
  suggested,
  idleLimit = 8,
  limit = 50,
  onRun,
  open: openProp,
  defaultOpen = false,
  onOpenChange,
  value: valueProp,
  defaultValue = "",
  onValueChange,
  shortcut: shortcutProp = "k",
  label = "Command palette",
  filter = filterCommands,
  disabled = false,
  children,
}: CommandPaletteProps) {
  const [openInner, setOpenInner] = React.useState(defaultOpen)
  const [queryInner, setQueryInner] = React.useState(defaultValue)
  const [pages, setPages] = React.useState<CommandItem[]>([])
  const [detail, setDetail] = React.useState<CommandItem | undefined>()
  const [highlighted, setHighlighted] = React.useState<CommandItem | undefined>()
  const open = openProp ?? openInner
  const query = valueProp ?? queryInner
  const page = pages[pages.length - 1]
  const phone = usePhone()
  const mac = useMac()
  const shortcut: string | false = shortcutProp === false ? false : shortcutProp.toLowerCase()
  const trigger = React.useRef<HTMLElement | null>(null)
  const input = React.useRef<HTMLInputElement | null>(null)

  // Closed by the app or by the palette, it starts over next time.
  const [wasOpen, setWasOpen] = React.useState(open)
  if (wasOpen !== open) {
    setWasOpen(open)
    if (!open) {
      setPages([])
      setDetail(undefined)
      setHighlighted(undefined)
    }
  }

  // Remembers who closed it, so clearing the search afterwards is marked the same way.
  const closedBy = React.useRef<OpenDetails | undefined>(undefined)
  const changeOpen = (next: boolean, details: OpenDetails) => {
    if (!next) closedBy.current = details
    onOpenChange?.(next, details)
    if (!details.isCanceled && openProp === undefined) setOpenInner(next)
  }
  const setQuery = (next: string, details: ValueDetails) => {
    onValueChange?.(next, details)
    if (!details.isCanceled && valueProp === undefined) setQueryInner(next)
  }
  const resetQuery = () => setQuery("", personDetails("none"))

  // Cleared through `setQuery` in both modes, so a controlled search hears about it.
  const wasOpenRef = React.useRef(open)
  React.useEffect(() => {
    const closed = wasOpenRef.current && !open
    wasOpenRef.current = open
    if (!closed || !latest.current.query) return
    const details = isAgentChange(closedBy.current) ? createAgentChangeDetails<ValueDetails>("none") : personDetails<ValueDetails>("none")
    latest.current.setQuery("", details)
  }, [open])

  const [shown, total = 0] = React.useMemo((): [Group[], number?] => {
    const list = page?.items ?? items
    if (query.trim()) {
      const matches = list.filter((item) => filter(item, query))
      return [groupsOf(matches.slice(0, limit)), matches.length]
    }
    if (page || (!recent?.length && !suggested)) return [groupsOf(list.slice(0, limit)), list.length]
    const byId = (ids: string[]) => ids.map((id) => list.find((item) => item.id === id)).filter((item): item is CommandItem => !!item)
    const first = byId(recent ?? []).slice(0, idleLimit)
    const rest = (suggested ? byId(suggested) : list).filter((item) => !first.includes(item)).slice(0, idleLimit - first.length)
    return [[{ key: RECENT, label: recentLabel, items: first }, ...groupsOf(rest)].filter((g) => g.items.length)]
  }, [items, page, query, recent, recentLabel, suggested, idleLimit, limit, filter])

  const pattern = React.useMemo(() => {
    const words = query.split(/\s+/).filter(Boolean).map((w) => w.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
    return words.length ? new RegExp(`(${words.join("|")})`, "ig") : undefined
  }, [query])

  const close = (event?: Event) => changeOpen(false, personDetails("imperative-action", event))

  const run: PaletteContext["run"] = (item, action, details) => {
    if (item.disabled) return "That command is disabled."
    onRun?.(item, action, details)
    if (details.isCanceled) return "The app refused the command."
    if (!latest.current.open) return undefined
    if (isAgentChange(details)) changeOpen(false, createAgentChangeDetails<OpenDetails>())
    else close(details.event)
    return undefined
  }

  const push = (item: CommandItem) => {
    setPages((p) => [...p, item])
    setDetail(undefined)
    resetQuery()
    // The row that opened it is gone, so the field keeps focus for the next search.
    requestAnimationFrame(() => input.current?.focus())
  }

  const pop = () => {
    if (detail) return setDetail(undefined)
    setPages((p) => p.slice(0, -1))
    resetQuery()
  }

  const pick = (item: CommandItem) => {
    if (item.disabled) return
    if (item.items) return push(item)
    if (phone && (item.actions?.length ?? 0) > 1) {
      input.current?.blur()
      return setDetail(item)
    }
    run(item, item.actions?.[0], personDetails("item-press"))
  }

  // Escape steps back out of a detail or sub-list before it closes the palette.
  const onDialogOpenChange = (next: boolean, details: OpenDetails) => {
    if (!next && details.reason === "escape-key" && (detail || page)) {
      details.cancel()
      return pop()
    }
    changeOpen(next, details)
  }

  // Back from a record, the field shows again and takes focus with the keyboard.
  const hadDetail = React.useRef(false)
  React.useEffect(() => {
    if (hadDetail.current && !detail && open) input.current?.focus()
    hadDetail.current = Boolean(detail)
  }, [detail, open])

  React.useEffect(() => {
    if (shortcut === false || disabled) return
    const onKeyDown = (event: KeyboardEvent) => {
      if (event.defaultPrevented || event.repeat || !isMod(event) || !isKey(event, shortcut)) return
      event.preventDefault()
      latest.current.changeOpen(!latest.current.open, personDetails("none", event))
    }
    document.addEventListener("keydown", onKeyDown)
    return () => document.removeEventListener("keydown", onKeyDown)
  }, [shortcut, disabled])

  // Read by handlers when they run. Its stable callers keep the context and memoized rows still.
  const latest = useAgentLatest({ open, query, highlighted, setQuery, changeOpen, push, pop, pick, run, close, onDialogOpenChange })
  const [stable] = React.useState(() => {
    const calls: Record<string, (...args: never[]) => unknown> = {}
    const now = latest.current as unknown as typeof calls
    for (const key in now) if (typeof now[key] == "function") calls[key] = (...args) => (latest.current as unknown as typeof calls)[key](...args)
    return calls as unknown as Pick<typeof latest.current, "setQuery" | "changeOpen" | "push" | "pop" | "pick" | "run" | "close" | "onDialogOpenChange">
  })
  const state = { part: useHasAgent() ? ("" as const) : undefined, label, open, disabled, phone, mac, shortcut, query, page, shown, total, pattern, detail }
  // Rebuilt only when what it holds changes; the highlight has its own context.
  const context: PaletteContext = React.useMemo(
    () => ({ ...state, ...stable, latest, setHighlighted, setDetail, trigger, input }),
    Object.values(state)
  )

  usePaletteAgent(context, page?.items ?? items, pages[pages.length - 2]?.items ?? items, stable.changeOpen, stable.push)

  return (
    <PaletteContext.Provider value={context}>
      <HighlightContext.Provider value={highlighted}>
        <BaseDialog.Root open={open} onOpenChange={stable.onDialogOpenChange}>
          {children}
        </BaseDialog.Root>
      </HighlightContext.Provider>
    </PaletteContext.Provider>
  )
}

const describe = (item: CommandItem) => ({
  label: item.label,
  ...(item.group ? { group: item.group } : {}),
  ...(item.actions ? { actions: item.actions.map((a) => a.label) } : {}),
  ...(item.items ? { opens: item.items.length + " more" } : {}),
})

/**
 * One combobox control for the agent: the trigger while closed, the search field while
 * open. It lists every command on the current level while closed and the matches while open.
 */
function usePaletteAgent(
  c: PaletteContext,
  level: CommandItem[],
  parent: CommandItem[],
  changeOpen: (next: boolean, details: OpenDetails) => void,
  push: (item: CommandItem) => void
) {
  const latest = useAgentLatest(c)
  const settle = async () => {
    await waitForAgent(() => latest.current.open && latest.current.input.current?.isConnected)
    return latest.current.shown.flatMap((g) => g.items).map(describe)
  }
  const opened = () => (latest.current.open ? undefined : changeAsAgent(changeOpen, true))

  useAgentControl(() => ({
    kind: "combobox",
    element: c.open ? c.input.current : c.trigger.current,
    label: c.label,
    hint: commandsHint(level),
    open: c.open,
    disabled: c.disabled,
    ...(c.open ? { value: c.query } : {}),
    items: (c.open ? c.shown.flatMap((g) => g.items) : level).map((i) => i.label),
    actions: {
      [c.open ? "close" : "open"]: () => changeAsAgent(changeOpen, !c.open),
      ...(c.page ? { back: () => (c.pop(), { result: parent.map(describe) }) } : {}),
      search: async (text: unknown) => {
        const refused = opened() || changeAsAgent(c.setQuery, String(text ?? ""))
        if (refused) return refused
        return { result: await settle() }
      },
      select: async (wanted: unknown, actionName?: unknown) => {
        const choices = level.map((item) => ({ value: item, label: item.label, disabled: item.disabled }))
        const match = matchAgentChoice(choices, wanted)
        if (typeof match === "string") return match
        const item = match.value
        if (item.items) {
          const refused = opened()
          if (refused) return refused
          push(item)
          return { result: item.items.map(describe) }
        }
        if (item.disabled) return "That command is disabled."
        let action = item.actions?.[0]
        let reason: CommandRunReason = "item-press"
        if (actionName != null) {
          const found = matchAgentChoice((item.actions ?? []).map((a) => ({ value: a, label: a.label })), actionName)
          if (typeof found === "string") return found
          action = found.value
          reason = "action-press"
        }
        return c.run(item, action, createAgentChangeDetails(reason))
      },
    },
  }))
}

const CommandPaletteTrigger = React.forwardRef<HTMLButtonElement, BaseDialog.Trigger.Props>(
  ({ className, children, disabled, ...props }, ref) => {
    const c = usePalette("CommandPaletteTrigger")
    return (
      <BaseDialog.Trigger
        ref={useMergedRef(ref, c.trigger)}
        data-agent-part={c.part}
        disabled={disabled || c.disabled}
        data-slot="command-palette-trigger"
        className={cn(
          "inline-flex h-9 w-full max-w-64 items-center gap-2 rounded-md border border-input bg-background px-3 text-sm text-muted-foreground",
          "hover:bg-accent hover:text-accent-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
          "disabled:pointer-events-none disabled:opacity-50",
          className
        )}
        {...props}
      >
        {children ?? (
          <>
            <Search className="h-4 w-4" aria-hidden />
            <span className="flex-1 text-left">Search</span>
            {c.shortcut && (
              <CommandPaletteShortcut className="hidden sm:inline-block">{`${c.mac ? "⌘" : "Ctrl"} ${c.shortcut.toUpperCase()}`}</CommandPaletteShortcut>
            )}
          </>
        )}
      </BaseDialog.Trigger>
    )
  }
)
CommandPaletteTrigger.displayName = "CommandPaletteTrigger"

/** Keeps the phone layout above the on-screen keyboard, which `100dvh` does not account for. */
function useKeyboardFit(el: HTMLElement | null, active: boolean) {
  React.useEffect(() => {
    const viewport = window.visualViewport
    if (!active || !viewport || !el) return
    const fit = () => {
      el.style.setProperty("--command-palette-height", `${viewport.height}px`)
      el.style.setProperty("--command-palette-top", `${viewport.offsetTop}px`)
    }
    fit()
    viewport.addEventListener("resize", fit)
    viewport.addEventListener("scroll", fit)
    return () => {
      viewport.removeEventListener("resize", fit)
      viewport.removeEventListener("scroll", fit)
    }
  }, [el, active])
}

// --info alone is under 4.5:1 on a light background; a quarter of the text colour lifts it.
const LINK = "text-[color:color-mix(in_oklab,var(--info)_75%,var(--foreground))]"

/**
 * The dialog, with Base UI's Autocomplete held open inside it. Full screen on a phone,
 * with the search at the bottom and the best match just above it.
 */
const CommandPaletteContent = React.forwardRef<HTMLDivElement, React.ComponentProps<typeof BaseDialog.Popup>>(
  ({ className, children, ...props }, ref) => {
    const c = usePalette("CommandPaletteContent")
    // State, not a ref: the portal mounts the popup after this renders.
    const [popup, setPopup] = React.useState<HTMLDivElement | null>(null)
    const merged = useMergedRef(ref, setPopup)
    useKeyboardFit(popup, c.open && c.phone)
    return (
      // Opened from the keyboard many times a day, so it never animates: Base UI's parts
      // directly, with no fade or scale.
      <BaseDialog.Portal>
        <BaseDialog.Backdrop className="fixed inset-0 z-50 hidden bg-black/30 sm:block dark:bg-black/55" />
        <BaseDialog.Viewport className="fixed inset-0 z-50 flex items-stretch justify-center overflow-auto sm:items-start sm:px-5 sm:pt-[min(12vh,7rem)]">
          <BaseDialog.Popup
            ref={merged}
            data-agent-part={c.part}
            aria-label={c.label}
            initialFocus={c.input}
            data-slot="command-palette-content"
            className={cn(
              "relative flex flex-col overflow-hidden bg-background text-foreground outline-none",
              "fixed inset-x-0 top-[var(--command-palette-top,0px)] h-[var(--command-palette-height,100dvh)] w-full",
              "sm:static sm:h-auto sm:w-[720px] sm:max-w-full sm:rounded-2xl sm:border sm:bg-popover sm:text-popover-foreground sm:shadow-[0_30px_80px_-20px_rgb(0_0_0/0.4)]",
              className
            )}
            {...props}
          >
            <AutocompletePrimitive.Root
              inline
              open
              items={c.shown}
              filteredItems={c.shown}
              value={c.query}
              onValueChange={c.setQuery}
              autoHighlight="always"
              keepHighlight
              itemToStringValue={(item: CommandItem) => item.label}
              onItemHighlighted={(item: CommandItem | undefined) => c.setHighlighted(item)}
            >
              {children}
            </AutocompletePrimitive.Root>
          </BaseDialog.Popup>
        </BaseDialog.Viewport>
      </BaseDialog.Portal>
    )
  }
)
CommandPaletteContent.displayName = "CommandPaletteContent"

interface CommandPaletteInputProps extends React.ComponentPropsWithoutRef<typeof AutocompletePrimitive.Input> {
  /** The phone's button that closes the palette. */
  cancelLabel?: string
}

/**
 * The search field. On a desktop it sits on top with an Esc hint; on a phone it sits at
 * the bottom with a Cancel button. Inside a sub-list it shows where you are.
 */
const CommandPaletteInput = React.forwardRef<HTMLInputElement, CommandPaletteInputProps>(
  ({ className, cancelLabel = "Cancel", placeholder = "Search or run a command", onKeyDown, ...props }, ref) => {
    const c = usePalette("CommandPaletteInput")
    const assign = useMergedRef(ref, c.input)
    return (
      <div
        className={cn(
          "order-last flex flex-none items-center border-t pb-[env(safe-area-inset-bottom)] sm:order-none sm:border-b sm:border-t-0 sm:pb-0",
          // A phone's record screen stands alone, with the keyboard down.
          c.detail && "hidden"
        )}
      >
        <div className="ml-3 mr-0.5 my-2 flex h-11 min-w-0 flex-1 items-center gap-2.5 rounded-xl bg-muted px-3 sm:m-0 sm:h-auto sm:rounded-none sm:bg-transparent sm:px-4 sm:py-3.5">
          <Search className="h-4 w-4 flex-none text-muted-foreground" aria-hidden />
          {c.page && (
            <button
              type="button"
              data-agent-part={c.part}
              onClick={c.pop}
              className="flex h-11 min-w-11 flex-none items-center justify-center sm:h-auto sm:min-w-0"
              aria-label={`Back from ${c.page.label}`}
            >
              <span className="rounded-md bg-background px-2 py-0.5 text-xs font-medium text-foreground sm:bg-muted">{c.page.label}</span>
            </button>
          )}
          <AutocompletePrimitive.Input
            ref={assign}
            data-slot="command-palette-input"
            // Inside a sub-list the field is named after it, and the status announces it.
            aria-label={c.page ? c.page.label : c.label}
            placeholder={c.page ? `Search ${c.page.label.toLowerCase()}` : placeholder}
            enterKeyHint="go"
            onKeyDown={(event) => {
              onKeyDown?.(event)
              if (event.nativeEvent.isComposing) return
              if (event.key === "Backspace" && event.currentTarget.value === "" && c.page) {
                event.preventDefault()
                return c.pop()
              }
              // Tab completes the highlighted item into the search, or opens its sub-list.
              // Once the search already reads it, Tab moves on to the preview's actions.
              const item = c.latest.current.highlighted
              if (event.key === "Tab" && !event.shiftKey && !isMod(event) && item && !item.disabled) {
                if (item.items) {
                  event.preventDefault()
                  return c.pick(item)
                }
                if (c.query !== item.label) {
                  event.preventDefault()
                  return c.setQuery(item.label, personDetails("none", event.nativeEvent))
                }
              }
              if (event.key === "Enter" && isMod(event)) {
                event.preventDefault()
                event.preventBaseUIHandler()
                if (item && !item.items && !item.disabled) {
                  c.run(item, item.actions?.[1] ?? item.actions?.[0], personDetails("keyboard", event.nativeEvent))
                }
                return
              }
              // The phone list runs bottom up, so each arrow moves the other way through it.
              const flip = { ArrowUp: "ArrowDown", ArrowDown: "ArrowUp" }[event.key]
              if (c.phone && flip && !reversed.has(event.nativeEvent)) {
                event.preventDefault()
                event.preventBaseUIHandler()
                const next = new KeyboardEvent("keydown", { key: flip, code: flip, bubbles: true, cancelable: true })
                reversed.add(next)
                event.currentTarget.dispatchEvent(next)
              }
            }}
            className={cn(
              "min-w-0 flex-1 text-ellipsis bg-transparent text-base outline-none placeholder:text-muted-foreground sm:text-[15px]",
              className
            )}
            {...props}
          />
          <kbd className="hidden rounded border border-b-2 bg-background px-[5px] py-px font-mono text-[10.5px] text-muted-foreground sm:inline-block">
            esc
          </kbd>
        </div>
        <button
          type="button"
          data-agent-part={c.part}
          onClick={(event) => c.close(event.nativeEvent)}
          className={cn("h-11 flex-none pl-0.5 pr-3.5 text-[15px] sm:hidden", LINK)}
        >
          {cancelLabel}
        </button>
      </div>
    )
  }
)
CommandPaletteInput.displayName = "CommandPaletteInput"

/** Lays the list and the preview side by side. */
const CommandPaletteBody = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>(
  ({ className, ...props }, ref) => (
    <div
      ref={ref}
      data-slot="command-palette-body"
      className={cn("relative flex min-h-0 flex-1 sm:h-[380px] sm:flex-none", className)}
      {...props}
    />
  )
)
CommandPaletteBody.displayName = "CommandPaletteBody"

function Highlight({ text, pattern }: { text: string; pattern: RegExp | undefined }) {
  if (!pattern) return <>{text}</>
  const parts = text.split(pattern)
  return (
    <>
      {parts.map((part, i) =>
        i % 2 ? (
          <mark key={i} className="bg-transparent font-semibold text-foreground">
            {part}
          </mark>
        ) : (
          part
        )
      )}
    </>
  )
}

const iconBox =
  "grid flex-none place-items-center bg-muted text-muted-foreground [&_svg]:h-4 [&_svg]:w-4 group-data-[highlighted]:bg-background group-data-[highlighted]:text-foreground"

interface CommandPaletteItemProps extends Omit<React.ComponentPropsWithoutRef<typeof AutocompletePrimitive.Item>, "value"> {
  item: CommandItem
}

interface RowProps extends CommandPaletteItemProps {
  pattern: RegExp | undefined
  pick: (item: CommandItem) => void
}

function joinRefs<T>(...refs: (React.Ref<T> | undefined)[]): React.RefCallback<T> {
  return (node) => {
    for (const ref of refs) {
      if (typeof ref === "function") ref(node)
      else if (ref) (ref as React.MutableRefObject<T | null>).current = node
    }
  }
}

/** Memoized, so a row re-renders only when its item or the search changes. */
const Row = React.memo(
  React.forwardRef<HTMLDivElement, RowProps>(({ item, pattern, pick, className, children, onClick, render, ...props }, ref) => {
    const drills = Boolean(item.items) || (item.actions?.length ?? 0) > 1
    return (
      <AutocompletePrimitive.Item
        ref={ref}
        value={item}
        disabled={item.disabled}
        data-slot="command-palette-item"
        // Base UI renders the highlighted row again on its own, which keeps this current.
        render={(rowProps, state) => {
          const own = { ...rowProps, "aria-selected": state.highlighted }
          if (typeof render === "function") return render(own, state)
          if (!render) return <div {...own} />
          const theirs = render.props as React.HTMLAttributes<HTMLDivElement> & { ref?: React.Ref<HTMLDivElement> }
          return React.cloneElement(render, { ...mergeProps<"div">(own, theirs), ref: joinRefs(rowProps.ref, theirs.ref) } as object)
        }}
        onClick={(event) => {
          onClick?.(event)
          pick(item)
        }}
        className={cn(
          // The highlight follows the keys and the pointer, so it moves with no transition.
          "group relative flex cursor-default select-none items-center gap-2.5 rounded-[9px] px-2.5 py-[11px] text-[15px] outline-none sm:py-2 sm:text-[13.5px]",
          "data-[highlighted]:bg-muted data-[disabled]:pointer-events-none data-[disabled]:opacity-50",
          className
        )}
        {...props}
      >
        {children ?? (
          <>
            {item.icon && <span className={cn(iconBox, "h-8 w-8 rounded-[9px] sm:h-[26px] sm:w-[26px] sm:rounded-[7px]")}>{item.icon}</span>}
            <span className="min-w-0 flex-1">
              <Highlight text={item.label} pattern={pattern} />
              {item.description && (
                <span className="block text-[13px] text-muted-foreground sm:ml-1.5 sm:inline sm:text-[12.5px]">
                  <Highlight text={item.description} pattern={pattern} />
                </span>
              )}
            </span>
            <span className="ml-auto flex flex-none items-center gap-1.5 text-xs tabular-nums text-muted-foreground">
              {item.meta}
              {item.shortcut && <CommandPaletteShortcut className="hidden sm:inline-block">{item.shortcut}</CommandPaletteShortcut>}
              {drills && <ChevronRight className="h-4 w-4 sm:hidden" aria-hidden />}
            </span>
          </>
        )}
      </AutocompletePrimitive.Item>
    )
  })
)
Row.displayName = "CommandPaletteRow"

/** One row. Its children replace the icon, label, meta and shortcut it shows by default. */
const CommandPaletteItem = React.forwardRef<HTMLDivElement, CommandPaletteItemProps>((props, ref) => {
  const c = usePalette("CommandPaletteItem")
  return <Row ref={ref} {...props} pattern={c.pattern} pick={c.pick} />
})
CommandPaletteItem.displayName = "CommandPaletteItem"

interface CommandPaletteListProps extends Omit<React.ComponentPropsWithoutRef<typeof AutocompletePrimitive.List>, "children"> {
  /** Renders each row. Defaults to a `CommandPaletteItem` for the item. */
  children?: (item: CommandItem) => React.ReactNode
  /** Shown when nothing matches. */
  empty?: React.ReactNode | ((query: string) => React.ReactNode)
}

/**
 * The grouped results. On a phone the groups and rows run bottom up, so the best match is
 * nearest the field and the thumb, while the reading order stays best match first.
 */
const CommandPaletteList = React.forwardRef<HTMLDivElement, CommandPaletteListProps>(
  ({ className, children, empty, ...props }, ref) => {
    const c = usePalette("CommandPaletteList")
    const row = children ?? ((item: CommandItem) => <Row key={item.id} item={item} pattern={c.pattern} pick={c.pick} />)
    const count = c.shown.reduce((n, g) => n + g.items.length, 0)
    const plural = (n: number, word: string) => `${n} ${word}${n === 1 ? "" : "s"}`
    const status = c.query.trim()
      ? count ? `${count < c.total ? count + " of " : ""}${plural(c.total, "result")}` : null
      : c.page && `${c.page.label}, ${count < c.total ? count + " of " : ""}${plural(c.total, "option")}`
    return (
      // A phone's record covers the list, so it leaves the tab order and the reading order.
      <div className="flex min-h-0 min-w-0 flex-1 flex-col" inert={Boolean(c.detail)}>
        {/* Counts matches, and names a sub-list as it opens. The empty state is its own live region. */}
        <AutocompletePrimitive.Status className="sr-only">{status}</AutocompletePrimitive.Status>
        <AutocompletePrimitive.Empty
          data-slot="command-palette-empty"
          className="mt-auto px-3 py-[30px] text-center text-[13px] text-muted-foreground empty:hidden sm:mt-0"
        >
          {typeof empty === "function" ? empty(c.query) : (empty ?? `Nothing matches “${c.query}”`)}
        </AutocompletePrimitive.Empty>
        <AutocompletePrimitive.List
          ref={ref}
          data-slot="command-palette-list"
          aria-label={`${c.label} results`}
          className={cn(
            "flex min-h-0 flex-1 flex-col-reverse overflow-y-auto px-2 pb-1 pt-1.5 [scrollbar-width:thin] sm:flex-col sm:px-1.5 sm:pb-2 sm:pt-1",
            className
          )}
          {...props}
        >
          {(group: Group) => (
            <AutocompletePrimitive.Group key={group.key} items={group.items} data-slot="command-palette-group">
              {group.label && (
                <AutocompletePrimitive.GroupLabel
                  data-slot="command-palette-group-label"
                  className="px-2.5 pb-1 pt-2.5 text-[11.5px] font-medium text-muted-foreground"
                >
                  {group.label}
                </AutocompletePrimitive.GroupLabel>
              )}
              <div className="flex flex-col-reverse sm:flex-col">
                <AutocompletePrimitive.Collection>{(item: CommandItem) => row(item)}</AutocompletePrimitive.Collection>
              </div>
            </AutocompletePrimitive.Group>
          )}
        </AutocompletePrimitive.List>
      </div>
    )
  }
)
CommandPaletteList.displayName = "CommandPaletteList"

/** A key hint. */
const CommandPaletteShortcut = React.forwardRef<HTMLElement, React.HTMLAttributes<HTMLElement>>(
  ({ className, ...props }, ref) => (
    <kbd
      ref={ref}
      data-slot="command-palette-shortcut"
      className={cn(
        "rounded border border-b-2 bg-background px-[5px] py-px font-mono text-[10.5px] font-normal text-muted-foreground",
        className
      )}
      {...props}
    />
  )
)
CommandPaletteShortcut.displayName = "CommandPaletteShortcut"

interface CommandPalettePreviewProps extends Omit<React.HTMLAttributes<HTMLDivElement>, "children"> {
  /** The body under the item's name, such as a record's fields. Defaults to its description. */
  children?: (item: CommandItem) => React.ReactNode
  /** Shown while nothing is highlighted. */
  placeholder?: React.ReactNode
  /** The button for an item with no actions of its own. */
  actionLabel?: string
  /** The phone's button back to the results. */
  backLabel?: string
  /** That button's accessible name. Defaults to "Back to", then `backLabel` in lower case. */
  backAriaLabel?: string
}

/**
 * The highlighted item, with its actions. Beside the list on a desktop; on a phone it is
 * the screen a record opens into, with the keyboard down and the actions at the bottom.
 */
const CommandPalettePreview = React.forwardRef<HTMLDivElement, CommandPalettePreviewProps>(
  ({ className, children, placeholder, actionLabel = "Open", backLabel = "Search", backAriaLabel, ...props }, ref) => {
    const c = usePalette("CommandPalettePreview")
    const highlighted = React.useContext(HighlightContext)
    const item = c.phone ? c.detail : highlighted
    // A record opening on a phone takes focus, so it is announced and the keys stay in it.
    const backButton = React.useRef<HTMLButtonElement | null>(null)
    React.useEffect(() => {
      if (c.phone && c.detail) backButton.current?.focus()
    }, [c.phone, c.detail])
    if (c.phone && !item) return null
    const mod = c.mac ? "⌘" : "Ctrl"
    const actions = item?.actions ?? [{ id: "", label: actionLabel }]
    const press = (action: CommandAction, event: React.MouseEvent) => {
      if (!item || item.disabled) return
      if (item.items) return c.pick(item)
      c.run(item, item.actions ? action : undefined, personDetails("action-press", event.nativeEvent))
    }
    const back = () => c.setDetail(undefined)
    return (
      <div
        ref={ref}
        data-slot="command-palette-preview"
        className={cn(
          "absolute inset-0 z-10 flex flex-col bg-background",
          "sm:static sm:z-auto sm:w-[270px] sm:flex-none sm:gap-3.5 sm:border-l sm:bg-card sm:p-[18px]",
          className
        )}
        {...props}
      >
        {c.phone && (
          <div className="flex items-center px-3.5 py-2.5">
            <button
              ref={backButton}
              type="button"
              onClick={back}
              aria-label={backAriaLabel ?? `Back to ${backLabel.toLowerCase()}`}
              className={cn(
                "flex h-11 min-w-11 items-center rounded-md px-0.5 text-base focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
                LINK
              )}
            >
              <ChevronLeft className="h-5 w-5" aria-hidden />
              {backLabel}
            </button>
          </div>
        )}
        {item ? (
          <>
            <div className="grid gap-4 px-5 py-1.5 sm:flex sm:flex-col sm:gap-3.5 sm:p-0">
              <div className="flex items-center gap-2.5">
                {item.icon && <span className={cn(iconBox, "hidden h-[34px] w-[34px] rounded-[9px] sm:grid")}>{item.icon}</span>}
                <div className="min-w-0">
                  <div className="text-[22px] font-semibold tracking-[-0.02em] sm:text-[15px] sm:tracking-normal">{item.label}</div>
                  {(c.phone ? item.description : item.kind) && (
                    <div className="mt-1 text-[15px] text-muted-foreground sm:mt-0 sm:text-[12.5px]">
                      {c.phone ? item.description : item.kind}
                    </div>
                  )}
                </div>
              </div>
              {children ? (
                children(item)
              ) : (
                item.description && !c.phone && <div className="text-[12.5px] leading-normal text-muted-foreground">{item.description}</div>
              )}
            </div>
            <div className="mt-auto grid gap-2 px-4 pb-[max(30px,env(safe-area-inset-bottom))] pt-3.5 sm:gap-1 sm:p-0">
              {actions.map((action, i) => (
                <button
                  key={action.id || i}
                  type="button"
                  disabled={item.disabled}
                  data-agent-part={c.part}
                  onClick={(event) => press(action, event)}
                  className={cn(
                    "flex items-center justify-center rounded-[13px] border bg-card p-3.5 text-base",
                    "sm:justify-between sm:rounded-[9px] sm:bg-background sm:px-2.5 sm:py-[7px] sm:text-[13px]",
                    i === 0 && "border-transparent bg-primary text-primary-foreground sm:bg-primary"
                  )}
                >
                  {action.label}
                  {i < 2 && (
                    <CommandPaletteShortcut
                      className={cn(
                        "hidden sm:inline-block",
                        i === 0 && "border-[color:color-mix(in_srgb,var(--primary-foreground)_35%,transparent)] bg-transparent text-inherit"
                      )}
                    >
                      {i === 0 ? "↵" : `${mod} ↵`}
                    </CommandPaletteShortcut>
                  )}
                </button>
              ))}
            </div>
          </>
        ) : (
          <div className="text-[12.5px] leading-normal text-muted-foreground">{placeholder}</div>
        )}
      </div>
    )
  }
)
CommandPalettePreview.displayName = "CommandPalettePreview"

export {
  CommandPalette,
  CommandPaletteTrigger,
  CommandPaletteContent,
  CommandPaletteInput,
  CommandPaletteBody,
  CommandPaletteList,
  CommandPaletteItem,
  CommandPaletteShortcut,
  CommandPalettePreview,
}
export type { CommandPaletteInputProps, CommandPaletteItemProps, CommandPaletteListProps, CommandPalettePreviewProps }
```
