Components Overlays

Command palette

Search a set of commands and run the selected command from the keyboard.

Live previewGo + JavaScript

Command palette

Usage

Set Items with a Label and machine-readable Value. Type in the search box, use Up or Down to highlight a match, and press Enter to run it. Escape closes the dialog. Listen for mono:command and read detail.value.

Register command with Head and pass its properties to Render. Go setup guide

Go usage
Go usage
err := mono.UI.Render(w, "command", mono.Props{
	"Close":      "Close",
	"EmptyLabel": "No matching commands.",
	"Items": []any{
		map[string]any{
			"Label": "Create a new issue",
			"Value": "new-issue",
		},
		map[string]any{
			"Label": "Review print proof",
			"Value": "review-proof",
		},
		map[string]any{
			"Label": "Open publishing queue",
			"Value": "publishing-queue",
		},
		map[string]any{
			"Label": "Export issue notes",
			"Value": "export-notes",
		},
	},
	"Label":       "Search commands",
	"Placeholder": "Search actions…",
	"Title":       "Command palette",
	"Trigger":     "Find a command",
})
Rendered HTML
Rendered HTML
<div id="demo-command" data-mono="command" class="mu-stack">
  <button type="button" class="mu-btn" data-open command="show-modal" commandfor="demo-command-dialog">Find a command</button>
  <dialog id="demo-command-dialog" class="mu-panel" aria-labelledby="demo-command-title">
    <div class="mu-stack">
      <div class="mu-row"><h3 id="demo-command-title" class="mu-title">Command palette</h3>
        <button class="mu-btn mu-ghost" type="button" data-close command="close" commandfor="demo-command-dialog">Close</button>
      </div>
      <label class="mu-stack mu-label">Search commands
        <input class="mu-field" type="search" role="combobox" aria-autocomplete="list" aria-expanded="true" aria-controls="demo-command-list" autocomplete="off" placeholder="Search actions…" autofocus>
      </label>
      <div id="demo-command-list" role="listbox" aria-label="Search commands">
        <button class="mu-item" id="demo-command-opt-0" type="button" role="option" aria-selected="false" tabindex="-1" data-value="new-issue">Create a new issue</button><button class="mu-item" id="demo-command-opt-1" type="button" role="option" aria-selected="false" tabindex="-1" data-value="review-proof">Review print proof</button><button class="mu-item" id="demo-command-opt-2" type="button" role="option" aria-selected="false" tabindex="-1" data-value="publishing-queue">Open publishing queue</button><button class="mu-item" id="demo-command-opt-3" type="button" role="option" aria-selected="false" tabindex="-1" data-value="export-notes">Export issue notes</button>
      </div>
      <p role="status" data-empty class="mu-muted" hidden>No matching commands.</p>
    </div>
  </dialog>
  <p data-result class="mu-small" role="status"></p>
</div>
Go template
Go template
<div id="{{.ID}}" data-mono="command" class="mu-stack">
  <button type="button" class="mu-btn" data-open command="show-modal" commandfor="{{.ID}}-dialog">{{.Trigger}}</button>
  <dialog id="{{.ID}}-dialog" class="mu-panel" aria-labelledby="{{.ID}}-title">
    <div class="mu-stack">
      <div class="mu-row"><h3 id="{{.ID}}-title" class="mu-title">{{.Title}}</h3>
        <button class="mu-btn mu-ghost" type="button" data-close command="close" commandfor="{{.ID}}-dialog">{{.Close}}</button>
      </div>
      <label class="mu-stack mu-label">{{.Label}}
        <input class="mu-field" type="search" role="combobox" aria-autocomplete="list" aria-expanded="true" aria-controls="{{.ID}}-list" autocomplete="off" placeholder="{{.Placeholder}}" autofocus>
      </label>
      <div id="{{.ID}}-list" role="listbox" aria-label="{{.Label}}">
        {{range $i, $item := .Items}}<button class="mu-item" id="{{$.ID}}-opt-{{$i}}" type="button" role="option" aria-selected="false" tabindex="-1" data-value="{{$item.Value}}">{{$item.Label}}</button>{{end}}
      </div>
      <p role="status" data-empty class="mu-muted" hidden>{{.EmptyLabel}}</p>
    </div>
  </dialog>
  <p data-result class="mu-small" role="status"></p>
