Command for Astro
Searchable Astro command lists for navigation and page actions.
Installation
bunx --bun shadcn@latest add @gnostic/commandnpx shadcn@latest add @gnostic/commandpnpm dlx shadcn@latest add @gnostic/commandInstall component packages
bun add @data-slot/commandnpm install @data-slot/commandpnpm add @data-slot/commandCopy the files
src/components/ui/command/command-dialog.astrosrc/components/ui/command/command-empty.astrosrc/components/ui/command/command-group.astrosrc/components/ui/command/command-input.astrosrc/components/ui/command/command-item.astrosrc/components/ui/command/command-list.astrosrc/components/ui/command/command-separator.astrosrc/components/ui/command/command-shortcut.astrosrc/components/ui/command/command.astrosrc/components/ui/command/index.tssrc/components/ui/command/types.ts
Usage
import {
Command,
CommandDialog,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
CommandSeparator,
CommandShortcut,
} from "@/components/ui/command"
<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:
Command
├── CommandInput
└── CommandList
├── CommandEmpty
├── CommandGroup
│ ├── CommandItem
│ └── CommandItem
├── CommandSeparator
└── CommandGroup
├── CommandItem
└── CommandItem
Examples
Basic
A simple command menu in a dialog.
With Groups
A command menu with groups, icons and separators.
Many Groups & Items
Scrollable command menu with multiple items.
API Reference
CommandProps
label?string
Accessible label announced for the command input
stringdefaultValue?string
Initial active item value
stringdefaultSearch?string
Initial search input value
stringshouldFilter?boolean
Disable built-in filtering and sorting
booleanloop?boolean
Wrap arrow-key navigation
booleandisablePointerSelection?boolean
Disable pointer-move selection
booleanvimBindings?boolean
Enable ctrl+j/k/n/p shortcuts
booleantrueSee the data-slot docs 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 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.