Toolbar
A row of related controls with one tab stop and arrow-key navigation, with toggles and grouped choices.
- Version 1.0.0
- Bootstrap 5.3+
- Requires Bootstrap CSS
- Source on GitHub
html
<div class="bootcn-toolbar" role="toolbar" aria-label="Text formatting" data-bootcn-toolbar>
<div class="bootcn-toolbar__group" role="group" aria-label="Style">
<button type="button" class="bootcn-toolbar__button" aria-pressed="false" tabindex="0">
<svg
class="bootcn-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="M7 5h6a3.5 3.5 0 0 1 0 7H7z" />
<path d="M7 12h7a3.5 3.5 0 0 1 0 7H7z" />
</svg>
<span class="visually-hidden">Bold</span>
</button>
<button type="button" class="bootcn-toolbar__button" aria-pressed="true" tabindex="-1">
<svg
class="bootcn-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="M19 4h-9" />
<path d="M14 20H5" />
<path d="M15 4 9 20" />
</svg>
<span class="visually-hidden">Italic</span>
</button>
<button type="button" class="bootcn-toolbar__button" aria-pressed="false" tabindex="-1">
<svg
class="bootcn-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="M6 4v6a6 6 0 0 0 12 0V4" />
<path d="M4 20h16" />
</svg>
<span class="visually-hidden">Underline</span>
</button>
</div>
<div class="bootcn-toolbar__separator" role="separator" aria-orientation="vertical"></div>
<div
class="bootcn-toolbar__group"
role="group"
aria-label="Alignment"
data-bootcn-toolbar-exclusive
>
<button type="button" class="bootcn-toolbar__button" aria-pressed="true" tabindex="-1">
<svg
class="bootcn-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="M21 6H3" />
<path d="M15 12H3" />
<path d="M17 18H3" />
</svg>
<span class="visually-hidden">Align left</span>
</button>
<button type="button" class="bootcn-toolbar__button" aria-pressed="false" tabindex="-1">
<svg
class="bootcn-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="M21 6H3" />
<path d="M17 12H7" />
<path d="M19 18H5" />
</svg>
<span class="visually-hidden">Align center</span>
</button>
<button type="button" class="bootcn-toolbar__button" aria-pressed="false" tabindex="-1">
<svg
class="bootcn-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="M21 6H3" />
<path d="M21 12H9" />
<path d="M21 18H7" />
</svg>
<span class="visually-hidden">Align right</span>
</button>
</div>
<div class="bootcn-toolbar__separator" role="separator" aria-orientation="vertical"></div>
<button type="button" class="bootcn-toolbar__button" tabindex="-1">
<svg
class="bootcn-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"
>
<rect x="8" y="8" width="13" height="13" rx="2" />
<path d="M16 8V5a2 2 0 0 0-2-2H5a2 2 0 0 0-2 2v9a2 2 0 0 0 2 2h3" />
</svg>
<span class="visually-hidden">Copy</span>
</button>
<button type="button" class="bootcn-toolbar__button" tabindex="-1">
<svg
class="bootcn-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="M3 6h18" />
<path d="M8 6V4h8v2" />
<path d="M19 6l-1 14H6L5 6" />
</svg>
<span class="visually-hidden">Delete</span>
</button>
</div>Installation
bash
npx bootcn add toolbarbash
pnpm dlx bootcn add toolbarbash
yarn dlx bootcn add toolbarbash
bunx --bun bootcn add toolbarThe CLI also installs the design tokens and these components: none. 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: none.
- Copy the files from Source into your project. With the default
bootcn.jsonthey go here:
| File | Default location |
|---|---|
toolbar.html | components/ui/toolbar.html |
toolbar.css | assets/css/toolbar.css |
toolbar.js | assets/js/toolbar.js |
Usage
Group related actions (formatting, view options) in a toolbar. Separate groups with role="separator". Keep the number of controls small; for many commands, use a Menubar.
JavaScript API
role="toolbar"withdata-bootcn-toolbarand anaria-label. One control hastabindex="0", the others-1(the script keeps it that way).- Buttons with
aria-pressedtoggle; inside a group markeddata-bootcn-toolbar-exclusive, pressing one releases the others. - Event
bootcn:toolbar:change(bubbles),detail.buttonanddetail.pressed. Bootcn.toolbar.init(root)/destroy().
Accessibility
- WAI-ARIA toolbar: one tab stop; Left/Right move between controls (wrapping), Home and End jump to the ends.
- Toggles announce "pressed"; groups are named ("Style", "Alignment"); icon-only buttons have visually hidden names.
Dependencies
- Bootstrap 5.3+ CSS.
themes/tokens/tokens.css.
Source
The exact files the CLI installs (toolbar@1.0.0). View on GitHub · Report an issue
html
<div class="bootcn-toolbar" role="toolbar" aria-label="Text formatting" data-bootcn-toolbar>
<div class="bootcn-toolbar__group" role="group" aria-label="Style">
<button type="button" class="bootcn-toolbar__button" aria-pressed="false" tabindex="0">
<svg
class="bootcn-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="M7 5h6a3.5 3.5 0 0 1 0 7H7z" />
<path d="M7 12h7a3.5 3.5 0 0 1 0 7H7z" />
</svg>
<span class="visually-hidden">Bold</span>
</button>
<button type="button" class="bootcn-toolbar__button" aria-pressed="true" tabindex="-1">
<svg
class="bootcn-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="M19 4h-9" />
<path d="M14 20H5" />
<path d="M15 4 9 20" />
</svg>
<span class="visually-hidden">Italic</span>
</button>
<button type="button" class="bootcn-toolbar__button" aria-pressed="false" tabindex="-1">
<svg
class="bootcn-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="M6 4v6a6 6 0 0 0 12 0V4" />
<path d="M4 20h16" />
</svg>
<span class="visually-hidden">Underline</span>
</button>
</div>
<div class="bootcn-toolbar__separator" role="separator" aria-orientation="vertical"></div>
<div
class="bootcn-toolbar__group"
role="group"
aria-label="Alignment"
data-bootcn-toolbar-exclusive
>
<button type="button" class="bootcn-toolbar__button" aria-pressed="true" tabindex="-1">
<svg
class="bootcn-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="M21 6H3" />
<path d="M15 12H3" />
<path d="M17 18H3" />
</svg>
<span class="visually-hidden">Align left</span>
</button>
<button type="button" class="bootcn-toolbar__button" aria-pressed="false" tabindex="-1">
<svg
class="bootcn-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="M21 6H3" />
<path d="M17 12H7" />
<path d="M19 18H5" />
</svg>
<span class="visually-hidden">Align center</span>
</button>
<button type="button" class="bootcn-toolbar__button" aria-pressed="false" tabindex="-1">
<svg
class="bootcn-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="M21 6H3" />
<path d="M21 12H9" />
<path d="M21 18H7" />
</svg>
<span class="visually-hidden">Align right</span>
</button>
</div>
<div class="bootcn-toolbar__separator" role="separator" aria-orientation="vertical"></div>
<button type="button" class="bootcn-toolbar__button" tabindex="-1">
<svg
class="bootcn-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"
>
<rect x="8" y="8" width="13" height="13" rx="2" />
<path d="M16 8V5a2 2 0 0 0-2-2H5a2 2 0 0 0-2 2v9a2 2 0 0 0 2 2h3" />
</svg>
<span class="visually-hidden">Copy</span>
</button>
<button type="button" class="bootcn-toolbar__button" tabindex="-1">
<svg
class="bootcn-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="M3 6h18" />
<path d="M8 6V4h8v2" />
<path d="M19 6l-1 14H6L5 6" />
</svg>
<span class="visually-hidden">Delete</span>
</button>
</div>css
.bootcn-toolbar {
display: inline-flex;
flex-wrap: wrap;
align-items: center;
gap: 0.25rem;
padding: 0.25rem;
border: var(--bootcn-border-width) solid var(--bootcn-border-color);
border-radius: var(--bootcn-radius-lg);
background-color: var(--bootcn-background);
box-shadow: var(--bootcn-shadow-sm);
}
.bootcn-toolbar__group {
display: inline-flex;
gap: 0.125rem;
}
.bootcn-toolbar__separator {
width: 1px;
height: 1.25rem;
margin: 0 0.25rem;
background-color: var(--bootcn-border-color);
}
.bootcn-toolbar__button {
display: inline-flex;
align-items: center;
justify-content: center;
width: 2rem;
height: 2rem;
border: 0;
border-radius: var(--bootcn-radius);
background: transparent;
color: var(--bootcn-foreground);
}
.bootcn-toolbar__button:hover {
background-color: var(--bootcn-muted);
}
.bootcn-toolbar__button[aria-pressed='true'] {
background-color: var(--bootcn-accent);
color: var(--bootcn-accent-foreground);
box-shadow: inset 0 0 0 1px var(--bootcn-border-color);
}
.bootcn-toolbar__button:focus-visible {
outline: 0;
box-shadow: var(--bootcn-focus-ring);
}js
(function () {
'use strict';
const ns = (window.Bootcn = window.Bootcn || {});
if (ns.toolbar) return; // already loaded: idempotent
const ROOT = '[role="toolbar"][data-bootcn-toolbar]';
let listening = false;
function controls(toolbar) {
return Array.from(toolbar.querySelectorAll('button, input, select, a[href]')).filter(
(el) => !el.disabled,
);
}
// Roving tabindex: the toolbar is one tab stop; arrows move inside it.
function focusControl(toolbar, control) {
controls(toolbar).forEach((el) => el.setAttribute('tabindex', el === control ? '0' : '-1'));
control.focus();
}
function onKeydown(event) {
const toolbar = event.target.closest && event.target.closest(ROOT);
if (!toolbar) return;
const list = controls(toolbar);
const index = list.indexOf(event.target);
if (index < 0) return;
let next = null;
if (event.key === 'ArrowRight') next = list[(index + 1) % list.length];
else if (event.key === 'ArrowLeft') next = list[(index - 1 + list.length) % list.length];
else if (event.key === 'Home') next = list[0];
else if (event.key === 'End') next = list[list.length - 1];
if (!next) return;
event.preventDefault();
focusControl(toolbar, next);
}
function onClick(event) {
const button = event.target.closest && event.target.closest(ROOT + ' button');
if (!button) return;
const toolbar = button.closest(ROOT);
focusControl(toolbar, button);
if (!button.hasAttribute('aria-pressed')) return;
const group = button.closest('[data-bootcn-toolbar-exclusive]');
if (group) {
group.querySelectorAll('[aria-pressed]').forEach((other) => {
other.setAttribute('aria-pressed', String(other === button));
});
} else {
button.setAttribute('aria-pressed', String(button.getAttribute('aria-pressed') !== 'true'));
}
toolbar.dispatchEvent(
new CustomEvent('bootcn:toolbar:change', {
bubbles: true,
detail: { button: button, pressed: button.getAttribute('aria-pressed') === 'true' },
}),
);
}
function init(root = document) {
root.querySelectorAll(ROOT).forEach((toolbar) => {
const list = controls(toolbar);
if (!list.some((el) => el.getAttribute('tabindex') === '0') && list[0]) {
list[0].setAttribute('tabindex', '0');
}
});
if (listening) return;
document.addEventListener('keydown', onKeydown);
document.addEventListener('click', onClick);
listening = true;
}
function destroy() {
if (!listening) return;
document.removeEventListener('keydown', onKeydown);
document.removeEventListener('click', onClick);
listening = false;
}
ns.toolbar = { init: init, destroy: destroy };
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => init());
} else {
init();
}
})();
