---
title: Command for Astro
description: "Searchable Astro command lists for navigation and page actions."
seo:
    title: Command for Astro | Shadcn & Tailwind CSS
related:
    - /components/dropdown-menu
    - /components/combobox
sidebar:
    label: "Command"
---

```astro
---
import CalculatorIcon from "@lucide/astro/icons/calculator"
import CalendarIcon from "@lucide/astro/icons/calendar"
import CreditCardIcon from "@lucide/astro/icons/credit-card"
import SettingsIcon from "@lucide/astro/icons/settings"
import SmileIcon from "@lucide/astro/icons/face-slightly-smiling"
import UserIcon from "@lucide/astro/icons/user"

import { Card, CardContent } from "@/components/ui/card"
import {
  Command,
  CommandEmpty,
  CommandGroup,
  CommandInput,
  CommandItem,
  CommandList,
  CommandSeparator,
  CommandShortcut,
} from "@/components/ui/command"
---

<Card class="w-full max-w-md p-0">
  <CardContent class="p-0">
    <Command>
      <CommandInput placeholder="Search commands..." />
      <CommandList>
        <CommandEmpty>No results found.</CommandEmpty>
        <CommandGroup heading="Suggestions">
          <CommandItem>
            <CalendarIcon />
            Calendar
          </CommandItem>
          <CommandItem>
            <SmileIcon />
            Search Emoji
          </CommandItem>
          <CommandItem>
            <CalculatorIcon />
            Calculator
          </CommandItem>
        </CommandGroup>
        <CommandSeparator />
        <CommandGroup heading="Settings">
          <CommandItem>
            <UserIcon />
            Profile
            <CommandShortcut>⌘P</CommandShortcut>
          </CommandItem>
          <CommandItem>
            <CreditCardIcon />
            Billing
            <CommandShortcut>⌘B</CommandShortcut>
          </CommandItem>
          <CommandItem>
            <SettingsIcon />
            Settings
            <CommandShortcut>⌘S</CommandShortcut>
          </CommandItem>
        </CommandGroup>
      </CommandList>
    </Command>
  </CardContent>
</Card>
```

## Installation

### Command

```sh
bunx --bun shadcn@latest add @gnostic/command
```

### Manual

Complete the [Installation guide](/docs/installation) first. Then copy the component files below.

Install the component packages:

```sh
bun add @data-slot/command
```

#### src/components/ui/command/command-dialog.astro

```astro
---
import type { ComponentProps } from "astro/types"

import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogTitle,
} from "../dialog/index.ts"

type Props = ComponentProps<typeof Dialog> & {
  title?: string
  description?: string
  contentClass?: string
  showCloseButton?: boolean
}

const {
  title = "Command Palette",
  description = "Search for a command to run...",
  contentClass,
  showCloseButton = false,
  ...props
} = Astro.props
---

<Dialog {...props}>
  <DialogContent
    data-command-dialog
    class:list={["top-1/3 translate-y-0 overflow-hidden p-0 gap-0 **:data-[slot=command]:border-0 **:data-[slot=command]:bg-transparent **:data-[slot=command-input-wrapper]:h-12 [&_[data-slot=command-input-wrapper]_svg]:size-5 **:data-[slot=command-input]:h-12 **:data-[slot=command-group-heading]:text-muted-foreground **:data-[slot=command-group-heading]:px-2 **:data-[slot=command-group-heading]:font-medium [&_[data-slot=command-group]:not([hidden])_~[data-slot=command-group]]:pt-0", contentClass]}
    showCloseButton={showCloseButton}
  >
    <div class="sr-only">
      <DialogTitle>{title}</DialogTitle>
      <DialogDescription>{description}</DialogDescription>
    </div>
    <slot />
  </DialogContent>
</Dialog>
```

#### src/components/ui/command/command-empty.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, hidden = true, ...props } = Astro.props
---

<div
  data-slot="command-empty"
  hidden={hidden}
  class={cn("not-empty:py-6 text-center text-sm", className)}
  {...props}
>
  <slot>No results found.</slot>
</div>
```

#### src/components/ui/command/command-group.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div"> & {
  heading?: string
  value?: string
  forceMount?: boolean
}

const { class: className, heading, value, forceMount, ...props } = Astro.props
---

<div
  data-slot="command-group"
  data-value={value}
  data-force-mount={forceMount ? "true" : undefined}
  class={cn("text-foreground [&_[data-slot=command-group-heading]]:text-muted-foreground overflow-hidden p-1 [&_[data-slot=command-group-heading]]:px-2 [&_[data-slot=command-group-heading]]:py-1.5 [&_[data-slot=command-group-heading]]:text-xs [&_[data-slot=command-group-heading]]:font-medium", className)}
  {...props}
>
  {heading && <div data-slot="command-group-heading">{heading}</div>}
  <slot />
</div>
```

