|
| 1 | +import { expect, screen, userEvent, waitFor, within } from "storybook/test"; |
| 2 | + |
| 3 | +import { FOUR_THEME_MODES } from "#storybook"; |
| 4 | + |
| 5 | +import { |
| 6 | + ContextMenu, |
| 7 | + ContextMenuContent, |
| 8 | + ContextMenuItem, |
| 9 | + ContextMenuSeparator, |
| 10 | + ContextMenuSub, |
| 11 | + ContextMenuSubContent, |
| 12 | + ContextMenuSubTrigger, |
| 13 | + ContextMenuTrigger, |
| 14 | +} from "./ContextMenu"; |
| 15 | + |
| 16 | +import type { Meta, StoryObj } from "@storybook/react-vite"; |
| 17 | + |
| 18 | +const TARGET_STYLE: React.CSSProperties = { |
| 19 | + display: "grid", |
| 20 | + placeItems: "center", |
| 21 | + width: 240, |
| 22 | + height: 120, |
| 23 | + border: "1px dashed var(--ui-description-foreground)", |
| 24 | +}; |
| 25 | + |
| 26 | +const MenuExample = (): React.JSX.Element => ( |
| 27 | + <ContextMenu> |
| 28 | + <ContextMenuTrigger asChild> |
| 29 | + <div style={TARGET_STYLE}>Right-click here</div> |
| 30 | + </ContextMenuTrigger> |
| 31 | + <ContextMenuContent> |
| 32 | + <ContextMenuItem> |
| 33 | + <span className="codicon codicon-play" aria-hidden="true" /> |
| 34 | + Start workspace |
| 35 | + </ContextMenuItem> |
| 36 | + <ContextMenuItem> |
| 37 | + <span className="codicon codicon-debug-restart" aria-hidden="true" /> |
| 38 | + Restart |
| 39 | + </ContextMenuItem> |
| 40 | + <ContextMenuItem disabled> |
| 41 | + <span className="codicon codicon-stop-circle" aria-hidden="true" /> |
| 42 | + Stop |
| 43 | + </ContextMenuItem> |
| 44 | + <ContextMenuSeparator /> |
| 45 | + <ContextMenuSub> |
| 46 | + <ContextMenuSubTrigger>More actions</ContextMenuSubTrigger> |
| 47 | + <ContextMenuSubContent> |
| 48 | + <ContextMenuItem>Open logs</ContextMenuItem> |
| 49 | + <ContextMenuItem>Edit settings</ContextMenuItem> |
| 50 | + </ContextMenuSubContent> |
| 51 | + </ContextMenuSub> |
| 52 | + </ContextMenuContent> |
| 53 | + </ContextMenu> |
| 54 | +); |
| 55 | + |
| 56 | +const meta: Meta<typeof MenuExample> = { |
| 57 | + title: "UI/ContextMenu", |
| 58 | + component: MenuExample, |
| 59 | +}; |
| 60 | + |
| 61 | +export default meta; |
| 62 | +type Story = StoryObj<typeof MenuExample>; |
| 63 | + |
| 64 | +/* Right-click at the target's center; without coords the contextmenu |
| 65 | + event fires at (0,0) and the menu opens detached from the target. */ |
| 66 | +async function rightClickCenter(target: Element): Promise<void> { |
| 67 | + const rect = target.getBoundingClientRect(); |
| 68 | + await userEvent.pointer({ |
| 69 | + keys: "[MouseRight]", |
| 70 | + target, |
| 71 | + coords: { |
| 72 | + clientX: rect.left + rect.width / 2, |
| 73 | + clientY: rect.top + rect.height / 2, |
| 74 | + }, |
| 75 | + }); |
| 76 | +} |
| 77 | + |
| 78 | +export const Closed: Story = {}; |
| 79 | + |
| 80 | +/* Chromatic crops to in-flow content, so snapshot stories reserve space |
| 81 | + for the portalled menu. */ |
| 82 | +const OVERLAY_SPACE: React.CSSProperties = { width: 620, height: 400 }; |
| 83 | + |
| 84 | +/* Opens the menu and its submenu so Chromatic snapshots the open state. */ |
| 85 | +export const Open: Story = { |
| 86 | + decorators: [ |
| 87 | + (Story) => ( |
| 88 | + <div style={OVERLAY_SPACE}> |
| 89 | + <Story /> |
| 90 | + </div> |
| 91 | + ), |
| 92 | + ], |
| 93 | + parameters: { chromatic: { modes: FOUR_THEME_MODES } }, |
| 94 | + play: async ({ canvasElement }) => { |
| 95 | + const canvas = within(canvasElement); |
| 96 | + await rightClickCenter(canvas.getByText("Right-click here")); |
| 97 | + const menu = await screen.findByRole("menu"); |
| 98 | + // Radix moves focus into the menu on open |
| 99 | + await waitFor(() => |
| 100 | + expect(menu.contains(document.activeElement)).toBe(true), |
| 101 | + ); |
| 102 | + // Keyboard avoids the submenu hover-open delay |
| 103 | + await userEvent.keyboard("{End}{ArrowRight}"); |
| 104 | + await screen.findByRole("menuitem", { name: "Open logs" }); |
| 105 | + }, |
| 106 | +}; |
| 107 | + |
| 108 | +export const EscapeClosesMenu: Story = { |
| 109 | + parameters: { chromatic: { disableSnapshot: true } }, |
| 110 | + play: async ({ canvasElement }) => { |
| 111 | + const canvas = within(canvasElement); |
| 112 | + await rightClickCenter(canvas.getByText("Right-click here")); |
| 113 | + await screen.findByRole("menu"); |
| 114 | + await userEvent.keyboard("{Escape}"); |
| 115 | + await waitFor(() => |
| 116 | + expect(screen.queryByRole("menu")).not.toBeInTheDocument(), |
| 117 | + ); |
| 118 | + }, |
| 119 | +}; |
0 commit comments