@data-slot/alert-dialog
v0.2.163
Published
Headless alert dialog component for vanilla JavaScript. Accessible, unstyled, and modal by default.
Maintainers
Readme
@data-slot/alert-dialog
Headless alert dialog component for vanilla JavaScript. Accessible, unstyled, and modal by default.
Installation
npm install @data-slot/alert-dialogUsage
<div data-slot="alert-dialog">
<button data-slot="alert-dialog-trigger">Delete project</button>
<div data-slot="alert-dialog-portal">
<div data-slot="alert-dialog-overlay" hidden></div>
<div data-slot="alert-dialog-content" hidden>
<div data-slot="alert-dialog-header">
<div data-slot="alert-dialog-media">!</div>
<h2 data-slot="alert-dialog-title">Delete project?</h2>
<p data-slot="alert-dialog-description">
This action cannot be undone.
</p>
</div>
<div data-slot="alert-dialog-footer">
<button data-slot="alert-dialog-cancel">Cancel</button>
<button data-slot="alert-dialog-action">Delete</button>
</div>
</div>
</div>
</div>
<script type="module">
import { createAlertDialog } from "@data-slot/alert-dialog";
const root = document.querySelector('[data-slot="alert-dialog"]');
const alertDialog = createAlertDialog(root);
root
.querySelector('[data-slot="alert-dialog-action"]')
?.addEventListener("click", () => {
// Run your confirm action, then close explicitly.
alertDialog.close();
});
</script>API
create(scope?)
Auto-discovers and binds all alert dialogs in a scope.
import { create } from "@data-slot/alert-dialog";
const controllers = create();createAlertDialog(root, options?)
import { createAlertDialog } from "@data-slot/alert-dialog";
const alertDialog = createAlertDialog(element, {
closeOnClickOutside: false,
onOpenChange: (open) => console.log(open),
});Options
| Option | Type | Default | Description |
| --- | --- | --- | --- |
| defaultOpen | boolean | false | Initial open state |
| onOpenChange | (open: boolean) => void | undefined | Called when open state changes |
| closeOnClickOutside | boolean | false | Close when clicking the overlay |
| closeOnEscape | boolean | true | Close when pressing Escape |
| lockScroll | boolean | true | Lock page scroll while open |
Controller
| Method | Description |
| --- | --- |
| open() | Open the alert dialog |
| close() | Close the alert dialog |
| toggle() | Toggle the alert dialog |
| destroy() | Remove listeners and cleanup |
| isOpen | Current open state |
Slots
Runtime slots
alert-dialog- Root elementalert-dialog-trigger- Element that toggles the alert dialogalert-dialog-portal- Optional element portaled todocument.bodyalert-dialog-overlay- Required overlayalert-dialog-content- Required modal contentalert-dialog-title- Title used foraria-labelledbyalert-dialog-description- Description used foraria-describedbyalert-dialog-cancel- Close button
Style-only slots
alert-dialog-headeralert-dialog-footeralert-dialog-mediaalert-dialog-action
alert-dialog-action is intentionally just a styled action slot. It does not close automatically.
State and events
data-state="open" | "closed"on root, portal, overlay, and contentdata-open/data-closedon root, portal, overlay, and contentdata-stack-indexon overlay and content when multiple modal layers are open
Events:
| Event | Detail | Description |
| --- | --- | --- |
| alert-dialog:change | { open: boolean } | Fired when open state changes |
| alert-dialog:set | { open: boolean } | Programmatically set open state |
Accessibility
role="alertdialog"on contentaria-modal="true"on contentaria-labelledby/aria-describedbywiring from title and description- Focus is trapped while open
- Focus returns to the trigger or previously focused element when closed
