AlertDialog Primitive
AlertDialog is a Starwind Runtime primitive in the dialog-native-overlay contract family.
Anatomy
Use the Astro primitive adapter to render AlertDialog anatomy with the Runtime wiring included.
---import { AlertDialog } from "@starwind-ui/astro/alert-dialog";---
<AlertDialog.Root> <AlertDialog.Trigger>Delete item</AlertDialog.Trigger> <AlertDialog.Backdrop /> <AlertDialog.Viewport> <AlertDialog.Popup> <AlertDialog.Title>Delete item?</AlertDialog.Title> <AlertDialog.Description>This action cannot be undone.</AlertDialog.Description> <AlertDialog.Close>Cancel</AlertDialog.Close> </AlertDialog.Popup> </AlertDialog.Viewport></AlertDialog.Root>Use the React primitive adapter when AlertDialog state participates in React rendering.
import { AlertDialog } from "@starwind-ui/react/alert-dialog";
export function Example() { return ( <AlertDialog.Root> <AlertDialog.Trigger>Delete item</AlertDialog.Trigger> <AlertDialog.Backdrop /> <AlertDialog.Viewport> <AlertDialog.Popup> <AlertDialog.Title>Delete item?</AlertDialog.Title> <AlertDialog.Description>This action cannot be undone.</AlertDialog.Description> <AlertDialog.Close>Cancel</AlertDialog.Close> </AlertDialog.Popup> </AlertDialog.Viewport> </AlertDialog.Root> );}Render the AlertDialog data-sw-* contract yourself, then initialize createAlertDialog.
<div data-sw-alert-dialog> <button data-sw-alert-dialog-trigger type="button" aria-haspopup="dialog">Delete item</button> <div data-sw-alert-dialog-backdrop hidden></div> <div data-sw-alert-dialog-viewport> <dialog data-sw-alert-dialog-popup role="alertdialog"> <h2 data-sw-alert-dialog-title>Delete item?</h2> <p data-sw-alert-dialog-description>This action cannot be undone.</p> <button data-sw-alert-dialog-close type="button">Cancel</button> </dialog> </div></div>
<script type="module"> import { createAlertDialog } from "@starwind-ui/runtime/alert-dialog";
const root = document.querySelector("[data-sw-alert-dialog]"); if (root) { createAlertDialog(root); }</script>API Reference
Root
The main element that owns the AlertDialog Runtime instance.
- Default element
- div
- Discovery hook
- data-sw-alert-dialog
- Role
- -
Props
open boolean -
- Description
- Controls whether AlertDialog is open.
- Kind
- control
- Targets
- root
- Full type
- boolean
defaultOpen boolean false
- Description
- Sets whether AlertDialog starts open.
- Kind
- control
- Targets
- root
- Full type
- boolean
closeOnEscape boolean true
- Description
- Closes AlertDialog when Escape is pressed.
- Kind
- option
- Targets
- -
- Full type
- boolean
closeOnOutsideInteract boolean false
- Description
- Closes AlertDialog when the user interacts outside it.
- Kind
- option
- Targets
- -
- Full type
- boolean
modal boolean true
- Description
- Makes AlertDialog behave as a modal overlay.
- Kind
- option
- Targets
- -
- Full type
- boolean
onCloseComplete (open: boolean, details: AlertDialogCloseCompleteDetails) => void -
- Description
- Runs after AlertDialog has finished closing.
- Kind
- callback
- Targets
- -
- Full type
- (open: boolean, details: AlertDialogCloseCompleteDetails) => void
onOpenChange (open: boolean, details: AlertDialogOpenChangeDetails) => void -
- Description
- Runs when AlertDialog opens or closes.
- Kind
- callback
- Targets
- -
- Full type
- (open: boolean, details: AlertDialogOpenChangeDetails) => void
Events
closeComplete onCloseComplete open: boolean
- Description
- Fires after AlertDialog has finished closing.
- DOM event
- starwind:close-complete
- Details type
- AlertDialogCloseCompleteDetails
- Timing
- after-state-commit
- Cancelable
- No
openChange onOpenChange open: boolean
- Description
- Fires when AlertDialog opens or closes.
- DOM event
- starwind:open-change
- Details type
- AlertDialogOpenChangeDetails
- Timing
- before-state-commit
- Cancelable
- Yes
State
open boolean open / defaultOpen
- Description
- Tracks whether AlertDialog is open.
- Initial attribute
- data-default-open
- Runtime getter
- getOpen
- Runtime setter
- setOpen
- State control support
- React supports controlled and default state with open and defaultOpen props. Runtime/HTML reads initial state from data-default-open, emits starwind:close-complete and starwind:open-change, and updates with setOpen. Astro adapters render initial/default state, but do not expose reactive controlled-state props for this state.
Data Attributes
Runtime hooks
State
Metadata
Trigger
The control that opens, closes, or targets the AlertDialog content.
- Default element
- button
- Discovery hook
- data-sw-alert-dialog-trigger
- Role
- -
Props
targetId string -
- Description
- Targets a specific root element by id.
- Kind
- attribute
- Targets
- trigger
- Full type
- string
Data Attributes
Runtime hooks
State
Metadata
Portal
Moves AlertDialog overlay content to the document body when needed.
- Default element
- div
- Discovery hook
- data-sw-alert-dialog-portal
- Role
- -
Data Attributes
Runtime hooks
Backdrop
The backdrop shown behind the AlertDialog overlay.
- Default element
- div
- Discovery hook
- data-sw-alert-dialog-backdrop
- Role
- -
Data Attributes
Runtime hooks
State
Viewport
The visible viewport for AlertDialog content.
- Default element
- div
- Discovery hook
- data-sw-alert-dialog-viewport
- Role
- -
Data Attributes
Runtime hooks
Popup
The floating content container for AlertDialog.
- Default element
- dialog
- Discovery hook
- data-sw-alert-dialog-popup
- Role
- alertdialog
Data Attributes
Runtime hooks
State
Title
The accessible title for AlertDialog.
- Default element
- h2
- Discovery hook
- data-sw-alert-dialog-title
- Role
- -
Data Attributes
Runtime hooks
Description
Supporting description text for AlertDialog.
- Default element
- p
- Discovery hook
- data-sw-alert-dialog-description
- Role
- -
Data Attributes
Runtime hooks
Close
A control that closes AlertDialog.
- Default element
- button
- Discovery hook
- data-sw-alert-dialog-close
- Role
- -
Data Attributes
Runtime hooks
Runtime API
- Factory
createAlertDialog- Import
@starwind-ui/runtime/alert-dialog- Root hook
- root
data-sw-alert-dialog - Option props
- closeOnEscape, closeOnOutsideInteract, defaultOpen, modal, onCloseComplete, onOpenChange, open
Option Lifecycles
| Option | Lifecycle |
|---|---|
| closeOnEscape | constructor-only |
| closeOnOutsideInteract | constructor-only |
| defaultOpen | constructor-only |
| modal | constructor-only |
| onCloseComplete | constructor-only |
| onOpenChange | constructor-only |
| open | setter-backed |