fix(studio): a sealed move of a layer with a later scale or rotation saves as a CSS translate (#8749)

* fix(studio): a sealed move of a layer with a later scale or rotation saves as a CSS translate

* fix(studio): a GSAP read puts back every attribute and nested cache field it changes
This commit is contained in:
Miguel Ángel
2026-11-24 19:28:54 +00:00
committed by GitHub
parent 416d9e036c
commit efb540744c
5 changed files with 130 additions and 30 deletions
@@ -152,16 +152,24 @@ describe("gsapSnapshot", () => {
expect(snapshot.targets[0]!.values.x).toBeCloseTo(600);
});
it("leaves a CSS-moved element's position to CSS when only its opacity is tweened", async () => {
it.each([
["only its opacity is tweened", { opacity: 0.5, duration: 10 }, 0],
["its only tween is a scale that starts later", { scale: 1.4, duration: 1 }, 3],
["its only tween is a rotation that starts later", { rotation: 30, duration: 1 }, 3],
])("leaves a CSS-moved element's position to CSS when %s", async (_, vars, at) => {
document.body.innerHTML = `<h1 id="title" style="translate: 0 -200px">Title</h1>`;
const title = document.getElementById("title")!;
const tl = realGsap.timeline({ paused: true });
tl.to("#title", { opacity: 0.5, duration: 10 }, 0);
tl.to("#title", vars, at);
tl.seek(0);
Object.assign(window, { gsap: realGsap, __timelines: { main: tl } });
const drawn = () =>
["translate", "transform"].map((name) => title.style.getPropertyValue(name));
const before = drawn();
await gsapSnapshot();
expect(gestureStart([handleFor(title)]).elements[0]!.gsap).toBeNull();
expect(drawn()).toEqual(before);
});
});
@@ -1,7 +1,7 @@
// The frame's GSAP tweens as plain data, for a parent that cannot reach the frame's window.
import type { EditingElementHandle } from "../editingElement";
import { handleFor } from "../editingHandles";
import { MOVES, rendersTransform } from "../editingGestureStart";
import { MOVES, rendersTransform, withoutGsapTakeover } from "../editingGestureStart";
import { isPlainRecord } from "../editingPort";
import { isElementNode } from "../domRealm";
import {
@@ -170,10 +170,12 @@ class SnapshotReader {
const value = Number(gsap?.getProperty?.(element, channel));
if (Number.isFinite(value)) values[channel] = value;
};
for (const channel of channels) read(channel);
const gsapOwnsPosition =
rendersTransform(element) || MOVES.some((channel) => channels.has(channel));
if (gsapOwnsPosition) for (const axis of ["x", "y"]) if (!(axis in values)) read(axis);
withoutGsapTakeover(element, () => {
for (const channel of channels) read(channel);
if (gsapOwnsPosition) for (const axis of ["x", "y"]) if (!(axis in values)) read(axis);
});
const className = element.getAttribute("class") ?? "";
if (className.length > MAX_SNAPSHOT_TEXT) throw new SnapshotBound("class names");
return { handle: handleFor(element), id: capped(element.id), className, values };
@@ -1,5 +1,5 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { gestureBase, gestureStart } from "./editingGestureStart";
import { gestureBase, gestureStart, withoutGsapTakeover } from "./editingGestureStart";
import { handleFor } from "./editingHandles";
type Tween = { vars: Record<string, unknown>; targets: () => unknown[]; duration: () => number };
@@ -285,3 +285,42 @@ describe("gestureBase", () => {
expect(gestureBase("0123456789abcdef:999")).toBeNull();
});
});
describe("withoutGsapTakeover", () => {
type Cached = { _gsap?: Record<string, unknown> };
it("puts back the attributes and GSAP cache a read changes on an element GSAP does not draw", () => {
document.body.innerHTML = `<h1 id="title" style="translate: 0 -200px">Title</h1>`;
const title = document.getElementById("title")! as HTMLElement & Cached;
const origin = { x: 1 };
title._gsap = { x: 0, origin };
const value = withoutGsapTakeover(title, () => {
title.style.translate = "none";
title.setAttribute("data-svg-origin", "10 20");
Object.assign(title._gsap!, { x: -200, renderTransform: () => {} });
origin.x = 99;
return 7;
});
expect(value).toBe(7);
expect(title.getAttribute("style")).toBe("translate: 0 -200px");
expect(title.hasAttribute("data-svg-origin")).toBe(false);
expect(title._gsap).toEqual({ x: 0, origin: { x: 1 } });
expect(title._gsap!.origin).toBe(origin);
});
it("drops a GSAP cache the read created", () => {
document.body.innerHTML = `<h1 id="title">Title</h1>`;
const title = document.getElementById("title")! as HTMLElement & Cached;
withoutGsapTakeover(title, () => (title._gsap = { renderTransform: () => {} }));
expect("_gsap" in title).toBe(false);
});
it("leaves an element GSAP already draws to GSAP", () => {
document.body.innerHTML = `<h1 id="title">Title</h1>`;
const title = document.getElementById("title")! as HTMLElement & Cached;
const renderTransform = () => {};
title._gsap = { renderTransform };
withoutGsapTakeover(title, () => title.setAttribute("style", "transform: scale(2)"));
expect(title.getAttribute("style")).toBe("transform: scale(2)");
});
});
@@ -165,6 +165,45 @@ function tweensOn(element: Element): GsapTween[] {
export const rendersTransform = (element: Element) =>
!!(element as { _gsap?: { renderTransform?: unknown } })._gsap?.renderTransform;
type Fields = Record<string, unknown>;
const isPlainFields = (value: unknown): value is Fields =>
!!value && typeof value === "object" && Object.getPrototypeOf(value) === Object.prototype;
function keptFields(fields: Fields, depth = 1): () => void {
const values = { ...fields };
const nested = depth > 0 ? Object.values(fields).filter(isPlainFields) : [];
const inner = nested.map((value) => keptFields(value, depth - 1));
return () => {
for (const key of Object.keys(fields))
if (!(key in values)) Reflect.deleteProperty(fields, key);
Object.assign(fields, values);
for (const restore of inner) restore();
};
}
function keptAttributes(element: Element): () => void {
const names = element.getAttributeNames();
const attributes = new Map(names.map((name) => [name, element.getAttribute(name) ?? ""]));
return () => {
for (const name of element.getAttributeNames())
if (!attributes.has(name)) element.removeAttribute(name);
for (const [name, value] of attributes)
if (element.getAttribute(name) !== value) element.setAttribute(name, value);
};
}
export function withoutGsapTakeover<T>(element: Element, read: () => T): T {
if (rendersTransform(element)) return read();
const host = element as { _gsap?: Fields };
const cache = host._gsap;
const restoreCache = cache ? keptFields(cache) : () => Reflect.deleteProperty(host, "_gsap");
const restoreAttributes = keptAttributes(element);
const value = read();
restoreAttributes();
restoreCache();
return value;
}
export type EditingGestureBase = {
x: number;
y: number;
@@ -197,7 +236,8 @@ function gsapPosition(element: Element): EditingGestureStart["gsap"] {
function gsapRotation(element: Element): number | null {
const turned = tweensOn(element).some((tween) => tween.vars && carries(tween.vars, TURNS));
return rendersTransform(element) || turned ? readGsap(element, "rotation") : null;
if (!rendersTransform(element) && !turned) return null;
return withoutGsapTakeover(element, () => readGsap(element, "rotation"));
}
function planarOf(element: StylableElement): EditingGestureStart["planar"] {
@@ -1,4 +1,4 @@
// Real Chrome: a CSS-placed element whose only tween is its opacity still moves by CSS once Studio has read GSAP.
// Real Chrome: a CSS-placed element GSAP does not draw yet still moves by CSS once Studio has read GSAP.
import { readFileSync } from "node:fs";
import { createRequire } from "node:module";
import type { Browser } from "puppeteer-core";
@@ -26,7 +26,7 @@ beforeAll(async () => {
afterAll(() => closeTestChrome(browser));
function previewHtml(): string {
function previewHtml(tween: string): string {
const gsap = readFileSync(require.resolve("gsap/dist/gsap.min.js"), "utf8");
const runtime = readFileSync(require.resolve("@hyperframes/core/runtime"), "utf8");
return `<!doctype html><html><head><meta charset="UTF-8" />
@@ -41,29 +41,40 @@ function previewHtml(): string {
<script>
window.__timelines = window.__timelines || {};
var tl = gsap.timeline({ paused: true });
tl.to("#title", { opacity: 0.5, duration: 4 }, 0);
${tween}
window.__timelines.main = tl;
</script>
<script>${runtime}</script></body></html>`;
}
// fails without the fix: the sealed read asked GSAP for x, GSAP took over the translate and the move saved as gsap.set.
it.each(MODES)("a move after a GSAP read still saves as a CSS translate (%s)", async (mode) => {
const preview = await openPreview(browser, studioBundle, previewHtml(), mode);
await preview.seek(1);
const start = await preview.page.evaluate(async () => {
const studio = (window as unknown as { hfStudio: Reads }).hfStudio;
const iframe = document.querySelector("iframe")!;
studio.usePlayerStore.setState({ currentTime: 1, isPlaying: false });
await studio.settleGsapSnapshot(iframe);
const query = await studio.previewRequest(iframe, "query", { selector: "#title" });
const handle = (query as { matches: Array<{ handle: string }> } | null)?.matches[0]?.handle;
return (await studio.previewGestureStart(iframe, [handle!]))?.elements[0];
});
expect(start).toMatchObject({ gsap: null });
const drawn = await preview.frame.evaluate(
() => getComputedStyle(document.getElementById("title")!).translate,
);
expect(drawn).toBe("0px -200px");
await preview.page.close();
});
const TWEENS = [
["only its opacity", 'tl.to("#title", { opacity: 0.5, duration: 4 }, 0);'],
["a scale that starts later", 'tl.to("#title", { scale: 1.4, duration: 1 }, 3);'],
["a rotation that starts later", 'tl.to("#title", { rotation: 30, duration: 1 }, 3);'],
];
const CASES = MODES.flatMap((mode) => TWEENS.map(([name, tween]) => [mode, name, tween]));
// fails without the fix: a sealed read asked GSAP for a transform value, GSAP took over the translate and the move
// saved as gsap.set (#8731 fixed the opacity case; a later scale or rotation still did it).
it.each(CASES)(
"a move after a GSAP read still saves as a CSS translate (%s, %s)",
async (mode, _, tween) => {
const preview = await openPreview(browser, studioBundle, previewHtml(tween!), mode!);
await preview.seek(1);
const start = await preview.page.evaluate(async () => {
const studio = (window as unknown as { hfStudio: Reads }).hfStudio;
const iframe = document.querySelector("iframe")!;
studio.usePlayerStore.setState({ currentTime: 1, isPlaying: false });
await studio.settleGsapSnapshot(iframe);
const query = await studio.previewRequest(iframe, "query", { selector: "#title" });
const handle = (query as { matches: Array<{ handle: string }> } | null)?.matches[0]?.handle;
return (await studio.previewGestureStart(iframe, [handle!]))?.elements[0];
});
expect(start).toMatchObject({ gsap: null });
const drawn = await preview.frame.evaluate(
() => getComputedStyle(document.getElementById("title")!).translate,
);
expect(drawn).toBe("0px -200px");
await preview.page.close();
},
);