# Command

- **[Atoms](/components/)**

A search box over a list of actions, and the Cmd+K palette that puts it in a dialog.

## When to use

- **Use for**: A fast way to go anywhere or do anything by typing its name: the Cmd+K palette.
- **Not for**: Choosing one value in a form. Use Combobox. Navigation people need to see. Use Tabs or a menu.
- **Pairs with**: Dialog, Kbd, Button.

## Preview

command.tsx

```
import { Theme } from '@/components/klarhimmel/theme/style';
import { Command, CommandDialog } from '@/components/klarhimmel/ui/command';

export default function Example() {
  return (
    <Theme theme="brand">
      <CommandDialog
        open
        onOpenChange={() => {}}
        title="Command menu"
        aria-label="Commands"
        hotkey="k"
        onSelect={(value) => console.log(value)}
        groups={[{ heading: 'Go to', items: [{ value: 'rooms', label: 'Rooms' }, { value: 'guests', label: 'Guests' }] }]}
      />
    </Theme>
  );
}
```

## Import

- **CLI**

  ```
  npx shadcn add @klarhimmel/command
  ```
- **Import**

  ```
  import { Command, CommandDialog } from '@/components/klarhimmel/ui/command';
  ```

- **Files**: command.tsx, in components/klarhimmel/ui/. With it: kbd, theme, utils. npm: lucide-react, radix-ui.
- **Render in**: Render it inside <Theme>, once near the root of your app. Tokens and fonts are scoped to it, so outside it the component is unstyled.
- **Limit**: Overlays render into the Theme root with position: fixed. A transform on an ancestor re-parents fixed (a dialog centres in the wrapper) and overflow: hidden clips menus. Render <Theme> at the app root, outside any transformed wrapper.

## Examples

- **Inline**

  ```
  import { useState } from 'react';
  import { Command } from '@/components/klarhimmel/ui/command';
  
  const groups = [
    { heading: 'Rooms', items: [{ value: 'sea', label: 'Seaside suite' }, { value: 'garden', label: 'Garden room' }, { value: 'loft', label: 'Loft', keywords: ['attic'] }] },
    { heading: 'Treatments', items: [{ value: 'massage', label: 'Deep tissue massage' }, { value: 'sauna', label: 'Sauna for two' }] },
  ];
  
  export default function CommandInline() {
    const [last, setLast] = useState('');
    return (
      <div className="flex w-[min(28rem,calc(100cqw-3rem))] flex-col gap-3">
        <div className="overflow-hidden rounded-kh-xl border border-kh-line bg-kh-surface shadow-kh-1">
          <Command aria-label="Find a room or treatment" groups={groups} onSelect={setLast} placeholder="Find a room or a treatment…" />
        </div>
        <p role="status" className="text-kh-small text-kh-muted">{last ? `Chose ${last}.` : 'Type, then Up and Down, then Enter.'}</p>
      </div>
    );
  }
  ```
- **Empty**

  ```
  import { Command } from '@/components/klarhimmel/ui/command';
  
  export default function CommandEmpty() {
    return (
      <div className="w-[min(28rem,calc(100cqw-3rem))] overflow-hidden rounded-kh-xl border border-kh-line bg-kh-surface shadow-kh-1">
        <Command
          aria-label="Search the help centre"
          query="refund policy"
          groups={[{ heading: 'Help articles', items: [{ value: 'cancel', label: 'Cancelling a booking' }, { value: 'pay', label: 'Paying by invoice' }] }]}
          emptyText="No help articles match. Try fewer words."
        />
      </div>
    );
  }
  ```

## Props

- **Command.groups**: Required. A group is { heading?, items }; an item is { value, label, keywords?, shortcut?, icon?, disabled? }. Every word typed must match a label or a keyword. Shortcuts only remind; the app binds the keys.
- **Command.onSelect**: Called with the item's value on Enter or click.
- **Command.placeholder**: The box ('Type a command or search…') and the empty message ('No results'). Pass them translated.
- **Command.emptyText**: Shown when nothing matches.
- **Command.aria-label**: Required: names the search box and the list.
- **Command.resultsLabel**: What is announced as you type: default "3 results". Pass it translated.
- **Command.query**
- **Command.onQueryChange**
- **Command.autoFocus**
- **Command.className**: Extra classes, merged after the component's own.
- **CommandDialog.open**
- **CommandDialog.onOpenChange**
- **CommandDialog.title**: Required: names the dialog for assistive tech. Not shown.
- **CommandDialog.hotkey**: A letter that opens and closes it with Cmd or Ctrl held: 'k'.
- **CommandDialog.className**: Extra classes, merged after the component's own.
- **CommandDialog.aria-label**: Required: names the search box and the list.
- **CommandDialog.onSelect**: Called with the item's value on Enter or click.
- **CommandDialog.placeholder**: The box ('Type a command or search…') and the empty message ('No results'). Pass them translated.
- **CommandDialog.emptyText**: Shown when nothing matches.
- **CommandDialog.groups**: Required. A group is { heading?, items }; an item is { value, label, keywords?, shortcut?, icon?, disabled? }. Every word typed must match a label or a keyword. Shortcuts only remind; the app binds the keys.
- **CommandDialog.resultsLabel**: What is announced as you type: default "3 results". Pass it translated.

## Accessibility

- Cmd or Ctrl + K · Opens and closes the palette (hotkey)
- Type · Narrows the list; every word must match
- Down / Up · Moves the highlight, wrapping; disabled items are skipped
- Enter · Chooses the highlighted item
- Escape · Clears the text; again, closes the dialog

[Accessibility](/get-started/accessibility)

The WAI-ARIA combobox with an always-open listbox: focus stays in the box and aria-activedescendant follows the highlight. The number of results is announced as you type.

[Previous](/components/date-picker)

[Next](/components/context-menu)
