From d208281091b1c00db1724a2ee3db0055955eb41b Mon Sep 17 00:00:00 2001 From: James Russo Date: Fri, 13 Nov 2026 08:29:42 +0000 Subject: [PATCH] fix(studio): open the canvas context menu when a pointer move overtakes its lookup (#7643) A pointer move superseded by a newer move or a leave returned null, not just skipped the hover update. The right-click path awaits that same lookup for its target when nothing is selected, so on a sandboxed preview, where the lookup crosses the frame's channel, a 1px jitter during the right-click bumped the counter and the menu never opened. The stale hover is still dropped; the result is returned either way. Co-authored-by: Claude Opus 5.5 --- .../editor/previewPointerTargets.test.ts | 22 +++++++++++++++++++ .../studio/src/hooks/usePreviewInteraction.ts | 3 +-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/studio/src/components/editor/previewPointerTargets.test.ts b/packages/studio/src/components/editor/previewPointerTargets.test.ts index 197f57844d..ace505d8dc 100644 --- a/packages/studio/src/components/editor/previewPointerTargets.test.ts +++ b/packages/studio/src/components/editor/previewPointerTargets.test.ts @@ -21,6 +21,7 @@ import { detachedFakeIframe } from "../../player/hooks/timelinePlayerTestHarness import { installReactActEnvironment } from "../../hooks/domSelectionTestHarness"; import { useDomSelection } from "../../hooks/useDomSelection"; import { usePreviewInteraction } from "../../hooks/usePreviewInteraction"; +import { useCanvasContextMenuState } from "./useCanvasContextMenuState"; import type { TimelineElement } from "../../player"; installReactActEnvironment(); @@ -175,6 +176,7 @@ describe("canvas hit-tests on an opaque frame match same-origin ones", () => { describe("useDomSelection over an opaque frame's canvas", () => { type Resolve = ReturnType["resolveDomSelectionFromPreviewPoint"]; let interaction: ReturnType | null = null; + let menu: ReturnType | null = null; function renderSelection(iframe: HTMLIFrameElement, wrapResolve = (resolve: Resolve) => resolve) { let hook: ReturnType | null = null; function Harness() { @@ -206,6 +208,14 @@ describe("useDomSelection over an opaque frame's canvas", () => { updateDomEditHoverSelection: hook.updateDomEditHoverSelection, setActiveGroupElement: hook.setActiveGroupElement, }); + // The overlay's right-click path, which awaits the same lookup a pointer move runs. + menu = useCanvasContextMenuState({ + selection: hook.domEditSelection, + selectionRef: { current: hook.domEditSelection }, + hoverSelectionRef: { current: hook.domEditHoverSelection }, + onCanvasPointerMoveRef: { current: interaction.handlePreviewCanvasPointerMove }, + onSelectionChangeRef: { current: hook.applyDomSelection }, + }); return null; } const root = createRoot(document.createElement("div")); @@ -256,6 +266,18 @@ describe("useDomSelection over an opaque frame's canvas", () => { expect(hook().domEditHoverSelection).toMatchObject({ id: "pair" }); }); + it("opens the menu on a right-click whose lookup a pointer move overtook", async () => { + const hook = renderSelection(opaqueFrame()); + const [clientX, clientY] = client(60, 60); + const rightClick = { clientX, clientY, preventDefault: () => {} }; + const opened = menu!.handleContextMenu(rightClick as React.MouseEvent); + const jitter = move(1050, 650); + await act(() => Promise.all([opened, jitter])); + expect(hook().domEditSelection).toMatchObject({ id: "title" }); + expect(menu!.contextMenu?.sel).toMatchObject({ id: "title" }); + expect(hook().domEditHoverSelection).toMatchObject({ id: "pair" }); + }); + it("takes a hover of the same element from a reloaded frame, whose handle is new", async () => { const hook = renderSelection(opaqueFrame()); const picked = (await act(() => diff --git a/packages/studio/src/hooks/usePreviewInteraction.ts b/packages/studio/src/hooks/usePreviewInteraction.ts index e353be8a47..1c229b5109 100644 --- a/packages/studio/src/hooks/usePreviewInteraction.ts +++ b/packages/studio/src/hooks/usePreviewInteraction.ts @@ -249,8 +249,7 @@ export function usePreviewInteraction({ preferClipAncestor: options?.preferClipAncestor ?? false, skipSourceProbe: true, }); - if (seq !== hoverSeqRef.current) return null; - updateDomEditHoverSelection(nextSelection); + if (seq === hoverSeqRef.current) updateDomEditHoverSelection(nextSelection); return nextSelection; }, [