Menubar
A horizontal bar of menus, like a desktop application, with full arrow-key navigation and checkable items.
- Version 1.0.0
- Bootstrap 5.3+
- Requires Bootstrap CSS
- Source on GitHub
html
<div class="bootcn-menubar" role="menubar" aria-label="Editor">
<div class="bootcn-menubar__menu" role="none">
<button
type="button"
class="bootcn-menubar__trigger"
id="bootcn-menubar-file"
role="menuitem"
aria-haspopup="menu"
aria-expanded="false"
aria-controls="bootcn-menubar-file-menu"
tabindex="0"
>
File
</button>
<ul
class="dropdown-menu bootcn-dropdown__menu bootcn-menubar__content"
id="bootcn-menubar-file-menu"
role="menu"
aria-labelledby="bootcn-menubar-file"
>
<li role="none">
<button
class="dropdown-item bootcn-dropdown__item"
type="button"
role="menuitem"
tabindex="-1"
>
New tab <span class="bootcn-dropdown__shortcut" aria-hidden="true">⌘T</span>
</button>
</li>
<li role="none">
<button
class="dropdown-item bootcn-dropdown__item"
type="button"
role="menuitem"
tabindex="-1"
>
New window <span class="bootcn-dropdown__shortcut" aria-hidden="true">⌘N</span>
</button>
</li>
<li role="separator"><hr class="dropdown-divider bootcn-dropdown__divider" /></li>
<li role="none">
<button
class="dropdown-item bootcn-dropdown__item"
type="button"
role="menuitem"
tabindex="-1"
>
Print… <span class="bootcn-dropdown__shortcut" aria-hidden="true">⌘P</span>
</button>
</li>
</ul>
</div>
<div class="bootcn-menubar__menu" role="none">
<button
type="button"
class="bootcn-menubar__trigger"
id="bootcn-menubar-edit"
role="menuitem"
aria-haspopup="menu"
aria-expanded="false"
aria-controls="bootcn-menubar-edit-menu"
tabindex="-1"
>
Edit
</button>
<ul
class="dropdown-menu bootcn-dropdown__menu bootcn-menubar__content"
id="bootcn-menubar-edit-menu"
role="menu"
aria-labelledby="bootcn-menubar-edit"
>
<li role="none">
<button
class="dropdown-item bootcn-dropdown__item"
type="button"
role="menuitem"
tabindex="-1"
>
Undo <span class="bootcn-dropdown__shortcut" aria-hidden="true">⌘Z</span>
</button>
</li>
<li role="none">
<button
class="dropdown-item bootcn-dropdown__item"
type="button"
role="menuitem"
tabindex="-1"
>
Redo <span class="bootcn-dropdown__shortcut" aria-hidden="true">⇧⌘Z</span>
</button>
</li>
<li role="separator"><hr class="dropdown-divider bootcn-dropdown__divider" /></li>
<li role="none">
<button
class="dropdown-item bootcn-dropdown__item"
type="button"
role="menuitem"
tabindex="-1"
>
Find… <span class="bootcn-dropdown__shortcut" aria-hidden="true">⌘F</span>
</button>
</li>
</ul>
</div>
<div class="bootcn-menubar__menu" role="none">
<button
type="button"
class="bootcn-menubar__trigger"
id="bootcn-menubar-view"
role="menuitem"
aria-haspopup="menu"
aria-expanded="false"
aria-controls="bootcn-menubar-view-menu"
tabindex="-1"
>
View
</button>
<ul
class="dropdown-menu bootcn-dropdown__menu bootcn-menubar__content"
id="bootcn-menubar-view-menu"
role="menu"
aria-labelledby="bootcn-menubar-view"
>
<li role="none">
<button
class="dropdown-item bootcn-dropdown__item bootcn-menubar__check"
type="button"
role="menuitemcheckbox"
aria-checked="false"
tabindex="-1"
>
<svg
class="bootcn-icon bootcn-menubar__check-icon"
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
focusable="false"
>
<path d="M20 6 9 17l-5-5" />
</svg>
Show bookmarks bar
</button>
</li>
<li role="none">
<button
class="dropdown-item bootcn-dropdown__item bootcn-menubar__check"
type="button"
role="menuitemcheckbox"
aria-checked="true"
tabindex="-1"
>
<svg
class="bootcn-icon bootcn-menubar__check-icon"
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
focusable="false"
>
<path d="M20 6 9 17l-5-5" />
</svg>
Show full URLs
</button>
</li>
<li role="separator"><hr class="dropdown-divider bootcn-dropdown__divider" /></li>
<li role="none">
<button
class="dropdown-item bootcn-dropdown__item"
type="button"
role="menuitem"
tabindex="-1"
>
Reload <span class="bootcn-dropdown__shortcut" aria-hidden="true">⌘R</span>
</button>
</li>
</ul>
</div>
</div>Installation
bash
npx bootcn add menubarbash
pnpm dlx bootcn add menubarbash
yarn dlx bootcn add menubarbash
bunx --bun bootcn add menubarThe CLI also installs the design tokens and these components: dropdown. It prints the <link> and <script> tags to add to your layout.
- Load Bootstrap 5.3 (Bootstrap CSS) and the design tokens.
- Install the components it uses:
dropdown. - Copy the files from Source into your project. With the default
bootcn.jsonthey go here:
| File | Default location |
|---|---|
menubar.html | components/ui/menubar.html |
menubar.css | assets/css/menubar.css |
menubar.js | assets/js/menubar.js |
Usage
Use a menubar for application-style commands (an editor, an admin tool), not for site navigation: for that, use the Navigation menu. Keep each menu short and put shortcuts next to items only if they really work.
JavaScript API
- A
role="menubar"element with classbootcn-menubar; each top item is a.bootcn-menubar__trigger(role="menuitem",aria-haspopup="menu",aria-controls= its menu). Only the first trigger hastabindex="0". - Menu items:
role="menuitem", orrole="menuitemcheckbox"witharia-checked(toggled on activation). - Event
bootcn:menubar:selecton the bar (bubbles),detail.itemanddetail.checked. Bootcn.menubar.init()/destroy().
Accessibility
- WAI-ARIA menubar: one tab stop (roving tabindex); Left/Right move between menus (and switch the open menu), Down/Enter/Space open the menu at the first item, Up opens it at the last.
- Inside a menu: Up/Down, Home/End and first-letter typeahead; Enter/Space activates; Escape closes and returns focus to the trigger; Tab closes.
- Checkable items announce their state (
aria-checked).
Dependencies
- Bootstrap 5.3+ CSS.
themes/tokens/tokens.css.- The
dropdowncomponent.
Source
The exact files the CLI installs (menubar@1.0.0). View on GitHub · Report an issue
html
<div class="bootcn-menubar" role="menubar" aria-label="Editor">
<div class="bootcn-menubar__menu" role="none">
<button
type="button"
class="bootcn-menubar__trigger"
id="bootcn-menubar-file"
role="menuitem"
aria-haspopup="menu"
aria-expanded="false"
aria-controls="bootcn-menubar-file-menu"
tabindex="0"
>
File
</button>
<ul
class="dropdown-menu bootcn-dropdown__menu bootcn-menubar__content"
id="bootcn-menubar-file-menu"
role="menu"
aria-labelledby="bootcn-menubar-file"
>
<li role="none">
<button
class="dropdown-item bootcn-dropdown__item"
type="button"
role="menuitem"
tabindex="-1"
>
New tab <span class="bootcn-dropdown__shortcut" aria-hidden="true">⌘T</span>
</button>
</li>
<li role="none">
<button
class="dropdown-item bootcn-dropdown__item"
type="button"
role="menuitem"
tabindex="-1"
>
New window <span class="bootcn-dropdown__shortcut" aria-hidden="true">⌘N</span>
</button>
</li>
<li role="separator"><hr class="dropdown-divider bootcn-dropdown__divider" /></li>
<li role="none">
<button
class="dropdown-item bootcn-dropdown__item"
type="button"
role="menuitem"
tabindex="-1"
>
Print… <span class="bootcn-dropdown__shortcut" aria-hidden="true">⌘P</span>
</button>
</li>
</ul>
</div>
<div class="bootcn-menubar__menu" role="none">
<button
type="button"
class="bootcn-menubar__trigger"
id="bootcn-menubar-edit"
role="menuitem"
aria-haspopup="menu"
aria-expanded="false"
aria-controls="bootcn-menubar-edit-menu"
tabindex="-1"
>
Edit
</button>
<ul
class="dropdown-menu bootcn-dropdown__menu bootcn-menubar__content"
id="bootcn-menubar-edit-menu"
role="menu"
aria-labelledby="bootcn-menubar-edit"
>
<li role="none">
<button
class="dropdown-item bootcn-dropdown__item"
type="button"
role="menuitem"
tabindex="-1"
>
Undo <span class="bootcn-dropdown__shortcut" aria-hidden="true">⌘Z</span>
</button>
</li>
<li role="none">
<button
class="dropdown-item bootcn-dropdown__item"
type="button"
role="menuitem"
tabindex="-1"
>
Redo <span class="bootcn-dropdown__shortcut" aria-hidden="true">⇧⌘Z</span>
</button>
</li>
<li role="separator"><hr class="dropdown-divider bootcn-dropdown__divider" /></li>
<li role="none">
<button
class="dropdown-item bootcn-dropdown__item"
type="button"
role="menuitem"
tabindex="-1"
>
Find… <span class="bootcn-dropdown__shortcut" aria-hidden="true">⌘F</span>
</button>
</li>
</ul>
</div>
<div class="bootcn-menubar__menu" role="none">
<button
type="button"
class="bootcn-menubar__trigger"
id="bootcn-menubar-view"
role="menuitem"
aria-haspopup="menu"
aria-expanded="false"
aria-controls="bootcn-menubar-view-menu"
tabindex="-1"
>
View
</button>
<ul
class="dropdown-menu bootcn-dropdown__menu bootcn-menubar__content"
id="bootcn-menubar-view-menu"
role="menu"
aria-labelledby="bootcn-menubar-view"
>
<li role="none">
<button
class="dropdown-item bootcn-dropdown__item bootcn-menubar__check"
type="button"
role="menuitemcheckbox"
aria-checked="false"
tabindex="-1"
>
<svg
class="bootcn-icon bootcn-menubar__check-icon"
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
focusable="false"
>
<path d="M20 6 9 17l-5-5" />
</svg>
Show bookmarks bar
</button>
</li>
<li role="none">
<button
class="dropdown-item bootcn-dropdown__item bootcn-menubar__check"
type="button"
role="menuitemcheckbox"
aria-checked="true"
tabindex="-1"
>
<svg
class="bootcn-icon bootcn-menubar__check-icon"
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
focusable="false"
>
<path d="M20 6 9 17l-5-5" />
</svg>
Show full URLs
</button>
</li>
<li role="separator"><hr class="dropdown-divider bootcn-dropdown__divider" /></li>
<li role="none">
<button
class="dropdown-item bootcn-dropdown__item"
type="button"
role="menuitem"
tabindex="-1"
>
Reload <span class="bootcn-dropdown__shortcut" aria-hidden="true">⌘R</span>
</button>
</li>
</ul>
</div>
</div>css
.bootcn-menubar {
display: inline-flex;
align-items: center;
gap: 0.25rem;
padding: 0.25rem;
border: var(--bootcn-border-width) solid var(--bootcn-border-color);
border-radius: var(--bootcn-radius);
background-color: var(--bootcn-background);
box-shadow: var(--bootcn-shadow-sm);
}
.bootcn-menubar__menu {
position: relative;
}
.bootcn-menubar__trigger {
padding: 0.25rem 0.75rem;
border: 0;
border-radius: var(--bootcn-radius-sm);
background: transparent;
color: var(--bootcn-foreground);
font-size: 0.875rem;
font-weight: 500;
}
.bootcn-menubar__trigger:hover,
.bootcn-menubar__trigger[aria-expanded='true'] {
background-color: var(--bootcn-accent);
}
.bootcn-menubar__trigger:focus-visible {
outline: 0;
box-shadow: var(--bootcn-focus-ring);
}
.bootcn-menubar__content {
top: calc(100% + 0.5rem);
left: -0.25rem;
}
.bootcn-menubar__content.show {
display: block;
position: absolute;
}
.bootcn-menubar__check-icon {
visibility: hidden;
}
.bootcn-menubar__check[aria-checked='true'] .bootcn-menubar__check-icon {
visibility: visible;
}js
(function () {
'use strict';
const ns = (window.Bootcn = window.Bootcn || {});
if (ns.menubar) return; // already loaded: idempotent
const BAR = '[role="menubar"].bootcn-menubar';
let listening = false;
function menuItems(menu) {
return Array.from(
menu.querySelectorAll('[role="menuitem"], [role="menuitemcheckbox"], [role="menuitemradio"]'),
).filter((item) => !item.disabled && item.getAttribute('aria-disabled') !== 'true');
}
// Up/Down/Home/End and typeahead inside an open menu. Returns true if handled.
function navigateMenu(menu, event) {
const items = menuItems(menu);
const index = items.indexOf(document.activeElement);
let next = null;
if (event.key === 'ArrowDown') next = items[(index + 1) % items.length];
else if (event.key === 'ArrowUp') next = items[(index - 1 + items.length) % items.length];
else if (event.key === 'Home') next = items[0];
else if (event.key === 'End') next = items[items.length - 1];
else if (event.key.length === 1 && /\S/.test(event.key)) {
const key = event.key.toLowerCase();
const rotated = items.slice(index + 1).concat(items.slice(0, index + 1));
next = rotated.find((item) => item.textContent.trim().toLowerCase().startsWith(key)) || null;
}
if (!next) return false;
event.preventDefault();
next.focus();
return true;
}
function triggers(bar) {
return Array.from(bar.querySelectorAll('.bootcn-menubar__trigger'));
}
function menuOf(trigger) {
return document.getElementById(trigger.getAttribute('aria-controls'));
}
function openTrigger(bar) {
return triggers(bar).find((t) => t.getAttribute('aria-expanded') === 'true') || null;
}
// Roving tabindex: only one trigger is in the tab order.
function focusTrigger(bar, trigger) {
triggers(bar).forEach((t) => t.setAttribute('tabindex', t === trigger ? '0' : '-1'));
trigger.focus();
}
function close(bar, returnFocus) {
const trigger = openTrigger(bar);
if (!trigger) return;
trigger.setAttribute('aria-expanded', 'false');
menuOf(trigger).classList.remove('show');
if (returnFocus) focusTrigger(bar, trigger);
}
function open(bar, trigger, focus) {
close(bar, false);
trigger.setAttribute('aria-expanded', 'true');
const menu = menuOf(trigger);
menu.classList.add('show');
triggers(bar).forEach((t) => t.setAttribute('tabindex', t === trigger ? '0' : '-1'));
const items = menuItems(menu);
if (focus === 'first' && items[0]) items[0].focus();
else if (focus === 'last' && items.length) items[items.length - 1].focus();
}
function adjacent(bar, trigger, step) {
const list = triggers(bar);
return list[(list.indexOf(trigger) + step + list.length) % list.length];
}
function activate(bar, item) {
if (item.getAttribute('role') === 'menuitemcheckbox') {
item.setAttribute('aria-checked', String(item.getAttribute('aria-checked') !== 'true'));
}
close(bar, true);
bar.dispatchEvent(
new CustomEvent('bootcn:menubar:select', {
bubbles: true,
detail: { item: item, checked: item.getAttribute('aria-checked') },
}),
);
}
function onKeydown(event) {
const bar = event.target.closest && event.target.closest(BAR);
if (!bar) return;
const trigger = event.target.closest('.bootcn-menubar__trigger');
const menu = event.target.closest('[role="menu"]');
if (trigger) {
if (event.key === 'ArrowRight' || event.key === 'ArrowLeft') {
event.preventDefault();
const next = adjacent(bar, trigger, event.key === 'ArrowRight' ? 1 : -1);
if (openTrigger(bar)) open(bar, next, 'first');
focusTrigger(bar, next);
if (openTrigger(bar)) menuItems(menuOf(next))[0].focus();
} else if (event.key === 'ArrowDown' || event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
open(bar, trigger, 'first');
} else if (event.key === 'ArrowUp') {
event.preventDefault();
open(bar, trigger, 'last');
} else if (event.key === 'Home' || event.key === 'End') {
event.preventDefault();
const list = triggers(bar);
focusTrigger(bar, event.key === 'Home' ? list[0] : list[list.length - 1]);
} else if (event.key === 'Escape') {
close(bar, true);
}
return;
}
if (!menu) return;
const owner = openTrigger(bar);
if (event.key === 'Escape') {
event.preventDefault();
close(bar, true);
} else if (event.key === 'ArrowRight' || event.key === 'ArrowLeft') {
event.preventDefault();
const next = adjacent(bar, owner, event.key === 'ArrowRight' ? 1 : -1);
open(bar, next, 'first');
} else if (event.key === 'Tab') {
close(bar, false);
} else if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
activate(bar, event.target.closest('[role^="menuitem"]'));
} else {
navigateMenu(menu, event);
}
}
function onClick(event) {
document.querySelectorAll(BAR).forEach((bar) => {
if (!bar.contains(event.target)) close(bar, false);
});
const bar = event.target.closest(BAR);
if (!bar) return;
const trigger = event.target.closest('.bootcn-menubar__trigger');
if (trigger) {
if (trigger.getAttribute('aria-expanded') === 'true') close(bar, true);
else open(bar, trigger, null);
return;
}
const item = event.target.closest('[role^="menuitem"]');
if (item && item.closest('[role="menu"]')) activate(bar, item);
}
// Moving the pointer to another trigger switches menus while one is open.
function onPointerOver(event) {
const trigger = event.target.closest && event.target.closest('.bootcn-menubar__trigger');
if (!trigger) return;
const bar = trigger.closest(BAR);
const current = openTrigger(bar);
if (current && current !== trigger) {
open(bar, trigger, null);
trigger.focus();
}
}
function init() {
if (listening) return;
document.addEventListener('keydown', onKeydown);
document.addEventListener('click', onClick);
document.addEventListener('pointerover', onPointerOver);
listening = true;
}
function destroy() {
document.querySelectorAll(BAR).forEach((bar) => close(bar, false));
if (!listening) return;
document.removeEventListener('keydown', onKeydown);
document.removeEventListener('click', onClick);
document.removeEventListener('pointerover', onPointerOver);
listening = false;
}
ns.menubar = { init: init, destroy: destroy };
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
})();
