Command
A cmdk command menu and the command palette dialog.
Command is the list with its search field, for use inline. CommandDialog puts the same list in a dialog: the command palette. The item binds no shortcut; open the dialog from a button or from a key the product binds.
Inline
import { CalendarIcon, FilePlusIcon, FolderPlusIcon, ReceiptIcon, SettingsIcon } from "@keyline-icons/react"
import {
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
CommandSeparator,
CommandShortcut,
} from "@/components/ui/command"
export default function CommandInline() {
return (
<Command label="Commands" className="h-auto max-w-sm border">
<CommandInput placeholder="Search commands" />
<CommandList>
<CommandEmpty>No command matches.</CommandEmpty>
<CommandGroup heading="Create">
<CommandItem>
<FolderPlusIcon />
New project
<CommandShortcut>⌘N</CommandShortcut>
</CommandItem>
<CommandItem>
<FilePlusIcon />
New task
<CommandShortcut>⌘T</CommandShortcut>
</CommandItem>
<CommandItem>
<ReceiptIcon />
New invoice
</CommandItem>
</CommandGroup>
<CommandSeparator />
<CommandGroup heading="Go to">
<CommandItem>
<CalendarIcon />
Release calendar
</CommandItem>
<CommandItem disabled>
<SettingsIcon />
Workspace settings
</CommandItem>
</CommandGroup>
</CommandList>
</Command>
)
}
In a dialog
Nothing run yet
Command palette
Search for a command to run.
"use client"
import { useState } from "react"
import { CalendarIcon, FolderPlusIcon, ReceiptIcon, SearchIcon } from "@keyline-icons/react"
import { Button } from "@/components/ui/button"
import {
Command,
CommandDialog,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
} from "@/components/ui/command"
export default function CommandInADialog() {
const [open, setOpen] = useState(false)
const [last, setLast] = useState<string>()
function run(command: string) {
setLast(command)
setOpen(false)
}
return (
<div className="flex flex-col items-center gap-3">
<Button variant="outline" onClick={() => setOpen(true)}>
<SearchIcon data-icon="inline-start" />
Open the command palette
</Button>
<p role="status" className="text-muted-foreground">
{last ? `Ran: ${last}` : "Nothing run yet"}
</p>
<CommandDialog
open={open}
onOpenChange={setOpen}
title="Command palette"
description="Search for a command to run."
>
<Command label="Command palette">
<CommandInput placeholder="Search commands" />
<CommandList>
<CommandEmpty>No command matches.</CommandEmpty>
<CommandGroup heading="Create">
<CommandItem onSelect={() => run("New project")}>
<FolderPlusIcon />
New project
</CommandItem>
<CommandItem onSelect={() => run("New invoice")}>
<ReceiptIcon />
New invoice
</CommandItem>
</CommandGroup>
<CommandGroup heading="Go to">
<CommandItem onSelect={() => run("Release calendar")}>
<CalendarIcon />
Release calendar
</CommandItem>
</CommandGroup>
</CommandList>
</Command>
</CommandDialog>
</div>
)
}
Checked items
"use client"
import { useState } from "react"
import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from "@/components/ui/command"
const statuses = ["Backlog", "In progress", "In review", "Done"]
export default function CommandCheckedItems() {
const [status, setStatus] = useState("In progress")
return (
<Command label="Task status" className="h-auto max-w-sm border">
<CommandInput placeholder="Set the status" />
<CommandList>
<CommandEmpty>No status matches.</CommandEmpty>
<CommandGroup heading="Status">
{statuses.map((name) => (
<CommandItem
key={name}
data-checked={name === status}
aria-checked={name === status}
onSelect={() => setStatus(name)}
>
{name}
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
)
}
Installation
npx shadcn@latest add @khaeli/commandbunx --bun shadcn@latest add @khaeli/commandInstall @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.
Installs with
- Registry items
- npm packages
- @keyline-icons/react ^2.2.0
- cmdk ^1.1.1
Keyboard
Typing- Filters the rows as you type and highlights the first match.
CommandEmptyshows when nothing matches. Focus stays in the search field throughout. Arrow DownArrow Up- Move the highlight between rows. They skip a disabled row and stop at the first and the last row.
HomeEnd- Highlight the first and the last row.
Enter- Runs the highlighted row's
onSelect. Escape- In
CommandDialog, closes the dialog and returns focus to what opened it. Inline it does nothing: the text stays in the field.
Accessible name
Commandtakes alabel: it becomes a hidden label for the search field. Without it the field has only its placeholder.CommandGroupis named by itsheading. Rows are options in a list box that the field controls.CommandDialogis named bytitleand described bydescription. Both are hidden from sight and default to English text, so set them.data-checkedon aCommandItemdraws the check mark and is not announced. Setaria-checkedbeside it, as the third example does.CommandShortcutis read as part of the row's name. It shows the keys and binds nothing.CommandSeparatoris a drawn line only: it is hidden from assistive technology, so the groups it divides are told apart by their headings.
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/command.json