Blog Build a command palette for your web app

Build a command palette for your web app

2 min read

Build a command palette for your web app
TL;DR A command palette is a searchable list of actions opened by a keyboard shortcut. Register the actions with labels and handlers, open the palette on a shortcut, filter as the user types, and run the selected action. It makes a power-user workflow out of features that already exist.

The command palette, that cmd+k overlay you find in the tools developers love, is one of the highest-leverage bits of UX you can add. It gives power users a fast, keyboard-driven way to do anything your app already does. Here is how to build one.

The three parts

A command palette is simpler than it looks. It has three pieces:

  • A registry of actions, each with a label and a handler.
  • A shortcut that opens the palette.
  • A filtered list that narrows as the user types, and runs the chosen action on select.

Register the actions

Define your actions as data, so the palette is just a view over them:

const actions = [
  { label: "Go to Dashboard", run: () => navigate("/dashboard") },
  { label: "New Post", run: () => navigate("/posts/new") },
  { label: "Toggle Dark Mode", run: () => toggleTheme() },
];

Anything the app can do becomes an entry here.

Open it with a shortcut

Listen for the shortcut globally and toggle the palette:

window.addEventListener("keydown", (e) => {
  if ((e.metaKey || e.ctrlKey) && e.key === "k") {
    e.preventDefault();
    openPalette();
  }
});

Filter and run

As the user types, filter the actions by label and show the matches. On Enter or click, run the selected action's handler and close the palette:

const results = actions.filter((a) =>
  a.label.toLowerCase().includes(query.toLowerCase())
);
// render results; on select: result.run(); closePalette();

A fuzzy match is nicer than a plain substring, but substring is a fine start.

The details that make it feel good

  • Full keyboard control. Arrow keys to move, Enter to run, Escape to close. A palette you have to reach for the mouse to use defeats the point.
  • Accessible. It is a modal dialog, so trap focus, label it, and return focus on close.
  • Fast. Keep the action list light so filtering is instant as the user types.

Why it is worth it

The clever part of a command palette is that it adds no features, it just exposes the ones you have in a faster interface. For anyone who lives in your app, typing a few letters beats clicking through menus every time. It is a small build that makes your product feel considerably more capable to the people who use it most.

FAQ

What is a command palette?

It is an overlay, usually opened with a keyboard shortcut like cmd+k, that lets users search for and run any action in your app by name. It turns navigation and commands into a single fast, keyboard-driven interface.

Why add one to a web app?

It gives power users speed. Instead of hunting through menus, they type a few letters and run the action. It also makes features discoverable, since everything the app can do is listed in one searchable place.

Do I need a library?

Not necessarily. The core is a modal, a list you filter as the user types, and a set of registered actions. A library can save time on accessibility and fuzzy matching, but the concept is simple enough to build yourself.