# Command

> A command palette built with Dialog and Autocomplete for searching and running commands.

Canonical URL: https://taut-ui.com/docs/components/command

```tsx
"use client";

import { useState } from "react";
import { Button } from "@/components/ui/button";
import {
  Command,
  CommandCollection,
  CommandDialog,
  CommandDialogPopup,
  CommandDialogTrigger,
  CommandEmpty,
  CommandGroup,
  CommandGroupLabel,
  CommandInput,
  CommandItem,
  CommandList,
  CommandPanel,
} from "@/components/ui/command";

interface Item {
  value: string;
  label: string;
}

interface Group {
  value: string;
  items: Item[];
}

const groups: Group[] = [
  {
    items: [
      { label: "Calendar", value: "calendar" },
      { label: "Search Emoji", value: "search-emoji" },
      { label: "Calculator", value: "calculator" },
    ],
    value: "Suggestions",
  },
  {
    items: [
      { label: "Profile", value: "profile" },
      { label: "Billing", value: "billing" },
      { label: "Settings", value: "settings" },
    ],
    value: "Account",
  },
];

export default function Particle() {
  const [open, setOpen] = useState(false);

  return (
    <CommandDialog onOpenChange={setOpen} open={open}>
      <CommandDialogTrigger render={<Button variant="outline" />}>
        Open Command
      </CommandDialogTrigger>
      <CommandDialogPopup aria-label="Command palette">
        <Command items={groups}>
          <CommandInput placeholder="Type a command or search…" />
          <CommandPanel>
            <CommandEmpty>No results found.</CommandEmpty>
            <CommandList>
              {(group: Group) => (
                <CommandGroup items={group.items} key={group.value}>
                  <CommandGroupLabel>{group.value}</CommandGroupLabel>
                  <CommandCollection>
                    {(item: Item) => (
                      <CommandItem
                        key={item.value}
                        onClick={() => setOpen(false)}
                        value={item.value}
                      >
                        {item.label}
                      </CommandItem>
                    )}
                  </CommandCollection>
                </CommandGroup>
              )}
            </CommandList>
          </CommandPanel>
        </Command>
      </CommandDialogPopup>
    </CommandDialog>
  );
}
```

## Installation [#installation]

**CLI**

```bash
npx shadcn@latest add https://taut-ui.com/r/command.json
```

**Manual**

- Install the following dependencies:

```bash
npm install @base-ui/react lucide-react
```

- Copy and paste the following code into your project.

