In this example, the dialog is opened and closed using the Storybook
interactions.{" "}
);
export const ModalInteractions: Story = {
args: {
children: "This dialog can be opened and closed using the button",
dialogTitle: "Interactive Dialog",
btnLabel: "Open Dialog",
},
decorators: [WithInstructions(instructions)],
play: async ({ canvasElement, step }) => {
const canvas = within(canvasElement);
// Find and click the open button
const openButton = canvas.getByRole("button", { name: /open dialog/i });
await step("Open Dialog", async () => {
await step("Open Dialog", async () => {
await userEvent.click(openButton, { delay: 1500 }); // Verify dialog is open
const dialog = canvas.getByRole("dialog");
expect(dialog).toBeVisible();
});
});
await step("Close Dialog", async () => {
const dialog = canvas.getByRole("dialog");
// Find and click close button
const closeButton = canvas.getByRole("button", {
name: /close dialog/i,
});
expect(closeButton).toHaveFocus();
await userEvent.click(closeButton, { delay: 1000 });
// Verify dialog is closed
expect(dialog).not.toBeVisible();
});
await step("Dialog focus order, close with cancel button", async () => {
await userEvent.click(openButton, { delay: 1000 });
const dialog = canvas.getByRole("dialog");
expect(dialog).toBeVisible();
expect(
canvas.getByRole("button", { name: /close dialog/i })
).toHaveFocus();
const cancelButton = canvas.getByRole("button", { name: /cancel/i });
await userEvent.tab();
expect(cancelButton).toHaveFocus();
await userEvent.keyboard(" ", { delay: 1000 });
expect(dialog).not.toBeVisible();
expect(openButton).toHaveFocus();
});
await step("Close Dialog with Escape Key", async () => {
expect(openButton).toHaveFocus();
await userEvent.click(openButton, { delay: 1000 });
await userEvent.tab();
expect(openButton).not.toHaveFocus();
const dialog = canvas.getByRole("dialog");
await userEvent.type(dialog, "{Escape}", { delay: 500 }); // Close the dialog with the keyboard
await waitFor(() => {
expect(dialog).not.toBeVisible();
});
});
},
} as Story;
export const IconTrigger: Story = {
args: {
children: "This dialog was opened from an icon button trigger.",
dialogTitle: "Settings",
btnLabel: "Settings",
icon: