Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

feat(AlertDialog): add XDSAlertDialog confirmation dialog (#1370)

Cindy Zhangca1d010217f2
diff --git a/apps/storybook/stories/AlertDialog.stories.tsx b/apps/storybook/stories/AlertDialog.stories.tsx
new file mode 100644
index 0000000000..80eed4f361
--- /dev/null
+++ b/apps/storybook/stories/AlertDialog.stories.tsx
@@ -0,0 +1,137 @@
+import {useState} from 'react';
+import type {Meta, StoryObj} from '@storybook/react';
+import {
+ XDSAlertDialog,
+ useXDSImperativeAlertDialog,
+} from '@xds/core/AlertDialog';
+import {XDSButton} from '@xds/core/Button';
+
+const meta: Meta<typeof XDSAlertDialog> = {
+ title: 'Core/XDSAlertDialog',
+ component: XDSAlertDialog,
+ tags: ['autodocs'],
+ argTypes: {
+ isOpen: {control: 'boolean'},
+ width: {control: 'number'},
+ actionVariant: {
+ control: 'select',
+ options: ['destructive', 'primary', 'secondary', 'ghost'],
+ },
+ },
+};
+
+export default meta;
+type Story = StoryObj<typeof XDSAlertDialog>;
+
+/**
+ * Delete confirmation — the most common alert dialog pattern.
+ */
+export const Delete: Story = {
+ render: () => {
+ const [isOpen, setIsOpen] = useState(false);
+ return (
+ <>
+ <XDSButton
+ label="Delete item"
+ variant="destructive"
+ onClick={() => setIsOpen(true)}
+ />
+ <XDSAlertDialog
+ isOpen={isOpen}
+ onOpenChange={setIsOpen}
+ title="Delete item?"
+ description="This action cannot be undone. The item and all its data will be permanently removed."
+ actionLabel="Delete"
+ onAction={() => setIsOpen(false)}
+ />
+ </>
+ );
+ },
+};
+
+/**
+ * Async action with loading state.
+ */
+export const Async: Story = {
+ render: () => {
+ const [isOpen, setIsOpen] = useState(false);
+ const [isLoading, setIsLoading] = useState(false);
+
+ return (
+ <>
+ <XDSButton
+ label="Revoke access"
+ variant="destructive"
+ onClick={() => setIsOpen(true)}
+ />
+ <XDSAlertDialog
+ isOpen={isOpen}
+ onOpenChange={setIsOpen}
+ title="Revoke access?"
+ description="This user will immediately lose access to all shared resources."
+ actionLabel="Revoke"
+ isActionLoading={isLoading}
+ onAction={async () => {
+ setIsLoading(true);
+ await new Promise(r => setTimeout(r, 2000));
+ setIsLoading(false);
+ setIsOpen(false);
+ }}
+ />
+ </>
+ );
+ },
+};
+
+/**
+ * Non-destructive confirmation with a primary action button.
+ */
+export const Informational: Story = {
+ render: () => {
+ const [isOpen, setIsOpen] = useState(false);
+ return (
+ <>
+ <XDSButton
+ label="Show notice"
+ variant="secondary"
+ onClick={() => setIsOpen(true)}
+ />
+ <XDSAlertDialog
+ isOpen={isOpen}
+ onOpenChange={setIsOpen}
+ title="Session expired"
+ description="Your session has expired. You will be redirected to the login page."
+ actionLabel="Sign in"
+ actionVariant="primary"
+ onAction={() => setIsOpen(false)}
+ />
+ </>
+ );
+ },
+};
+
+/**
+ * Imperative API — no state management needed.
+ */
+export const Imperative: Story = {
+ render: () => {
+ const alert = useXDSImperativeAlertDialog();
+ return (
+ <>
+ <XDSButton
+ label="Delete item"
+ variant="destructive"
+ onClick={() =>
+ alert.show({
+ title: 'Delete item?',
+ description: 'This action cannot be undone.',
+ actionLabel: 'Delete',
+ onAction: () => alert.hide(),
+ })
+ }
+ />
+ {alert.element}
+ </>
+ );
+ },
+};
diff --git a/packages/core/package.json b/packages/core/package.json
index 4250611160..5fd781ca0d 100644
--- a/packages/core/package.json
+++ b/packages/core/package.json
@@ -34,6 +34,12 @@
"require": "./dist/theme/syntax/index.js"
},
"./xds.md": "./xds.md",
+ "./AlertDialog": {
+ "source": "./src/AlertDialog/index.ts",
+ "types": "./dist/AlertDialog/index.d.ts",
+ "import": "./dist/AlertDialog/index.mjs",
+ "require": "./dist/AlertDialog/index.js"
+ },
"./AppShell": {
"source": "./src/AppShell/index.ts",
"types": "./dist/AppShell/index.d.ts",
diff --git a/packages/core/src/AlertDialog/AlertDialog.doc.mjs b/packages/core/src/AlertDialog/AlertDialog.doc.mjs
new file mode 100644
index 0000000000..dcb680d68a
--- /dev/null
+++ b/packages/core/src/AlertDialog/AlertDialog.doc.mjs
@@ -0,0 +1,81 @@
+/** @type {import('../docs-types').ComponentDoc} */
+
+export const docs = {
+ name: 'AlertDialog',
+ description:
+ 'Confirmation dialog for destructive or irreversible actions. Uses role="alertdialog" with required title, description, and cancel/action buttons.',
+ keywords: [
+ 'alert',
+ 'alertdialog',
+ 'confirm',
+ 'confirmation',
+ 'destructive',
+ 'delete',
+ 'modal',
+ 'dialog',
+ ],
+ features: [
+ 'ARIA alertdialog: Uses role="alertdialog" with aria-labelledby and aria-describedby',
+ 'No backdrop dismiss: Cannot be closed by clicking outside the dialog',
+ 'Escape = Cancel: Pressing Escape triggers the cancel action',
+ 'Built-in buttons: Ghost cancel and configurable action button rendered internally',
+ 'Action does not auto-close: Supports async operations with loading states',
+ 'Imperative API: useXDSImperativeAlertDialog for fire-and-forget usage',
+ ],
+ examples: [
+ {
+ label: 'Delete confirmation',
+ code: `import {XDSAlertDialog} from '@xds/core/AlertDialog';
+import {XDSButton} from '@xds/core/Button';
+import {useState} from 'react';
+
+function Example() {
+ const [isOpen, setIsOpen] = useState(false);
+ return (
+ <>
+ <XDSButton label="Delete" variant="danger" onClick={() => setIsOpen(true)} />
+ <XDSAlertDialog
+ isOpen={isOpen}
+ onOpenChange={setIsOpen}
+ title="Delete item?"
+ description="This action cannot be undone."
+ actionLabel="Delete"
+ onAction={() => setIsOpen(false)}
+ />
+ </>
+ );
+}`,
+ },
+ {
+ label: 'Imperative',
+ code: `import {useXDSImperativeAlertDialog} from '@xds/core/AlertDialog';
+
+function Example() {
+ const alert = useXDSImperativeAlertDialog();
+ return (
+ <>
+ <button onClick={() => alert.show({
+ title: 'Delete?',
+ description: 'This cannot be undone.',
+ actionLabel: 'Delete',
+ onAction: () => alert.hide(),
+ })}>Delete</button>
+ {alert.element}
+ </>
+ );
+}`,
+ },
+ ],
+ props: [
+ {name: 'isOpen', type: 'boolean', required: true, description: 'Whether the dialog is open.'},
+ {name: 'onOpenChange', type: '(isOpen: boolean) => unknown', required: true, description: 'Visibility change callback.'},
+ {name: 'title', type: 'string', required: true, description: 'Dialog title. Linked via aria-labelledby.'},
+ {name: 'description', type: 'string', required: true, description: 'Consequence description. Linked via aria-describedby.'},
+ {name: 'actionLabel', type: 'string', required: true, description: 'Action button label.'},
+ {name: 'onAction', type: '() => unknown', required: true, description: 'Called when action button is clicked. Does NOT auto-close.'},
+ {name: 'cancelLabel', type: 'string', default: "'Cancel'", description: 'Cancel button label.'},
+ {name: 'actionVariant', type: 'XDSButtonVariant', default: "'destructive'", description: 'Action button variant.'},
+ {name: 'isActionLoading', type: 'boolean', description: 'Shows loading spinner on the action button.'},
+ {name: 'width', type: 'number | string', default: '400', description: 'Dialog width.'},
+ ],
+};
diff --git a/packages/core/src/AlertDialog/XDSAlertDialog.test.tsx b/packages/core/src/AlertDialog/XDSAlertDialog.test.tsx
new file mode 100644
index 0000000000..23aad27efb
--- /dev/null
+++ b/packages/core/src/AlertDialog/XDSAlertDialog.test.tsx
@@ -0,0 +1,112 @@
+/**
+ * @file XDSAlertDialog.test.tsx
+ * @input Uses vitest, @testing-library/react, XDSAlertDialog component
+ * @output Unit tests for XDSAlertDialog component behavior
+ * @position Testing; validates XDSAlertDialog.tsx implementation
+ *
+ * SYNC: When XDSAlertDialog.tsx changes, update tests to match new behavior
+ */
+
+import {describe, it, expect, vi, beforeEach} from 'vitest';
+import {render, screen, fireEvent} from '@testing-library/react';
+import {XDSAlertDialog} from './XDSAlertDialog';
+
+beforeEach(() => {
+ HTMLDialogElement.prototype.showModal = vi.fn(function (
+ this: HTMLDialogElement,
+ ) {
+ this.setAttribute('open', '');
+ });
+ HTMLDialogElement.prototype.close = vi.fn(function (this: HTMLDialogElement) {
+ this.removeAttribute('open');
+ });
+});
+
+describe('XDSAlertDialog', () => {
+ const defaultProps = {
+ isOpen: true,
+ onOpenChange: vi.fn(),
+ title: 'Delete item?',
+ description: 'This action cannot be undone.',
+ actionLabel: 'Delete',
+ onAction: vi.fn(),
+ };
+
+ it('renders with alertdialog role', () => {
+ render(<XDSAlertDialog {...defaultProps} />);
+ expect(screen.getByRole('alertdialog')).toBeInTheDocument();
+ });
+
+ it('renders title and description', () => {
+ render(<XDSAlertDialog {...defaultProps} />);
+ expect(screen.getByText('Delete item?')).toBeInTheDocument();
+ expect(
+ screen.getByText('This action cannot be undone.'),
+ ).toBeInTheDocument();
+ });
+
+ it('links title via aria-labelledby', () => {
+ render(<XDSAlertDialog {...defaultProps} />);
+ const dialog = screen.getByRole('alertdialog');
+ const labelledBy = dialog.getAttribute('aria-labelledby');
+ expect(labelledBy).toBeTruthy();
+ const titleEl = document.getElementById(labelledBy!);
+ expect(titleEl).toHaveTextContent('Delete item?');
+ });
+
+ it('links description via aria-describedby', () => {
+ render(<XDSAlertDialog {...defaultProps} />);
+ const dialog = screen.getByRole('alertdialog');
+ const describedBy = dialog.getAttribute('aria-describedby');
+ expect(describedBy).toBeTruthy();
+ const descEl = document.getElementById(describedBy!);
+ expect(descEl).toHaveTextContent('This action cannot be undone.');
+ });
+
+ it('renders cancel and action buttons', () => {
+ render(<XDSAlertDialog {...defaultProps} />);
+ expect(screen.getByText('Cancel')).toBeInTheDocument();
+ expect(screen.getByText('Delete')).toBeInTheDocument();
+ });
+
+ it('uses custom cancel label', () => {
+ render(<XDSAlertDialog {...defaultProps} cancelLabel="Never mind" />);
+ expect(screen.getByText('Never mind')).toBeInTheDocument();
+ });
+
+ it('calls onOpenChange(false) when cancel is clicked', () => {
+ const onOpenChange = vi.fn();
+ render(<XDSAlertDialog {...defaultProps} onOpenChange={onOpenChange} />);
+ fireEvent.click(screen.getByText('Cancel'));
+ expect(onOpenChange).toHaveBeenCalledWith(false);
+ });
+
+ it('calls onAction when action is clicked', () => {
+ const onAction = vi.fn();
+ render(<XDSAlertDialog {...defaultProps} onAction={onAction} />);
+ fireEvent.click(screen.getByText('Delete'));
+ expect(onAction).toHaveBeenCalled();
+ });
+
+ it('does not call onOpenChange when action is clicked', () => {
+ const onOpenChange = vi.fn();
+ render(<XDSAlertDialog {...defaultProps} onOpenChange={onOpenChange} />);
+ fireEvent.click(screen.getByText('Delete'));
+ expect(onOpenChange).not.toHaveBeenCalled();
+ });
+
+ it('does not render when isOpen is false', () => {
+ render(<XDSAlertDialog {...defaultProps} isOpen={false} />);
+ expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument();
+ });
+
+ it('accepts custom width', () => {
+ render(<XDSAlertDialog {...defaultProps} width={600} />);
+ expect(screen.getByRole('alertdialog')).toBeInTheDocument();
+ });
+
+ it('defaults cancel label to Cancel', () => {
+ render(<XDSAlertDialog {...defaultProps} />);
+ expect(screen.getByText('Cancel')).toBeInTheDocument();
+ });
+});
diff --git a/packages/core/src/AlertDialog/XDSAlertDialog.tsx b/packages/core/src/AlertDialog/XDSAlertDialog.tsx
new file mode 100644
index 0000000000..40f818ece4
--- /dev/null
+++ b/packages/core/src/AlertDialog/XDSAlertDialog.tsx
@@ -0,0 +1,167 @@
+'use client';
+
+/**
+ * @file XDSAlertDialog.tsx
+ * @input Uses React, XDSDialog, XDSLayout, XDSHeading, XDSText, XDSButton
+ * @output Exports XDSAlertDialog component, XDSAlertDialogProps type
+ * @position Core implementation; consumed by index.ts, tested by XDSAlertDialog.test.tsx
+ *
+ * SYNC: When modified, update these files to stay in sync:
+ * - /packages/core/src/AlertDialog/AlertDialog.doc.mjs (props table, features, examples)
+ * - /packages/core/src/AlertDialog/XDSAlertDialog.test.tsx (tests for new/changed behavior)
+ * - /packages/core/src/AlertDialog/index.ts (exports if types change)
+ * - /apps/storybook/stories/AlertDialog.stories.tsx (storybook stories)
+ */
+
+import {useId, useCallback} from 'react';
+import {XDSDialog} from '../Dialog';
+import {XDSLayout} from '../Layout/XDSLayout';
+import {XDSLayoutContent} from '../Layout/XDSLayoutContent';
+import {XDSLayoutFooter} from '../Layout/XDSLayoutFooter';
+import {XDSHStack} from '../Stack';
+import {XDSHeading} from '../Text/XDSHeading';
+import {XDSText} from '../Text/XDSText';
+import {XDSButton, type XDSButtonVariant} from '../Button';
+import {xdsClassName} from '../utils';
+
+export interface XDSAlertDialogProps {
+ /**
+ * Whether the dialog is open.
+ */
+ isOpen: boolean;
+
+ /**
+ * Callback fired when the dialog visibility changes.
+ * Called with `false` when cancel is clicked or Escape is pressed.
+ */
+ onOpenChange: (isOpen: boolean) => unknown;
+
+ /**
+ * Dialog title. Linked to the dialog via `aria-labelledby`.
+ */
+ title: string;
+
+ /**
+ * Consequence description. Linked to the dialog via `aria-describedby`.
+ */
+ description: string;
+
+ /**
+ * Label for the cancel button. Rendered as a ghost XDSButton.
+ * Clicking cancel calls `onOpenChange(false)`.
+ * @default 'Cancel'
+ */
+ cancelLabel?: string;
+
+ /**
+ * Label for the action button.
+ */
+ actionLabel: string;
+
+ /**
+ * Variant for the action button.
+ * @default 'destructive'
+ */
+ actionVariant?: XDSButtonVariant;
+
+ /**
+ * Whether the action button shows a loading spinner.
+ */
+ isActionLoading?: boolean;
+
+ /**
+ * Callback fired when the action button is clicked.
+ * The dialog does NOT auto-close — call `onOpenChange(false)` when done.
+ */
+ onAction: () => unknown;
+
+ /**
+ * The width of the dialog.
+ * Numbers are treated as pixels, strings are used as-is.
+ * @default 400
+ */
+ width?: number | string;
+}
+
+/**
+ * A confirmation dialog for destructive or irreversible actions.
+ *
+ * Uses `role="alertdialog"` and requires explicit user action to dismiss.
+ * Cannot be dismissed by clicking outside. Escape key triggers cancel.
+ * Initial focus goes to the cancel button (least destructive action).
+ *
+ * @example
+ * ```
+ * <XDSAlertDialog
+ * isOpen={isOpen}
+ * onOpenChange={setIsOpen}
+ * title="Delete item?"
+ * description="This action cannot be undone."
+ * actionLabel="Delete"
+ * onAction={async () => { await deleteItem(); setIsOpen(false); }}
+ * />
+ * ```
+ */
+export function XDSAlertDialog({
+ isOpen,
+ onOpenChange,
+ title,
+ description,
+ cancelLabel = 'Cancel',
+ actionLabel,
+ actionVariant = 'destructive',
+ isActionLoading,
+ onAction,
+ width = 400,
+}: XDSAlertDialogProps) {
+ const titleId = useId();
+ const descriptionId = useId();
+
+ const handleCancel = useCallback(() => {
+ onOpenChange(false);
+ }, [onOpenChange]);
+
+ return (
+ <XDSDialog
+ isOpen={isOpen}
+ onOpenChange={onOpenChange}
+ width={width}
+ purpose="form"
+ role="alertdialog"
+ aria-labelledby={titleId}
+ aria-describedby={descriptionId}
+ className={xdsClassName('alert-dialog')}>
+ <XDSLayout
+ content={
+ <XDSLayoutContent>
+ <XDSHeading level={2} id={titleId}>
+ {title}
+ </XDSHeading>
+ <XDSText type="body" color="secondary" id={descriptionId}>
+ {description}
+ </XDSText>
+ </XDSLayoutContent>
+ }
+ footer={
+ <XDSLayoutFooter>
+ <XDSHStack gap={2} hAlign="end">
+ <XDSButton
+ variant="ghost"
+ label={cancelLabel}
+ onClick={handleCancel}
+ />
+ <XDSButton
+ variant={actionVariant}
+ label={actionLabel}
+ onClick={onAction}
+ isLoading={isActionLoading}
+ />
+ </XDSHStack>
+ </XDSLayoutFooter>
+ }
+ />
+ </XDSDialog>
+ );
+}
+
+XDSAlertDialog.displayName = 'XDSAlertDialog';
diff --git a/packages/core/src/AlertDialog/index.ts b/packages/core/src/AlertDialog/index.ts
new file mode 100644
index 0000000000..471b78e56c
--- /dev/null
+++ b/packages/core/src/AlertDialog/index.ts
@@ -0,0 +1,7 @@
+'use client';
+
+export {XDSAlertDialog} from './XDSAlertDialog';
+export type {XDSAlertDialogProps} from './XDSAlertDialog';
+
+export {useXDSImperativeAlertDialog} from './useXDSImperativeAlertDialog';
+export type {XDSImperativeAlertDialogReturn} from './useXDSImperativeAlertDialog';
diff --git a/packages/core/src/AlertDialog/useXDSImperativeAlertDialog.tsx b/packages/core/src/AlertDialog/useXDSImperativeAlertDialog.tsx
new file mode 100644
index 0000000000..76253f1a57
--- /dev/null
+++ b/packages/core/src/AlertDialog/useXDSImperativeAlertDialog.tsx
@@ -0,0 +1,82 @@
+'use client';
+
+/**
+ * @file useXDSImperativeAlertDialog.tsx
+ * @input Uses React, XDSAlertDialog
+ * @output Exports useXDSImperativeAlertDialog hook
+ * @position Utility hook; wraps XDSAlertDialog with imperative show/hide API
+ *
+ * SYNC: When modified, update these files to stay in sync:
+ * - /packages/core/src/AlertDialog/index.ts (exports)
+ * - /packages/core/src/AlertDialog/AlertDialog.doc.mjs
+ * - /apps/storybook/stories/AlertDialog.stories.tsx
+ */
+
+import {useState, useCallback, useMemo, type ReactNode} from 'react';
+import {XDSAlertDialog, type XDSAlertDialogProps} from './XDSAlertDialog';
+
+type AlertDialogOptions = Omit<XDSAlertDialogProps, 'isOpen' | 'onOpenChange'>;
+
+export interface XDSImperativeAlertDialogReturn {
+ /** Show the alert dialog. */
+ show: (options: AlertDialogOptions) => void;
+ /** Hide the alert dialog. */
+ hide: () => void;
+ /** Whether the dialog is currently open. */
+ isOpen: boolean;
+ /** Render this in your JSX tree. */
+ element: ReactNode;
+}
+
+/**
+ * Imperative alert dialog — show/hide without managing state.
+ *
+ * @example
+ * ```
+ * const alert = useXDSImperativeAlertDialog();
+ *
+ * const handleDelete = () => {
+ * alert.show({
+ * title: 'Delete item?',
+ * description: 'This action cannot be undone.',
+ * actionLabel: 'Delete',
+ * onAction: async () => { await deleteItem(); alert.hide(); },
+ * });
+ * };
+ *
+ * return (
+ * <>
+ * <button onClick={handleDelete}>Delete</button>
+ * {alert.element}
+ * </>
+ * );
+ * ```
+ */
+export function useXDSImperativeAlertDialog(): XDSImperativeAlertDialogReturn {
+ const [isOpen, setIsOpen] = useState(false);
+ const [options, setOptions] = useState<AlertDialogOptions | null>(null);
+
+ const show = useCallback((newOptions: AlertDialogOptions) => {
+ setOptions(newOptions);
+ setIsOpen(true);
+ }, []);
+
+ const hide = useCallback(() => {
+ setIsOpen(false);
+ }, []);
+
+ const element = useMemo(() => {
+ if (!options) return null;
+ return (
+ <XDSAlertDialog
+ {...options}
+ isOpen={isOpen}
+ onOpenChange={open => {
+ if (!open) setIsOpen(false);
+ }}
+ />
+ );
+ }, [isOpen, options]);
+
+ return {show, hide, isOpen, element};
+}
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index df1aff7df4..7fae9b60d3 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -68,6 +68,7 @@ export * from './TreeList';
// Keyboard shortcut display
export * from './Kbd';
+export * from './AlertDialog';
export * from './Dialog';
export * from './DropdownMenu';
export * from './MoreMenu';