#### src/components/ui/command/command-input.astro

```astro
---
import type { HTMLAttributes } from "astro/types"
import SearchIcon from "@lucide/astro/icons/search"

import { cn } from "cn"

type Props = HTMLAttributes<"input"> & {
  wrapperClass?: string
}

const { class: className, wrapperClass, ...props } = Astro.props
---

<div
  data-slot="command-input-wrapper"
  class={cn("px-3 pt-2 pb-0", wrapperClass)}
>
  <div data-slot="command-input-group" class="flex h-12 items-center border-b border-border bg-transparent">
    <div class="flex items-center pl-2">
      <SearchIcon
        data-slot="command-input-icon"
        class="size-4.5 shrink-0 opacity-80 sm:size-4"
      />
    </div>
    <input
      type="text"
      data-slot="command-input"
      class={cn("placeholder:text-muted-foreground h-full min-w-0 flex-1 bg-transparent px-2.5 text-base outline-none sm:text-sm disabled:cursor-not-allowed disabled:opacity-64", className)}
      {...props}
    />
  </div>
</div>
```

#### src/components/ui/command/command-item.astro

```astro
---
import type { HTMLAttributes } from "astro/types"
import CheckIcon from "@lucide/astro/icons/check"

import { cn } from "cn"

type Props = HTMLAttributes<"a"> & {
  disabled?: boolean
  href?: string
  value?: string
  label?: string
  keywords?: string | string[]
  forceMount?: boolean
}

const {
  class: className,
  disabled,
  href,
  value,
  label,
  keywords,
  forceMount,
  ...props
} = Astro.props

const Tag = href ? "a" : "div"
const keywordsValue = Array.isArray(keywords) ? keywords.join(", ") : keywords
---

<Tag
  data-slot="command-item"
  data-disabled={disabled ? "true" : undefined}
  data-value={value}
  data-label={label}
  data-keywords={keywordsValue}
  data-force-mount={forceMount ? "true" : undefined}
  href={href}
  class={cn("flex min-h-8 cursor-default select-none items-center gap-2 rounded-sm px-2 text-base text-foreground outline-none data-disabled:pointer-events-none data-selected:bg-accent data-inset:ps-8 data-[variant=destructive]:text-destructive-foreground data-selected:text-accent-foreground data-disabled:opacity-64 sm:min-h-7 sm:text-sm [&>svg:not([class*='opacity-'])]:opacity-80 [&>svg:not([class*='size-'])]:size-4.5 sm:[&>svg:not([class*='size-'])]:size-4 [&>svg]:pointer-events-none [&>svg]:-mx-0.5 [&>svg]:shrink-0 relative py-1.5 [&[href]]:cursor-pointer [&[href]]:no-underline group/command-item", className)}
  {...props}
>
  <slot />
  <CheckIcon class="ml-auto opacity-0 group-has-data-[slot=command-shortcut]/command-item:hidden group-data-selected/command-item:opacity-100" />
</Tag>
```

#### src/components/ui/command/command-list.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, ...props } = Astro.props
---

<div
  data-slot="command-list"
  class={cn("not-empty:scroll-py-2 not-empty:p-2 max-h-72 overflow-x-hidden overflow-y-auto", className)}
  {...props}
>
  <slot />
</div>
```

#### src/components/ui/command/command-separator.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, ...props } = Astro.props
---

<div
  data-slot="command-separator"
  class={cn("my-2 h-px bg-border", className)}
  {...props}
></div>
```

#### src/components/ui/command/command-shortcut.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"span">

const { class: className, ...props } = Astro.props
---

<span
  data-slot="command-shortcut"
  class={cn("ms-auto font-medium font-sans text-muted-foreground/72 text-xs tracking-widest", className)}
  {...props}
>
  <slot />
</span>
```

#### src/components/ui/command/command.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"
import type { CommandProps } from "./types.ts"

type Props = HTMLAttributes<"div"> & CommandProps

const {
  class: className,
  label,
  defaultValue,
  defaultSearch,
  shouldFilter,
  loop,
  disablePointerSelection,
  vimBindings,
  ...props
} = Astro.props
---

<div
  data-slot="command"
  data-label={label}
  data-default-value={defaultValue}
  data-default-search={defaultSearch}
  data-should-filter={shouldFilter}
  data-loop={loop}
  data-disable-pointer-selection={disablePointerSelection}
  data-vim-bindings={vimBindings}
  class={cn("relative flex size-full flex-col overflow-hidden rounded-2xl bg-popover text-popover-foreground", className)}
  {...props}
>
  <slot />
</div>

<script>
  import { create } from "@data-slot/command"

  const initCommand = () => {
    create()

    document.querySelectorAll("[data-slot=command]").forEach((command) => {
      if (!(command instanceof HTMLElement)) return
      if (command.dataset.linkNavigation === "true") return

      command.dataset.linkNavigation = "true"
      command.addEventListener("command:select", (event) => {
        const { detail } = event as CustomEvent<{ value?: unknown }>
        const value = detail?.value
        if (typeof value !== "string") return

        const item = Array.from(
          command.querySelectorAll("[data-slot=command-item][href]"),
        ).find((item) => item.getAttribute("data-value") === value)
        const href = item?.getAttribute("href")

        if (href) window.location.href = href
      })
    })
  }

  initCommand()

  document.addEventListener("astro:page-load", initCommand)
</script>
```

