From 1ddd425587b78e9b247cb87e4a1a17012a1876c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Sat, 7 Nov 2026 02:46:21 +0000 Subject: [PATCH] fix(core): words in a scene the preview mounts later keep their spaces (#7342) * fix(core): words in a scene the preview mounts later keep their spaces * fix(core): a scene laid out for its scripts ignores clicks until it shows --- .../core/src/runtime/init.swapScenes.test.ts | 21 ++++++++++ packages/core/src/runtime/init.ts | 41 ++++++++++++++++++- 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/packages/core/src/runtime/init.swapScenes.test.ts b/packages/core/src/runtime/init.swapScenes.test.ts index 80a0cec09b..e628c98de5 100644 --- a/packages/core/src/runtime/init.swapScenes.test.ts +++ b/packages/core/src/runtime/init.swapScenes.test.ts @@ -1961,6 +1961,27 @@ describe("the preview's scene window", () => { expect(sceneHost("s2").getAttribute("style")).not.toContain("display: none"); }); + it("lays out a scene the visibility pass hid by display, unseen, while its script builds its timeline", async () => { + const seen = + `var h = document.querySelector('[data-hf-scene="s2"]:not(style):not(script)');` + + `document.body.setAttribute("data-s2-seen", [getComputedStyle(h).display, h.style.opacity, h.hasAttribute("inert")].join(" "));`; + const leaf: Scene = { + ...S2, + body: "

two

", + extraAttrs: ' style="position: static"', + script: seen, + }; + bootWindowed([S0, S1, leaf], trackingRoot().root); + await tick(); + // Paused in the middle scene, the last one mounts as next, hidden until it plays. + await seekAndSettle(2.5); + for (let i = 0; i < 5; i++) await tick(); + expect(document.body.getAttribute("data-s2-seen")).toBe("block 0 true"); + expect(sceneHost("s2").getAttribute("style")).toContain("display: none"); + expect(sceneHost("s2").getAttribute("style")).not.toContain("opacity"); + expect(sceneHost("s2").hasAttribute("inert")).toBe(false); + }); + it("mounts a scene Studio holds for editing and keeps it until Studio lets go", async () => { bootWindowed([S0, S1, S2], trackingRoot().root); await tick(); diff --git a/packages/core/src/runtime/init.ts b/packages/core/src/runtime/init.ts index e8d379f5a6..a282c0d683 100644 --- a/packages/core/src/runtime/init.ts +++ b/packages/core/src/runtime/init.ts @@ -4323,9 +4323,48 @@ export function initSandboxRuntimeModular(): void { } } }; + // GSAP reads the transform of an element it cannot lay out by moving it out and back before its next + // element, past the text between; a host the visibility pass hid by display is laid out, unseen and inert, meanwhile. + const layOutHiddenHosts = (hosts: Element[]): (() => void) => { + const hidden = hosts.filter((host): host is StylableElement => + displayBeforeHide.has(host as StylableElement), + ); + const opacities = hidden.map((host) => { + const before = displayBeforeHide.get(host)!; + if (before.value) host.style.setProperty("display", before.value, before.priority); + else host.style.removeProperty("display"); + const opacity = { + value: host.style.getPropertyValue("opacity"), + priority: host.style.getPropertyPriority("opacity"), + inert: host.hasAttribute("inert"), + }; + host.style.setProperty("opacity", "0", "important"); + host.setAttribute("inert", ""); + return opacity; + }); + return () => + hidden.forEach((host, i) => { + const ours = + host.style.getPropertyValue("opacity") === "0" && + host.style.getPropertyPriority("opacity") === "important"; + const { value, priority, inert } = opacities[i]!; + if (ours && value) host.style.setProperty("opacity", value, priority); + else if (ours) host.style.removeProperty("opacity"); + if (!inert) host.removeAttribute("inert"); + if (displayBeforeHide.has(host)) host.style.display = "none"; + }); + }; + const runSceneScripts = async (hosts: Element[], scripts: Element[]) => { + const putHidesBack = layOutHiddenHosts(hosts); + try { + await runSceneScriptsLaidOut(hosts, scripts); + } finally { + putHidesBack(); + } + }; // Runs a scene's scripts, then records what they started after returning: a script resumed by a font // wait builds there, past the recorder that wraps it. With several scenes, a late animation's scene is unknown. - const runSceneScripts = async (hosts: Element[], scripts: Element[]) => { + const runSceneScriptsLaidOut = async (hosts: Element[], scripts: Element[]) => { const globalTimeline = window.gsap?.globalTimeline; const before = new Set(globalTimeline?.getChildren?.(false, true, true)); await runScriptsAfterFonts(scripts);