Command Menu

Search pages and run commands from the keyboard with ⌘K.

Press ⌘K, or click the button.

Installation

pnpm dlx shadcn@latest add checkfu/uicontext/command-menu

Usage

import { CommandMenu } from "./ui/uicontext/components/kit/CommandMenu.ts"
const Message = UiSchema.TaggedEnum({ RequestedCommand: { id: Schema.String } })export const AppCommands = (h: Html.H<typeof Message.Type>) =>	CommandMenu(h, {		props: {			trigger: "Search",			commands: [				{ id: "new-issue", label: "New issue", group: "Issue", shortcut: ["C"] },				{ id: "go-inbox", label: "Go to inbox", group: "Navigation", shortcut: ["G", "I"] },			],		},		onEmit: {			RequestedCommand: ({ id }) => Message.RequestedCommand({ id }),			Searched: () => undefined,			Opened: () => undefined,			Closed: () => undefined,		},	})

Examples

Recent commands and loose matching

Pass recent and the menu opens on what the person ran last. Letters in order find a command, so “chth” finds Change theme.

Try “gtr” for Go to roadmap, or “csv”.

With search results

From the top, a search reaches commands on inner pages, shown with their path. Searched tells the app each query; while it looks, loading shows a bar, and what it finds joins commands.

Open it and type “om”, “platform” or “roadmap”.

API reference

Props

CommandMenu props
NameTypeDescription
commandsrequiredCommandItem[]Every command the menu can run or open, flat; a command with a parent lives on that command's page
labelstringThe menu's accessible name
placeholderstringThe search field's placeholder
contextstring | nullWhat the commands act on, shown above the input: "ENG-12 Fix sign-in"
loadingbooleanThe app is fetching more results for the query
shortcutbooleanMod+K opens it (true unless set false, as when the app opens it itself)
triggerstringA button that opens it, with this label ("Search"), for people who don't know Mod+K
agentIdstringThis menu's id for agents, unique on the page
recentstring[]The ids of commands the person ran lately, latest first: listed under "Recent" when the menu opens. The app keeps them, from RequestedCommand.

Events

CommandMenu events
EventPayloadDescription
RequestedCommand{ id: string }A command was run, by Enter, a click or an agent: the app does it
Searched{ query: string }The query changed: the app may add results for it (people, issues, pages)
Opened{}The menu opened
Closed{}The menu closed, after a command ran or on Escape

Agent actions

What agents can do with CommandMenu
ActionDescription
ClickedCommandRun a command by its id (see page_query), on the context page_snapshot shows

Accessibility

  • ⌘K or Ctrl+K opens it anywhere on the page. Arrows move, Enter runs, Escape goes back a level or closes.
  • The input is a combobox and the commands its listbox, so screen readers announce the active command.
  • Disabled commands stay in the list and say why they can't run.

Source