Community JavaScript Snippet

useCommandK Palette Hook

The cmd-k palette hook I wire into every internal tool. Owns global keyboard shortcuts, fuzzy match, arrow-key navigation, and a focus trap, with no external dependencies.

useCommandK Palette Hook

The cmd-k palette hook I wire into every internal tool. Owns global keyboard shortcuts, fuzzy match, arrow-key navigation, and a focus trap, with no external dependencies.

JavaScript
Frontend
3 snippets
react
hooks
accessibility
command-palette
zurihayes

By @zurihayes

January 21, 2026

·

Updated May 18, 2026

1,168 views

6

4.4 (13)

I never reach for fuse.js for a command palette anymore: a 25-line fuzzy matcher is enough and ranks better for the shape of palette commands. Three things make it work: subsequence matching means tdt finds Toggle Dark Theme; run-length scoring means contiguous matches outscore scattered ones; and a small bonus for word-boundary matches surfaces Settings when you type set. Keeping the function pure means I can sort 200 commands at 60fps in JS without a single render, and I can table-test it with five lines of vitest. The matches array is what I pass back to highlight characters in the rendered list.