Command Palette
A command palette opened with Command-K or Ctrl-K, with filtering, arrow keys, and Enter to run a command.

The palette is a dialog plus a filter input. Arrow keys move the active row. Enter runs it. Escape closes, because showModal() already does that.
Use this next to the focus-trapped dialog if you need the same keyboard rules on a form.
HTML
<button type="button" id="openPalette">Commands</button>
<dialog id="palette" aria-label="Commands">
<input id="paletteQuery" type="text" placeholder="Type a command" autocomplete="off" />
<ul id="paletteList" role="listbox"></ul>
</dialog>
CSS
#palette { border: 0; border-radius: 16px; padding: 0.75rem; width: min(28rem, 92vw); }
#palette::backdrop { background: rgba(12, 10, 8, 0.55); }
#palette input, #palette button { font: inherit; }
#paletteList { list-style: none; margin: 0.5rem 0 0; padding: 0; }
#paletteList button {
width: 100%;
text-align: left;
border: 0;
background: transparent;
padding: 0.55rem 0.7rem;
border-radius: 8px;
}
#paletteList button[aria-selected="true"] { background: #f3e7df; }
JavaScript
const commands = [
{ id: "snippets", label: "Open snippets", run: () => location.assign("/snippets/") },
{ id: "templates", label: "Open templates", run: () => location.assign("/templates/") },
{ id: "cursor", label: "Cursor effects", run: () => location.assign("/snippet/cursor-effects-and-animation-with-css-and-js/") },
];
const dialog = document.getElementById("palette");
const input = document.getElementById("paletteQuery");
const list = document.getElementById("paletteList");
let active = 0;
let shown = commands;
let lastQuery = "";
function paint() {
list.innerHTML = "";
shown.forEach((item, index) => {
const li = document.createElement("li");
const button = document.createElement("button");
button.type = "button";
button.textContent = item.label;
button.setAttribute("aria-selected", index === active ? "true" : "false");
button.addEventListener("click", () => choose(index));
li.append(button);
list.append(li);
});
}
function render(query) {
const q = query.trim().toLowerCase();
if (q !== lastQuery) {
lastQuery = q;
active = 0;
shown = commands.filter((item) => item.label.toLowerCase().includes(q));
}
paint();
}
function choose(index) {
const item = shown[index];
if (!item) return;
dialog.close();
item.run();
}
function open() {
dialog.showModal();
input.value = "";
render("");
input.focus();
}
document.getElementById("openPalette").addEventListener("click", open);
window.addEventListener("keydown", (event) => {
if ((event.metaKey || event.ctrlKey) && event.key.toLowerCase() === "k") {
event.preventDefault();
open();
}
});
input.addEventListener("input", () => render(input.value));
dialog.addEventListener("keydown", (event) => {
if (event.key === "ArrowDown") {
event.preventDefault();
active = Math.min(active + 1, Math.max(shown.length - 1, 0));
paint();
}
if (event.key === "ArrowUp") {
event.preventDefault();
active = Math.max(active - 1, 0);
paint();
}
if (event.key === "Enter") {
event.preventDefault();
choose(active);
}
});
render only resets the active row when the query changes. Arrow keys call paint through render with the same query, so the highlight moves instead of jumping back to the first row.