Overview
TheAlertDialog component displays a modal dialog that requires user confirmation before proceeding with an action. It supports both normal and destructive variants, making it ideal for delete confirmations, data loss warnings, and other critical user decisions.
Basic Usage
Destructive Variant
Use thedestructive prop for dangerous actions like deletions:
With Trigger Button
Use thechildren snippet to provide a trigger element:
Custom Description with Snippet
Provide rich content using thedescription snippet:
Props
Controls the dialog’s open state. Use
bind:open for two-way binding.When
true, the action button uses the danger variant (red). Use for destructive actions like deletions.The dialog title displayed in the header.
Simple text description. Use this for plain text or the
description snippet for rich content.Text displayed on the cancel button.
Text displayed on the action (confirm) button.
Bindable reference to the cancel button element.
Bindable reference to the action button element.
Callback fired when the dialog is cancelled (via cancel button, ESC key, or backdrop click).
Callback fired when the action button is clicked.
Svelte snippet for custom description content. Use this instead of
descriptionText for rich HTML content.Svelte snippet for the trigger element. When provided, clicking this element opens the dialog.
Behavior
Auto-closing
The dialog automatically closes when:- The user clicks the cancel button
- The user clicks the action button
- The user presses the ESC key
- The user clicks outside the dialog (backdrop)
The
oncancel callback is fired for ESC and backdrop clicks, but not when the action button is clicked.Focus Management
When the dialog opens, focus is automatically placed on the action button (the second button in the footer). This allows users to quickly confirm by pressing Enter.Accessibility
- Built on
bits-uiAlertDialog primitives with full ARIA support - Keyboard navigation: ESC to close, Tab to navigate between buttons
- Focus trap: keeps focus within the dialog while open
- Backdrop click to dismiss
- Screen reader announcements for dialog title and description
TypeScript
The component is fully typed with theAlertDialogProps interface:
Related Components
- Notification - For non-blocking feedback
- BaseButton - For dialog triggers and actions