|
| 1 | +import React from "react"; |
| 2 | +import { StoryObj } from "@storybook/react"; |
| 3 | +import { userEvent, within, expect } from "@storybook/test"; |
| 4 | + |
| 5 | +import Button from "."; |
| 6 | +import Box from "../../box"; |
| 7 | + |
| 8 | +import { allowInteractions } from "../../../../.storybook/interaction-toggle/reduced-motion"; |
| 9 | +import DefaultDecorator from "../../../../.storybook/utils/default-decorator"; |
| 10 | + |
| 11 | +export default { |
| 12 | + title: "Button/Interactions", |
| 13 | + parameters: { |
| 14 | + themeProvider: { chromatic: { theme: "sage" } }, |
| 15 | + }, |
| 16 | +}; |
| 17 | + |
| 18 | +type Story = StoryObj<typeof Button>; |
| 19 | + |
| 20 | +export const FocusAndHoverStates: Story = { |
| 21 | + render: () => ( |
| 22 | + <Box display="flex" flexDirection="column" gap={1}> |
| 23 | + <Button variantType="primary">Primary</Button> |
| 24 | + <Button variantType="secondary">Secondary</Button> |
| 25 | + <Button variantType="tertiary">Tertiary</Button> |
| 26 | + <Button variantType="subtle">Subtle</Button> |
| 27 | + <Button variantType="primary" variant="destructive"> |
| 28 | + Primary Destructive |
| 29 | + </Button> |
| 30 | + <Button variantType="secondary" variant="destructive"> |
| 31 | + Secondary Destructive |
| 32 | + </Button> |
| 33 | + |
| 34 | + <Button variant="ai">AI Primary</Button> |
| 35 | + |
| 36 | + <Button variantType="secondary" size="xs"> |
| 37 | + Secondary |
| 38 | + </Button> |
| 39 | + <Button variantType="tertiary" size="xs"> |
| 40 | + Tertiary |
| 41 | + </Button> |
| 42 | + <Button variantType="subtle" size="xs"> |
| 43 | + Subtle |
| 44 | + </Button> |
| 45 | + </Box> |
| 46 | + ), |
| 47 | + play: async ({ canvasElement }) => { |
| 48 | + if (!allowInteractions()) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + const canvas = within(canvasElement); |
| 53 | + const buttons = canvas.getAllByRole("button"); |
| 54 | + |
| 55 | + for (let i = 0; i < buttons.length; i++) { |
| 56 | + await userEvent.hover(buttons[i]); |
| 57 | + await userEvent.click(buttons[i]); |
| 58 | + await expect(buttons[i]).toHaveFocus(); |
| 59 | + if (i < buttons.length - 1) await userEvent.tab(); |
| 60 | + } |
| 61 | + }, |
| 62 | + decorators: [ |
| 63 | + (StoryToRender) => ( |
| 64 | + <DefaultDecorator> |
| 65 | + <StoryToRender /> |
| 66 | + </DefaultDecorator> |
| 67 | + ), |
| 68 | + ], |
| 69 | +}; |
| 70 | +FocusAndHoverStates.storyName = "Focus and Hover States"; |
| 71 | +FocusAndHoverStates.parameters = { |
| 72 | + pseudo: { |
| 73 | + hover: "[]", |
| 74 | + focus: "[]", |
| 75 | + }, |
| 76 | +}; |
0 commit comments