Tooltip
Short hints on hover and focus, built on Bootstrap Tooltip, dismissible with Escape.
Installation
bash
npx bootcn add tooltipbash
pnpm dlx bootcn add tooltipbash
yarn dlx bootcn add tooltipbash
bunx --bun bootcn add tooltipThe CLI also installs the design tokens and these components: button. It prints the <link> and <script> tags to add to your layout.
Usage
html
<button
type="button"
class="btn bootcn-button bootcn-button--outline"
data-bs-toggle="tooltip"
data-bs-title="Add to library"
data-bs-custom-class="bootcn-tooltip"
>
Hover or focus me
</button>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.9/dist/js/bootstrap.bundle.min.js"></script>
<script src="assets/js/tooltip.js"></script>All of Bootstrap's data-bs-* tooltip options work (placement, trigger, delay, …). data-bs-custom-class="bootcn-tooltip" applies the Bootcn style.
JavaScript API
js
window.Bootcn.tooltip.init(container); // create tooltips under container (idempotent)
window.Bootcn.tooltip.destroy(container); // dispose theminit() runs on page load for the whole document. Call it again with the new container after inserting HTML.
Dependencies
- Bootstrap 5.3+ CSS and JavaScript bundle (Tooltip + Popper). If the bundle is missing,
tooltip.jslogs one console error. themes/tokens/tokens.css,button(example triggers).
Accessibility
- Put tooltips only on focusable elements, so keyboard users see them too.
- A tooltip supplements an accessible name; it does not replace it. Icon buttons still need
aria-label. - Bootstrap links the tooltip with
aria-describedbywhile it is shown. - Do not put interactive content in a tooltip; use a Popover instead.
Source
The exact files the CLI installs (tooltip@1.1.0). View on GitHub · Report an issue
html
<div class="d-flex flex-wrap gap-2">
<button
type="button"
class="btn bootcn-button bootcn-button--outline"
data-bs-toggle="tooltip"
data-bs-title="Add to library"
data-bs-custom-class="bootcn-tooltip"
>
Hover or focus me
</button>
<button
type="button"
class="btn bootcn-button bootcn-button--outline bootcn-button--icon"
aria-label="Settings"
data-bs-toggle="tooltip"
data-bs-title="Settings"
data-bs-placement="bottom"
data-bs-custom-class="bootcn-tooltip"
>
<svg
width="18"
height="18"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
aria-hidden="true"
focusable="false"
>
<circle cx="12" cy="12" r="3" />
<path
d="M12 2v3M12 19v3M2 12h3M19 12h3M4.9 4.9l2.1 2.1M17 17l2.1 2.1M4.9 19.1 7 17M17 7l2.1-2.1"
/>
</svg>
</button>
</div>css
.bootcn-tooltip {
--bs-tooltip-bg: var(--bootcn-primary);
--bs-tooltip-color: var(--bootcn-primary-foreground);
--bs-tooltip-border-radius: var(--bootcn-radius);
--bs-tooltip-padding-x: 0.75rem;
--bs-tooltip-padding-y: 0.375rem;
--bs-tooltip-font-size: 0.75rem;
--bs-tooltip-opacity: 1;
}
.bootcn-tooltip .tooltip-inner {
box-shadow: var(--bootcn-shadow-md);
font-weight: 500;
}js
(function () {
'use strict';
const ns = (window.Bootcn = window.Bootcn || {});
if (ns.tooltip) return; // already loaded: idempotent
const SELECTOR = '[data-bs-toggle="tooltip"]';
let listening = false;
function plugin() {
return window.bootstrap && window.bootstrap.Tooltip;
}
function ensureBootstrap() {
if (plugin()) return true;
console.error(
'[bootcn] tooltip: Bootstrap JavaScript is required. Load the Bootstrap bundle (window.bootstrap.Tooltip) before bootcn tooltip.js.',
);
return false;
}
function targets(root) {
const found = [];
if (root instanceof Element && root.matches(SELECTOR)) found.push(root);
if (root && typeof root.querySelectorAll === 'function') {
root.querySelectorAll(SELECTOR).forEach(function (element) {
found.push(element);
});
}
return found;
}
// WCAG 1.4.13: content shown on hover or focus must be dismissible without moving focus.
function handleKeydown(event) {
if (event.key !== 'Escape') return;
const Plugin = plugin();
if (!Plugin) return;
document.querySelectorAll(SELECTOR + '[aria-describedby]').forEach(function (element) {
const instance = Plugin.getInstance(element);
if (instance) instance.hide();
});
}
function init(root = document) {
if (!ensureBootstrap()) return;
const Plugin = plugin();
targets(root).forEach(function (element) {
Plugin.getOrCreateInstance(element);
});
if (!listening) {
document.addEventListener('keydown', handleKeydown);
listening = true;
}
}
function destroy(root = document) {
const Plugin = plugin();
if (Plugin) {
targets(root).forEach(function (element) {
const instance = Plugin.getInstance(element);
if (instance) instance.dispose();
});
}
if (root === document && listening) {
document.removeEventListener('keydown', handleKeydown);
listening = false;
}
}
ns.tooltip = { init: init, destroy: destroy };
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', function () {
init(document);
});
} else {
init(document);
}
})();
