//
// Copyright 2026 DXOS.org
//
import '../../theme/index.css';
import { type Meta, type StoryObj } from '@storybook/react-vite';
import React, { useState } from 'react';
import { expect, fireEvent, userEvent, waitFor, within } from 'storybook/test';
import { type Size } from '../../sizes.ts';
import { byTestId, expectPopupSize } from '../../testing/geometry.ts';
import { withLayout, withTheme } from '../../testing/index.ts';
import { SIZE_ARG_TYPES, type SizeArgs, withSizes } from '../../testing/stories.tsx';
import { Button } from '../Button/Button.tsx';
import * as Dialog from '../Dialog/Dialog.tsx';
import { Group } from '../Group/Group.tsx';
import * as Typography from '../Typography/Typography.tsx';
import * as AlertDialog from './AlertDialog.tsx';
type ConfirmProps = {
/** Overrides the size the dialog inherits from its trigger's row. */
contentSize?: Size;
testId: string;
/** Mark the Action as the control that takes focus on open. */
autofocusAction?: boolean;
onAction: () => void;
};
const Confirm = ({ contentSize, testId, autofocusAction, onAction }: ConfirmProps) => (
Delete space?
Its objects are removed for every member.
Cancel
Delete
);
/**
* An alert dialog focusing Cancel on open, taking its trigger row's size, and one whose Action is marked with
* `DIALOG_AUTOFOCUS_ATTRIBUTE`, `lg` at every size.
*/
const DefaultStory = ({ size = 'md' }: SizeArgs) => {
const [deleted, setDeleted] = useState(0);
return (
setDeleted((count) => count + 1)} />
setDeleted((count) => count + 1)}
/>
Deleted {deleted}
);
};
const meta = {
title: 'ui/react-ui-core/components/AlertDialog',
render: DefaultStory,
decorators: [withSizes(), withLayout({ classNames: 'p-0 w-[32rem]' }), withTheme()],
args: { size: 'md' },
argTypes: SIZE_ARG_TYPES,
parameters: { layout: 'centered' },
} satisfies Meta;
export default meta;
type Story = StoryObj;
export const Default: Story = {};
/**
* An `alertdialog` named by its title and described by its description; it opens with focus on Cancel (the least
* destructive choice) unless a control carries `DIALOG_AUTOFOCUS_ATTRIBUTE`. Neither Escape nor a click outside dismisses
* it; Cancel closes it without acting; Action runs its handler and closes. The story ends open.
*/
export const Test: Story = {
args: { allSizes: true },
play: async ({ canvasElement }) => {
const body = within(canvasElement.ownerDocument.body);
const trigger = byTestId(canvasElement, 'confirm-md-trigger');
await userEvent.click(trigger);
let dialog = await body.findByRole('alertdialog', { name: 'Delete space?' });
await expect(dialog).toHaveAccessibleDescription('Its objects are removed for every member.');
const cancel = within(dialog).getByRole('button', { name: 'Cancel' });
await waitFor(() => expect(cancel).toHaveFocus());
// An outside click does not dismiss an alert dialog.
const outside = dialog.parentElement ?? canvasElement.ownerDocument.body;
await fireEvent.pointerDown(outside);
await fireEvent.pointerUp(outside);
await fireEvent.click(outside);
await new Promise((resolve) => setTimeout(resolve, 100));
await waitFor(() => expect(body.getByRole('alertdialog')).toBe(dialog));
await userEvent.click(cancel);
await waitFor(() => expect(body.queryByRole('alertdialog')).toBeNull());
await expect(byTestId(canvasElement, 'deleted-md')).toHaveTextContent('Deleted 0');
await userEvent.click(trigger);
dialog = await body.findByRole('alertdialog');
await waitFor(() => expect(within(dialog).getByRole('button', { name: 'Cancel' })).toHaveFocus());
await userEvent.keyboard('{Escape}');
await new Promise((resolve) => setTimeout(resolve, 100));
await expect(body.getByRole('alertdialog')).toBe(dialog);
await userEvent.click(within(dialog).getByRole('button', { name: 'Cancel' }));
await waitFor(() => expect(body.queryByRole('alertdialog')).toBeNull());
await userEvent.click(trigger);
dialog = await body.findByRole('alertdialog');
await userEvent.click(within(dialog).getByRole('button', { name: 'Delete' }));
await waitFor(() => expect(body.queryByRole('alertdialog')).toBeNull());
await expect(byTestId(canvasElement, 'deleted-md')).toHaveTextContent('Deleted 1');
// The dialog takes its trigger's row size.
await userEvent.click(byTestId(canvasElement, 'confirm-sm-trigger'));
dialog = await body.findByRole('alertdialog');
await expectPopupSize(dialog, 'sm');
await userEvent.click(within(dialog).getByRole('button', { name: 'Cancel' }));
await waitFor(() => expect(body.queryByRole('alertdialog')).toBeNull());
// The marked control takes focus instead of Cancel; rest open.
await userEvent.click(byTestId(canvasElement, 'marked-md-trigger'));
dialog = await body.findByRole('alertdialog');
await waitFor(() => expect(within(dialog).getByRole('button', { name: 'Delete' })).toHaveFocus());
// An explicit size wins over the inherited one.
await expectPopupSize(dialog, 'lg');
},
};