```tsx title="components/ui/command.tsx"
"use client";

import { Dialog as CommandDialogPrimitive } from "@base-ui/react/dialog";
import { SearchIcon } from "lucide-react";
import type * as React from "react";
import { Ripple } from "@/lib/ripple";
import { cn } from "@/lib/utils";
import {
  Autocomplete,
  AutocompleteCollection,
  AutocompleteEmpty,
  AutocompleteGroup,
  AutocompleteGroupLabel,
  AutocompleteInput,
  AutocompleteItem,
  AutocompleteList,
  AutocompleteSeparator,
} from "@/components/ui/autocomplete";

export const CommandDialog: typeof CommandDialogPrimitive.Root =
  CommandDialogPrimitive.Root;

export const CommandDialogPortal: typeof CommandDialogPrimitive.Portal =
  CommandDialogPrimitive.Portal;

export const CommandCreateHandle: typeof CommandDialogPrimitive.createHandle =
  CommandDialogPrimitive.createHandle;

export function CommandDialogTrigger(
  props: CommandDialogPrimitive.Trigger.Props,
): React.ReactElement {
  return (
    <CommandDialogPrimitive.Trigger
      data-slot="command-dialog-trigger"
      {...props}
    />
  );
}

export function CommandDialogBackdrop({
  className,
  ...props
}: CommandDialogPrimitive.Backdrop.Props): React.ReactElement {
  return (
    <CommandDialogPrimitive.Backdrop
      className={cn(
        "fixed inset-0 z-50 bg-black/32 backdrop-blur-xs transition-opacity duration-150 ease-out data-ending-style:opacity-0 data-starting-style:opacity-0 motion-reduce:transition-none",
        className,
      )}
      data-slot="command-dialog-backdrop"
      {...props}
    />
  );
}

export function CommandDialogViewport({
  className,
  ...props
}: CommandDialogPrimitive.Viewport.Props): React.ReactElement {
  return (
    <CommandDialogPrimitive.Viewport
      className={cn(
        "fixed inset-0 z-50 flex flex-col items-center p-4 sm:p-6 sm:pt-[12dvh]",
        className,
      )}
      data-slot="command-dialog-viewport"
      {...props}
    />
  );
}

export function CommandDialogPopup({
  className,
  children,
  portalProps,
  ...props
}: CommandDialogPrimitive.Popup.Props & {
  portalProps?: CommandDialogPrimitive.Portal.Props;
}): React.ReactElement {
  return (
    <CommandDialogPortal {...portalProps}>
      <CommandDialogBackdrop />
      <CommandDialogViewport>
        <CommandDialogPrimitive.Popup
          className={cn(
            "relative isolate row-start-1 flex max-h-124 min-h-0 w-full min-w-70 max-w-xl origin-center flex-col overflow-hidden rounded-3xl bg-[color-mix(in_oklab,var(--color-popover),var(--color-foreground)_5%)] text-popover-foreground shadow-[0_12px_36px_rgb(0_0_0/0.24),0_2px_8px_rgb(0_0_0/0.12)] outline-none will-change-transform dark:bg-popover",
            "scale-[calc(1-0.05*var(--nested-dialogs))] opacity-[calc(1-var(--nested-dialogs))]",
            "[transition-duration:350ms,150ms,350ms] [transition-property:scale,opacity,translate] [transition-timing-function:linear(0,0.42_6%,0.86_14%,1.06_24%,1.07_32%,1.02_46%,0.995_60%,1),ease-out,linear(0,0.42_6%,0.86_14%,1.06_24%,1.07_32%,1.02_46%,0.995_60%,1)]",
            "data-ending-style:scale-85 data-starting-style:scale-85 data-ending-style:opacity-0 data-starting-style:opacity-0 data-ending-style:duration-150 data-ending-style:ease-out motion-reduce:transition-none",
            className,
          )}
          data-slot="command-dialog-popup"
          {...props}
        >
          {children}
        </CommandDialogPrimitive.Popup>
      </CommandDialogViewport>
    </CommandDialogPortal>
  );
}

export function Command({
  autoHighlight = "always",
  keepHighlight = true,
  ...props
}: React.ComponentProps<typeof Autocomplete>): React.ReactElement {
  return (
    <Autocomplete
      autoHighlight={autoHighlight}
      inline
      keepHighlight={keepHighlight}
      open
      {...props}
    />
  );
}

export function CommandInput({
  className,
  placeholder = undefined,
  variant = "filled",
  ...props
}: React.ComponentProps<typeof AutocompleteInput>): React.ReactElement {
  return (
    <div className="p-2 pb-1.5">
      <AutocompleteInput
        autoFocus
        className={cn(
          "bg-background shadow-none focus-within:shadow-[inset_0_0_0_0.125rem_var(--color-primary)] data-popup-open:shadow-none data-popup-open:focus-within:shadow-[inset_0_0_0_0.125rem_var(--color-primary)]",
          className,
        )}
        placeholder={placeholder}
        startAddon={<SearchIcon />}
        variant={variant}
        {...props}
      />
    </div>
  );
}

export function CommandPanel({
  className,
  ...props
}: React.ComponentProps<"div">): React.ReactElement {
  return (
    <div
      className={cn(
        "relative mx-2 not-has-[+[data-slot=command-footer]]:mb-2 min-h-0 flex-1 overflow-hidden rounded-2xl bg-background text-card-foreground",
        className,
      )}
      data-slot="command-panel"
      {...props}
    />
  );
}

export function CommandList({
  className,
  ...props
}: React.ComponentProps<typeof AutocompleteList>): React.ReactElement {
  return (
    <AutocompleteList
      className={cn(
        "max-h-80 not-empty:scroll-py-1.5 not-empty:p-1.5",
        className,
      )}
      data-slot="command-list"
      {...props}
    />
  );
}

export function CommandEmpty({
  className,
  ...props
}: React.ComponentProps<typeof AutocompleteEmpty>): React.ReactElement {
  return (
    <AutocompleteEmpty
      className={cn(
        "not-empty:py-10 text-center text-muted-foreground text-sm",
        className,
      )}
      data-slot="command-empty"
      {...props}
    />
  );
}

export function CommandGroup({
  className,
  ...props
}: React.ComponentProps<typeof AutocompleteGroup>): React.ReactElement {
  return (
    <AutocompleteGroup
      className={cn("[[role=group]+&]:mt-1", className)}
      data-slot="command-group"
      {...props}
    />
  );
}

export function CommandGroupLabel({
  className,
  ...props
}: React.ComponentProps<typeof AutocompleteGroupLabel>): React.ReactElement {
  return (
    <AutocompleteGroupLabel
      className={cn(
        "px-3 py-1.5 font-medium text-muted-foreground text-xs",
        className,
      )}
      data-slot="command-group-label"
      {...props}
    />
  );
}

export const CommandCollection: typeof AutocompleteCollection =
  AutocompleteCollection;

export function CommandItem({
  className,
  children,
  ...props
}: React.ComponentProps<typeof AutocompleteItem>): React.ReactElement {
  return (
    <AutocompleteItem
      className={cn(
        "group relative flex min-h-9 cursor-default select-none items-center gap-2.5 rounded-lg px-3 py-1.5 font-medium text-sm outline-none",
        "transition-[border-radius] duration-350 ease-[linear(0,0.42_6%,0.86_14%,1.06_24%,1.07_32%,1.02_46%,0.995_60%,1)] motion-reduce:transition-none",
        "pointer-coarse:min-h-10 hover:rounded-xl active:rounded-xl data-highlighted:rounded-xl",
        "before:pointer-events-none before:absolute before:inset-0 before:rounded-[inherit] before:bg-current before:opacity-0",
        "hover:before:opacity-8 focus-visible:before:opacity-10 active:before:opacity-10 data-highlighted:before:opacity-8 data-highlighted:active:before:opacity-10",
        "data-[variant=destructive]:text-destructive",
        "data-disabled:pointer-events-none data-inset:ps-9.5 data-disabled:opacity-38",
        "[&>svg:not([class*='size-'])]:size-4 [&>svg]:pointer-events-none [&>svg]:shrink-0 [&>svg]:text-muted-foreground data-highlighted:[&>svg]:text-foreground",
        className,
      )}
      data-slot="command-item"
      {...props}
    >
      {children}
      <Ripple hoverOpacity={0} />
    </AutocompleteItem>
  );
}

export function CommandSeparator({
  className,
  ...props
}: React.ComponentProps<typeof AutocompleteSeparator>): React.ReactElement {
  return (
    <AutocompleteSeparator
      className={cn("mx-2.5 my-1 h-px bg-border last:hidden", className)}
      data-slot="command-separator"
      {...props}
    />
  );
}

export function CommandShortcut({
  className,
  ...props
}: React.ComponentProps<"kbd">): React.ReactElement {
  return (
    <kbd
      className={cn(
        "ms-auto font-medium font-mono text-xs tracking-widest opacity-72",
        className,
      )}
      data-slot="command-shortcut"
      {...props}
    />
  );
}

export function CommandFooter({
  className,
  ...props
}: React.ComponentProps<"div">): React.ReactElement {
  return (
    <div
      className={cn(
        "flex items-center justify-between gap-4 px-4 py-2.5 text-muted-foreground text-xs",
        className,
      )}
      data-slot="command-footer"
      {...props}
    />
  );
}

export { CommandDialogPrimitive };
```

