Dialog
An accessible confirm dialog built on Bootstrap's modal, with a declarative confirm event.
Installation
bash
npx bootcn add dialogbash
pnpm dlx bootcn add dialogbash
yarn dlx bootcn add dialogbash
bunx --bun bootcn add dialogThe 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--destructive"
data-bootcn-dialog-confirm
data-bs-toggle="modal"
data-bs-target="#example-dialog"
>
Delete project
</button>
<div
class="modal fade bootcn-dialog"
id="example-dialog"
tabindex="-1"
aria-labelledby="example-dialog-title"
aria-describedby="example-dialog-description"
>
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h2 class="modal-title bootcn-dialog__title" id="example-dialog-title">Delete?</h2>
<button
type="button"
class="btn-close"
data-bs-dismiss="modal"
aria-label="Close dialog"
></button>
</div>
<div class="modal-body">
<p class="bootcn-dialog__description" id="example-dialog-description">
This action cannot be undone.
</p>
</div>
<div class="modal-footer bootcn-dialog__footer">
<button
type="button"
class="btn bootcn-button bootcn-button--outline bootcn-button--sm"
data-bs-dismiss="modal"
>
Cancel
</button>
<button
type="button"
class="btn bootcn-button bootcn-button--destructive bootcn-button--sm"
data-bootcn-dialog-confirm-button
data-bs-dismiss="modal"
>
Delete
</button>
</div>
</div>
</div>
</div>Attributes
data-bootcn-dialog-confirm— on the trigger. Marks it as a confirm trigger; the confirm event is dispatched on this element.data-bootcn-dialog-confirm-button— on the confirm button inside the modal. Clicking it dispatchesbootcn:dialog:confirmon the trigger that opened the modal.
data-bs-toggle, data-bs-target and data-bs-dismiss are Bootstrap's own and must be kept.
Events
| Event | Dispatched on | Detail |
|---|---|---|
bootcn:dialog:confirm | the confirm trigger | { trigger, button } |
The event bubbles. Example:
html
<script>
document.addEventListener('bootcn:dialog:confirm', function (event) {
console.log('Confirmed by', event.detail.button);
});
</script>JavaScript API
js
window.Bootcn.dialog.init(); // idempotent; runs automatically on load
window.Bootcn.dialog.destroy(); // removes listenersdialog.js uses event delegation on document, so dialogs added to the page after load (for example by fetch + innerHTML) work without calling init again. Both methods accept an optional root argument for consistency with other components, but always act on the whole document.
Dependencies
- Bootstrap 5.3+ CSS and JavaScript bundle (
window.bootstrap.Modal). If the bundle is missing,dialog.jslogs one clear console error and does nothing. themes/tokens/tokens.css.- The Button component (
button) for the actions in the example.
Accessibility
- Use
aria-labelledbyfor the title andaria-describedbyfor the description so screen readers announce both. - Bootstrap traps focus inside the modal, closes on Escape, and returns focus to the trigger;
dialog.jsadds a fallback focus-return if focus is lost. - Keep a visible close control with an accessible name.
Source
The exact files the CLI installs (dialog@1.1.0). View on GitHub · Report an issue
html
<button
type="button"
class="btn bootcn-button bootcn-button--destructive"
data-bootcn-dialog-confirm
data-bs-toggle="modal"
data-bs-target="#bootcn-example-dialog"
>
Delete project
</button>
<div
class="modal fade bootcn-dialog"
id="bootcn-example-dialog"
tabindex="-1"
role="dialog"
aria-modal="true"
aria-labelledby="bootcn-example-dialog-title"
aria-describedby="bootcn-example-dialog-description"
>
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h2 class="modal-title bootcn-dialog__title" id="bootcn-example-dialog-title">
Delete this project?
</h2>
<button
type="button"
class="btn-close"
data-bs-dismiss="modal"
aria-label="Close dialog"
></button>
</div>
<div class="modal-body">
<p class="bootcn-dialog__description" id="bootcn-example-dialog-description">
This permanently removes the project and all of its data. This action cannot be undone.
</p>
</div>
<div class="modal-footer bootcn-dialog__footer">
<button
type="button"
class="btn bootcn-button bootcn-button--outline bootcn-button--sm"
data-bs-dismiss="modal"
>
Cancel
</button>
<button
type="button"
class="btn bootcn-button bootcn-button--destructive bootcn-button--sm"
data-bootcn-dialog-confirm-button
data-bs-dismiss="modal"
>
Delete project
</button>
</div>
</div>
</div>
</div>css
.bootcn-dialog .modal-content {
border: var(--bootcn-border-width) solid var(--bootcn-border-color);
border-radius: var(--bootcn-dialog-radius);
background-color: var(--bootcn-background);
box-shadow: var(--bootcn-dialog-shadow);
}
/* No dividers: header, body and footer share one padded surface. */
.bootcn-dialog .modal-header {
align-items: flex-start;
padding: var(--bootcn-dialog-padding) var(--bootcn-dialog-padding) 0;
border-bottom: 0;
}
.bootcn-dialog .modal-body {
padding: 0.5rem var(--bootcn-dialog-padding) 0;
font-size: 0.875rem;
}
.bootcn-dialog .modal-footer {
flex-wrap: wrap;
gap: 0.5rem;
padding: var(--bootcn-dialog-padding);
border-top: 0;
}
.bootcn-dialog .modal-footer > * {
margin: 0;
}
.bootcn-dialog__title {
margin: 0;
font-size: 1.125rem;
font-weight: 600;
line-height: 1.3;
}
.bootcn-dialog__description {
margin: 0;
color: var(--bootcn-muted-foreground);
}js
(function () {
'use strict';
const ns = (window.Bootcn = window.Bootcn || {});
if (ns.dialog) return;
const TRIGGER_SELECTOR = '[data-bootcn-dialog-confirm]';
const CONFIRM_BUTTON_SELECTOR = '[data-bootcn-dialog-confirm-button]';
let attached = false;
const triggersByModal = new WeakMap();
function ensureBootstrap() {
if (window.bootstrap && window.bootstrap.Modal) return true;
console.error(
'[bootcn] dialog: Bootstrap JavaScript is required. Load the Bootstrap bundle (window.bootstrap.Modal) before bootcn dialog.js.',
);
return false;
}
function handleShown(event) {
const modal = event.target;
const trigger = event.relatedTarget;
if (
modal instanceof Element &&
trigger instanceof Element &&
trigger.matches(TRIGGER_SELECTOR)
) {
triggersByModal.set(modal, trigger);
}
}
function handleClick(event) {
const target = event.target;
if (!(target instanceof Element)) return;
const button = target.closest(CONFIRM_BUTTON_SELECTOR);
if (!(button instanceof Element)) return;
const modal = button.closest('.modal');
if (!(modal instanceof Element)) return;
const trigger = triggersByModal.get(modal);
if (!(trigger instanceof Element)) return;
trigger.dispatchEvent(
new CustomEvent('bootcn:dialog:confirm', {
bubbles: true,
detail: { trigger: trigger, button: button },
}),
);
}
function handleHidden(event) {
const modal = event.target;
if (!(modal instanceof Element)) return;
const trigger = triggersByModal.get(modal);
if (!(trigger instanceof Element)) return;
triggersByModal.delete(modal);
const active = document.activeElement;
if (trigger.isConnected && active !== trigger && !modal.contains(active)) {
trigger.focus();
}
}
// Behavior is attached once with event delegation on `document`, so it
// covers dialogs added later too. `root` is accepted for API consistency
// with other components; init/destroy always act on the whole document.
function init(root = document) {
if (!root) return;
if (!ensureBootstrap()) return;
if (attached) return;
document.addEventListener('shown.bs.modal', handleShown);
document.addEventListener('click', handleClick);
document.addEventListener('hidden.bs.modal', handleHidden);
attached = true;
}
function destroy(root = document) {
if (!root) return;
if (!attached) return;
document.removeEventListener('shown.bs.modal', handleShown);
document.removeEventListener('click', handleClick);
document.removeEventListener('hidden.bs.modal', handleHidden);
attached = false;
}
ns.dialog = { init: init, destroy: destroy };
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', function () {
init(document);
});
} else {
init(document);
}
})();
