diff --git a/packages/studio/src/components/editor/useMotionPathData.test.tsx b/packages/studio/src/components/editor/useMotionPathData.test.tsx index e1aa8ed654..6edb68d5f9 100644 --- a/packages/studio/src/components/editor/useMotionPathData.test.tsx +++ b/packages/studio/src/components/editor/useMotionPathData.test.tsx @@ -70,6 +70,78 @@ it("draws an axis the tween leaves alone where GSAP renders it, so the playhead } }); +/** A frame whose window Studio cannot reach, as a sealed preview's. */ +function sealedIframe(): HTMLIFrameElement { + const iframe = document.createElement("iframe"); + const blocked = new Proxy( + {}, + { + get: () => { + throw new DOMException("blocked", "SecurityError"); + }, + }, + ); + Object.defineProperty(iframe, "contentWindow", { value: blocked }); + return iframe; +} + +// fails without the fix: before a sealed frame's x/y read landed, the path drew its y axis at 0. +it("waits for a sealed frame's x/y before drawing the axis the tween leaves alone", () => { + runtime.read = { + keyframes: [ + { percentage: 66.667, properties: { x: 60 } }, + { percentage: 100, properties: { x: 120 } }, + ], + }; + const iframe = sealedIframe(); + let points: string | undefined; + function Probe() { + const ref = useRef(iframe); + points = useMotionPathData(ref, "#box").geometry?.points; + return null; + } + const root = createRoot(document.body.appendChild(document.createElement("div"))); + try { + act(() => root.render()); + expect(points).toBeUndefined(); + + runtime.gsapPosition = { x: 60, y: 30 }; + act(() => vi.advanceTimersByTime(250)); + expect(points).toBe("60,30 120,30"); + } finally { + act(() => root.unmount()); + } +}); + +// fails without the fix: a path kept while the read was pending stayed drawn after the selection moved on. +it("drops the kept path when another layer is selected before a sealed read lands", () => { + runtime.read = { + keyframes: [ + { percentage: 66.667, properties: { x: 60 } }, + { percentage: 100, properties: { x: 120 } }, + ], + }; + runtime.gsapPosition = { x: 60, y: 30 }; + const iframe = sealedIframe(); + let points: string | undefined; + function Probe({ selector }: { selector: string }) { + const ref = useRef(iframe); + points = useMotionPathData(ref, selector).geometry?.points; + return null; + } + const root = createRoot(document.body.appendChild(document.createElement("div"))); + try { + act(() => root.render()); + expect(points).toBe("60,30 120,30"); + + runtime.gsapPosition = null; + act(() => root.render()); + expect(points).toBeUndefined(); + } finally { + act(() => root.unmount()); + } +}); + it("reads no layout per frame until there is a path or a create ring to draw", () => { const iframe = document.createElement("iframe"); document.body.append(iframe); diff --git a/packages/studio/src/components/editor/useMotionPathData.ts b/packages/studio/src/components/editor/useMotionPathData.ts index 6ce7599b9e..582b704a59 100644 --- a/packages/studio/src/components/editor/useMotionPathData.ts +++ b/packages/studio/src/components/editor/useMotionPathData.ts @@ -2,6 +2,7 @@ import { useEffect, useRef, useState, type RefObject } from "react"; import { isHtmlElement } from "@hyperframes/core/runtime/dom-realm"; import { readRuntimeKeyframes } from "../../hooks/gsapRuntimeKeyframes"; import { readGsapPositionFromIframe } from "../../hooks/gsapPositionDetection"; +import { useGsapSnapshotVersion } from "../../hooks/gsapFrameSnapshot"; import { elementHome, transformWDivisor } from "@hyperframes/core/runtime/editing-port"; import { isElementVisibleForOverlay } from "./domEditOverlayGeometry"; import { @@ -26,6 +27,9 @@ function rectsClose(a: Rect, b: Rect): boolean { const sealedPlugins = new WeakMap(); +const sealedReadPending = (iframe: HTMLIFrameElement | null) => + !!iframe && previewFrameOpaque(iframe); + export function hasMotionPathPlugin(iframe: HTMLIFrameElement | null): boolean { if (iframe && previewFrameOpaque(iframe)) return sealedPlugins.get(iframe) ?? false; try { @@ -84,6 +88,7 @@ export function useMotionPathData( } { const [rect, setRect] = useState(null); const [geometry, setGeometry] = useState(null); + const snapshotVersion = useGsapSnapshotVersion(); const resolvedForRef = useRef(null); const geometryResolved = resolvedForRef.current === selector; const [visibleInPreview, setVisibleInPreview] = useState(true); @@ -180,6 +185,10 @@ export function useMotionPathData( // Position-only: never let a co-located size/scale tween shadow the path. const read = readRuntimeKeyframes(iframeRef.current, selector, undefined, ["x", "y"]); const base = read ? readGsapPositionFromIframe(iframeRef.current, selector) : null; + if (read && !base && sealedReadPending(iframeRef.current)) { + if (resolvedForRef.current !== selector) setGeometry(null); + return; + } const next = buildMotionPathGeometry(read, base ?? undefined); setGeometry((prev) => prev?.kind === next?.kind && @@ -192,7 +201,7 @@ export function useMotionPathData( recompute(); const id = window.setInterval(recompute, 250); return () => window.clearInterval(id); - }, [selector, iframeRef]); + }, [selector, iframeRef, snapshotVersion]); return { rect, geometry, geometryResolved, visibleInPreview, home, pScale }; }