- Update the import paths to match your project setup.

## Usage [#usage]

```tsx
import { Button } from "@/components/ui/button"
import {
  Command,
  CommandCollection,
  CommandDialog,
  CommandDialogPopup,
  CommandDialogTrigger,
  CommandEmpty,
  CommandFooter,
  CommandGroup,
  CommandGroupLabel,
  CommandInput,
  CommandItem,
  CommandList,
  CommandPanel,
  CommandSeparator,
  CommandShortcut,
} from "@/components/ui/command"
```

```tsx
const items = [
  { value: "calendar", label: "Calendar" },
  { value: "search-emoji", label: "Search Emoji" },
  { value: "calculator", label: "Calculator" },
]

<CommandDialog>
  <CommandDialogTrigger render={<Button variant="outline" />}>
    Open Command
  </CommandDialogTrigger>
  <CommandDialogPopup aria-label="Command palette">
    <Command items={items}>
      <CommandInput placeholder="Type a command or search…" />
      <CommandPanel>
        <CommandEmpty>No results found.</CommandEmpty>
        <CommandList>
          {(item) => (
            <CommandItem key={item.value} value={item.value}>
              {item.label}
            </CommandItem>
          )}
        </CommandList>
      </CommandPanel>
    </Command>
  </CommandDialogPopup>
</CommandDialog>
```

