Command Palette
Open a searchable list of actions with Ctrl+K, and run one with Enter or a click. An sv-dialog holds a search field and a listbox with selection="none", input-element lets the field drive the list, and the optionclicked event runs the command.
File
View
Help
No command matches.
Code
The following example assumes that you have imported the components and set up the theme.
<div class="listbox-palette">5 collapsed lines
<button type="button" class="btn" data-sv-dialog-open="palette-dialog"> Search commands <kbd>Ctrl K</kbd> </button> <output class="status"></output></div>
<sv-dialog id="palette-dialog" aria-label="Command palette">61 collapsed lines
<input id="palette-search" type="text" class="search" aria-label="Command" autocomplete="off" autofocus placeholder="Type a command" spellcheck="false" />
<sv-listbox class="commands" aria-label="Commands" input-element="#palette-search" selection="none" > <div aria-labelledby="palette-file" role="group"> <span id="palette-file" class="heading">File</span> <button value="new-file"> <span data-sv-listbox-label>New file</span> <kbd>Ctrl N</kbd> </button> <button value="open-file"> <span data-sv-listbox-label>Open file</span> <kbd>Ctrl O</kbd> </button> <button value="save"> <span data-sv-listbox-label>Save</span> <kbd>Ctrl S</kbd> </button> </div>
<div aria-labelledby="palette-view" role="group"> <span id="palette-view" class="heading">View</span> <button value="toggle-sidebar"> <span data-sv-listbox-label>Toggle sidebar</span> <kbd>Ctrl B</kbd> </button> <button value="zoom-in"> <span data-sv-listbox-label>Zoom in</span> <kbd>Ctrl +</kbd> </button> <button value="zoom-out"> <span data-sv-listbox-label>Zoom out</span> <kbd>Ctrl -</kbd> </button> </div>
<div aria-labelledby="palette-help" role="group"> <span id="palette-help" class="heading">Help</span> <button value="shortcuts"> <span data-sv-listbox-label>Keyboard shortcuts</span> </button> <button disabled value="report-issue"> <span data-sv-listbox-label>Report an issue</span> </button> </div>
<p class="empty" data-sv-listbox-empty>No command matches.</p> </sv-listbox></sv-dialog>
<script type="module">24 collapsed lines
const dialog = document.querySelector("#palette-dialog"); const search = document.querySelector("#palette-search"); const commands = dialog.querySelector(".commands"); const status = document.querySelector(".listbox-palette .status");
document.addEventListener("keydown", event => { if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === "k") { event.preventDefault(); dialog.open(); } });
dialog.addEventListener("openedchange", () => { if (!dialog.opened) return;
search.value = ""; commands.query = ""; });
commands.addEventListener("optionclicked", event => { const label = event.detail.option.querySelector("[data-sv-listbox-label]").textContent; status.textContent = `Ran "${label}"`; dialog.close(); });</script>
<style>102 collapsed lines
.listbox-palette { display: grid; grid-template-rows: 1fr auto 1fr; gap: 1em; justify-items: center; block-size: 100%; color: var(--sv-gray-2);
.btn { display: flex; grid-row: 2; gap: 0.75em; align-items: center; }
.status { grid-row: 3; } }
#palette-dialog { &::part(wrapper) { max-inline-size: 32rem; margin-block-start: 12vh; }
&::part(content) { display: grid; padding: 0; }
.search { padding: 1em; font: inherit; color: inherit; background: none; border: none; border-block-end: solid 2px var(--sv-gray-5);
&:focus-visible { outline: none; border-block-end-color: var(--sv-accent); } }
.commands { max-block-size: 20rem; padding: 0.75em; background: none; border: none; border-radius: 0; }
[role="group"] { display: grid; gap: 0.125em; }
.heading { padding: 0.75em 0.5em 0.25em; font-size: 0.75em; font-weight: 600; line-height: 1.25; color: var(--sv-gray-3); }
[role="option"] { display: flex; align-items: baseline; justify-content: space-between; padding: 0.5em; font: inherit; line-height: 1.25; color: inherit; cursor: pointer; background: none; border: none; border-radius: var(--sv-bdr-rad-sm);
&[data-sv-active] { background-color: var(--sv-gray-5); }
&:disabled { cursor: not-allowed; opacity: 0.5; } }
.empty { padding: 1em 0.5em; margin: 0; color: var(--sv-gray-3); text-align: center; } }
:is(.listbox-palette, #palette-dialog) kbd { font-family: inherit; font-size: 0.8em; color: var(--sv-gray-3); }</style>