Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/core/src/runtime/init.swapScenes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: "<p>two</p>",
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();
Expand Down
41 changes: 40 additions & 1 deletion packages/core/src/runtime/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown>(globalTimeline?.getChildren?.(false, true, true));
await runScriptsAfterFonts(scripts);
Expand Down
Loading