Command palette
A search box for jumping to pages and running actions — opened with Ctrl K (⌘ K on a Mac). Write the items as plain links and buttons; Moey does the searching, sorting and keyboard.
Try it right here: press Ctrl K to search these docs — that's a Moey palette too.
<dialog class="palette" id="cmdk" aria-label="Search">
<div class="palette-search">
<i class="mi mi-search"></i>
<input placeholder="Search pages and actions…">
</div>
<div class="palette-list">
<div class="palette-group">
<div class="palette-group-title">Pages</div>
<a class="palette-item" href="/orders"><i class="mi mi-package"></i>Orders<span class="palette-meta">1,284</span></a>
<a class="palette-item" href="/customers" data-moey-keywords="clients people"><i class="mi mi-users"></i>Customers</a>
</div>
<div class="palette-group">
<div class="palette-group-title">Actions</div>
<button class="palette-item" onclick="newOrder()"><i class="mi mi-plus"></i>New order<kbd>N</kbd></button>
<button class="palette-item theme-red"><i class="mi mi-log-out"></i>Sign out</button>
</div>
</div>
<div class="palette-empty">No results found.</div>
<div class="palette-footer">…key hints (hidden on touch screens)…</div>
</dialog>
That's all — no JavaScript to write. Ctrl K opens the first palette on the page, typing filters, ↑ ↓ move, Enter runs the item (a link opens, a button is clicked) and the palette closes. A click outside or Esc closes it too — it's a search, so there's nothing to lose. A button with data-moey-open="#cmdk" opens it as well.
How search works
- Every word you type has to appear in the item's text or its
data-moey-keywords— so "inv team" finds "Invite a teammate". - Best matches move to the top: the label starting with your word first, then a word in the label, then anywhere, then keywords only.
- Accents don't matter (cafe finds Café). Groups with no matches hide; the
.palette-emptymessage shows when nothing is left.
Search-only items
Long lists — every customer, every docs section — shouldn't fill the palette before you type. Add data-moey-search-only and the item only shows up as a search result.
<a class="palette-item" href="/customers/42" data-moey-search-only>
<span class="avatar avatar-xs">LT</span>Lena Torres<span class="palette-detail">lena@moey.test</span>
</a>
Results from your server
Listen for moey:search, put the results into the list, then call Moey.palette(el) to filter and select again. Here the "server" is a short delay.
const palette = document.getElementById('orders-search');
const results = palette.querySelector('#results');
palette.addEventListener('moey:search', async (e) => {
const orders = await fetch(`/api/orders?q=${encodeURIComponent(e.detail.query)}`).then((r) => r.json());
results.innerHTML = orders.map((o) =>
`<a class="palette-item" href="/orders/${o.id}"><i class="mi mi-package"></i>#${o.id}<span class="palette-meta">${o.total}</span></a>`
).join('');
Moey.palette(palette); // filter & select the first result
});
Results from the server should already match, so put their search words in data-moey-keywords if the label doesn't contain them.
Shortcuts
data-moey-hotkey sets the key that opens a palette. mod means Ctrl on Windows and Linux, ⌘ on a Mac. This one opens with Ctrl Shift P — like a code editor:
<dialog class="palette" data-moey-hotkey="mod+shift+p">…</dialog>
<dialog class="palette" data-moey-hotkey="/">…</dialog> <!-- a bare key: ignored while typing in a field -->
<dialog class="palette" data-moey-hotkey="none">…</dialog> <!-- only opens from a button or code -->
Options
| Class / attribute | What it does |
|---|---|
.palette-item | A result: an <a> (opens) or <button> (runs its click) |
.palette-group + .palette-group-title | A titled section; hides when none of its items match |
.palette-detail · .palette-meta · <kbd> | Quiet text after the label · a note or shortcut at the end |
theme-* on an item | Colors it, e.g. theme-red for Sign out |
data-moey-keywords | Extra words that find the item |
data-moey-search-only | Hidden until something is typed |
data-moey-keep-open | Choosing this item doesn't close the palette |
aria-disabled="true" | Shown, but can't be chosen (skipped when nothing else is selected) |
data-moey-hotkey | Opening shortcut. Default mod+k on the first palette, none on the rest |
data-moey-keep-query | Keep the last search when it reopens |
data-moey-close-outside="false" | A click outside doesn't close it |
.palette-lg · --mo-palette-width · --mo-palette-height | Bigger · your own width · how tall the list grows before it scrolls |
From code & events
Moey.open(palette); // open it
Moey.palette(palette, 'refund'); // open it with a search filled in (or re-filter after adding items)
Moey.close(palette);
palette.addEventListener('moey:search', (e) => e.detail.query); // on every keystroke
palette.addEventListener('moey:select', (e) => { // an item was chosen
if (e.detail.item.dataset.id === 'beta') e.preventDefault(); // cancel: stays open, nothing runs
});
It's a real <dialog>, so focus stays inside while it's open and returns to where you were afterwards. The input is a combobox for screen readers: the highlighted result is announced as you move.