#### src/components/ui/command/index.ts

```ts
export { default as Command } from './command.astro'
export { default as CommandDialog } from './command-dialog.astro'
export { default as CommandInput } from './command-input.astro'
export { default as CommandList } from './command-list.astro'
export { default as CommandEmpty } from './command-empty.astro'
export { default as CommandGroup } from './command-group.astro'
export { default as CommandItem } from './command-item.astro'
export { default as CommandSeparator } from './command-separator.astro'
export { default as CommandShortcut } from './command-shortcut.astro'
export type { CommandProps } from './types.ts'
```

#### src/components/ui/command/types.ts

```ts
import type { CommandOptions } from '@data-slot/command'

/**
 * Command configuration rendered as data attributes.
 */
export type CommandProps = Pick<
    CommandOptions,
    | 'label'
    | 'defaultValue'
    | 'defaultSearch'
    | 'shouldFilter'
    | 'loop'
    | 'disablePointerSelection'
    | 'vimBindings'
>
```

Component classes use the preset configured during project setup. Select the same preset in the docs before copying Manual source. To change installed component classes, edit or reinstall their source. See [Theming](/docs/theming) for details.


## Usage

```ts
import {
    Command,
    CommandDialog,
    CommandEmpty,
    CommandGroup,
    CommandInput,
    CommandItem,
    CommandList,
    CommandSeparator,
    CommandShortcut,
} from "@/components/ui/command"
```

```astro
<Command class="ring-foreground/10 max-w-sm rounded-lg ring-1">
  <CommandInput placeholder="Search commands..." />
  <CommandList>
    <CommandEmpty>No results found.</CommandEmpty>
    <CommandGroup heading="Suggestions">
      <CommandItem>Calendar</CommandItem>
      <CommandItem>Search Emoji</CommandItem>
      <CommandItem>Calculator</CommandItem>
    </CommandGroup>
    <CommandSeparator />
    <CommandGroup heading="Settings">
      <CommandItem>Profile</CommandItem>
      <CommandItem>Billing</CommandItem>
      <CommandItem>Settings</CommandItem>
    </CommandGroup>
  </CommandList>
</Command>
```

## Composition

Arrange the parts as shown for a `Command`:

```text
Command
├── CommandInput
└── CommandList
    ├── CommandEmpty
    ├── CommandGroup
    │   ├── CommandItem
    │   └── CommandItem
    ├── CommandSeparator
    └── CommandGroup
        ├── CommandItem
        └── CommandItem
```

## Examples

### Basic

A simple command menu in a dialog.

```astro
---
import CalculatorIcon from "@lucide/astro/icons/calculator"
import CalendarIcon from "@lucide/astro/icons/calendar"
import SearchIcon from "@lucide/astro/icons/search"

import {
  Command,
  CommandEmpty,
  CommandGroup,
  CommandInput,
  CommandItem,
  CommandList,
} from "@/components/ui/command"
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "@/components/ui/dialog"
---

<Dialog>
  <DialogTrigger variant="outline">Open Command Menu</DialogTrigger>
  <DialogContent
    class="top-1/3 translate-y-0 overflow-hidden rounded-xl! p-0"
    showCloseButton={false}
  >
    <DialogHeader class="sr-only">
      <DialogTitle>Command Palette</DialogTitle>
      <DialogDescription>Search for a command to run...</DialogDescription>
    </DialogHeader>
    <Command>
      <CommandInput placeholder="Search commands..." />
      <CommandList>
        <CommandEmpty>No results found.</CommandEmpty>
        <CommandGroup heading="Suggestions">
          <CommandItem>
            <CalendarIcon />
            Calendar
          </CommandItem>
          <CommandItem>
            <SearchIcon />
            Search Emoji
          </CommandItem>
          <CommandItem>
            <CalculatorIcon />
            Calculator
          </CommandItem>
        </CommandGroup>
      </CommandList>
    </Command>
  </DialogContent>
</Dialog>
```