## Examples [#examples]

### With Keyboard Shortcut [#with-keyboard-shortcut]

```tsx
"use client";

import { useEffect, useState } from "react";
import { Button } from "@/components/ui/button";
import {
  Command,
  CommandDialog,
  CommandDialogPopup,
  CommandDialogTrigger,
  CommandEmpty,
  CommandInput,
  CommandItem,
  CommandList,
  CommandPanel,
  CommandShortcut,
} from "@/components/ui/command";
import { Kbd, KbdGroup } from "@/components/ui/kbd";

interface Item {
  value: string;
  label: string;
  shortcut: string;
}

const items: Item[] = [
  { label: "New File", shortcut: "⌘N", value: "new-file" },
  { label: "Open Folder", shortcut: "⌘O", value: "open-folder" },
  { label: "Save", shortcut: "⌘S", value: "save" },
  { label: "Toggle Sidebar", shortcut: "⌘B", value: "toggle-sidebar" },
];

export default function Particle() {
  const [open, setOpen] = useState(false);

  useEffect(() => {
    const down = (e: KeyboardEvent): void => {
      if (e.key === "j" && (e.metaKey || e.ctrlKey)) {
        e.preventDefault();
        setOpen((prev) => !prev);
      }
    };

    document.addEventListener("keydown", down);
    return () => document.removeEventListener("keydown", down);
  }, []);

  return (
    <CommandDialog onOpenChange={setOpen} open={open}>
      <CommandDialogTrigger render={<Button variant="outline" />}>
        Search
        <KbdGroup>
          <Kbd>⌘</Kbd>
          <Kbd>J</Kbd>
        </KbdGroup>
      </CommandDialogTrigger>
      <CommandDialogPopup aria-label="Command palette">
        <Command items={items}>
          <CommandInput placeholder="Search commands…" />
          <CommandPanel>
            <CommandEmpty>No results found.</CommandEmpty>
            <CommandList>
              {(item: Item) => (
                <CommandItem
                  key={item.value}
                  onClick={() => setOpen(false)}
                  value={item.value}
                >
                  <span className="flex-1">{item.label}</span>
                  <CommandShortcut>{item.shortcut}</CommandShortcut>
                </CommandItem>
              )}
            </CommandList>
          </CommandPanel>
        </Command>
      </CommandDialogPopup>
    </CommandDialog>
  );
}
```

## API Reference [#api-reference]

### Command [#command]

The root component that wraps `Autocomplete` with command palette defaults: `inline`, `open={true}`, `autoHighlight="always"`, and `keepHighlight={true}`.

| Prop            | Type                            | Default    | Description                                          |
| --------------- | ------------------------------- | ---------- | ---------------------------------------------------- |
| `items`         | `readonly unknown[]`            | -          | Items (or groups) used to filter and render the list |
| `open`          | `boolean`                       | `true`     | Controls whether the command is open                 |
| `autoHighlight` | `boolean \| "always"`           | `"always"` | Controls automatic highlighting of items             |
| `keepHighlight` | `boolean`                       | `true`     | Keeps the highlighted item while the list updates    |
| `...props`      | Base UI Autocomplete Root props | -          | All other Autocomplete props are supported           |

### CommandDialog [#commanddialog]

Alias for `Dialog.Root` from Base UI.

| Prop           | Type                      | Description                                  |
| -------------- | ------------------------- | -------------------------------------------- |
| `open`         | `boolean`                 | Controls whether the dialog is open          |
| `onOpenChange` | `(open: boolean) => void` | Callback fired when the open state changes   |
| `...props`     | Base UI Dialog Root props | All standard dialog attributes are supported |

### CommandDialogTrigger [#commanddialogtrigger]

The trigger button that opens the command dialog.

| Prop        | Type                         | Description                                          |
| ----------- | ---------------------------- | ---------------------------------------------------- |
| `render`    | `React.ReactElement`         | Element to render as the trigger                     |
| `className` | `string`                     | Additional CSS classes                               |
| `...props`  | Base UI Dialog Trigger props | All standard dialog trigger attributes are supported |

### CommandDialogPopup [#commanddialogpopup]

The popup container that renders the command palette inside a dialog portal, backdrop, and viewport.

| Prop          | Type                       | Description                                        |
| ------------- | -------------------------- | -------------------------------------------------- |
| `className`   | `string`                   | Additional CSS classes                             |
| `portalProps` | `Dialog.Portal.Props`      | Props forwarded to the internal portal             |
| `...props`    | Base UI Dialog Popup props | All standard dialog popup attributes are supported |

