modern-cmdk
v1.7.2
Published
Headless command palette engine: a framework-agnostic TypeScript core with fuzzy search, frecency ranking and keyboard shortcuts, plus a React 19 adapter.
Maintainers
Readme
Headless command palette engine: a framework-agnostic TypeScript core with fuzzy search, frecency ranking and keyboard shortcuts, plus a React 19 adapter.
npm install modern-cmdk react react-dom radix-ui7.7 kB gzip core · 12.0 kB with the React adapter · ESM only · React ≥ 19 · TypeScript ≥ 5.6 · Requirements
Quick start
'use client';
import { Command } from 'modern-cmdk/react';
import 'modern-cmdk/styles.css';
function toggleColorScheme(): void {
const root = document.documentElement;
root.style.colorScheme =
root.style.colorScheme === 'dark' ? 'light' : 'dark';
}
export function CommandPalette() {
return (
<Command label="Commands">
<Command.Input placeholder="Type a command…" />
<Command.List>
<Command.Empty>No results.</Command.Empty>
<Command.Group heading="Page">
<Command.Item
value="Copy page link"
keywords={['url', 'share']}
onSelect={() => navigator.clipboard.writeText(location.href)}
>
Copy page link
</Command.Item>
<Command.Item
value="Toggle dark mode"
shortcut="Mod+Shift+L"
onSelect={toggleColorScheme}
>
Toggle dark mode
<Command.Shortcut shortcut="Mod+Shift+L" />
</Command.Item>
</Command.Group>
<Command.Group heading="Go to">
<Command.Item
value="Settings"
keywords={['preferences']}
onSelect={() => location.assign('/settings')}
>
Settings
</Command.Item>
</Command.Group>
</Command.List>
</Command>
);
}Typing filters the items with the built-in fuzzy scorer; keywords are searched but not shown. The shortcut on an item binds the key for as long as the palette is mounted, wherever focus is, and stops the key's default action and its propagation, so bind keys the page does not need. Command.Shortcut only displays it: ⌘⇧L on Apple platforms, Ctrl+Shift+L elsewhere.
What it does
- Framework-agnostic core.
createCommandMachineis a typed state machine that imports no framework; DOM APIs are feature-detected, so it is safe to import on a server. Core API - Fuzzy search with match ranges for
Command.Highlight; pass your own scorer orSearchEngine. Basic usage - Frecency ranking, opt-in, kept in memory or in IndexedDB (
idb-keyval, the one dependency, loads only then). Frecency - Keyboard shortcuts. Write
Mod+Konce: it is parsed, shown as ⌘ on Apple platforms and Ctrl elsewhere, matched against key events and checked for conflicts. Shortcuts - React 19 adapter. Compound
Command.*components onuseSyncExternalStore;refis a prop, with noforwardRef;Command.Activitywraps React's<Activity>(React 19.2 and later). React API - WAI-ARIA combobox and listbox that announce the result count. Accessibility
- Virtualization that switches on above 100 matches and off at 50 or fewer. Virtualization
- Async items with Suspense through
Command.AsyncItems. Async items - A stylesheet for structure, in
@layer command: layout, motion, and rules for forced colours, increased contrast and reduced motion. The active row, surfaces and borders are yours to style. Theming - SSR-safe: the React entries carry
'use client'. SSR and Next.js - Explicit resource management: the machine is
DisposableandAsyncDisposable, sousingandawait usingrelease it; telemetry hooks,useCommandDevtoolsandCommandErrorBoundaryare built in. Core API
Open it with ⌘K or Ctrl+K
'use client';
import { matchesShortcut, parseShortcut } from 'modern-cmdk';
import { Command } from 'modern-cmdk/react';
import { useEffect, useState } from 'react';
export function CommandMenu() {
const [open, setOpen] = useState(false);
useEffect(() => {
const toggle = parseShortcut('Mod+K');
const controller = new AbortController();
document.addEventListener(
'keydown',
(event) => {
if (!matchesShortcut(event, toggle)) return;
event.preventDefault();
setOpen((isOpen) => !isOpen);
},
{ signal: controller.signal },
);
return () => controller.abort();
}, []);
const run = (action: () => void) => () => {
setOpen(false);
action();
};
return (
<Command.Dialog open={open} onOpenChange={setOpen} label="Command menu">
<Command.Input placeholder="Search…" />
<Command.List>
<Command.Empty>No results.</Command.Empty>
<Command.Item
value="Settings"
onSelect={run(() => location.assign('/settings'))}
>
Settings
</Command.Item>
<Command.Item value="Print this page" onSelect={run(() => print())}>
Print this page
</Command.Item>
</Command.List>
</Command.Dialog>
);
}Command.Dialog binds no open key, so you pass open. Mod+K matches ⌘K on Apple platforms and Ctrl+K elsewhere, and Caps Lock does not break it. Escape and a click outside call onOpenChange(false); selecting an item does not close the dialog by itself, so run closes it first.
Without React
import { createCommandMachine, itemId } from 'modern-cmdk';
const commands = new Map([
[itemId('new-file'), 'New file'],
[itemId('open-recent'), 'Open recent'],
[itemId('toggle-theme'), 'Toggle theme'],
]);
/** Renders the filtered commands into `list` until disposed. */
export function mountPalette(
input: HTMLInputElement,
list: HTMLUListElement,
): Disposable {
const machine = createCommandMachine({
items: commands
.entries()
.map(([id, value]) => ({ id, value }))
.toArray(),
});
const controller = new AbortController();
const render = () => {
const rows = machine.getState().filteredIds.map((id) => {
const row = document.createElement('li');
row.textContent = commands.get(id) ?? '';
return row;
});
list.replaceChildren(...rows);
};
const subscription = machine.subscribeState(render);
render();
input.addEventListener(
'input',
() => machine.send({ type: 'SEARCH_CHANGE', query: input.value }),
{ signal: controller.signal },
);
return {
[Symbol.dispose]() {
controller.abort();
subscription[Symbol.dispose]();
machine[Symbol.dispose]();
},
};
}send() is batched: the state changes on the next animation frame (a microtask where there is none), then subscribers run. mountPalette returns a Disposable, so a caller can write using palette = mountPalette(input, list);.
Entry points
modern-cmdk: the core (machine, search, frecency, shortcuts, telemetry). Imports no peer.modern-cmdk/react:Commandand its parts, the hooks,CommandErrorBoundary.'use client'.modern-cmdk/react/dialog:CommandDialogalone.'use client'.modern-cmdk/styles.css: the stylesheet, the only file with side effects.
ESM only. The Core API and React API pages document the functions, components and hooks of each entry.
items,groups: items and groups registered at creation; in React,<Command.Item>and<Command.Group>register themselves.filter: your scorer,(item, query) => number | false, orfalseto turn filtering off.searchEngine: aSearchEnginethat replaces the built-in scorer; one you pass is yours to dispose.frecency:{ enabled, storage, namespace, decayConfig }; history in memory by default, ornew IdbFrecencyStorage().shortcutAction:'direct'(default): a shortcut calls the item'sonSelect.'select': it selects the item, so frecency and the root'sonSelectrun too.disabledItems:'show'(default) lists disabled items without making them selectable;'hide'drops them.loop: arrow keys wrap from the last item to the first (defaulttrue).activeId,onActiveChange: control the active item.open,onOpenChange: the open state;Command.Dialogtreatsopenas controlled.onSelect,onSearchChange: called on every select of an enabled item, and on every query.label: the accessible name (default'Command palette').telemetry:onPaletteOpen,onPaletteClose,onSearchComplete,onItemSelected, called at the end of each batch.search: deprecated name ofsearchEngine.virtualizeThreshold: accepted but not read: the list virtualizes above 100 matches and stops at 50 or fewer.
In React, shortcutAction, frecency, searchEngine, whether filter is a function and, on <Command>, open are read once, at mount.
Coming from cmdk
cmdk code moves over with modern-cmdk-codemod: four transforms, one per run, in this order: should-filter, forward-ref, data-attrs, import-rewrite. Start with a dry run, which writes nothing:
npx modern-cmdk-codemod --dry-run should-filter ./srcThe migration guide covers what the codemods leave to you. This package also ships a modern-cmdk bin over the same transforms. It is deprecated: it warns on every run and keeps lenient exit codes, so use modern-cmdk-codemod. The ten third-party packages that bin bundles are listed with their licences in THIRD-PARTY-NOTICES.md, inside this package.
A rewrite of cmdk for React 19.
Size and speed
| Measure | Value |
|---|--:|
| Core, modern-cmdk | 7.7 kB gzip[^size] |
| Core + modern-cmdk/react | 12.0 kB gzip[^size] |
| modern-cmdk/styles.css | 4.0 kB gzip[^size] |
| Search 10,000 items for one query | median 3.8 ms[^bench] |
Most of the unpacked size npm lists is the deprecated modern-cmdk bin and the packages it bundles (3.15 MB); no application bundle imports it.
[^size]: size-limit, gzip: the JavaScript entries bundled with rolldown and minified with NODE_ENV=production, the stylesheet as shipped; react, react-dom and radix-ui are not counted; 1 kB = 1,000 bytes. Record: size-history/1.7.0.json.
[^bench]: createSearchEngine() searching an index of 10,000 items for 'apple' and materialising the results; indexing is not timed. The median of five runs' p50 on a GitHub-hosted runner (AMD EPYC 7763, 4 cores), recorded on 2026-11-08; runner noise is ±20–40 %. Record: baseline.json. Method: Benchmarks.
Requirements
| Area | Requirement |
|---|---|
| Module format | ESM only |
| React | react and react-dom >=19.0.0 <21.0.0, for the React entries (optional peers) |
| radix-ui | >=1.4.0 <2.0.0, a required peer: npm 7+ and pnpm install it, with react and react-dom as its own peers, even if you import only the core |
| TypeScript | 5.6 or later, with "lib": ["ESNext", "DOM"] |
| Browsers | Chrome and Edge 122, Firefox 131, Safari 18.4: the built code uses iterator helpers |
| Node | No engines field; any Node with iterator helpers. The deprecated bin needs Node 26.4.0 or later |
| Other package managers | pnpm add and yarn add take the same four names |
The modern-cmdk packages
| Package | Use it to |
|---|---|
| modern-cmdk | build the palette: core engine, React adapter, stylesheet |
| create-modern-cmdk | start a Vite, React 19 and TypeScript project with a palette in it |
| modern-cmdk-codemod | move code written for cmdk to modern-cmdk |
Links
Documentation · Changelog · Releases · Issues · Security policy · Repository
License
MIT © 2026 Albert Badalov
