FRENVersion française

App navigation

Command palette

Palette de commandes

Demo

Loading demo…

A search field that opens with a shortcut, usually Cmd+K, and lists every action in the app. Type a few letters, pick with the keyboard, and the action runs. It saves people from digging through deep menus.

Story

Did you know

Modern devs rediscovered it through Sublime Text, but the idea is nearly fifty years old: Emacs' M-x already let you type the name of any command.

Timeline

  1. 1970sEmacs: M-x runs any command by name.
  2. 2005Spotlight on Mac OS X, alongside launchers like Quicksilver and later Alfred.
  3. 2011Sublime Text 2 popularizes the "Command Palette".
  4. 2019+Cmd+K becomes a SaaS standard (Linear, Notion, Vercel).

Vocabulary

Names

FR
Palette de commandes, barre de commandes, lanceur
EN
Command palette, Cmd+K menu, command bar, command menu, quick switcher, omnibox

Common uses

  • Reach any action from the keyboard
  • Search files, pages or people
  • Replace deep menus in pro tools

Often paired with

The components it usually sits with, by page type, in reading order.

Code

<dialog id="palette" aria-label="Command palette">
  <input id="q" role="combobox" aria-expanded="true" aria-controls="list"
         aria-autocomplete="list" placeholder="Type a command…">
  <ul id="list" role="listbox"></ul>
</dialog>

<script>
  const commands = ['New project', 'Invite a member', 'Dark mode', 'Open settings']
  let hits = commands, active = 0

  function render() {
    hits = commands.filter(c => c.toLowerCase().includes(q.value.toLowerCase()))
    list.replaceChildren(...hits.map((c, i) => {
      const li = Object.assign(document.createElement('li'), { id: 'opt-' + i, textContent: c })
      li.setAttribute('role', 'option')
      li.setAttribute('aria-selected', i === active)
      li.onclick = () => choose(c)
      return li
    }))
    if (hits.length) q.setAttribute('aria-activedescendant', 'opt-' + active)
    else q.removeAttribute('aria-activedescendant')
  }
  function choose(cmd) { console.log('Run:', cmd); palette.close() }

  addEventListener('keydown', e => {
    if ((e.metaKey || e.ctrlKey) && e.key === 'k') {
      e.preventDefault(); q.value = ''; active = 0; render(); palette.showModal()
    }
  })
  q.oninput = () => { active = 0; render() }
  q.onkeydown = e => {
    if (e.key === 'ArrowDown') active = Math.min(active + 1, hits.length - 1)
    else if (e.key === 'ArrowUp') active = Math.max(active - 1, 0)
    else if (e.key === 'Enter' && hits[active]) return choose(hits[active])
    else return
    e.preventDefault(); render()
  }
</script>

Good to know

  • <dialog> + showModal() give you an inert background, Escape and focus on the first field when it opens, for free.
  • Ctrl+K is already taken by Chrome and Firefox (web search), so preventDefault() is a must. Show ⌘K on Mac and Ctrl K elsewhere.
  • When the list scrolls, call scrollIntoView({ block: 'nearest' }) on the active option, or the selection drifts out of view.
  • includes() is enough for twenty commands. Beyond that, switch to fuzzy search that forgives typos. In React, the cmdk library has become the go-to.

Accessibility

Must have

  • The input gets role="combobox", aria-expanded and aria-controls pointing to the list. The list has role="listbox", each row role="option" with aria-selected.
  • Focus stays in the input. The selection moves with aria-activedescendant: up/down arrows to move, Enter to run, Escape to close.
  • Give the dialog a name (aria-label) and label the input too: a placeholder alone is not enough.

Often forgotten

  • Announce the result count in an aria-live="polite" region: screen readers stay silent when the list filters.
  • Drop aria-activedescendant when nothing matches. An id pointing to a removed option breaks the announcement.
  • Return focus to the previous element on close. <dialog> does it for you, a home-made palette has to code it.

Nice to have

  • Show each command's direct shortcut on the right: the palette teaches people the keyboard.
  • Group options with role="group" and an aria-label (Navigation, Actions…).
  • Support Home and End to jump to the first or last option.

Who uses it