Skip to content

Commit 1ddd425

Browse files
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
1 parent 25a6101 commit 1ddd425

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

‎packages/core/src/runtime/init.swapScenes.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,6 +1961,27 @@ describe("the preview's scene window", () => {
19611961
expect(sceneHost("s2").getAttribute("style")).not.toContain("display: none");
19621962
});
19631963

1964+
it("lays out a scene the visibility pass hid by display, unseen, while its script builds its timeline", async () => {
1965+
const seen =
1966+
`var h = document.querySelector('[data-hf-scene="s2"]:not(style):not(script)');` +
1967+
`document.body.setAttribute("data-s2-seen", [getComputedStyle(h).display, h.style.opacity, h.hasAttribute("inert")].join(" "));`;
1968+
const leaf: Scene = {
1969+
...S2,
1970+
body: "<p>two</p>",
1971+
extraAttrs: ' style="position: static"',
1972+
script: seen,
1973+
};
1974+
bootWindowed([S0, S1, leaf], trackingRoot().root);
1975+
await tick();
1976+
// Paused in the middle scene, the last one mounts as next, hidden until it plays.
1977+
await seekAndSettle(2.5);
1978+
for (let i = 0; i < 5; i++) await tick();
1979+
expect(document.body.getAttribute("data-s2-seen")).toBe("block 0 true");
1980+
expect(sceneHost("s2").getAttribute("style")).toContain("display: none");
1981+
expect(sceneHost("s2").getAttribute("style")).not.toContain("opacity");
1982+
expect(sceneHost("s2").hasAttribute("inert")).toBe(false);
1983+
});
1984+
19641985
it("mounts a scene Studio holds for editing and keeps it until Studio lets go", async () => {
19651986
bootWindowed([S0, S1, S2], trackingRoot().root);
19661987
await tick();

‎packages/core/src/runtime/init.ts‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4323,9 +4323,48 @@ export function initSandboxRuntimeModular(): void {
43234323
}
43244324
}
43254325
};
4326+
// GSAP reads the transform of an element it cannot lay out by moving it out and back before its next
4327+
// element, past the text between; a host the visibility pass hid by display is laid out, unseen and inert, meanwhile.
4328+
const layOutHiddenHosts = (hosts: Element[]): (() => void) => {
4329+
const hidden = hosts.filter((host): host is StylableElement =>
4330+
displayBeforeHide.has(host as StylableElement),
4331+
);
4332+
const opacities = hidden.map((host) => {
4333+
const before = displayBeforeHide.get(host)!;
4334+
if (before.value) host.style.setProperty("display", before.value, before.priority);
4335+
else host.style.removeProperty("display");
4336+
const opacity = {
4337+
value: host.style.getPropertyValue("opacity"),
4338+
priority: host.style.getPropertyPriority("opacity"),
4339+
inert: host.hasAttribute("inert"),
4340+
};
4341+
host.style.setProperty("opacity", "0", "important");
4342+
host.setAttribute("inert", "");
4343+
return opacity;
4344+
});
4345+
return () =>
4346+
hidden.forEach((host, i) => {
4347+
const ours =
4348+
host.style.getPropertyValue("opacity") === "0" &&
4349+
host.style.getPropertyPriority("opacity") === "important";
4350+
const { value, priority, inert } = opacities[i]!;
4351+
if (ours && value) host.style.setProperty("opacity", value, priority);
4352+
else if (ours) host.style.removeProperty("opacity");
4353+
if (!inert) host.removeAttribute("inert");
4354+
if (displayBeforeHide.has(host)) host.style.display = "none";
4355+
});
4356+
};
4357+
const runSceneScripts = async (hosts: Element[], scripts: Element[]) => {
4358+
const putHidesBack = layOutHiddenHosts(hosts);
4359+
try {
4360+
await runSceneScriptsLaidOut(hosts, scripts);
4361+
} finally {
4362+
putHidesBack();
4363+
}
4364+
};
43264365
// Runs a scene's scripts, then records what they started after returning: a script resumed by a font
43274366
// wait builds there, past the recorder that wraps it. With several scenes, a late animation's scene is unknown.
4328-
const runSceneScripts = async (hosts: Element[], scripts: Element[]) => {
4367+
const runSceneScriptsLaidOut = async (hosts: Element[], scripts: Element[]) => {
43294368
const globalTimeline = window.gsap?.globalTimeline;
43304369
const before = new Set<unknown>(globalTimeline?.getChildren?.(false, true, true));
43314370
await runScriptsAfterFonts(scripts);

0 commit comments

Comments
 (0)