Alert Dialog
A modal confirmation for destructive actions that only closes when the user answers.
12345678910111213141516171819202122232425262728293031323334353637import { AlertDialog } from "#components/ui/ui-sdk/alert-dialog.tsx";import { Button } from "#components/ui/ui-sdk/button.tsx";import { redPalette } from "@stylexblocks/ui/themes/palettes.ts";
export function Example() { return ( // UI SDK's AlertDialog: always modal, ignores outside presses, and // focuses its first Close part (Cancel) when it opens. Content opens in // the browser's top layer, so there is no Portal or Positioner. <AlertDialog.Root size="sm"> <AlertDialog.Trigger render={<Button variant="surface" palette={redPalette} />} > Delete project </AlertDialog.Trigger> <AlertDialog.Backdrop /> <AlertDialog.Content> <AlertDialog.Header> <AlertDialog.Title>Delete project?</AlertDialog.Title> </AlertDialog.Header> <AlertDialog.Body> <AlertDialog.Description> This action cannot be undone. </AlertDialog.Description> </AlertDialog.Body> <AlertDialog.Footer> <AlertDialog.Cancel render={<Button variant="surface" />}> Cancel </AlertDialog.Cancel> <Button variant="solid" palette={redPalette} onClick={deleteProject}> Delete project </Button> </AlertDialog.Footer> </AlertDialog.Content> </AlertDialog.Root> );}Sizes
Install
1npx stylexblocks add alert-dialogSource
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495import * as stylex from "@stylexjs/stylex";import { useContext } from "react";import { AlertDialog as UiSdkAlertDialog, AlertDialogBackdrop, AlertDialogClose, AlertDialogContent, AlertDialogDescription, AlertDialogTitle, AlertDialogTrigger,} from "@ui-sdk/react/alert-dialog";import { createSlotRecipeContext } from "#lib/ui-sdk/create-slot-recipe-context.tsx";import { defineSlotRecipe, fromMap, resolveSlotStyles, type StyleOverrideProps,} from "#lib/recipe-props.ts";import { colorPalette } from "@stylexblocks/ui/color-palette.stylex.ts";import { semanticShadows } from "@stylexblocks/ui/semantic-tokens/shadows.stylex.ts";import { durations } from '@stylexblocks/ui/tokens/durations.stylex.ts'import { fontSizes } from '@stylexblocks/ui/tokens/font-sizes.stylex.ts'import { fontWeights } from '@stylexblocks/ui/tokens/font-weights.stylex.ts'import { lineHeights } from '@stylexblocks/ui/tokens/line-heights.stylex.ts'import { semanticColors } from '@stylexblocks/ui/semantic-tokens/colors.stylex.ts'import { semanticRadii } from '@stylexblocks/ui/semantic-tokens/radii.stylex.ts'import { sizes } from '@stylexblocks/ui/tokens/sizes.stylex.ts'import { spacing } from '@stylexblocks/ui/tokens/spacing.stylex.ts'import { zIndices } from '@stylexblocks/ui/tokens/z-indices.stylex.ts'
const dialogBackdrop = stylex.create({ base: { position: 'fixed', left: 0, top: 0, width: '100vw', height: '100dvh', zIndex: zIndices.layer4, animationName: { default: null, ':is([data-open], [data-state="open"])': 'fade-in', ':is([data-closed], [data-state="closed"])': 'fade-out', }, animationDuration: { default: null, ':is([data-open], [data-state="open"])': durations.moderate, ':is([data-closed], [data-state="closed"])': durations.faster, }, },})
const dialogPositioner = stylex.create({ base: { display: 'flex', width: '100vw', height: '100dvh', position: 'fixed', left: 0, top: 0, '--dialog-z-index': zIndices.layer4, zIndex: 'calc(var(--dialog-z-index) + var(--layer-index, 0))', justifyContent: 'center', },})
const dialogPositionerSizes = stylex.create({ cover: { padding: spacing._10, },})
const dialogPositionerPlacements = stylex.create({ center: { alignItems: 'center', }, top: { alignItems: 'flex-start', }, bottom: { alignItems: 'flex-end', },})
const dialogPositionerScrollBehaviors = stylex.create({ inside: { overflow: 'hidden', }, outside: { overflow: 'auto', pointerEvents: 'auto', },})
const dialogContent = stylex.create({ base: { display: 'flex', flexDirection: 'column', position: 'relative', width: '100%', outline: 'none', fontSize: fontSizes.sm, lineHeight: lineHeights.sm, marginBlock: 'var(--dialog-margin, var(--dialog-base-margin))', zIndex: 'calc(var(--dialog-z-index) + var(--layer-index, 0))', '--dialog-padding': spacing._6, '--dialog-radius': semanticRadii.panel, '--dialog-concentric-radius': 'max(0px, calc(var(--dialog-radius) - var(--dialog-padding)))', animationDuration: { default: null, ':is([data-open], [data-state="open"])': durations.moderate, ':is([data-closed], [data-state="closed"])': durations.faster, }, },})
const dialogContentSizes = stylex.create({ xs: { maxWidth: sizes.sm, }, sm: { maxWidth: sizes.md, }, md: { maxWidth: sizes.lg, }, lg: { maxWidth: '2xl', }, xl: { maxWidth: '4xl', }, cover: { width: '100%', height: '100%', '--dialog-margin': '0', }, full: { maxWidth: '100vw', minHeight: '100vh', '--dialog-margin': '0', '--dialog-radius': '0px', borderRadius: 0, },})
const dialogContentVariants = stylex.create({ confirm: { textAlign: 'center', },})
const dialogContentPlacements = stylex.create({ center: { '--dialog-base-margin': 'auto', marginInline: 'auto', }, top: { '--dialog-base-margin': spacing._16, marginInline: 'auto', }, bottom: { '--dialog-base-margin': spacing._16, marginInline: 'auto', },})
const dialogContentScrollBehaviors = stylex.create({ inside: { maxHeight: 'calc(100% - 7.5rem)', },})
const dialogContentMotionPresets = stylex.create({ scale: { animationName: { default: null, ':is([data-open], [data-state="open"])': 'scale-in, fade-in', ':is([data-closed], [data-state="closed"])': 'scale-out, fade-out', }, }, 'slide-in-bottom': { animationName: { default: null, ':is([data-open], [data-state="open"])': 'slide-from-bottom, fade-in', ':is([data-closed], [data-state="closed"])': 'slide-to-bottom, fade-out', }, }, 'slide-in-top': { animationName: { default: null, ':is([data-open], [data-state="open"])': 'slide-from-top, fade-in', ':is([data-closed], [data-state="closed"])': 'slide-to-top, fade-out', }, }, 'slide-in-left': { animationName: { default: null, ':is([data-open], [data-state="open"])': 'slide-from-left, fade-in', ':is([data-closed], [data-state="closed"])': 'slide-to-left, fade-out', }, }, 'slide-in-right': { animationName: { default: null, ':is([data-open], [data-state="open"])': 'slide-from-right, fade-in', ':is([data-closed], [data-state="closed"])': 'slide-to-right, fade-out', }, },})
const dialogHeader = stylex.create({ base: { display: 'flex', flex: 0, paddingInline: spacing._6, paddingBlock: spacing._4, paddingBottom: spacing._2, },})
const dialogHeaderVariants = stylex.create({ confirm: { flexDirection: 'column', alignItems: 'center', },})
const dialogBody = stylex.create({ base: { flex: 1, paddingInline: spacing._6, paddingBlock: spacing._4, },})
const dialogBodyScrollBehaviors = stylex.create({ inside: { overflow: 'auto', },})
const dialogFooter = stylex.create({ base: { display: 'flex', alignItems: 'center', justifyContent: 'flex-end', gap: spacing._3, paddingInline: spacing._6, paddingTop: spacing._2, paddingBottom: spacing._4, },})
const dialogFooterVariants = stylex.create({ confirm: { display: 'flex', flexDirection: 'column-reverse', justifyContent: 'stretch', alignItems: 'stretch', },})
const dialogTitle = stylex.create({ base: { fontSize: fontSizes.lg, lineHeight: lineHeights.lg, fontWeight: fontWeights.medium, },})
const dialogDescription = stylex.create({ base: { color: semanticColors.fgMuted, },})
const dialogCloseTrigger = stylex.create({ base: { position: 'absolute', top: spacing._4, right: spacing._4, },})
export type DialogVariant = keyof typeof dialogContentVariants
export type DialogPlacement = keyof typeof dialogPositionerPlacements
export type DialogScrollBehavior = keyof typeof dialogPositionerScrollBehaviors
export type DialogSize = keyof typeof dialogPositionerSizes
export type DialogMotionPreset = keyof typeof dialogContentMotionPresets
// The dialog recipe, tuned for confirmations: alert dialogs default to the// narrow `sm` width and sit in the middle of the viewport.const alertDialogRecipe = defineSlotRecipe({ slots: { backdrop: { styles: dialogBackdrop }, positioner: { styles: dialogPositioner, sizes: dialogPositionerSizes, placements: dialogPositionerPlacements, }, content: { styles: dialogContent, sizes: dialogContentSizes, placements: dialogContentPlacements, motionPresets: dialogContentMotionPresets, }, header: { styles: dialogHeader }, body: { styles: dialogBody }, footer: { styles: dialogFooter }, title: { styles: dialogTitle }, description: { styles: dialogDescription }, }, defaultVariants: { size: "sm", placement: "center", motionPreset: "scale", },});
// Local chrome the recipe leaves to the composition layer: the backdrop's// fixed fill + wash, the panel's surface, and the palette-tinted icon tile// that marks the dialog as a warning.const chrome = stylex.create({ backdrop: { position: "fixed", inset: 0, backgroundColor: semanticColors.bgBackdrop, }, content: { backgroundColor: semanticColors.bgPanel, borderRadius: "var(--dialog-radius)", boxShadow: semanticShadows.overlay, }, header: { flexDirection: "column", alignItems: "flex-start", gap: spacing._3, paddingTop: spacing._6, }, body: { paddingTop: 0 }, footer: { paddingBottom: spacing._6 }, icon: { display: "inline-flex", alignItems: "center", justifyContent: "center", flexShrink: 0, width: sizes._10, height: sizes._10, borderRadius: semanticRadii.control, backgroundColor: colorPalette.subtle, color: colorPalette.fg, },});
// UI SDK's Title renders an `h2` and Description a `p`: drop UA margins.const text = stylex.create({ reset: { margin: 0 },});
// UI SDK's content is itself the top-layer element, so a wrapper positioner// can't lay it out. These styles do the positioner's job on the content:// fixed to the viewport, centered inline by the recipe's `marginInline:// auto`, and pinned per placement (the recipe's block margins then give the// top/bottom gap, or `auto` centers it).const topLayer = stylex.create({ base: { position: "fixed", insetInline: 0, height: "fit-content", maxHeight: "calc(100dvh - 2 * var(--dialog-margin, 4rem))", overflowY: "auto", },});
const topLayerPlacements = stylex.create({ center: { top: 0, bottom: 0 }, top: { top: 0, bottom: "auto" }, bottom: { top: "auto", bottom: 0 },});
const contentSizeFix = stylex.create({ lg: { maxWidth: "42rem" }, xl: { maxWidth: "56rem" },});
const { withRootProvider, withContext, VariantContext } = createSlotRecipeContext(alertDialogRecipe);
const ContentElement = AlertDialogContent as React.ElementType;
/** * UI SDK's content opens in the browser's top layer, so there is no * positioner part: Content carries the recipe's content slot plus the * positioner's placement. Alert dialogs ignore outside presses. */function Content( allProps: Omit<React.ComponentProps<typeof AlertDialogContent>, "style"> & StyleOverrideProps,) { const { palette, style, className, ...rest } = allProps as Record<string, unknown>; const v = useContext(VariantContext); // resolved outside `stylex.props`: StyleX drops a `stylex.create` whose // only reference sits inside a props call it can't compile statically const sizeFix = fromMap(contentSizeFix, v.size); const base = topLayer.base; const placement = fromMap(topLayerPlacements, v.placement); const sx = stylex.props( ...([ palette, ...resolveSlotStyles(alertDialogRecipe.slots.content, v), chrome.content, sizeFix, base, placement, style, ] as unknown as ReadonlyArray<never>), ); return ( <ContentElement {...rest} className={ typeof className === "string" && className.length > 0 ? [sx.className, className].filter(Boolean).join(" ") : sx.className } style={sx.style} /> );}Content.displayName = "AlertDialogContent";
/** * AlertDialog — UI SDK's alert dialog (role `alertdialog`, always modal, * no dismissal on outside press; Escape still cancels) wearing the dialog * slot recipe. The content opens in the browser's top layer, so there is no * Portal or Positioner; focus trap and inert page come from the engine. * `<AlertDialog.Root size>` resolves the recipe once; parts inherit it * through context and compose via `render`. `Cancel` is the engine's close * part: the alert dialog focuses its first Close part on open, so the * destructive action is never the default. */export const AlertDialog = { Root: withRootProvider(UiSdkAlertDialog, "AlertDialogRoot"), Trigger: AlertDialogTrigger, Backdrop: withContext( AlertDialogBackdrop, (v) => [ ...resolveSlotStyles(alertDialogRecipe.slots.backdrop, v), chrome.backdrop, ], "AlertDialogBackdrop", ), Content, Header: withContext( "div", (v) => [ ...resolveSlotStyles(alertDialogRecipe.slots.header, v), chrome.header, ], "AlertDialogHeader", ), Icon: withContext("div", () => [chrome.icon], "AlertDialogIcon"), Body: withContext( "div", (v) => [...resolveSlotStyles(alertDialogRecipe.slots.body, v), chrome.body], "AlertDialogBody", ), Footer: withContext( "div", (v) => [ ...resolveSlotStyles(alertDialogRecipe.slots.footer, v), chrome.footer, ], "AlertDialogFooter", ), Title: withContext( AlertDialogTitle, (v) => [...resolveSlotStyles(alertDialogRecipe.slots.title, v), text.reset], "AlertDialogTitle", ), Description: withContext( AlertDialogDescription, (v) => [ ...resolveSlotStyles(alertDialogRecipe.slots.description, v), text.reset, ], "AlertDialogDescription", ), Cancel: AlertDialogClose,};
Examples
Installable blocks built with Alert Dialog — real use cases, delivered as source you own.
Two-column storefront checkout with express wallets, shipping and billing address fields, selectable delivery cards, auto-formatted card entry with network detection, promo code discounts, gift notes, inline validation, and a sticky collapsible order summary.
Release backlog and change-request table bundling rows into collapsible status, priority, or team groups with per-group counts, expand/collapse-all, query search and release/priority filters, inline stage and priority pickers, a resizable Splitter inspector with readiness checklist and activity timeline, and narrow-screen drawer handoff.
Commerce order detail page with breadcrumb navigation, fulfillment and payment status badges, itemized line-item table with product photo thumbnails, invoice breakdown, live shipment tracking timeline, customer and shipping sidebar cards with collapsible risk analysis, cancel-order confirmation dialog, and toast feedback.
Left-rail application shell pairing a full-height collapsible sidebar (workspace switcher, quick actions, grouped route navigation, collapsible pinned collections with row menus, and user footer menu) with a mobile drawer fallback and interactive creative workspace views.
Focused Cinder Cloud API keys and service tokens table with environment segment filter, status select, live search, sortable column headers, multi-row selection with a contextual floating ActionBar, per-row overflow menus, create-token dialog, destructive confirmation AlertDialog, pagination, and empty state.