# Command

A ⌘K search over actions and records, with recent items, groups and server results.

- Family: overlay
- Import: `import { Command } from "@senvenli/react/overlay";`
- Page: https://design.senvenli.com/docs/components/command
- JSON: https://design.senvenli.com/docs/components/command.json

## Example: Actions and pages

Opened by a button; it closes when an item is chosen.

```tsx
import { Button } from "@senvenli/react/action";
import { Text } from "@senvenli/react/display";
import { Stack } from "@senvenli/react/layout";
import { Command, type CommandGroup } from "@senvenli/react/overlay";
import { useState } from "react";

const GROUPS: CommandGroup[] = [
  {
    id: "actions",
    title: "Actions",
    items: [
      { id: "create-order", label: "Create order", icon: "plus", shortcut: "N", kind: "action" },
      { id: "send-invoice", label: "Send invoice", icon: "send", kind: "action" },
      { id: "invite-member", label: "Invite team member", icon: "user-plus", kind: "action", keywords: ["add user"] },
    ],
  },
  {
    id: "pages",
    title: "Pages",
    items: [
      { id: "orders", label: "Orders", icon: "shopping-cart", kind: "page" },
      { id: "customers", label: "Customers", icon: "users", kind: "page" },
      { id: "settings", label: "Settings", icon: "settings", kind: "page" },
    ],
  },
];

export default function Example() {
  const [isOpen, setIsOpen] = useState(false);
  const [chosen, setChosen] = useState("nothing yet");
  return (
    <Stack direction="horizontal" space="space-150">
      <Button iconBefore="search" onPress={() => setIsOpen(true)}>
        Search
      </Button>
      <Text tone="subtle">Chosen: {chosen}</Text>
      <Command isOpen={isOpen} onOpenChange={setIsOpen} groups={GROUPS} onSelect={(item) => setChosen(item.label)} />
    </Stack>
  );
}
```

## Examples

### With ⌘K and recent items

The shortcut prop binds ⌘K (Ctrl K elsewhere); recent records show while the query is empty.

```tsx
import { Button } from "@senvenli/react/action";
import { Kbd, Text } from "@senvenli/react/display";
import { Stack } from "@senvenli/react/layout";
import { Command, toast, type CommandGroup, type CommandItem } from "@senvenli/react/overlay";
import { useState } from "react";

const GROUPS: CommandGroup[] = [
  {
    id: "actions",
    title: "Actions",
    items: [
      { id: "create-invoice", label: "Create invoice", icon: "plus", shortcut: "I", kind: "action" },
      { id: "export", label: "Export customers", icon: "download", kind: "action" },
    ],
  },
];

const RECENT: CommandItem[] = [
  { id: "inv-2041", label: "Analytical Engines Ltd", detail: "INV-2041", kind: "record", status: { label: "Paid", tone: "success" } },
  { id: "inv-2038", label: "Northwind Traders", detail: "INV-2038", kind: "record", status: { label: "Overdue", tone: "danger" } },
];

export default function Example() {
  const [isOpen, setIsOpen] = useState(false);
  return (
    <Stack direction="horizontal" space="space-150">
      <Button onPress={() => setIsOpen(true)}>Open the palette</Button>
      <Text tone="subtle">
        or press <Kbd isModifier /> <Kbd>K</Kbd>
      </Text>
      <Command
        shortcut
        isOpen={isOpen}
        onOpenChange={setIsOpen}
        groups={GROUPS}
        recent={RECENT}
        onSelect={(item) => toast(`Opened ${item.detail ?? item.label}`)}
      />
    </Stack>
  );
}
```

### Records from the server

The search prop finds records as people type; they show before the actions.

```tsx
import { Button } from "@senvenli/react/action";
import { Command, toast, type CommandGroup, type CommandItem } from "@senvenli/react/overlay";
import { useState } from "react";

const GROUPS: CommandGroup[] = [
  { id: "actions", title: "Actions", items: [{ id: "create-customer", label: "Create customer", icon: "user-plus", kind: "action" }] },
];

const CUSTOMERS: CommandItem[] = [
  { id: "ada", label: "Ada Lovelace", detail: "CUS-0101", icon: "user", kind: "record", keywords: ["ada@example.com"] },
  { id: "alan", label: "Alan Turing", detail: "CUS-0102", icon: "user", kind: "record", keywords: ["alan@example.com"] },
  { id: "grace", label: "Grace Hopper", detail: "CUS-0103", icon: "user", kind: "record", keywords: ["grace@example.com"] },
];

// Stands in for `fetch("/api/search?q=…", { signal })`.
async function search(query: string): Promise<CommandGroup[]> {
  await new Promise((resolve) => setTimeout(resolve, 300));
  const text = query.toLowerCase();
  const items = CUSTOMERS.filter((customer) => [customer.label, customer.detail, ...(customer.keywords ?? [])].join(" ").toLowerCase().includes(text));
  return items.length ? [{ id: "customers", title: "Customers", items }] : [];
}

export default function Example() {
  const [isOpen, setIsOpen] = useState(false);
  return (
    <>
      <Button iconBefore="search" onPress={() => setIsOpen(true)}>
        Find a customer
      </Button>
      <Command isOpen={isOpen} onOpenChange={setIsOpen} groups={GROUPS} search={search} onSelect={(item) => toast(`Opened ${item.label}`)} />
    </>
  );
}
```

## Guidelines

### When to use it

- An app with many pages and actions people want to reach by typing.

### Use something else when

- [combobox](https://design.senvenli.com/docs/components/combobox.md): People pick a value for a field, not an action or a record.
- [menu](https://design.senvenli.com/docs/components/menu.md): A short fixed list of actions under a button.

## Accessibility

Keyboard:

- Ctrl K / ⌘K: Opens and closes it when bound.
- Arrow keys, Enter: Moves and runs the focused item.

Built in:

- Matching ignores case and accents.
- Announces "No results".

You provide:

- Group names and item labels.

## API reference

### Command

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| isOpen | `boolean` |  | no |  |
| defaultOpen | `boolean` |  | no |  |
| onOpenChange | `(isOpen: boolean) => void` |  | no |  |
| groups | `readonly CommandGroup[]` |  | yes | Actions and pages, most used first; matched here, diacritic-insensitive. |
| search | `(query: string, signal: AbortSignal) => Promise<readonly CommandGroup[]>` |  | no | Records found by the server (codes, order numbers, names, phones); shown before the groups. |
| recent | `readonly CommandItem[]` | `[]` | no | Shown while the query is empty, above the five most used actions. |
| onSelect | `(item: CommandItem) => void` |  | yes |  |
| shortcut | `boolean` |  | no | Bind ⌘K / Ctrl K to open and close it. |

### Tokens it reads

`--ds-background-neutral-subtle-hovered`, `--ds-background-selected`, `--ds-blanket`, `--ds-border`, `--ds-icon-subtle`, `--ds-radius-medium`, `--ds-radius-xlarge`, `--ds-shadow-overlay`, `--ds-space-050`, `--ds-space-100`, `--ds-space-150`, `--ds-space-200`, `--ds-space-300`, `--ds-surface-overlay`, `--ds-surface-sunken`, `--ds-text`, `--ds-text-subtle`, `--ds-text-subtlest`, `--font-ds-mono`

## Related

- [combobox](https://design.senvenli.com/docs/components/combobox.md)
