Skip to content
Merged
24 changes: 24 additions & 0 deletions packages/studio/src/components/nle/NLEPreview.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,30 @@ describe("NLEPreview", () => {
},
);

// Chrome stops rendering a hidden or clipped cross-origin frame, and a sealed one is cross-origin.
it.each([
["opaque" as const, ["opacity: 0"], ["visibility", "clip-path", "z-index"]],
[undefined, ["visibility: hidden", "clip-path: inset(100%)"], ["opacity"]],
])(
"keeps a reload's preview out of sight, and a sealed one rendering (%s)",
(sandboxOrigin, present, absent) => {
const view = renderPreview(
[
{ gen: 0, role: "live" },
{ gen: 1, role: "shadow", url: "/api/projects/p/preview?_t=1" },
],
{ sandboxOrigin },
);
const shadow = [...view.stage.querySelectorAll('[data-testid="mock-player"]')]
.map((player) => player.getAttribute("style") ?? "")
.filter((style) => style.includes("pointer-events: none"));
expect(shadow).toHaveLength(1);
for (const rule of present) expect(shadow[0]).toContain(rule);
for (const rule of absent) expect(shadow[0]).not.toContain(rule);
view.cleanup();
},
);

it("covers the live preview with the cached frame-0 poster until it is ready to show", () => {
const view = renderPreview();
const poster = () =>
Expand Down
8 changes: 2 additions & 6 deletions packages/studio/src/components/nle/NLEPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ import {
} from "./previewZoom";
import { RULER_GUTTER_PX, usePreviewGuidesStore } from "../editor/previewGuidesStore";
import { PreviewZoomOverlay, usePreviewZoomOverlay } from "./PreviewZoomOverlay";
import {
PREVIEW_STAGE_INSET_PX,
resolvePreviewStageSize,
SHADOW_IFRAME_STYLE,
} from "./previewStage";
import { PREVIEW_STAGE_INSET_PX, resolvePreviewStageSize, shadowIframeStyle } from "./previewStage";
import { usePreviewFirstFrameTelemetry } from "../../player/hooks/usePreviewFirstFrameTelemetry";
import { PreviewPoster, usePreviewPoster } from "./PreviewPoster";
interface NLEPreviewProps extends Pick<
Expand Down Expand Up @@ -534,7 +530,7 @@ export const NLEPreview = memo(function NLEPreview({
suppressLoadingOverlay
noLoadTimeout
sandboxOrigin={sandboxOrigin}
style={SHADOW_IFRAME_STYLE}
style={shadowIframeStyle(sandboxOrigin)}
/>
),
)}
Expand Down
12 changes: 11 additions & 1 deletion packages/studio/src/components/nle/previewStage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,24 @@ export const PREVIEW_STAGE_INSET_PX = 8;

// clip-path as well as visibility: the player's loading overlay sets its own
// visibility:visible and would otherwise paint over the live frame.
export const SHADOW_IFRAME_STYLE: CSSProperties = {
const SHADOW_IFRAME_STYLE: CSSProperties = {
position: "absolute",
inset: 0,
visibility: "hidden",
clipPath: "inset(100%)",
pointerEvents: "none",
};

const RENDERING_SHADOW_IFRAME_STYLE: CSSProperties = {
position: "absolute",
inset: 0,
opacity: 0,
pointerEvents: "none",
};

export const shadowIframeStyle = (sandboxOrigin: "opaque" | undefined): CSSProperties =>
sandboxOrigin === "opaque" ? RENDERING_SHADOW_IFRAME_STYLE : SHADOW_IFRAME_STYLE;

export function resolvePreviewStageSize(
viewportWidth: number,
viewportHeight: number,
Expand Down
7 changes: 7 additions & 0 deletions packages/studio/src/player/hooks/useShadowPreviewReload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ function shadowPagePhase(iframe: HTMLIFrameElement | null): string {
const pageGotTo = (iframe: HTMLIFrameElement | null) =>
shadowPlayerReachable(iframe) ? "" : ` Its page had got to: ${shadowPagePhase(iframe)}.`;

function hyperframesPlayerOf(iframe: HTMLIFrameElement): Element | null {
const root = iframe.getRootNode();
return root instanceof ShadowRoot ? root.host : null;
}

function isDocumentHidden(): boolean {
return typeof document !== "undefined" && document.visibilityState === "hidden";
}
Expand Down Expand Up @@ -293,6 +298,7 @@ export function useShadowPreviewReload({
// The store takes the new document's timeline only now that it is the one on screen, and reads it there.
const retired = iframeRef.current;
iframeRef.current = shadow;
hyperframesPlayerOf(shadow)?.toggleAttribute("inert", false);
pending.commit();
shadowIframeRef.current = null;
attachIframeShortcutListeners();
Expand Down Expand Up @@ -403,6 +409,7 @@ export function useShadowPreviewReload({

const setShadowIframeNode = useCallback((node: HTMLIFrameElement | null) => {
shadowIframeRef.current = node;
if (node) hyperframesPlayerOf(node)?.toggleAttribute("inert", true);
const gen = budgetGenRef.current;
if (!node || !shadowFrameLeftPageRef.current || gen == null || gen !== shadowGenRef.current)
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,26 @@ describe("shadow reload readiness and failure", () => {
unmount(root);
});

// The player turns pointer events back on for its frame, so a reload's frame would take the live one's clicks.
it("keeps a reload's player out of clicks and focus until it is promoted, then gives them back", () => {
const { getApi, gen, root } = beginReload();
const shadow = makeShadowWithSpies();
const player = document.createElement("hyperframes-player");
player.attachShadow({ mode: "open" }).append(shadow.iframe);
document.body.append(player);

act(() => getApi().setShadowIframeNode(shadow.iframe));
expect(player.hasAttribute("inert")).toBe(true);

act(() => {
getApi().onShadowIframeLoad(gen);
getApi().onShadowReadyChange(gen, true);
});
expect(getApi().iframeRef.current).toBe(shadow.iframe);
expect(player.hasAttribute("inert")).toBe(false);
unmount(root);
});

it("keeps a playing film playing through a reload, from where the live frame had reached", () => {
const { getApi, root } = renderTimelinePlayerHarness();
const { adapter: liveAdapter, win } = makeAdapterWindow();
Expand Down
Loading