Command menu

Search everything. Press ⌘K.

Installation

Terminal
npx shadcn@latest add https://fabrickit.org/r/command.json

This copies the component into components/fabricui and its engine into lib/fabricui. It's your code from then on. First time? Add the theme once: see Installation.

Code

components/fabricui/command.tsx
"use client";

import * as React from "react";
import { mount } from "@/lib/fabricui/mount";
import { CommandMenu as CommandEngine } from "@/lib/fabricui/command-engine";
import "./command.css";

const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;

/** One command. */
export type CommandItem = {
  id: string;
  label: string;
  /** The name of one of `icons`. */
  icon?: string;
  /** Its shortcut, shown on the right, one key per entry (["⌘", "N"]). */
components/fabricui/command.css
/*
 * command.css — Command menu (command.js). Moved out of command.html.
 */
/*
 * Command menu (⌘K). Search, grouped commands, keyboard first. It pops in
 * like the Dialog (cloth-drop.js pop(): a soft scale-up with one wave
 * rippling out from the middle) and fades out quietly. Typing during the
 * pop hands over to the live menu at once, so results never lag.
 */

kbd:where(.fui-command, .fui-command *) {
  box-sizing: border-box;
  min-width: 20px;
  height: 20px;
  padding: 0 5px;
  display: inline-grid;
lib/fabricui/command-engine.js
/*
 * command-engine.js — Command menu (command.css). Moved out of command.html so it can be used anywhere.
 * (Fabric Kit: components/fabricui/command.tsx renders the markup and starts it.
 * The commands and their icons, which were the page's GROUPS and ICONS, and
 * the page's esc / match / highlight helpers, come in here.)
 */
import { ClothDrop } from "./cloth-drop";

let menus = 0;   // each menu's ids are its own (two menus can share item ids)
const esc = s => String(s).replace(/[&<>"]/g, c => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" })[c]);

/**
 * Match `q` against a label: a run of the query inside the label scores best,
 * then a word start, then the letters in order anywhere (so "nwp" finds
 * "New project"). Returns the matched positions (for highlighting) or null.
 */

Usage

Usage
import { CommandMenu } from "@/components/fabricui/command";

const ICONS = { plus: '<path d="M12 5V19M5 12H19" />' };

export function Search() {
  return (
    // the backdrop and menu cover the nearest positioned ancestor
    <div className="relative grid min-h-[600px] place-items-center">
      <CommandMenu
        icons={ICONS}
        groups={[{ name: "Suggestions", items: [{ id: "new", label: "New project", icon: "plus", keys: ["⌘", "N"], words: "create add" }] }]}
        onRun={item => console.log(item.id)}
      />
    </div>
  );
}

Props

PropTypeDefault
groups

The commands in titled groups. CommandItem: { id, label, icon?, keys?, words?, href? }; words are more terms it's found by, keys its shortcut shown on the right.

{ name: string; items: CommandItem[] }[]—
icons

Icons by name (an item's icon): each an icon's inner SVG markup on a 24-unit grid.

Record<string, string>—
onRun

Called with the command picked (Enter or a click) as the menu closes.

(item: CommandItem) => void—
open

Open, if you control it.

boolean—
onOpenChange

Called when it opens or closes: the trigger, ⌘K / Ctrl+K, Escape, the backdrop, or a run.

(open: boolean) => void—
triggerLabel

The trigger's text.

string"Search…"
placeholder

The search field's placeholder.

string"Type a command or search…"
className

Added to the trigger, backdrop and menu.

string—