Alert Dialog
A modal dialog that interrupts the user with important content and expects a response.
Import
import { alert } from "$lib/stores/alert"; Simple Alert
// Simple Alert Example
alert.open({
title: "Success",
message: "Operation completed successfully",
onConfirm: () => console.log("Confirmed")
});Confirmation Dialog
// Confirmation Dialog Example
alert.open({
title: "Confirm Delete",
message: "Are you sure you want to delete this item? This cannot be undone.",
onConfirm: () => {
console.log("User confirmed delete");
// Perform delete operation
},
onCancel: () => {
console.log("User cancelled delete");
}
});Properties
Name
Type
Description
title
Type
string
Default: ""
The title displayed at the top of the dialog.
message
Type
string
Default: ""
The main content message of the dialog.
onConfirm
Type
() => void
Default: undefined
Optional callback function when the confirm button is clicked.
onCancel
Type
() => void
Default: undefined
Optional callback function when the cancel button is clicked.
Implementation Details
Component Location
The AlertDialog component is implemented in src/lib/components/AlertDialog.svelte but is mounted globally in src/routes/+layout.svelte.
Global Store
The state management is handled by a dedicated store in src/lib/stores/alert.ts. This allows the dialog to be triggered from anywhere in the application without prop
drilling.
Why Global?
The AlertDialog is mounted in the root layout because:
- It ensures only one instance exists application-wide
- Prevents z-index conflicts with other modals
- Makes it accessible from any component via the store
- Maintains consistent styling and behavior across the app