Search pages and run commands from the keyboard with ⌘K.
Installation
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.
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.
API reference
Props
| Name | Type | Description |
|---|---|---|
commandsrequired | CommandItem[] | Every command the menu can run or open, flat; a command with a parent lives on that command's page |
label | string | The menu's accessible name |
placeholder | string | The search field's placeholder |
context | string | null | What the commands act on, shown above the input: "ENG-12 Fix sign-in" |
loading | boolean | The app is fetching more results for the query |
shortcut | boolean | Mod+K opens it (true unless set false, as when the app opens it itself) |
trigger | string | A button that opens it, with this label ("Search"), for people who don't know Mod+K |
agentId | string | This menu's id for agents, unique on the page |
recent | string[] | The ids of commands the person ran lately, latest first: listed under "Recent" when the menu opens. The app keeps them, from RequestedCommand. |
Events
| Event | Payload | Description |
|---|---|---|
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
| Action | Description |
|---|---|
ClickedCommand | Run 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.