---
title: "Alert Dialog · Ark UI — StyleX Blocks"
description: "A modal confirmation for destructive actions that only closes when the user answers."
url: "https://stylexblocks.com/components/ark-ui/alert-dialog/"
agent:
  instructions: "Use this search API to find relevant components and blocks across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://stylexblocks.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://developers.cloudflare.com/ai-search/how-to/search-for-agents/"
---

# Alert Dialog

A modal confirmation for destructive actions that only closes when the user answers.

- Behavior engine: Ark UI
- Also available for: [Base UI](https://stylexblocks.com/components/base-ui/alert-dialog.md), [UI SDK](https://stylexblocks.com/components/ui-sdk/alert-dialog.md)
- Category: Feedback
- Sizes: xs, sm, md
- HTML page: https://stylexblocks.com/components/ark-ui/alert-dialog/

## Install

Configure the project once (sets the behavior engine and import aliases):

```sh
npx stylexblocks init
```

Then add the item as source you own:

```sh
npx stylexblocks add alert-dialog
```

Equivalent: `npx shadcn add @stylexblocks/alert-dialog`. Registry item JSON: https://stylexblocks.com/r/ark-ui/alert-dialog.json

## Usage

```tsx
import { AlertDialog } from "#components/ui/ark-ui/alert-dialog.tsx";
import { Button } from "#components/ui/ark-ui/button.tsx";
import { redPalette } from "@stylexblocks/ui/themes/palettes.ts";

export function Example() {
  return (
    // role="alertdialog" under the hood: outside clicks don't dismiss, and
    // the Cancel trigger receives focus when the dialog opens.
    <AlertDialog.Root size="sm">
      <AlertDialog.Trigger asChild>
        <Button variant="surface" palette={redPalette}>Delete project</Button>
      </AlertDialog.Trigger>
      <AlertDialog.Portal>
        <AlertDialog.Backdrop />
        <AlertDialog.Positioner>
          <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 asChild>
                <Button variant="surface">Cancel</Button>
              </AlertDialog.Cancel>
              <Button variant="solid" palette={redPalette} onClick={deleteProject}>
                Delete project
              </Button>
            </AlertDialog.Footer>
          </AlertDialog.Content>
        </AlertDialog.Positioner>
      </AlertDialog.Portal>
    </AlertDialog.Root>
  );
}
```

## Source

`components/ui/ark-ui/alert-dialog.tsx`

```tsx
import * as stylex from "@stylexjs/stylex";
import { Dialog as ArkDialog } from "@ark-ui/react/dialog";
import { Portal } from "@ark-ui/react/portal";
import { createSlotRecipeContext } from "#lib/ark-ui/create-slot-recipe-context.tsx";
import {
  defineSlotRecipe,
  fromMap,
  resolveSlotStyles,
} 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,
  },
});

const contentSizeFix = stylex.create({
  lg: { maxWidth: "42rem" },
  xl: { maxWidth: "56rem" },
});

const { withRootProvider, withContext } =
  createSlotRecipeContext(alertDialogRecipe);

/**
 * Ark's dialog machine in its `alertdialog` role: the machine then refuses
 * to close on outside clicks and moves initial focus to the close trigger
 * (the Cancel button), so the destructive action is never the default.
 */
function AlertDialogRoot(props: ArkDialog.RootProps) {
  return <ArkDialog.Root role="alertdialog" {...props} />;
}

/**
 * AlertDialog — a modal confirmation for destructive or irreversible
 * actions. Same portal, focus trap, and scroll lock as Dialog, wearing the
 * dialog slot recipe; `role="alertdialog"` keeps it open until the user
 * picks an answer (Escape still cancels). `<AlertDialog.Root size>` resolves
 * the recipe once; parts inherit it through context and compose via
 * `asChild`. `Cancel` is the machine's close trigger: put the safe choice
 * there and it receives focus on open.
 */
export const AlertDialog = {
  Root: withRootProvider(AlertDialogRoot, "AlertDialogRoot"),
  Trigger: ArkDialog.Trigger,
  Context: ArkDialog.Context,
  Portal,
  Backdrop: withContext(
    ArkDialog.Backdrop,
    (v) => [
      ...resolveSlotStyles(alertDialogRecipe.slots.backdrop, v),
      chrome.backdrop,
    ],
    "AlertDialogBackdrop",
  ),
  Positioner: withContext(
    ArkDialog.Positioner,
    "positioner",
    "AlertDialogPositioner",
  ),
  Content: withContext(
    ArkDialog.Content,
    (v) => [
      ...resolveSlotStyles(alertDialogRecipe.slots.content, v),
      chrome.content,
      fromMap(contentSizeFix, v.size),
    ],
    "AlertDialogContent",
  ),
  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(ArkDialog.Title, "title", "AlertDialogTitle"),
  Description: withContext(
    ArkDialog.Description,
    "description",
    "AlertDialogDescription",
  ),
  Cancel: ArkDialog.CloseTrigger,
};
```
