AlertDialog
Modal dialog for critical confirmations requiring user attention and explicit action
Usage
import { AlertDialog } from "@lenso/ui";"use client";
// Adapted from HeroUI v3.2.6 alert-dialog-default (Apache-2.0).import * as stylex from "@stylexjs/stylex";import { AlertDialog, Button } from "@lenso/ui";
const styles = stylex.create({ popup: { maxWidth: 400 } });
export function Default() { return ( <AlertDialog> <AlertDialog.Trigger render={<Button variant="danger" />}>Delete Project</AlertDialog.Trigger> <AlertDialog.Portal> <AlertDialog.Backdrop /> <AlertDialog.Viewport> <AlertDialog.Popup xstyle={styles.popup}> <AlertDialog.Close aria-label="Close dialog" /> <AlertDialog.Header> <AlertDialog.Icon variant="danger" /> <AlertDialog.Title>Delete project permanently?</AlertDialog.Title> </AlertDialog.Header> <AlertDialog.Body> <AlertDialog.Description> This will permanently delete <strong>My Awesome Project</strong> and all of its data. This action cannot be undone. </AlertDialog.Description> </AlertDialog.Body> <AlertDialog.Footer> <AlertDialog.Close render={<Button variant="tertiary" />}>Cancel</AlertDialog.Close> <AlertDialog.Close render={<Button variant="danger" />}> Delete Project </AlertDialog.Close> </AlertDialog.Footer> </AlertDialog.Popup> </AlertDialog.Viewport> </AlertDialog.Portal> </AlertDialog> );}Local adaptation source above. Derived from HeroUI v3.2.6 source.
Anatomy
import {AlertDialog, Button} from "@lenso/ui";
export default () => ( <AlertDialog> <Button>Open Alert Dialog</Button> <AlertDialog.Backdrop> <AlertDialog.Container> <AlertDialog.Dialog> <AlertDialog.CloseTrigger /> {/* Optional: Close button */} <AlertDialog.Header> <AlertDialog.Icon /> {/* Optional: Status icon */} <AlertDialog.Heading /> </AlertDialog.Header> <AlertDialog.Body /> <AlertDialog.Footer /> </AlertDialog.Dialog> </AlertDialog.Container> </AlertDialog.Backdrop> </AlertDialog>);Examples
Statuses
"use client";// Adapted from HeroUI v3.2.6 alert-dialog-statuses (Apache-2.0).import * as stylex from "@stylexjs/stylex";import { AlertDialog, Button } from "@lenso/ui";
const styles = stylex.create({ row: { display: "flex", flexWrap: "wrap", gap: 16 }, popup: { maxWidth: 400 }, accent: { backgroundColor: "var(--accent-soft)", color: "var(--accent-soft-foreground)" }, success: { backgroundColor: "var(--success-soft)", color: "var(--success-soft-foreground)" }, warning: { backgroundColor: "var(--warning-soft)", color: "var(--warning-soft-foreground)" }, danger: { backgroundColor: "var(--danger-soft)", color: "var(--danger-soft-foreground)" },});
export function Statuses() { const examples = [ { status: "accent", trigger: "Sign Out", header: "Sign out of your account?", body: "You'll need to sign in again to access your account. Any unsaved changes will be lost.", cancel: "Stay Signed In", confirm: "Sign Out", }, { status: "success", trigger: "Complete Task", header: "Complete this task?", body: "This will mark the task as complete and notify all team members. The task will be moved to your completed list.", cancel: "Not Yet", confirm: "Mark Complete", }, { status: "warning", trigger: "Discard Changes", header: "Discard unsaved changes?", body: "You have unsaved changes that will be permanently lost. Are you sure you want to discard them?", cancel: "Keep Editing", confirm: "Discard", }, { status: "danger", trigger: "Delete Account", header: "Delete your account?", body: "This will permanently delete your account and remove all your data from our servers. This action is irreversible.", cancel: "Cancel", confirm: "Delete Account", }, ] as const; return ( <div {...stylex.props(styles.row)}> {examples.map(({ status, trigger, header, body, cancel, confirm }) => ( <AlertDialog key={status}> <AlertDialog.Trigger render={<Button xstyle={styles[status]} />}> {trigger} </AlertDialog.Trigger> <AlertDialog.Portal> <AlertDialog.Backdrop /> <AlertDialog.Viewport> <AlertDialog.Popup xstyle={styles.popup}> <AlertDialog.Close aria-label="Close dialog" /> <AlertDialog.Header> <AlertDialog.Icon variant={status} /> <AlertDialog.Title>{header}</AlertDialog.Title> </AlertDialog.Header> <AlertDialog.Body> <AlertDialog.Description>{body}</AlertDialog.Description> </AlertDialog.Body> <AlertDialog.Footer> <AlertDialog.Close render={<Button variant="tertiary" />}> {cancel} </AlertDialog.Close> <AlertDialog.Close render={<Button variant={status === "danger" ? "danger" : "primary"} />} > {confirm} </AlertDialog.Close> </AlertDialog.Footer> </AlertDialog.Popup> </AlertDialog.Viewport> </AlertDialog.Portal> </AlertDialog> ))} </div> );}Local adaptation source above. Derived from HeroUI v3.2.6 source.
Placements
"use client";// Adapted from HeroUI v3.2.6 alert-dialog-placements (Apache-2.0).import * as stylex from "@stylexjs/stylex";import { AlertDialog, Button } from "@lenso/ui";
const styles = stylex.create({ row: { display: "flex", flexWrap: "wrap", gap: 16 }, popup: { maxWidth: 400 },});
export function Placements() { const placements = ["auto", "top", "center", "bottom"] as const; return ( <div {...stylex.props(styles.row)}> {placements.map((placement) => ( <AlertDialog key={placement}> <AlertDialog.Trigger render={<Button variant="secondary" />}> {placement.charAt(0).toUpperCase() + placement.slice(1)} </AlertDialog.Trigger> <AlertDialog.Portal> <AlertDialog.Backdrop /> <AlertDialog.Viewport> <AlertDialog.Popup placement={placement} xstyle={styles.popup}> <AlertDialog.Close aria-label="Close dialog" /> <AlertDialog.Header> <AlertDialog.Icon variant="accent" /> <AlertDialog.Title> {placement === "auto" ? "Auto Placement" : `${placement.charAt(0).toUpperCase() + placement.slice(1)} Position`} </AlertDialog.Title> </AlertDialog.Header> <AlertDialog.Body> <AlertDialog.Description> {placement === "auto" ? "Automatically positions at the bottom on mobile and center on desktop for optimal user experience." : `This dialog is positioned at the ${placement} of the viewport. Critical confirmations are typically centered for maximum attention.`} </AlertDialog.Description> </AlertDialog.Body> <AlertDialog.Footer> <AlertDialog.Close render={<Button variant="tertiary" />}> Cancel </AlertDialog.Close> <AlertDialog.Close render={<Button />}>Confirm</AlertDialog.Close> </AlertDialog.Footer> </AlertDialog.Popup> </AlertDialog.Viewport> </AlertDialog.Portal> </AlertDialog> ))} </div> );}Local adaptation source above. Derived from HeroUI v3.2.6 source.
Sizes
"use client";// Adapted from HeroUI v3.2.6 alert-dialog-sizes (Apache-2.0).import { Rocket } from "@gravity-ui/icons";import * as stylex from "@stylexjs/stylex";import { AlertDialog, Button } from "@lenso/ui";
const styles = stylex.create({ row: { display: "flex", flexWrap: "wrap", gap: 16 }, icon: { backgroundColor: "var(--default)", color: "var(--foreground)" }, rocket: { width: 20, height: 20 },});
export function Sizes() { const sizes = ["xs", "sm", "md", "lg", "cover"] as const; return ( <div {...stylex.props(styles.row)}> {sizes.map((size) => ( <AlertDialog key={size}> <AlertDialog.Trigger render={<Button variant="secondary" />}> {size.charAt(0).toUpperCase() + size.slice(1)} </AlertDialog.Trigger> <AlertDialog.Portal> <AlertDialog.Backdrop /> <AlertDialog.Viewport> <AlertDialog.Popup size={size}> <AlertDialog.Close aria-label="Close dialog" /> <AlertDialog.Header> <AlertDialog.Icon variant="default" xstyle={styles.icon}> <Rocket {...stylex.props(styles.rocket)} /> </AlertDialog.Icon> <AlertDialog.Title> Size: {size.charAt(0).toUpperCase() + size.slice(1)} </AlertDialog.Title> </AlertDialog.Header> <AlertDialog.Body> <AlertDialog.Description> {size === "cover" ? ( <> This alert dialog uses the <code>cover</code> size variant. It spans the full screen with margins: 16px on mobile and 40px on desktop. Maintains rounded corners and standard padding. Perfect for critical confirmations that need maximum width while preserving alert dialog aesthetics. </> ) : ( <> This alert dialog uses the <code>{size}</code> size variant. On mobile devices, all sizes adapt to near full-width for optimal viewing. On desktop, each size provides a different maximum width to suit various content needs. </> )} </AlertDialog.Description> </AlertDialog.Body> <AlertDialog.Footer> <AlertDialog.Close render={<Button variant="tertiary" />}> Cancel </AlertDialog.Close> <AlertDialog.Close render={<Button />}>Confirm</AlertDialog.Close> </AlertDialog.Footer> </AlertDialog.Popup> </AlertDialog.Viewport> </AlertDialog.Portal> </AlertDialog> ))} </div> );}Local adaptation source above. Derived from HeroUI v3.2.6 source.
Controlled State
"use client";// Adapted from HeroUI v3.2.6 alert-dialog-controlled (Apache-2.0).// The second controller uses a reducer instead of the React Aria overlay-state hook.import { useReducer, useState } from "react";import * as stylex from "@stylexjs/stylex";import { AlertDialog, Button } from "@lenso/ui";
const styles = stylex.create({ column: { display: "flex", maxWidth: 448, flexDirection: "column", gap: 32 }, section: { display: "flex", flexDirection: "column", gap: 12 }, heading: { fontSize: 18, fontWeight: 600, color: "var(--foreground)" }, caption: { fontSize: 14, lineHeight: 1.625, textWrap: "pretty", color: "var(--muted)" }, panel: { display: "flex", flexDirection: "column", alignItems: "flex-start", gap: 12, borderRadius: 16, backgroundColor: "var(--surface)", padding: 16, boxShadow: "var(--shadow-sm)", }, status: { fontSize: 12, color: "var(--muted)" }, value: { fontFamily: "monospace", fontWeight: 500, color: "var(--foreground)" }, actions: { display: "flex", gap: 8 }, popup: { maxWidth: 400 },});
export function Controlled() { const [isOpen, setIsOpen] = useState(false); const [reducerOpen, dispatch] = useReducer( (open: boolean, action: "open" | "close" | "toggle") => action === "toggle" ? !open : action === "open", false, ); return ( <div {...stylex.props(styles.column)}> {([false, true] as const).map((reducer) => { const open = reducer ? reducerOpen : isOpen; const setOpen = (value: boolean) => reducer ? dispatch(value ? "open" : "close") : setIsOpen(value); return ( <div key={String(reducer)} {...stylex.props(styles.section)}> <h3 {...stylex.props(styles.heading)}> {reducer ? "With useReducer()" : "With React.useState()"} </h3> <p {...stylex.props(styles.caption)}> {reducer ? ( "Use a reducer to manage open, close, and toggle actions." ) : ( <> Control the alert dialog using React's <code>useState</code> hook for simple state management. Perfect for basic use cases. </> )} </p> <div {...stylex.props(styles.panel)}> <p {...stylex.props(styles.status)}> Status: <span {...stylex.props(styles.value)}>{open ? "open" : "closed"}</span> </p> <div {...stylex.props(styles.actions)}> <Button size="sm" variant="secondary" onClick={() => setOpen(true)}> Open Dialog </Button> <Button size="sm" variant="tertiary" onClick={() => (reducer ? dispatch("toggle") : setIsOpen((value) => !value))} > Toggle </Button> </div> </div> <AlertDialog open={open} onOpenChange={setOpen}> <AlertDialog.Portal> <AlertDialog.Backdrop /> <AlertDialog.Viewport> <AlertDialog.Popup xstyle={styles.popup}> <AlertDialog.Close aria-label="Close dialog" /> <AlertDialog.Header> <AlertDialog.Icon variant={reducer ? "success" : "accent"} /> <AlertDialog.Title> Controlled with {reducer ? "useReducer()" : "useState()"} </AlertDialog.Title> </AlertDialog.Header> <AlertDialog.Body> <AlertDialog.Description> {reducer ? ( "The reducer handles open, close, and toggle actions while the native Root owns focus and dismissal." ) : ( <> This alert dialog is controlled by React's <code>useState</code>{" "} hook. Pass <code>open</code> and <code>onOpenChange</code> props to manage the dialog state externally. </> )} </AlertDialog.Description> </AlertDialog.Body> <AlertDialog.Footer> <AlertDialog.Close render={<Button variant="tertiary" />}> Cancel </AlertDialog.Close> <AlertDialog.Close render={<Button />}>Confirm</AlertDialog.Close> </AlertDialog.Footer> </AlertDialog.Popup> </AlertDialog.Viewport> </AlertDialog.Portal> </AlertDialog> </div> ); })} </div> );}Local adaptation source above. Derived from HeroUI v3.2.6 source.
Custom Icon
"use client";// Adapted from HeroUI v3.2.6 alert-dialog-custom-icon (Apache-2.0).import { LockOpen } from "@gravity-ui/icons";import * as stylex from "@stylexjs/stylex";import { AlertDialog, Button } from "@lenso/ui";
const styles = stylex.create({ popup: { maxWidth: 400 }, lock: { width: 20, height: 20 },});
export function CustomIcon() { return ( <AlertDialog> <AlertDialog.Trigger render={<Button variant="secondary" />}> Reset Password </AlertDialog.Trigger> <AlertDialog.Portal> <AlertDialog.Backdrop /> <AlertDialog.Viewport> <AlertDialog.Popup xstyle={styles.popup}> <AlertDialog.Close aria-label="Close dialog" /> <AlertDialog.Header> <AlertDialog.Icon variant="warning"> <LockOpen {...stylex.props(styles.lock)} /> </AlertDialog.Icon> <AlertDialog.Title>Reset your password?</AlertDialog.Title> </AlertDialog.Header> <AlertDialog.Body> <AlertDialog.Description> We'll send a password reset link to your email address. You'll need to create a new password to regain access to your account. </AlertDialog.Description> </AlertDialog.Body> <AlertDialog.Footer> <AlertDialog.Close render={<Button variant="tertiary" />}>Cancel</AlertDialog.Close> <AlertDialog.Close render={<Button />}>Send Reset Link</AlertDialog.Close> </AlertDialog.Footer> </AlertDialog.Popup> </AlertDialog.Viewport> </AlertDialog.Portal> </AlertDialog> );}Local adaptation source above. Derived from HeroUI v3.2.6 source.
Custom Trigger
"use client";// Adapted from HeroUI v3.2.6 alert-dialog-custom-trigger (Apache-2.0).import { TrashBin } from "@gravity-ui/icons";import * as stylex from "@stylexjs/stylex";import { AlertDialog, Button } from "@lenso/ui";
const styles = stylex.create({ trigger: { display: "flex", alignItems: "center", gap: 12, borderRadius: 16, backgroundColor: { default: "var(--surface)", ":hover": "var(--surface-secondary)" }, padding: 16, boxShadow: "var(--shadow-xs)", userSelect: "none", }, iconBox: { display: "flex", width: 48, height: 48, flexShrink: 0, alignItems: "center", justifyContent: "center", borderRadius: 12, backgroundColor: "var(--danger-soft)", color: "var(--danger-soft-foreground)", }, copy: { display: "flex", flex: 1, flexDirection: "column", gap: 2, textAlign: "start" }, heading: { fontSize: 14, fontWeight: 600 }, caption: { fontSize: 12, color: "var(--muted)" }, popup: { maxWidth: 400 }, largeGlyph: { width: 24, height: 24 }, glyph: { width: 20, height: 20 },});
export function CustomTrigger() { return ( <AlertDialog> <AlertDialog.Trigger xstyle={styles.trigger}> <div {...stylex.props(styles.iconBox)}> <TrashBin {...stylex.props(styles.largeGlyph)} /> </div> <div {...stylex.props(styles.copy)}> <p {...stylex.props(styles.heading)}>Delete Item</p> <p {...stylex.props(styles.caption)}>Permanently remove this item</p> </div> </AlertDialog.Trigger> <AlertDialog.Portal> <AlertDialog.Backdrop /> <AlertDialog.Viewport> <AlertDialog.Popup xstyle={styles.popup}> <AlertDialog.Close aria-label="Close dialog" /> <AlertDialog.Header> <AlertDialog.Icon variant="danger"> <TrashBin {...stylex.props(styles.glyph)} /> </AlertDialog.Icon> <AlertDialog.Title>Delete this item?</AlertDialog.Title> </AlertDialog.Header> <AlertDialog.Body> <AlertDialog.Description> Use <code>AlertDialog.Trigger</code> to create custom trigger elements beyond standard buttons. This example shows a card-style trigger with icons and descriptive text. </AlertDialog.Description> </AlertDialog.Body> <AlertDialog.Footer> <AlertDialog.Close render={<Button variant="tertiary" />}>Cancel</AlertDialog.Close> <AlertDialog.Close render={<Button variant="danger" />}> Delete Item </AlertDialog.Close> </AlertDialog.Footer> </AlertDialog.Popup> </AlertDialog.Viewport> </AlertDialog.Portal> </AlertDialog> );}Local adaptation source above. Derived from HeroUI v3.2.6 source.
Backdrop Variants
"use client";// Adapted from HeroUI v3.2.6 alert-dialog-backdrop-variants (Apache-2.0).import * as stylex from "@stylexjs/stylex";import { AlertDialog, Button } from "@lenso/ui";
const styles = stylex.create({ row: { display: "flex", flexWrap: "wrap", gap: 16 }, popup: { maxWidth: 400 },});
export function BackdropVariants() { const variants = ["opaque", "blur", "transparent"] as const; return ( <div {...stylex.props(styles.row)}> {variants.map((variant) => ( <AlertDialog key={variant}> <AlertDialog.Trigger render={<Button variant="secondary" />}> {variant.charAt(0).toUpperCase() + variant.slice(1)} </AlertDialog.Trigger> <AlertDialog.Portal> <AlertDialog.Backdrop variant={variant} /> <AlertDialog.Viewport> <AlertDialog.Popup xstyle={styles.popup}> <AlertDialog.Close aria-label="Close dialog" /> <AlertDialog.Header> <AlertDialog.Icon variant="accent" /> <AlertDialog.Title> Backdrop: {variant.charAt(0).toUpperCase() + variant.slice(1)} </AlertDialog.Title> </AlertDialog.Header> <AlertDialog.Body> <AlertDialog.Description> {variant === "opaque" ? "An opaque dark backdrop that completely obscures the background, providing maximum focus on the dialog." : variant === "blur" ? "A blurred backdrop that softly obscures the background while maintaining visual context." : "A transparent backdrop that keeps the background fully visible, useful for less critical confirmations."} </AlertDialog.Description> </AlertDialog.Body> <AlertDialog.Footer> <AlertDialog.Close render={<Button variant="tertiary" />}> Cancel </AlertDialog.Close> <AlertDialog.Close render={<Button />}>Confirm</AlertDialog.Close> </AlertDialog.Footer> </AlertDialog.Popup> </AlertDialog.Viewport> </AlertDialog.Portal> </AlertDialog> ))} </div> );}Local adaptation source above. Derived from HeroUI v3.2.6 source.
Custom Backdrop
"use client";// Adapted from HeroUI v3.2.6 alert-dialog-custom-backdrop (Apache-2.0).import { TriangleExclamation } from "@gravity-ui/icons";import * as stylex from "@stylexjs/stylex";import { AlertDialog, Button } from "@lenso/ui";
const styles = stylex.create({ backdrop: { backgroundColor: "transparent", backgroundImage: { default: "linear-gradient(to top, rgb(69 10 10 / 90%), rgb(69 10 10 / 50%), transparent)", ':where([data-theme="dark"]) &': "linear-gradient(to top, rgb(69 10 10 / 95%), rgb(69 10 10 / 60%), transparent)", }, }, popup: { maxWidth: 420 }, header: { alignItems: "center", textAlign: "center" }, glyph: { width: 20, height: 20 }, footer: { flexDirection: "column-reverse" }, action: { width: "100%" },});
export function CustomBackdrop() { return ( <AlertDialog> <AlertDialog.Trigger render={<Button variant="danger" />}>Delete Account</AlertDialog.Trigger> <AlertDialog.Portal> <AlertDialog.Backdrop variant="blur" xstyle={styles.backdrop} /> <AlertDialog.Viewport> <AlertDialog.Popup xstyle={styles.popup}> <AlertDialog.Close aria-label="Close dialog" /> <AlertDialog.Header xstyle={styles.header}> <AlertDialog.Icon variant="danger"> <TriangleExclamation {...stylex.props(styles.glyph)} /> </AlertDialog.Icon> <AlertDialog.Title>Permanently delete your account?</AlertDialog.Title> </AlertDialog.Header> <AlertDialog.Body> <AlertDialog.Description> This action cannot be undone. All your data, settings, and content will be permanently removed from our servers. The dramatic red backdrop emphasizes the severity and irreversibility of this decision. </AlertDialog.Description> </AlertDialog.Body> <AlertDialog.Footer xstyle={styles.footer}> <AlertDialog.Close render={<Button xstyle={styles.action} />}> Keep Account </AlertDialog.Close> <AlertDialog.Close render={<Button variant="danger" xstyle={styles.action} />}> Delete Forever </AlertDialog.Close> </AlertDialog.Footer> </AlertDialog.Popup> </AlertDialog.Viewport> </AlertDialog.Portal> </AlertDialog> );}Local adaptation source above. Derived from HeroUI v3.2.6 source.
Dismiss Behavior
"use client";// Adapted from HeroUI v3.2.6 alert-dialog-dismiss-behavior (Apache-2.0).import { CircleInfo } from "@gravity-ui/icons";import * as stylex from "@stylexjs/stylex";import { AlertDialog, Button } from "@lenso/ui";
const styles = stylex.create({ column: { display: "flex", maxWidth: 384, flexDirection: "column", gap: 24 }, section: { display: "flex", flexDirection: "column", gap: 8 }, heading: { fontSize: 18, fontWeight: 600 }, caption: { fontSize: 14, lineHeight: "20px", color: "var(--muted)" }, popup: { maxWidth: 400 }, circle: { width: 20, height: 20 },});
export function DismissBehavior() { return ( <div {...stylex.props(styles.column)}> {([false, true] as const).map((blockEscape) => ( <div key={String(blockEscape)} {...stylex.props(styles.section)}> <h3 {...stylex.props(styles.heading)}> {blockEscape ? "Keyboard dismissal" : "Backdrop dismissal"} </h3> <p {...stylex.props(styles.caption)}> {blockEscape ? "ESC is disabled for this critical confirmation. Use the action buttons to dismiss it." : "Alert dialogs require explicit action. Backdrop clicks do not dismiss the dialog."} </p> <AlertDialog onOpenChange={(open, details) => { if (!open && blockEscape && details.reason === "escape-key") details.cancel(); }} > <AlertDialog.Trigger render={<Button variant="secondary" />}> Open Alert Dialog </AlertDialog.Trigger> <AlertDialog.Portal> <AlertDialog.Backdrop /> <AlertDialog.Viewport> <AlertDialog.Popup xstyle={styles.popup}> <AlertDialog.Close aria-label="Close dialog" /> <AlertDialog.Header> <AlertDialog.Icon variant={blockEscape ? "accent" : "danger"}> <CircleInfo {...stylex.props(styles.circle)} /> </AlertDialog.Icon> <AlertDialog.Title> {blockEscape ? "Keyboard dismissal disabled" : "Backdrop dismissal disabled"} </AlertDialog.Title> <AlertDialog.Description xstyle={styles.caption}> {blockEscape ? "ESC key is disabled" : "Clicking the backdrop won't close this alert dialog"} </AlertDialog.Description> </AlertDialog.Header> <AlertDialog.Body> <p> {blockEscape ? "Press ESC - nothing happens. You must use the action buttons to dismiss this alert dialog." : "Try clicking outside this alert dialog on the overlay - it won't close. You must use the action buttons to dismiss it."} </p> </AlertDialog.Body> <AlertDialog.Footer> <AlertDialog.Close render={<Button variant="tertiary" />}> Cancel </AlertDialog.Close> <AlertDialog.Close render={<Button />}>Confirm</AlertDialog.Close> </AlertDialog.Footer> </AlertDialog.Popup> </AlertDialog.Viewport> </AlertDialog.Portal> </AlertDialog> </div> ))} </div> );}Local adaptation source above. Derived from HeroUI v3.2.6 source.
Close Methods
"use client";// Adapted from HeroUI v3.2.6 alert-dialog-close-methods (Apache-2.0).import { useRef } from "react";import { AlertDialog as BaseAlertDialog } from "@base-ui/react/alert-dialog";import * as stylex from "@stylexjs/stylex";import { AlertDialog, Button } from "@lenso/ui";
const styles = stylex.create({ column: { display: "flex", maxWidth: 672, flexDirection: "column", gap: 32 }, section: { display: "flex", flexDirection: "column", gap: 8 }, heading: { fontSize: 18, fontWeight: 600 }, caption: { fontSize: 14, color: "var(--muted)" }, popup: { maxWidth: 400 },});
export function CloseMethods() { const actionsRef = useRef<BaseAlertDialog.Root.Actions | null>(null); return ( <div {...stylex.props(styles.column)}> <div {...stylex.props(styles.section)}> <h3 {...stylex.props(styles.heading)}>Using AlertDialog.Close</h3> <p {...stylex.props(styles.caption)}> Compose a Button with <code>AlertDialog.Close</code> to close the dialog automatically. </p> <AlertDialog> <AlertDialog.Trigger render={<Button variant="secondary" />}> Open Dialog </AlertDialog.Trigger> <AlertDialog.Portal> <AlertDialog.Backdrop /> <AlertDialog.Viewport> <AlertDialog.Popup xstyle={styles.popup}> <AlertDialog.Header> <AlertDialog.Icon variant="accent" /> <AlertDialog.Title>Using AlertDialog.Close</AlertDialog.Title> </AlertDialog.Header> <AlertDialog.Body> <AlertDialog.Description> Click either button below - both use <code>AlertDialog.Close</code> and will close the dialog automatically. </AlertDialog.Description> </AlertDialog.Body> <AlertDialog.Footer> <AlertDialog.Close render={<Button variant="tertiary" />}> Cancel </AlertDialog.Close> <AlertDialog.Close render={<Button />}>Confirm</AlertDialog.Close> </AlertDialog.Footer> </AlertDialog.Popup> </AlertDialog.Viewport> </AlertDialog.Portal> </AlertDialog> </div> <div {...stylex.props(styles.section)}> <h3 {...stylex.props(styles.heading)}>Using Root actions</h3> <p {...stylex.props(styles.caption)}> Access the native <code>close</code> method through the Root's{" "} <code>actionsRef</code>. This gives you full control over when and how to close the dialog, allowing you to add custom logic before closing. </p> <AlertDialog actionsRef={actionsRef}> <AlertDialog.Trigger render={<Button variant="secondary" />}> Open Dialog </AlertDialog.Trigger> <AlertDialog.Portal> <AlertDialog.Backdrop /> <AlertDialog.Viewport> <AlertDialog.Popup xstyle={styles.popup}> <AlertDialog.Header> <AlertDialog.Icon variant="success" /> <AlertDialog.Title>Using Root actions</AlertDialog.Title> </AlertDialog.Header> <AlertDialog.Body> <AlertDialog.Description> The buttons below use the <code>close</code> method from Root actions. You can add validation or other logic before calling{" "} <code>actionsRef.current.close()</code>. </AlertDialog.Description> </AlertDialog.Body> <AlertDialog.Footer> <Button variant="tertiary" onClick={() => actionsRef.current?.close()}> Cancel </Button> <Button onClick={() => actionsRef.current?.close()}>Confirm</Button> </AlertDialog.Footer> </AlertDialog.Popup> </AlertDialog.Viewport> </AlertDialog.Portal> </AlertDialog> </div> </div> );}Local adaptation source above. Derived from HeroUI v3.2.6 source.
Custom Animations
"use client";// Adapted from HeroUI v3.2.6 alert-dialog-custom-animations (Apache-2.0).import { ArrowUpFromLine, Sparkles } from "@gravity-ui/icons";import * as stylex from "@stylexjs/stylex";import { AlertDialog, Button } from "@lenso/ui";
const scaleIn = stylex.keyframes({ from: { opacity: 0, transform: "scale(.95)" }, to: { opacity: 1, transform: "scale(1)" },});const scaleOut = stylex.keyframes({ from: { opacity: 1, transform: "scale(1)" }, to: { opacity: 0, transform: "scale(.95)" },});const slideIn = stylex.keyframes({ from: { opacity: 0, transform: "translateY(16px)" }, to: { opacity: 1, transform: "translateY(0)" },});const slideOut = stylex.keyframes({ from: { opacity: 1, transform: "translateY(0)" }, to: { opacity: 0, transform: "translateY(8px)" },});const styles = stylex.create({ row: { display: "flex", flexWrap: "wrap", gap: 16 }, popup: { maxWidth: 400 }, glyph: { width: 20, height: 20 }, description: { marginTop: 4 }, scaleBackdrop: { transitionDuration: { default: "400ms", ":is([data-ending-style])": "200ms", "@media (prefers-reduced-motion: reduce)": "0ms", }, transitionTimingFunction: { default: "cubic-bezier(.16,1,.3,1)", ":is([data-ending-style])": "cubic-bezier(.7,0,.84,0)", }, }, slideBackdrop: { transitionDuration: { default: "500ms", ":is([data-ending-style])": "200ms", "@media (prefers-reduced-motion: reduce)": "0ms", }, transitionTimingFunction: { default: "cubic-bezier(.25,1,.5,1)", ":is([data-ending-style])": "cubic-bezier(.5,0,.75,0)", }, }, scale: { animationName: { default: scaleIn, ":is([data-ending-style])": scaleOut, "@media (prefers-reduced-motion: reduce)": "none", }, animationDuration: { default: "400ms", ":is([data-ending-style])": "200ms" }, animationTimingFunction: { default: "cubic-bezier(.16,1,.3,1)", ":is([data-ending-style])": "cubic-bezier(.7,0,.84,0)", }, transitionProperty: "none", }, slide: { animationName: { default: slideIn, ":is([data-ending-style])": slideOut, "@media (prefers-reduced-motion: reduce)": "none", }, animationDuration: { default: "500ms", ":is([data-ending-style])": "200ms" }, animationTimingFunction: { default: "cubic-bezier(.25,1,.5,1)", ":is([data-ending-style])": "cubic-bezier(.5,0,.75,0)", }, transitionProperty: "none", },});
export function CustomAnimations() { const animations = [ { name: "Kinematic Scale", Icon: Sparkles, popup: styles.scale, backdrop: styles.scaleBackdrop, description: "Physics-based elastic scaling. Simulates a high-damping spring system with fast transient response and prolonged settling time. Ideal for Alert Dialogs and Modals.", }, { name: "Fluid Slide", Icon: ArrowUpFromLine, popup: styles.slide, backdrop: styles.slideBackdrop, description: "Simulates movement through a medium with fluid resistance. Eliminates mechanical linearity for a natural, grounded feel. Perfect for Bottom Sheets or Toasts.", }, ]; return ( <div {...stylex.props(styles.row)}> {animations.map(({ name, Icon, popup, backdrop, description }) => ( <AlertDialog key={name}> <AlertDialog.Trigger render={<Button variant="secondary" />}>{name}</AlertDialog.Trigger> <AlertDialog.Portal> <AlertDialog.Backdrop xstyle={backdrop} /> <AlertDialog.Viewport> <AlertDialog.Popup xstyle={[styles.popup, popup]}> <AlertDialog.Close aria-label="Close dialog" /> <AlertDialog.Header> <AlertDialog.Icon variant="accent"> <Icon {...stylex.props(styles.glyph)} /> </AlertDialog.Icon> <AlertDialog.Title>{name} Animation</AlertDialog.Title> </AlertDialog.Header> <AlertDialog.Body> <AlertDialog.Description xstyle={styles.description}> {description} </AlertDialog.Description> </AlertDialog.Body> <AlertDialog.Footer> <AlertDialog.Close render={<Button variant="tertiary" />}> Close </AlertDialog.Close> <AlertDialog.Close render={<Button />}>Try Again</AlertDialog.Close> </AlertDialog.Footer> </AlertDialog.Popup> </AlertDialog.Viewport> </AlertDialog.Portal> </AlertDialog> ))} </div> );}Local adaptation source above. Derived from HeroUI v3.2.6 source.
Custom Portal
"use client";// Adapted from HeroUI v3.2.6 alert-dialog-custom-portal (Apache-2.0).import { useCallback, useState } from "react";import * as stylex from "@stylexjs/stylex";import { AlertDialog, Button } from "@lenso/ui";
const styles = stylex.create({ column: { display: "flex", flexDirection: "column", gap: 16 }, text: { fontSize: 14 }, caption: { fontSize: 14, color: "var(--muted)" }, code: { borderRadius: 4, paddingInline: 4, paddingBlock: 2, fontSize: 12 }, container: { position: "relative", display: "flex", height: 380, alignItems: "center", justifyContent: "center", overflow: "hidden", borderRadius: "var(--radius)", backgroundColor: "color-mix(in oklab, var(--muted) 20%, transparent)", transform: "translateZ(0)", }, backdrop: { height: "100%" }, viewport: { height: "100%", maxHeight: "100%" }, popup: { height: "100%", maxHeight: "100%", maxWidth: 448 },});
export function CustomPortal() { const [portalContainer, setPortalContainer] = useState<HTMLDivElement | null>(null); const setPortalRef = useCallback((node: HTMLDivElement | null) => setPortalContainer(node), []); return ( <div {...stylex.props(styles.column)}> <div> <p {...stylex.props(styles.text)}> Render alert dialogs inside a custom container instead of <code>document.body</code> </p> <p {...stylex.props(styles.caption)}> Apply <code {...stylex.props(styles.code)}>transform: translateZ(0)</code> to the container to create a new stacking context. </p> </div> <div ref={setPortalRef} {...stylex.props(styles.container)}> {!!portalContainer && ( <AlertDialog> <AlertDialog.Trigger render={<Button />}>Open Alert Dialog</AlertDialog.Trigger> <AlertDialog.Portal container={portalContainer}> <AlertDialog.Backdrop xstyle={styles.backdrop} /> <AlertDialog.Viewport xstyle={styles.viewport}> <AlertDialog.Popup xstyle={styles.popup}> <AlertDialog.Close aria-label="Close dialog" /> <AlertDialog.Header> <AlertDialog.Icon variant="accent" /> <AlertDialog.Title>Custom Portal</AlertDialog.Title> </AlertDialog.Header> <AlertDialog.Body> {Array.from({ length: 3 }, (_, index) => ( <p key={index} {...stylex.props(styles.caption)}> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </p> ))} </AlertDialog.Body> <AlertDialog.Footer> <AlertDialog.Close render={<Button variant="tertiary" />}> Cancel </AlertDialog.Close> <AlertDialog.Close render={<Button />}>Confirm</AlertDialog.Close> </AlertDialog.Footer> </AlertDialog.Popup> </AlertDialog.Viewport> </AlertDialog.Portal> </AlertDialog> )} </div> </div> );}Local adaptation source above. Derived from HeroUI v3.2.6 source.
Customization
Tailwind CSS
"use client";// Adapted from HeroUI v3.2.6 alert-dialog-custom-styles (Apache-2.0).import * as stylex from "@stylexjs/stylex";import { AlertDialog, Button } from "@lenso/ui";
const styles = stylex.create({ backdrop: { backgroundColor: { default: "color-mix(in oklab, var(--overlay) 50%, transparent)", ':where([data-theme="dark"]) &': "color-mix(in oklab, var(--overlay) 60%, transparent)", }, }, popup: { position: "relative", overflow: "hidden", maxWidth: 400, borderWidth: 1, borderStyle: "solid", borderColor: { default: "color-mix(in oklab, var(--border) 80%, transparent)", ':where([data-theme="dark"]) &': "color-mix(in oklab, var(--border) 90%, transparent)", }, backgroundColor: "var(--surface)", boxShadow: { default: "0 25px 50px -12px rgb(0 0 0 / 25%), 0 0 0 1px color-mix(in oklab, var(--accent) 10%, transparent)", ':where([data-theme="dark"]) &': "0 25px 50px -12px rgb(0 0 0 / 25%), 0 0 0 1px color-mix(in oklab, var(--accent) 15%, transparent)", }, }, glow: { pointerEvents: "none", position: "absolute", insetInline: 0, top: 0, height: 96, backgroundImage: { default: "linear-gradient(to bottom, color-mix(in oklab, var(--accent) 6%, transparent), transparent)", ':where([data-theme="dark"]) &': "linear-gradient(to bottom, color-mix(in oklab, var(--accent) 10%, transparent), transparent)", }, }, line: { pointerEvents: "none", position: "absolute", insetInline: 32, top: 0, height: 1, backgroundImage: { default: "linear-gradient(to right, transparent, color-mix(in oklab, var(--accent) 35%, transparent), transparent)", ':where([data-theme="dark"]) &': "linear-gradient(to right, transparent, color-mix(in oklab, var(--accent) 45%, transparent), transparent)", }, }, relative: { position: "relative" }, caption: { color: "var(--muted)" }, foreground: { color: "var(--foreground)" },});
export function CustomStyles() { return ( <AlertDialog> <AlertDialog.Trigger render={<Button variant="secondary" />}>Sign out</AlertDialog.Trigger> <AlertDialog.Portal> <AlertDialog.Backdrop variant="blur" xstyle={styles.backdrop} /> <AlertDialog.Viewport> <AlertDialog.Popup xstyle={styles.popup}> <div aria-hidden="true" {...stylex.props(styles.glow)} /> <div aria-hidden="true" {...stylex.props(styles.line)} /> <AlertDialog.Header xstyle={styles.relative}> <AlertDialog.Icon variant="accent" /> <AlertDialog.Title>Sign out of your account?</AlertDialog.Title> </AlertDialog.Header> <AlertDialog.Body xstyle={styles.relative}> <AlertDialog.Description xstyle={styles.caption}> You will be signed out on this device. Unsaved work in{" "} <strong {...stylex.props(styles.foreground)}>Acme Workspace</strong> may be lost unless it was saved to the cloud. </AlertDialog.Description> </AlertDialog.Body> <AlertDialog.Footer> <AlertDialog.Close render={<Button variant="tertiary" />}> Stay signed in </AlertDialog.Close> <AlertDialog.Close render={<Button />}>Sign out</AlertDialog.Close> </AlertDialog.Footer> </AlertDialog.Popup> </AlertDialog.Viewport> </AlertDialog.Portal> </AlertDialog> );}Local adaptation source above. Derived from HeroUI v3.2.6 source.
Global CSS
To customize the AlertDialog component classes, you can use the @layer components directive.
@layer components { .alert-dialog__backdrop { @apply bg-gradient-to-br from-black/60 to-black/80; }
.alert-dialog__dialog { @apply rounded-2xl border border-red-500/20 shadow-2xl; }
.alert-dialog__header { @apply gap-4; }
.alert-dialog__icon { @apply size-16; }
.alert-dialog__close-trigger { @apply rounded-full bg-white/10 hover:bg-white/20; }}Styling Reference
HeroUI follows the BEM methodology to ensure component variants and states are reusable and easy to customize.
CSS Classes
The AlertDialog component uses these CSS classes (View source styles):
Base Classes [!toc]
.alert-dialog__trigger- Trigger element that opens the alert dialog.alert-dialog__backdrop- Overlay backdrop behind the dialog.alert-dialog__container- Positioning wrapper with placement support.alert-dialog__dialog- Dialog content container.alert-dialog__header- Header section for icon and title.alert-dialog__heading- Heading text styles.alert-dialog__body- Main content area.alert-dialog__footer- Footer section for actions.alert-dialog__icon- Icon container with status colors.alert-dialog__close-trigger- Close button element
Backdrop Variants [!toc]
.alert-dialog__backdrop--opaque- Opaque colored backdrop (default).alert-dialog__backdrop--blur- Blurred backdrop with glass effect.alert-dialog__backdrop--transparent- Transparent backdrop (no overlay)
Status Variants (Icon) [!toc]
.alert-dialog__icon--default- Default gray status.alert-dialog__icon--accent- Accent blue status.alert-dialog__icon--success- Success green status.alert-dialog__icon--warning- Warning orange status.alert-dialog__icon--danger- Danger red status
Interactive States
The component supports these interactive states:
- Focus:
:focus-visibleor[data-focus-visible="true"]- Applied to trigger, dialog, and close button - Hover:
:hoveror[data-hovered="true"]- Applied to close button on hover - Active:
:activeor[data-pressed="true"]- Applied to close button when pressed - Entering:
[data-entering]- Applied during dialog opening animation - Exiting:
[data-exiting]- Applied during dialog closing animation - Placement:
[data-placement="*"]- Applied based on dialog position (auto, top, center, bottom)
API Reference
AlertDialog
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | - | Trigger and container elements |
AlertDialog.Trigger
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | - | Custom trigger content |
className | string | - | CSS classes |
AlertDialog.Backdrop
| Prop | Type | Default | Description |
|---|---|---|---|
variant | "opaque" | "blur" | "transparent" | "opaque" | Backdrop overlay style |
isDismissable | boolean | false | Close on backdrop click |
isKeyboardDismissDisabled | boolean | true | Disable ESC key to close |
isOpen | boolean | - | Controlled open state |
onOpenChange | (isOpen: boolean) => void | - | Open state change handler |
className | string | (values) => string | - | Backdrop CSS classes |
UNSTABLE_portalContainer | HTMLElement | - | Custom portal container |
AlertDialog.Container
| Prop | Type | Default | Description |
|---|---|---|---|
placement | "auto" | "center" | "top" | "bottom" | "auto" | Dialog position on screen |
size | "xs" | "sm" | "md" | "lg" | "cover" | "md" | Alert Dialog size variant |
className | string | (values) => string | - | Container CSS classes |
AlertDialog.Dialog
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | ({close}) => ReactNode | - | Content or render function |
className | string | - | CSS classes |
role | string | "alertdialog" | ARIA role |
aria-label | string | - | Accessibility label |
aria-labelledby | string | - | ID of label element |
aria-describedby | string | - | ID of description element |
AlertDialog.Header
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | - | Header content (typically Icon and Heading) |
className | string | - | CSS classes |
AlertDialog.Heading
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | - | Heading text |
className | string | - | CSS classes |
AlertDialog.Body
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | - | Body content |
className | string | - | CSS classes |
AlertDialog.Footer
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | - | Footer content (typically action buttons) |
className | string | - | CSS classes |
AlertDialog.Icon
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | - | Custom icon element |
status | "default" | "accent" | "success" | "warning" | "danger" | "danger" | Status color variant |
className | string | - | CSS classes |
AlertDialog.CloseTrigger
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | - | Custom close button |
className | string | (values) => string | - | CSS classes |
useOverlayState Hook
import {useOverlayState} from "@lenso/ui";
const state = useOverlayState({ defaultOpen: false, onOpenChange: (isOpen) => console.log(isOpen),});
state.isOpen; // Current statestate.open(); // Open dialogstate.close(); // Close dialogstate.toggle(); // Toggle statestate.setOpen(); // Set state directlyAccessibility
Implements WAI-ARIA AlertDialog pattern:
- Focus trap: Focus locked within alert dialog
- Keyboard:
ESCcloses (when enabled),Tabcycles elements - Screen readers: Proper ARIA attributes with
role="alertdialog" - Scroll lock: Body scroll disabled when open
- Required action: Defaults to requiring explicit user action (no backdrop/ESC dismiss)