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
- 1970sEmacs:
M-xruns any command by name. - 2005Spotlight on Mac OS X, alongside launchers like Quicksilver and later Alfred.
- 2011Sublime Text 2 popularizes the "Command Palette".
- 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, thecmdklibrary has become the go-to.
Accessibility
Must have
- The input gets
role="combobox",aria-expandedandaria-controlspointing to the list. The list hasrole="listbox", each rowrole="option"witharia-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-activedescendantwhen 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 anaria-label(Navigation, Actions…). - Support Home and End to jump to the first or last option.