# Command

A command palette: a search field over a list that filters as you type, with arrow keys, Enter and a dialog version for ⌘K.

Docs: https://ballpoint.st9wd.com/docs/command

```bash
npx shadcn@latest add @ballpoint/command
```

```tsx
import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from "@/components/ui/command"

<Command>
  <CommandInput placeholder="Type a command or search…" />
  <CommandList>
    <CommandEmpty>No results found.</CommandEmpty>
    <CommandGroup heading="Suggestions">
      <CommandItem onSelect={() => …}>Calendar</CommandItem>
    </CommandGroup>
  </CommandList>
</Command>
```

Props:

- `shouldFilter`: `boolean` (default `true`). Set false to filter the list yourself, e.g. with results from a server.
- `filter`: `(value, search, keywords?) => number`. Scores an item against the search, 0 to 1; 0 hides it. The default prefers text that starts with the search, then text that contains it.
- `loop`: `boolean` (default `false`). Wrap from the last item to the first with the arrow keys.
- `framed`: `boolean` (default `false`). Pencils a box round the palette, for one set on the page. In a CommandDialog the dialog is the box.
- `CommandItem value / keywords`: `string / string[]`. What the search looks at. The value defaults to the item's text.
- `CommandItem onSelect`: `(value: string) => void`. Called when the item is chosen with Enter or a click.
- `CommandDialog`: `Dialog props`. The palette in a dialog; open and onOpenChange work as on Dialog.

## Example

```tsx
import { InkIcon } from "@/components/ui/ink-icons";
import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, CommandSeparator, CommandShortcut } from "@/components/ui/command";

export default function CommandDemo() {
  return (
    <Command framed seed="command-demo" className="w-full max-w-sm bg-popover">
      <CommandInput placeholder="Type a command or search…" />
      <CommandList>
        <CommandEmpty>No results found.</CommandEmpty>
        <CommandGroup heading="Suggestions">
          <CommandItem>
            <InkIcon name="calendar" /> Calendar
          </CommandItem>
          <CommandItem>
            <InkIcon name="smile" /> Search emoji
          </CommandItem>
          <CommandItem disabled>
            <InkIcon name="percent" /> Calculator
          </CommandItem>
        </CommandGroup>
        <CommandSeparator />
        <CommandGroup heading="Settings">
          <CommandItem>
            <InkIcon name="user" /> Profile <CommandShortcut>⌘P</CommandShortcut>
          </CommandItem>
          <CommandItem>
            <InkIcon name="credit-card" /> Billing <CommandShortcut>⌘B</CommandShortcut>
          </CommandItem>
          <CommandItem>
            <InkIcon name="settings" /> Settings <CommandShortcut>⌘S</CommandShortcut>
          </CommandItem>
        </CommandGroup>
      </CommandList>
    </Command>
  );
}
```

## In a dialog

Open the palette from anywhere with ⌘K.

```tsx
"use client";

import { useEffect, useState } from "react";
import { Button } from "@/components/ui/button";
import { CommandDialog, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, CommandShortcut } from "@/components/ui/command";
import { InkIcon } from "@/components/ui/ink-icons";
import { Kbd } from "@/components/ui/kbd";

export default function CommandDialogExample() {
  const [open, setOpen] = useState(false);
  useEffect(() => {
    const down = (event: KeyboardEvent) => {
      if (event.key === "k" && (event.metaKey || event.ctrlKey)) {
        event.preventDefault();
        setOpen((o) => !o);
      }
    };
    document.addEventListener("keydown", down);
    return () => document.removeEventListener("keydown", down);
  }, []);
  return (
    <>
      <Button variant="outline" onClick={() => setOpen(true)} seed="cd-open">
        Search <Kbd>⌘K</Kbd>
      </Button>
      <CommandDialog open={open} onOpenChange={setOpen} className="max-w-lg">
        <CommandInput placeholder="Search the notebook…" />
        <CommandList>
          <CommandEmpty>Nothing found.</CommandEmpty>
          <CommandGroup heading="Pages">
            <CommandItem onSelect={() => setOpen(false)}>
              <InkIcon name="file-text" /> Letter to Ada
            </CommandItem>
            <CommandItem onSelect={() => setOpen(false)}>
              <InkIcon name="map" /> Route home
            </CommandItem>
            <CommandItem onSelect={() => setOpen(false)}>
              <InkIcon name="list" /> Shopping list
            </CommandItem>
          </CommandGroup>
          <CommandGroup heading="Actions">
            <CommandItem onSelect={() => setOpen(false)}>
              <InkIcon name="plus" /> New page <CommandShortcut>⌘N</CommandShortcut>
            </CommandItem>
            <CommandItem onSelect={() => setOpen(false)}>
              <InkIcon name="settings" /> Open settings <CommandShortcut>⌘,</CommandShortcut>
            </CommandItem>
          </CommandGroup>
        </CommandList>
      </CommandDialog>
    </>
  );
}
```

## As a picker

Keywords help a search find an item by what it is, not only by its name.

```tsx
"use client";

import { useState } from "react";
import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from "@/components/ui/command";
import { InkGlyph } from "@/lib/ink-glyphs";

const papers = [
  { value: "cream", label: "Cream", keywords: ["warm", "default"] },
  { value: "legal", label: "Legal pad", keywords: ["yellow", "ruled"] },
  { value: "white", label: "White", keywords: ["plain"] },
  { value: "slip", label: "Torn slip", keywords: ["scrap", "ragged"] },
];

export default function CommandSelect() {
  const [paper, setPaper] = useState("cream");
  return (
    <div className="flex w-full max-w-sm flex-col gap-4">
      <Command framed seed="command-select" className="bg-popover">
        <CommandInput placeholder="Find a paper…" />
        <CommandList>
          <CommandEmpty>No paper like that.</CommandEmpty>
          <CommandGroup heading="Papers">
            {papers.map((p) => (
              <CommandItem key={p.value} value={p.label} keywords={p.keywords} onSelect={() => setPaper(p.value)}>
                {p.label}
                {paper === p.value && <InkGlyph name="check" className="ml-auto" />}
              </CommandItem>
            ))}
          </CommandGroup>
        </CommandList>
      </Command>
      <p className="text-ink-2" aria-live="polite">
        Writing on {papers.find((p) => p.value === paper)?.label.toLowerCase()}. Try &quot;yellow&quot; or &quot;scrap&quot;.
      </p>
    </div>
  );
}
```
