Skip to content

Alert dialog ​

A modal confirmation that interrupts the user for an important decision, with Cancel focused first.

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

Installation ​

bash
npx bootcn add alert-dialog
bash
pnpm dlx bootcn add alert-dialog
bash
yarn dlx bootcn add alert-dialog
bash
bunx --bun bootcn add alert-dialog

The CLI also installs the design tokens and these components: dialog, button. It prints the <link> and <script> tags to add to your layout.

Usage ​

Use it only for decisions that need an answer now, such as deleting data. It is a Bootstrap modal with data-bs-backdrop="static": clicking outside does not close it, Escape and the buttons do. Put the confirm handler on the destructive button.

JavaScript API ​

alert-dialog.js listens for Bootstrap's shown.bs.modal on .bootcn-alert-dialog modals: it restores role="alertdialog" (Bootstrap sets dialog) and moves focus to the element with data-bootcn-alert-dialog-cancel. Bootcn.alertDialog.init() / destroy() add and remove that listener.

Accessibility ​

  • role="alertdialog" with aria-labelledby and aria-describedby, so the question and its consequence are announced together.
  • Focus starts on Cancel, the least destructive action, and returns to the trigger when the dialog closes (Bootstrap).
  • Bootstrap traps focus inside while it is open.

Dependencies ​

  • Bootstrap 5.3+ CSS and the JS bundle.
  • themes/tokens/tokens.css.
  • The dialog component.
  • The button component.

Source ​

The exact files the CLI installs (alert-dialog@1.0.0). View on GitHub · Report an issue

html
<button
  type="button"
  class="btn bootcn-button bootcn-button--outline"
  data-bs-toggle="modal"
  data-bs-target="#bootcn-alert-dialog"
>
  Delete account
</button>

<div
  class="modal fade bootcn-dialog bootcn-alert-dialog"
  id="bootcn-alert-dialog"
  tabindex="-1"
  role="alertdialog"
  aria-modal="true"
  aria-labelledby="bootcn-alert-dialog-title"
  aria-describedby="bootcn-alert-dialog-description"
  data-bs-backdrop="static"
>
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content">
      <div class="modal-header">
        <h2 class="modal-title bootcn-dialog__title" id="bootcn-alert-dialog-title">
          Delete your account?
        </h2>
      </div>
      <div class="modal-body">
        <p class="bootcn-dialog__description" id="bootcn-alert-dialog-description">
          This permanently deletes your account and your data. You cannot undo it.
        </p>
      </div>
      <div class="modal-footer bootcn-alert-dialog__footer">
        <button
          type="button"
          class="btn bootcn-button bootcn-button--outline"
          data-bs-dismiss="modal"
          data-bootcn-alert-dialog-cancel
        >
          Cancel
        </button>
        <button
          type="button"
          class="btn bootcn-button bootcn-button--destructive"
          data-bs-dismiss="modal"
        >
          Delete account
        </button>
      </div>
    </div>
  </div>
</div>
css
/* Buttons stack on phones, with the safe action last (nearest the thumb). */
@media (max-width: 575.98px) {
  .bootcn-alert-dialog__footer {
    flex-direction: column-reverse;
    align-items: stretch;
  }

  .bootcn-alert-dialog__footer > .bootcn-button {
    width: 100%;
  }
}
js
(function () {
  'use strict';

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

  const SELECTOR = '.bootcn-alert-dialog';
  let listening = false;

  // Bootstrap sets role="dialog" when a modal opens; an alert dialog keeps
  // role="alertdialog", and focus starts on the safe action (Cancel).
  function onShown(event) {
    const dialog = event.target;
    if (!(dialog instanceof Element) || !dialog.matches(SELECTOR)) return;
    dialog.setAttribute('role', 'alertdialog');
    const cancel = dialog.querySelector('[data-bootcn-alert-dialog-cancel]');
    if (cancel) cancel.focus();
  }

  function init() {
    if (!window.bootstrap || !window.bootstrap.Modal) {
      console.error(
        '[bootcn] alert-dialog: Bootstrap JavaScript is required. Load the Bootstrap bundle before bootcn alert-dialog.js.',
      );
      return;
    }
    if (listening) return;
    document.addEventListener('shown.bs.modal', onShown);
    listening = true;
  }

  function destroy() {
    if (!listening) return;
    document.removeEventListener('shown.bs.modal', onShown);
    listening = false;
  }

  ns.alertDialog = { init: init, destroy: destroy };

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

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