```astro
---
import KeyboardIcon from "@lucide/astro/icons/keyboard"
import MailIcon from "@lucide/astro/icons/mail"
import RocketIcon from "@lucide/astro/icons/rocket"
import SettingsIcon from "@lucide/astro/icons/settings"

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

<Command class="ring-foreground/10 w-full max-w-md ring-1">
  <CommandInput placeholder="Search shortcuts..." />
  <CommandList>
    <CommandEmpty>No results found.</CommandEmpty>
    <CommandGroup heading="Quick actions">
      <CommandItem>
        <MailIcon />
        Send email
        <CommandShortcut>⌘E</CommandShortcut>
      </CommandItem>
      <CommandItem>
        <RocketIcon />
        Deploy project
        <CommandShortcut>⌘D</CommandShortcut>
      </CommandItem>
      <CommandItem>
        <SettingsIcon />
        Open settings
        <CommandShortcut>⌘,</CommandShortcut>
      </CommandItem>
      <CommandItem>
        <KeyboardIcon />
        Keyboard shortcuts
        <CommandShortcut>?</CommandShortcut>
      </CommandItem>
    </CommandGroup>
  </CommandList>
</Command>
```

### With Groups

A command menu with groups, icons and separators.

```astro
---
import BoltIcon from "@lucide/astro/icons/bolt"
import CalendarIcon from "@lucide/astro/icons/calendar"
import CreditCardIcon from "@lucide/astro/icons/credit-card"
import FileTextIcon from "@lucide/astro/icons/file-text"
import FolderIcon from "@lucide/astro/icons/folder"
import SettingsIcon from "@lucide/astro/icons/settings"
import UserIcon from "@lucide/astro/icons/user"

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

<Command class="ring-foreground/10 w-full max-w-md ring-1">
  <CommandInput placeholder="Search actions..." />
  <CommandList>
    <CommandEmpty>No results found.</CommandEmpty>
    <CommandGroup heading="Suggestions">
      <CommandItem>
        <CalendarIcon />
        Calendar
      </CommandItem>
      <CommandItem>
        <FileTextIcon />
        Notes
      </CommandItem>
      <CommandItem>
        <FolderIcon />
        Projects
      </CommandItem>
    </CommandGroup>
    <CommandSeparator />
    <CommandGroup heading="Account">
      <CommandItem>
        <UserIcon />
        Profile
      </CommandItem>
      <CommandItem>
        <CreditCardIcon />
        Billing
      </CommandItem>
      <CommandItem>
        <SettingsIcon />
        Settings
      </CommandItem>
      <CommandItem>
        <BoltIcon />
        API Keys
      </CommandItem>
    </CommandGroup>
  </CommandList>
</Command>
```

### Many Groups & Items

Scrollable command menu with multiple items.

```astro
---
import ClockIcon from "@lucide/astro/icons/clock"

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

const recentSearches = Array.from({ length: 18 }, (_, index) => ({
  label: `Recent search ${index + 1}`,
  value: `recent-search-${index + 1}`,
}))
---

<Command class="ring-foreground/10 h-[24.5rem] w-full max-w-md ring-1">
  <CommandInput placeholder="Search recent activity..." />
  <CommandList>
    <CommandEmpty>No results found.</CommandEmpty>
    <CommandGroup heading="Recent">
      {recentSearches.map((item) => (
        <CommandItem value={item.value}>
          <ClockIcon />
          {item.label}
        </CommandItem>
      ))}
    </CommandGroup>
  </CommandList>
</Command>
```

## API Reference

### CommandProps

<AutoTypeTable path="../../packages/ui/src/components/ui/command/types.ts" name="CommandProps" />

See the [data-slot docs](https://github.com/bejamas/data-slot/blob/main/packages/command/README.md) for more information on interactivity.

### Events

Dispatch these events on the command root. The root emits the outbound events.

#### Outbound events

| Event                   | Detail                      |
| ----------------------- | --------------------------- |
| `command:change`        | `{ value: string \| null }` |
| `command:search-change` | `{ search: string }`        |
| `command:select`        | `{ value: string }`         |

#### Inbound events

| Event         | Detail                                        |
| ------------- | --------------------------------------------- |
| `command:set` | `{ value?: string \| null, search?: string }` |

Read the [installed source](/r/command.json) for the complete prop types.

## When to use Command

Use Command for a labelled list of actions that can be filtered. Group related commands and give each item a useful search value. CommandDialog places the list in an existing modal dialog.

## Keyboard and behavior

The data-slot controller handles list navigation and selection. Your code handles the selected action. A displayed shortcut does not register a keyboard listener. If you add a global shortcut, avoid conflicts with text input and browser commands.

## Theme and layout

Shared popover colors and item focus styles keep the list readable in both modes. Keep descriptions brief and test an empty search. Large or remote command sets need their own data and loading logic.