</div>
Component CSS
Component CSS
[data-mono="command"] > button { align-self: flex-start; justify-self: start; }
[data-mono="command"] [data-result]:empty { display: none; }
[data-mono="command"] dialog { margin: auto; padding: 1.1rem; border: 1px solid #c4c4c4; border-radius: .65rem; width: min(25rem, calc(100% - 2rem)); max-height: calc(100dvh - 2rem); overflow: auto; color: #151515; background: white; }
[data-mono="command"] dialog::backdrop { background: #1118; }
[data-mono="command"] dialog .mu-row { justify-content: space-between; }
[data-mono="command"] [role="listbox"] { max-height: min(16rem, 44dvh); overflow: auto; }
[data-mono="command"] [role="option"]:not([hidden]) { display: block; width: 100%; padding: .65rem; text-align: start; border-radius: .3rem; }
[data-mono="command"] [role="option"]:is([aria-selected="true"], :hover) { background: #eee; }
JavaScript module
JavaScript module
export function mount(root) {
  const dialog = root.querySelector('dialog');
  const trigger = root.querySelector('[data-open]');
  const input = dialog.querySelector('input');
  const items = [...dialog.querySelectorAll('[role="option"]')];
  let matches = [];
  let active = -1;

  function highlight(index) {
    active = index;
    for (const item of items) item.setAttribute('aria-selected', String(item === matches[index]));
    if (matches[index]) {
      input.setAttribute('aria-activedescendant', matches[index].id);
      matches[index].scrollIntoView({ block: 'nearest' });
    } else input.removeAttribute('aria-activedescendant');
  }
  function filter() {
    const query = input.value.trim().toLocaleLowerCase();
    matches = items.filter(item => item.textContent.toLocaleLowerCase().includes(query));
    for (const item of items) item.hidden = !matches.includes(item);
    dialog.querySelector('[data-empty]').hidden = matches.length > 0;
    highlight(matches.length ? 0 : -1);
  }
  function choose(item) {
    if (!item) return;
    root.querySelector('[data-result]').textContent = item.textContent;
    dialog.close();
    root.dispatchEvent(new CustomEvent('mono:command', { bubbles: true, detail: { value: item.dataset.value } }));
  }
  trigger.addEventListener('click', event => {
    event.preventDefault();
    if (!dialog.open) dialog.showModal();
    input.value = '';
    filter();
    input.focus();
  });
  dialog.querySelector('[data-close]').addEventListener('click', event => {
    event.preventDefault();
    dialog.close();
  });
  dialog.addEventListener('close', () => trigger.focus());
  input.addEventListener('input', filter);
  input.addEventListener('keydown', event => {
    if (event.isComposing || !matches.length) return;
    if (event.key === 'ArrowDown') highlight((active + 1) % matches.length);
    else if (event.key === 'ArrowUp') highlight((active - 1 + matches.length) % matches.length);
    else if (event.key === 'Enter') choose(matches[active]);
    else return;
    event.preventDefault();
  });
  items.forEach(item => {
    item.addEventListener('mousedown', event => event.preventDefault());
    item.addEventListener('click', () => choose(item));
  });
}
Use the HTML in a static site

Copy the rendered HTML above. Download these assets into the same folder as your page, then add the tags below to its head.

Shared CSS Component CSS Ready-to-use module

HTML head
<link rel="stylesheet" href="./base.css">
<link rel="stylesheet" href="./command.css">
<script type="module" src="./command.js"></script>

Copy into your project

Run this command from the Mono checkout. Set -out to the component directory in your application.

Terminal
go run ./cmd/mono-ui add -out ./components command
Example properties (JSON)
example.json
{
  "Close": "Close",
  "EmptyLabel": "No matching commands.",
  "Items": [
    {
      "Label": "Create a new issue",
      "Value": "new-issue"
    },
    {
      "Label": "Review print proof",
      "Value": "review-proof"
    },
    {
      "Label": "Open publishing queue",
      "Value": "publishing-queue"
    },
    {
      "Label": "Export issue notes",
      "Value": "export-notes"
    }
  ],
  "Label": "Search commands",
  "Placeholder": "Search actions…",
  "Title": "Command palette",
  "Trigger": "Find a command"
}