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/commandComponent classes: Default Gnostic.
Complete the Installation guide first. Then copy the component files below.
Install component packages
bun add @data-slot/commandCopy the files
src/components/ui/command/command-dialog.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
---
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
---
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
---
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
---
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
---
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
---
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
---
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
---
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
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
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'
>
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.