Command Palette
Press Ctrl or Cmd + K (or the button) to open a searchable list of actions, move with arrows and run with Enter.
AI prompt
Design a command palette overlay opened with Ctrl or Cmd + K. A centred 340 pixel panel with a search input ("Type a command…") and a list of six actions with small shortcut hints on the right: New file, Open settings, Toggle theme, Share, Copy link, Sign out. Typing filters the list and bolds the matched letters, arrow keys move the selection, Enter runs it and shows "Ran: New file" in the page behind, Escape closes. Use role combobox with aria-activedescendant for the input and role listbox for the list. Light panel, 14 pixel radius, soft shadow, backdrop blur, 150 millisecond fade-drop with prefers-reduced-motion fallback. Plain HTML, CSS and JavaScript.About this pattern
Definition
A command palette is a keyboard-first overlay with a search field and a filtered list of actions. It lets people reach any command without hunting through menus.
Purpose & UX principles
- Open with a global shortcut; show recent or common commands before typing.
- Filter as you type, highlight the match and keep the first result selected so Enter works immediately.
- Show shortcuts next to commands, confirm the action and close the palette.
Interaction states
- Closed, open with all commands, filtered, empty ("No results"), and a confirmation after running a command.
Motion specification
- Panel fades and drops 8px over 150ms; selection highlight moves instantly.
- Reduced motion: fade only.
Responsive behaviour
- On phones open it as a full-width sheet near the top; keep rows at least 44px tall.
- Limit visible rows to about seven and scroll the rest.
Code
<div class="bx"><button id="op" type="button">Open palette <kbd>⌘K</kbd></button><p class="res" id="res" aria-live="polite">Press Ctrl or ⌘ + K.</p>
<div class="bk" id="bk" hidden><div class="pl"><input id="q" role="combobox" aria-expanded="true" aria-controls="ls" aria-activedescendant="" placeholder="Type a command…" autocomplete="off"><ul id="ls" role="listbox"></ul></div></div></div>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
.bx{width:min(300px,100%);display:grid;gap:10px;justify-items:start}
#op{height:34px;padding:0 14px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:13px;cursor:pointer;display:flex;gap:8px;align-items:center}
kbd{font:11px/1 inherit;padding:3px 6px;border-radius:5px;background:#eeeeea;color:var(--muted)}
.res{font-size:12.5px;color:var(--muted)}
.bk{position:fixed;inset:0;background:rgba(241,241,239,.7);-webkit-backdrop-filter:blur(3px);backdrop-filter:blur(3px);display:grid;align-items:start;justify-items:center;padding:20px 12px}
.bk[hidden]{display:none}
.pl{width:min(340px,100%);border-radius:14px;background:var(--surface);border:1px solid var(--line);box-shadow:0 14px 40px rgba(0,0,0,.14);overflow:hidden;animation:dr .15s ease-out}
@keyframes dr{from{opacity:0;transform:translateY(-8px)}}
.pl input{width:100%;height:42px;border:0;border-bottom:1px solid var(--line);padding:0 14px;font-size:14px;outline:none}
.pl ul{list-style:none;padding:6px;max-height:190px;overflow:auto}
.pl li{display:flex;justify-content:space-between;align-items:center;padding:8px 10px;border-radius:8px;font-size:13px;cursor:pointer}
.pl li[aria-selected=true]{background:#efefeb}.pl li mark{background:none;font-weight:700}.pl li span{font-size:11px;color:var(--muted)}
.no{padding:14px;text-align:center;font-size:12.5px;color:var(--muted)}
button:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
@media (prefers-reduced-motion:reduce){.pl{animation:none}}const C=[['New file','⌘N'],['Open settings','⌘,'],['Toggle theme','⌘D'],['Share','⌘S'],['Copy link','⌘L'],['Sign out','']],$=id=>document.getElementById(id),bk=$('bk'),q=$('q'),ls=$('ls'),res=$('res'),op=$('op');let cur=[],sel=0;
function esc(s){return s.replace(/[&<>]/g,c=>({'&':'&','<':'<','>':'>'}[c]));}
function render(){const t=q.value.trim().toLowerCase();cur=C.filter(c=>c[0].toLowerCase().includes(t));sel=Math.min(sel,Math.max(0,cur.length-1));
ls.innerHTML=cur.length?cur.map((c,i)=>{const k=t?c[0].toLowerCase().indexOf(t):-1,n=k>=0?esc(c[0].slice(0,k))+'<mark>'+esc(c[0].slice(k,k+t.length))+'</mark>'+esc(c[0].slice(k+t.length)):esc(c[0]);return '<li role="option" id="o'+i+'" aria-selected="'+(i===sel)+'" data-i="'+i+'">'+n+'<span>'+c[1]+'</span></li>';}).join(''):'<div class="no">No results</div>';
q.setAttribute('aria-activedescendant',cur.length?'o'+sel:'');}
function open(){bk.hidden=false;q.value='';sel=0;render();q.focus();}
function close(){bk.hidden=true;op.focus();}
function run(i){if(!cur[i])return;res.textContent='Ran: '+cur[i][0];close();}
op.addEventListener('click',open);q.addEventListener('input',()=>{sel=0;render();});
q.addEventListener('keydown',e=>{if(e.key==='ArrowDown'||e.key==='ArrowUp'){e.preventDefault();if(cur.length){sel=(sel+(e.key==='ArrowDown'?1:-1)+cur.length)%cur.length;render();$('o'+sel).scrollIntoView({block:'nearest'});}}else if(e.key==='Enter'){e.preventDefault();run(sel);}else if(e.key==='Escape'){e.preventDefault();close();}});
ls.addEventListener('click',e=>{const li=e.target.closest('li');if(li)run(+li.dataset.i);});ls.addEventListener('pointermove',e=>{const li=e.target.closest('li');if(li&&+li.dataset.i!==sel){sel=+li.dataset.i;ls.querySelectorAll('li').forEach((x,i)=>x.setAttribute('aria-selected',String(i===sel)));}});
bk.addEventListener('pointerdown',e=>{if(e.target===bk)close();});
document.addEventListener('keydown',e=>{if((e.metaKey||e.ctrlKey)&&e.key.toLowerCase()==='k'){e.preventDefault();bk.hidden?open():close();}});One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Command Palette — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
.bx{width:min(300px,100%);display:grid;gap:10px;justify-items:start}
#op{height:34px;padding:0 14px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:13px;cursor:pointer;display:flex;gap:8px;align-items:center}
kbd{font:11px/1 inherit;padding:3px 6px;border-radius:5px;background:#eeeeea;color:var(--muted)}
.res{font-size:12.5px;color:var(--muted)}
.bk{position:fixed;inset:0;background:rgba(241,241,239,.7);-webkit-backdrop-filter:blur(3px);backdrop-filter:blur(3px);display:grid;align-items:start;justify-items:center;padding:20px 12px}
.bk[hidden]{display:none}
.pl{width:min(340px,100%);border-radius:14px;background:var(--surface);border:1px solid var(--line);box-shadow:0 14px 40px rgba(0,0,0,.14);overflow:hidden;animation:dr .15s ease-out}
@keyframes dr{from{opacity:0;transform:translateY(-8px)}}
.pl input{width:100%;height:42px;border:0;border-bottom:1px solid var(--line);padding:0 14px;font-size:14px;outline:none}
.pl ul{list-style:none;padding:6px;max-height:190px;overflow:auto}
.pl li{display:flex;justify-content:space-between;align-items:center;padding:8px 10px;border-radius:8px;font-size:13px;cursor:pointer}
.pl li[aria-selected=true]{background:#efefeb}.pl li mark{background:none;font-weight:700}.pl li span{font-size:11px;color:var(--muted)}
.no{padding:14px;text-align:center;font-size:12.5px;color:var(--muted)}
button:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
@media (prefers-reduced-motion:reduce){.pl{animation:none}}
</style></head>
<body><div class="stage">
<div class="bx"><button id="op" type="button">Open palette <kbd>⌘K</kbd></button><p class="res" id="res" aria-live="polite">Press Ctrl or ⌘ + K.</p>
<div class="bk" id="bk" hidden><div class="pl"><input id="q" role="combobox" aria-expanded="true" aria-controls="ls" aria-activedescendant="" placeholder="Type a command…" autocomplete="off"><ul id="ls" role="listbox"></ul></div></div></div>
</div>
<script>
const C=[['New file','⌘N'],['Open settings','⌘,'],['Toggle theme','⌘D'],['Share','⌘S'],['Copy link','⌘L'],['Sign out','']],$=id=>document.getElementById(id),bk=$('bk'),q=$('q'),ls=$('ls'),res=$('res'),op=$('op');let cur=[],sel=0;
function esc(s){return s.replace(/[&<>]/g,c=>({'&':'&','<':'<','>':'>'}[c]));}
function render(){const t=q.value.trim().toLowerCase();cur=C.filter(c=>c[0].toLowerCase().includes(t));sel=Math.min(sel,Math.max(0,cur.length-1));
ls.innerHTML=cur.length?cur.map((c,i)=>{const k=t?c[0].toLowerCase().indexOf(t):-1,n=k>=0?esc(c[0].slice(0,k))+'<mark>'+esc(c[0].slice(k,k+t.length))+'</mark>'+esc(c[0].slice(k+t.length)):esc(c[0]);return '<li role="option" id="o'+i+'" aria-selected="'+(i===sel)+'" data-i="'+i+'">'+n+'<span>'+c[1]+'</span></li>';}).join(''):'<div class="no">No results</div>';
q.setAttribute('aria-activedescendant',cur.length?'o'+sel:'');}
function open(){bk.hidden=false;q.value='';sel=0;render();q.focus();}
function close(){bk.hidden=true;op.focus();}
function run(i){if(!cur[i])return;res.textContent='Ran: '+cur[i][0];close();}
op.addEventListener('click',open);q.addEventListener('input',()=>{sel=0;render();});
q.addEventListener('keydown',e=>{if(e.key==='ArrowDown'||e.key==='ArrowUp'){e.preventDefault();if(cur.length){sel=(sel+(e.key==='ArrowDown'?1:-1)+cur.length)%cur.length;render();$('o'+sel).scrollIntoView({block:'nearest'});}}else if(e.key==='Enter'){e.preventDefault();run(sel);}else if(e.key==='Escape'){e.preventDefault();close();}});
ls.addEventListener('click',e=>{const li=e.target.closest('li');if(li)run(+li.dataset.i);});ls.addEventListener('pointermove',e=>{const li=e.target.closest('li');if(li&&+li.dataset.i!==sel){sel=+li.dataset.i;ls.querySelectorAll('li').forEach((x,i)=>x.setAttribute('aria-selected',String(i===sel)));}});
bk.addEventListener('pointerdown',e=>{if(e.target===bk)close();});
document.addEventListener('keydown',e=>{if((e.metaKey||e.ctrlKey)&&e.key.toLowerCase()==='k'){e.preventDefault();bk.hidden?open():close();}});
</script>
</body></html>