Components Overlays
Menubar
Arrange application commands into keyboard-accessible menus.
Usage
Each top-level item has a Label and an Items array of commands with Label and Value. Left and Right switch menus, Down opens one, and Escape returns to its trigger. Listen for mono:command to connect command values to app actions.
Register menubar with Head and pass its properties to Render. Go setup guide
Go usage
err := mono.UI.Render(w, "menubar", mono.Props{
"Items": []any{
map[string]any{
"Items": []any{
map[string]any{
"Label": "New proof",
"Value": "new",
},
map[string]any{
"Label": "Duplicate proof",
"Value": "duplicate",
},
map[string]any{
"Label": "Export proof",
"Value": "export",
},
},
"Label": "File",
},
map[string]any{
"Items": []any{
map[string]any{
"Label": "Fit to page",
"Value": "fit",
},
map[string]any{
"Label": "Actual size",
"Value": "actual",
},
},
"Label": "View",
},
map[string]any{
"Items": []any{
map[string]any{
"Label": "Show shortcuts",
"Value": "shortcuts",
},
map[string]any{
"Label": "About the editor",
"Value": "about",
},
},
"Label": "Help",
},
},
"Label": "Proof editor",
})Rendered HTML
<div id="demo-menubar" data-mono="menubar" class="mu-stack">
<div role="menubar" aria-label="Proof editor">
<div role="none">
<button class="mu-btn mu-ghost" id="demo-menubar-trigger-0" type="button" role="menuitem" aria-haspopup="menu" aria-expanded="false" aria-controls="demo-menubar-menu-0" popovertarget="demo-menubar-menu-0">File</button>
<div class="mu-panel" id="demo-menubar-menu-0" role="menu" aria-labelledby="demo-menubar-trigger-0" popover>
<button type="button" class="mu-item" tabindex="-1" role="menuitem" data-value="new">New proof</button><button type="button" class="mu-item" tabindex="-1" role="menuitem" data-value="duplicate">Duplicate proof</button><button type="button" class="mu-item" tabindex="-1" role="menuitem" data-value="export">Export proof</button>
</div>
</div><div role="none">
<button class="mu-btn mu-ghost" id="demo-menubar-trigger-1" type="button" role="menuitem" aria-haspopup="menu" aria-expanded="false" aria-controls="demo-menubar-menu-1" popovertarget="demo-menubar-menu-1">View</button>
<div class="mu-panel" id="demo-menubar-menu-1" role="menu" aria-labelledby="demo-menubar-trigger-1" popover>
<button type="button" class="mu-item" tabindex="-1" role="menuitem" data-value="fit">Fit to page</button><button type="button" class="mu-item" tabindex="-1" role="menuitem" data-value="actual">Actual size</button>
</div>
</div><div role="none">
<button class="mu-btn mu-ghost" id="demo-menubar-trigger-2" type="button" role="menuitem" aria-haspopup="menu" aria-expanded="false" aria-controls="demo-menubar-menu-2" popovertarget="demo-menubar-menu-2">Help</button>
<div class="mu-panel" id="demo-menubar-menu-2" role="menu" aria-labelledby="demo-menubar-trigger-2" popover>
<button type="button" class="mu-item" tabindex="-1" role="menuitem" data-value="shortcuts">Show shortcuts</button><button type="button" class="mu-item" tabindex="-1" role="menuitem" data-value="about">About the editor</button>
</div>
</div>
</div>
<p data-result class="mu-small" role="status"></p>
</div>Go template
<div id="{{.ID}}" data-mono="menubar" class="mu-stack">
<div role="menubar" aria-label="{{.Label}}">
{{range $i, $item := .Items}}<div role="none">
<button class="mu-btn mu-ghost" id="{{$.ID}}-trigger-{{$i}}" type="button" role="menuitem" aria-haspopup="menu" aria-expanded="false" aria-controls="{{$.ID}}-menu-{{$i}}" popovertarget="{{$.ID}}-menu-{{$i}}">{{$item.Label}}</button>
<div class="mu-panel" id="{{$.ID}}-menu-{{$i}}" role="menu" aria-labelledby="{{$.ID}}-trigger-{{$i}}" popover>
{{range $item.Items}}<button type="button" class="mu-item" tabindex="-1" role="menuitem" data-value="{{.Value}}"{{if .Disabled}} disabled{{end}}>{{.Label}}</button>{{end}}
</div>
</div>{{end}}
</div>
<p data-result class="mu-small" role="status"></p>
</div>Component CSS
[data-mono="menubar"] [role="menubar"] { display: flex; gap: .15rem; width: fit-content; border: 1px solid #ccc; padding: .2rem; border-radius: .45rem; }
[data-mono="menubar"] [role="menu"] { position: fixed; inset: auto; margin: 0; min-width: 10rem; max-width: calc(100vw - 1rem); max-height: calc(100dvh - 1rem); overflow: auto; padding: .3rem; border: 1px solid #c6c6c6; border-radius: .45rem; background: white; color: #171717; box-shadow: 0 .55rem 1.75rem #0002; }
[data-mono="menubar"] [role="menu"] button { display: block; width: 100%; text-align: start; border-radius: .25rem; padding: .5rem .65rem; }
[data-mono="menubar"] [role="menu"] button:focus { outline: 1px solid #777; outline-offset: -1px; background: #eee; }
[data-mono="menubar"] [data-result]:empty { display: none; }JavaScript module
export function mount(root) {
const bar = root.querySelector('[role="menubar"]');
const triggers = [...bar.querySelectorAll('[aria-haspopup="menu"]')];
const menus = [...bar.querySelectorAll('[role="menu"]')];
const commands = menus.map(menu => [...menu.querySelectorAll('button:not(:disabled)')]);
const cycle = (index, delta, length) => (index + delta + length) % length;
function activeTrigger(index) {
triggers.forEach((button, i) => { button.tabIndex = i === index ? 0 : -1; });
}
function show(index, last = false) {
const items = commands[index];
if (!items.length) return;
activeTrigger(index);
const menu = menus[index];
menu.showPopover();
triggers[index].setAttribute('aria-expanded', 'true');
const rect = triggers[index].getBoundingClientRect();
const left = Math.max(8, Math.min(rect.left, document.documentElement.clientWidth - menu.offsetWidth - 8));
const top = Math.max(8, Math.min(rect.bottom + 4, document.documentElement.clientHeight - menu.offsetHeight - 8));
menu.style.left = left + 'px';
menu.style.top = top + 'px';
(last ? items.at(-1) : items[0]).focus();
}
function close(index) {
menus[index].hidePopover();
triggers[index].focus();
}
activeTrigger(0);
triggers.forEach((trigger, i) => {
const menu = menus[i];
const items = commands[i];
trigger.addEventListener('focus', () => activeTrigger(i));
trigger.addEventListener('click', event => {
event.preventDefault();
if (menu.matches(':popover-open')) close(i);
else show(i);
});
menu.addEventListener('toggle', () => {
trigger.setAttribute('aria-expanded', String(menu.matches(':popover-open')));
});
trigger.addEventListener('keydown', event => {
let next;
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') show(i, event.key === 'ArrowUp');
else if (event.key === 'ArrowRight') next = cycle(i, 1, triggers.length);
else if (event.key === 'ArrowLeft') next = cycle(i, -1, triggers.length);
else if (event.key === 'Home') next = 0;
else if (event.key === 'End') next = triggers.length - 1;
else return;
event.preventDefault();
if (next !== undefined) triggers[next].focus();
});
menu.addEventListener('keydown', event => {
const at = items.indexOf(document.activeElement);
if (event.key === 'ArrowDown') items[cycle(at, 1, items.length)].focus();
else if (event.key === 'ArrowUp') items[cycle(at, -1, items.length)].focus();
else if (event.key === 'Home') items[0].focus();
else if (event.key === 'End') items.at(-1).focus();
else if (event.key === 'ArrowLeft' || event.key === 'ArrowRight') {
menu.hidePopover();
show(cycle(i, event.key === 'ArrowRight' ? 1 : -1, triggers.length));
} else if (event.key === 'Escape') close(i);
else if (event.key === 'Tab') { close(i); return; }
else return;
event.preventDefault();
});
menu.addEventListener('click', event => {
const command = event.target.closest('button:not(:disabled)');
if (!command || !menu.contains(command)) return;
root.querySelector('[data-result]').textContent = command.textContent;
close(i);
root.dispatchEvent(new CustomEvent('mono:command', { bubbles: true, detail: { value: command.dataset.value } }));
});
});
}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
<link rel="stylesheet" href="./base.css">
<link rel="stylesheet" href="./menubar.css">
<script type="module" src="./menubar.js"></script>Copy into your project
Run this command from the Mono checkout. Set -out to the component directory in your application.
go run ./cmd/mono-ui add -out ./components menubarExample properties (JSON)
{
"Items": [
{
"Items": [
{
"Label": "New proof",
"Value": "new"
},
{
"Label": "Duplicate proof",
"Value": "duplicate"
},
{
"Label": "Export proof",
"Value": "export"
}
],
"Label": "File"
},
{
"Items": [
{
"Label": "Fit to page",
"Value": "fit"
},
{
"Label": "Actual size",
"Value": "actual"
}
],
"Label": "View"
},
{
"Items": [
{
"Label": "Show shortcuts",
"Value": "shortcuts"
},
{
"Label": "About the editor",
"Value": "about"
}
],
"Label": "Help"
}
],
"Label": "Proof editor"
}