### CommandInput [#commandinput]

The search input field with an integrated search icon.

| Prop          | Type                             | Default     | Description                                              |
| ------------- | -------------------------------- | ----------- | -------------------------------------------------------- |
| `placeholder` | `string`                         | -           | Placeholder text for the input                           |
| `variant`     | `"filled" \| "outline"`          | `"outline"` | Visual variant of the input                              |
| `className`   | `string`                         | -           | Additional CSS classes                                   |
| `...props`    | Base UI Autocomplete Input props | -           | All standard autocomplete input attributes are supported |

### CommandPanel [#commandpanel]

The bordered container that wraps `CommandList` and `CommandEmpty`.

| Prop        | Type                          | Description                                |
| ----------- | ----------------------------- | ------------------------------------------ |
| `className` | `string`                      | Additional CSS classes                     |
| `...props`  | `React.ComponentProps<"div">` | All standard div attributes are supported  |

### CommandList [#commandlist]

A scrollable container for command items. Wraps `AutocompleteList`.

| Prop        | Type                            | Description                                             |
| ----------- | ------------------------------- | ------------------------------------------------------- |
| `className` | `string`                        | Additional CSS classes                                  |
| `...props`  | Base UI Autocomplete List props | All standard autocomplete list attributes are supported |

### CommandEmpty [#commandempty]

Displays a message when no results are found.

| Prop        | Type                             | Description                                              |
| ----------- | -------------------------------- | -------------------------------------------------------- |
| `className` | `string`                         | Additional CSS classes                                   |
| `...props`  | Base UI Autocomplete Empty props | All standard autocomplete empty attributes are supported |

### CommandGroup [#commandgroup]

Groups related command items together. Wraps `AutocompleteGroup`.

| Prop        | Type                             | Description                                              |
| ----------- | -------------------------------- | -------------------------------------------------------- |
| `items`     | `readonly unknown[]`             | The array of items in this group                         |
| `className` | `string`                         | Additional CSS classes                                   |
| `...props`  | Base UI Autocomplete Group props | All standard autocomplete group attributes are supported |

### CommandGroupLabel [#commandgrouplabel]

Displays a label for a command group.

| Prop        | Type                                   | Description                                                    |
| ----------- | -------------------------------------- | -------------------------------------------------------------- |
| `className` | `string`                               | Additional CSS classes                                         |
| `...props`  | Base UI Autocomplete Group Label props | All standard autocomplete group label attributes are supported |

### CommandCollection [#commandcollection]

Used within `CommandGroup` to render grouped items. Alias for `AutocompleteCollection`.

| Prop       | Type                                  | Description                                                       |
| ---------- | ------------------------------------- | ----------------------------------------------------------------- |
| `...props` | Base UI Autocomplete Collection props | All standard autocomplete collection attributes are supported     |

### CommandItem [#commanditem]

An individual selectable command item. Wraps `AutocompleteItem`.

| Prop        | Type                            | Description                                             |
| ----------- | ------------------------------- | ------------------------------------------------------- |
| `value`     | `unknown`                       | The value of the item                                   |
| `className` | `string`                        | Additional CSS classes                                  |
| `...props`  | Base UI Autocomplete Item props | All standard autocomplete item attributes are supported |

### CommandSeparator [#commandseparator]

A visual divider between command groups or items.

| Prop        | Type                                 | Description                                                  |
| ----------- | ------------------------------------ | ------------------------------------------------------------ |
| `className` | `string`                             | Additional CSS classes                                       |
| `...props`  | Base UI Autocomplete Separator props | All standard autocomplete separator attributes are supported |

### CommandShortcut [#commandshortcut]

Displays keyboard shortcuts inside a `CommandItem`.

| Prop        | Type                          | Description                               |
| ----------- | ----------------------------- | ----------------------------------------- |
| `className` | `string`                      | Additional CSS classes                    |
| `...props`  | `React.ComponentProps<"kbd">` | All standard kbd attributes are supported |

### CommandFooter [#commandfooter]

A footer section for displaying hints or keyboard shortcuts below `CommandPanel`.

| Prop        | Type                          | Description                               |
| ----------- | ----------------------------- | ----------------------------------------- |
| `className` | `string`                      | Additional CSS classes                    |
| `...props`  | `React.ComponentProps<"div">` | All standard div attributes are supported |
