Skip to content

Popover ​

Rich click-triggered overlays built on Bootstrap Popover, closable with Escape.

  • Version 1.1.0
  • Bootstrap 5.3+
  • Requires Bootstrap CSS + Bootstrap JS bundle
  • Source on GitHub

Installation ​

bash
npx bootcn add popover
bash
pnpm dlx bootcn add popover
bash
yarn dlx bootcn add popover
bash
bunx --bun bootcn add popover

The 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="popover"
  data-bs-title="Storage"
  data-bs-content="You are using 7.2 GB of your 10 GB plan."
  data-bs-custom-class="bootcn-popover"
>
  Show usage
</button>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.9/dist/js/bootstrap.bundle.min.js"></script>
<script src="assets/js/popover.js"></script>
  • data-bs-trigger="focus" closes the popover on the next click anywhere (use a <button> trigger).
  • Content is text. Bootstrap's html option is off by default; if you enable it, only pass trusted, sanitized HTML.

JavaScript API ​

js
window.Bootcn.popover.init(container); // idempotent
window.Bootcn.popover.destroy(container);

Dependencies ​

  • Bootstrap 5.3+ CSS and JavaScript bundle (Popover + Popper).
  • themes/tokens/tokens.css, button (example triggers).

Accessibility ​

  • Triggers are buttons; Bootstrap sets aria-describedby to the open popover.
  • Escape closes the popover and focus stays on the trigger.
  • Popovers are not modal and do not trap focus. For forms or complex content, use the Dialog component.
  • Bootstrap renders the popover title as an <h3>. Use popovers with a title on pages that already have an <h2>, or set Bootstrap's template option to a different heading level, so heading levels do not skip.

Source ​

The exact files the CLI installs (popover@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="popover"
    data-bs-title="Storage"
    data-bs-content="You are using 7.2 GB of your 10 GB plan."
    data-bs-custom-class="bootcn-popover"
  >
    Show usage
  </button>
  <button
    type="button"
    class="btn bootcn-button bootcn-button--ghost"
    data-bs-toggle="popover"
    data-bs-trigger="focus"
    data-bs-placement="bottom"
    data-bs-content="Dismissed when focus moves elsewhere."
    data-bs-custom-class="bootcn-popover"
  >
    Dismiss on next click
  </button>
</div>
css
.bootcn-popover {
  --bs-popover-border-color: var(--bootcn-border-color);
  --bs-popover-border-radius: var(--bootcn-radius);
  --bs-popover-inner-border-radius: calc(var(--bootcn-radius) - var(--bootcn-border-width));
  --bs-popover-box-shadow: var(--bootcn-shadow-md);
  --bs-popover-bg: var(--bootcn-background);
  --bs-popover-header-bg: transparent;
  --bs-popover-header-padding-x: 1rem;
  --bs-popover-header-padding-y: 0.75rem;
  --bs-popover-body-padding-x: 1rem;
  --bs-popover-body-padding-y: 0.75rem;
  --bs-popover-body-color: var(--bootcn-muted-foreground);
  --bs-popover-header-color: var(--bootcn-foreground);
  --bs-popover-font-size: 0.875rem;

  box-shadow: var(--bs-popover-box-shadow);
}

.bootcn-popover .popover-header {
  padding-bottom: 0;
  border-bottom: 0;
  font-weight: 600;
}
js
(function () {
  'use strict';

  const ns = (window.Bootcn = window.Bootcn || {});
  if (ns.popover) return; // already loaded: idempotent

  const SELECTOR = '[data-bs-toggle="popover"]';
  let listening = false;

  function plugin() {
    return window.bootstrap && window.bootstrap.Popover;
  }

  function ensureBootstrap() {
    if (plugin()) return true;
    console.error(
      '[bootcn] popover: Bootstrap JavaScript is required. Load the Bootstrap bundle (window.bootstrap.Popover) before bootcn popover.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;
  }

  // Escape closes open popovers; focus stays on the trigger that opened them.
  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.popover = { init: init, destroy: destroy };

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', function () {
      init(document);
    });
  } else {
    init(document);
  }
})();

Released under the MIT License. Not affiliated with Bootstrap or shadcn/ui.