Skip to content

Write HTML. Render video. Built for agents.

fix(studio): clip strips resize on the next frame, ending ResizeObserver loop errors (#5187)

Miguel Ángela1803448b8b8
diff --git a/packages/studio/src/components/ui/Menu.test.tsx b/packages/studio/src/components/ui/Menu.test.tsx
index c6c9ce8d10..6929b5605b 100644
--- a/packages/studio/src/components/ui/Menu.test.tsx
+++ b/packages/studio/src/components/ui/Menu.test.tsx
@@ -48,6 +48,13 @@ function clickWithMouse(target: Element): void {
});
}
+function rightClick(target: Element): void {
+ const init = { bubbles: true, cancelable: true, composed: true };
+ act(() => {
+ target.dispatchEvent(new MouseEvent("contextmenu", init));
+ });
+}
+
function key(k: string, target: Element | null = document.activeElement): void {
const init = { key: k, bubbles: true, cancelable: true, composed: true };
act(() => {
@@ -241,11 +248,7 @@ describe("ContextMenu", () => {
expect(trigger().textContent).toBe("clip");
expect(popup()).toBeNull();
- act(() => {
- trigger().dispatchEvent(
- new MouseEvent("contextmenu", { bubbles: true, cancelable: true, composed: true }),
- );
- });
+ rightClick(trigger());
await settle();
expect(popup()).not.toBeNull();
@@ -257,6 +260,65 @@ describe("ContextMenu", () => {
});
});
+describe("container={null}", () => {
+ /** Rendered inline means inside the trigger's own parent, not a portal on the body. */
+ function expectInlineBeside(menu: HTMLElement | null): void {
+ expect(menu).not.toBeNull();
+ expect(trigger().parentElement?.contains(menu)).toBe(true);
+ }
+
+ it("opens a Menu inline, next to its trigger, and closes on Escape", async () => {
+ render(
+ <div>
+ <Menu container={null} trigger={<button data-testid="trigger">Actions</button>}>
+ <MenuItem>Bring to front</MenuItem>
+ </Menu>
+ </div>,
+ );
+ clickWithMouse(trigger());
+ await settle();
+ expectInlineBeside(popup());
+
+ key("Escape");
+ await settle();
+ expect(popup()).toBeNull();
+ });
+
+ it("opens a ContextMenu inline, next to its area", async () => {
+ render(
+ <div>
+ <ContextMenu container={null} trigger={<div data-testid="trigger">clip</div>}>
+ <MenuItem>Split</MenuItem>
+ </ContextMenu>
+ </div>,
+ );
+ rightClick(trigger());
+ await settle();
+ expectInlineBeside(popup());
+ });
+
+ it("opens a Popover inline on its first render", async () => {
+ render(
+ <div>
+ <Popover
+ container={null}
+ defaultOpen
+ trigger={<button data-testid="trigger">Speed</button>}
+ >
+ <span>1x</span>
+ </Popover>
+ </div>,
+ );
+ await settle();
+ expectInlineBeside(document.querySelector<HTMLElement>("[role=dialog]"));
+ });
+
+ it("still portals to the body when no container is passed", async () => {
+ await openActionMenu();
+ expect(trigger().parentElement?.contains(popup())).toBe(false);
+ });
+});
+
describe("Popover", () => {
it("leaves the keys of a text field inside it alone", async () => {
// The Menu owns ArrowDown and typeahead; a Popover must not, or the rename
diff --git a/packages/studio/src/components/ui/Menu.tsx b/packages/studio/src/components/ui/Menu.tsx
index 3df683e63c..6ad219f3ec 100644
--- a/packages/studio/src/components/ui/Menu.tsx
+++ b/packages/studio/src/components/ui/Menu.tsx
@@ -5,6 +5,7 @@
import { ContextMenu as BaseContextMenu } from "@base-ui/react/context-menu";
import { Menu as BaseMenu } from "@base-ui/react/menu";
+import { useRef } from "react";
import type { ComponentPropsWithoutRef, ElementType, ReactElement, ReactNode } from "react";
import { cn } from "./cn";
@@ -20,7 +21,19 @@ type StyledProps<T extends ElementType> = Omit<ComponentPropsWithoutRef<T>, "cla
export type PopupPreviewState = "open";
/** Where the portal puts the popup. `null` keeps it inline, next to its trigger. */
-type PortalContainer = ComponentPropsWithoutRef<typeof BaseMenu.Portal>["container"];
+export type PortalContainer = ComponentPropsWithoutRef<typeof BaseMenu.Portal>["container"];
+
+const CONTENTS = { display: "contents" } as const;
+
+/** Base UI reads a `null` container as "not resolved yet" and renders nothing, so give `null` a host by the trigger. */
+export function useInlinePortal(container: PortalContainer) {
+ const host = useRef<HTMLSpanElement>(null);
+ if (container !== null) return { host: null, portal: { container } };
+ return {
+ host: <span ref={host} style={CONTENTS} />,
+ portal: { container: host, style: CONTENTS },
+ };
+}
/** Matches Tooltip's gap from its trigger, and its viewport margin. */
const SIDE_OFFSET = 6;
@@ -77,7 +90,7 @@ interface PositionedProps {
side?: "top" | "bottom" | "left" | "right";
align?: "start" | "center" | "end";
sideOffset?: number;
- /** Portal target. Pass the shadow root when the trigger lives in one. */
+ /** Portal target. Pass the shadow root when the trigger lives in one; `null` keeps it inline. */
container?: PortalContainer;
/** Names the popup for assistive tech. A menu with no name is unlabelled. */
"aria-label"?: string;
@@ -109,10 +122,12 @@ export function Menu({
"data-preview-state": previewState,
...root
}: MenuProps) {
+ const inline = useInlinePortal(container);
return (
<BaseMenu.Root {...root}>
<BaseMenu.Trigger render={trigger} />
- <BaseMenu.Portal container={container}>
+ {inline.host}
+ <BaseMenu.Portal {...inline.portal}>
<BaseMenu.Positioner
side={side}
align={align}
@@ -153,10 +168,12 @@ export function ContextMenu({
"data-preview-state": previewState,
...root
}: Omit<ContextMenuProps, "side" | "align" | "sideOffset">) {
+ const inline = useInlinePortal(container);
return (
<BaseContextMenu.Root {...root}>
<BaseContextMenu.Trigger render={trigger} />
- <BaseContextMenu.Portal container={container}>
+ {inline.host}
+ <BaseContextMenu.Portal {...inline.portal}>
<BaseContextMenu.Positioner collisionPadding={VIEWPORT_MARGIN} className={POPUP_LAYER}>
<BaseContextMenu.Popup
aria-label={ariaLabel}
diff --git a/packages/studio/src/components/ui/Popover.tsx b/packages/studio/src/components/ui/Popover.tsx
index 877e8925ec..9c2a45da8c 100644
--- a/packages/studio/src/components/ui/Popover.tsx
+++ b/packages/studio/src/components/ui/Popover.tsx
@@ -6,9 +6,13 @@
import { Popover as BasePopover } from "@base-ui/react/popover";
import type { ComponentPropsWithoutRef, ReactElement, ReactNode } from "react";
import { cn } from "./cn";
-import { POPUP_LAYER, popupSurface, type PopupPreviewState } from "./Menu";
-
-type PortalContainer = ComponentPropsWithoutRef<typeof BasePopover.Portal>["container"];
+import {
+ POPUP_LAYER,
+ popupSurface,
+ useInlinePortal,
+ type PopupPreviewState,
+ type PortalContainer,
+} from "./Menu";
/** Matches Menu's gap from its trigger, and its viewport margin. */
const SIDE_OFFSET = 6;
@@ -33,7 +37,7 @@ interface PopoverProps extends Omit<ComponentPropsWithoutRef<typeof BasePopover.
sideOffset?: number;
/** Points the popup at its trigger. Off by default; the default gap from the trigger grows by the arrow's height. */
arrow?: boolean;
- /** Portal target. Pass the shadow root when the trigger lives in one. */
+ /** Portal target. Pass the shadow root when the trigger lives in one; `null` keeps it inline. */
container?: PortalContainer;
/** Names the popup for assistive tech. */
"aria-label"?: string;
@@ -60,10 +64,12 @@ export function Popover({
"data-preview-state": previewState,
...root
}: PopoverProps) {
+ const inline = useInlinePortal(container);
return (
<BasePopover.Root {...root}>
<BasePopover.Trigger render={trigger} />
- <BasePopover.Portal container={container}>
+ {inline.host}
+ <BasePopover.Portal {...inline.portal}>
<BasePopover.Positioner
side={side}
align={align}
diff --git a/packages/studio/src/hooks/useThumbnailStripSize.test.tsx b/packages/studio/src/hooks/useThumbnailStripSize.test.tsx
index 0e81d61a3d..629b89e416 100644
--- a/packages/studio/src/hooks/useThumbnailStripSize.test.tsx
+++ b/packages/studio/src/hooks/useThumbnailStripSize.test.tsx
@@ -18,9 +18,125 @@ vi.mock("../player/components/timelineZoomInput", () => ({
Reflect.set(globalThis, "IS_REACT_ACT_ENVIRONMENT", true);
+/** Runs the frames the hook asked for, as the browser does after the observer's delivery. */
+function nextFrames() {
+ vi.useFakeTimers({ toFake: ["requestAnimationFrame", "cancelAnimationFrame"] });
+ return () => act(() => vi.advanceTimersToNextFrame());
+}
+
+// A strip that re-rendered inside the observer's delivery resized the timeline Chromium had already measured that
+// frame, which it reported as a ResizeObserver loop on every frame of a trim.
+it("applies a reported size on the next frame, never inside the observer's delivery", () => {
+ const originalResizeObserver = globalThis.ResizeObserver;
+ globalThis.ResizeObserver = MockResizeObserver as unknown as typeof ResizeObserver;
+ const runFrames = nextFrames();
+ const host = document.createElement("div");
+ document.body.append(host);
+ Object.defineProperty(host, "clientWidth", { configurable: true, value: 300 });
+ Object.defineProperty(host, "clientHeight", { configurable: true, value: 40 });
+ const root = createRoot(host);
+ function Harness() {
+ const [size, ref] = useThumbnailStripSize();
+ return <div ref={ref}>{`${size.width}x${size.height}`}</div>;
+ }
+ try {
+ act(() => root.render(<Harness />));
+ reportResize(310, 40);
+ reportResize(320, 40);
+ expect(vi.getTimerCount()).toBe(1);
+ expect(host.textContent).toBe("300x40");
+ runFrames();
+ expect(host.textContent).toBe("320x40");
+ } finally {
+ act(() => root.unmount());
+ host.remove();
+ vi.useRealTimers();
+ vi.restoreAllMocks();
+ globalThis.ResizeObserver = originalResizeObserver;
+ }
+});
+
+it("cancels a pending resize frame when the last strip unmounts", () => {
+ const originalResizeObserver = globalThis.ResizeObserver;
+ globalThis.ResizeObserver = MockResizeObserver as unknown as typeof ResizeObserver;
+ const runFrames = nextFrames();
+ const host = document.createElement("div");
+ document.body.append(host);
+ const root = createRoot(host);
+ function Harness() {
+ const [, ref] = useThumbnailStripSize();
+ return <div ref={ref} />;
+ }
+ try {
+ act(() => root.render(<Harness />));
+ reportResize(320, 40);
+ expect(vi.getTimerCount()).toBe(1);
+ act(() => root.unmount());
+ expect(vi.getTimerCount()).toBe(0);
+ runFrames();
+ } finally {
+ act(() => root.unmount());
+ host.remove();
+ vi.useRealTimers();
+ vi.restoreAllMocks();
+ globalThis.ResizeObserver = originalResizeObserver;
+ }
+});
+
+it("drops a pending size when its strip unmounts, so a strip remounted on that box keeps its own", () => {
+ const originalResizeObserver = globalThis.ResizeObserver;
+ globalThis.ResizeObserver = MockResizeObserver as unknown as typeof ResizeObserver;
+ const runFrames = nextFrames();
+ const host = document.createElement("div");
+ document.body.append(host);
+ const root = createRoot(host);
+ function Strip() {
+ const [size, ref] = useThumbnailStripSize();
+ return <div ref={ref}>{`${size.width}x${size.height}`}</div>;
+ }
+ function Box({ id }: { id: string }) {
+ const box = (element: HTMLDivElement | null) => {
+ if (!element) return;
+ Object.defineProperty(element, "clientWidth", { configurable: true, value: 300 });
+ Object.defineProperty(element, "clientHeight", { configurable: true, value: 40 });
+ };
+ return (
+ <div ref={box} data-testid="box">
+ <Strip key={id} />
+ </div>
+ );
+ }
+ // The other strip keeps the shared observer alive across the remount.
+ const render = (id: string) =>
+ act(() =>
+ root.render(
+ <>
+ <div>
+ <Strip />
+ </div>
+ <Box id={id} />
+ </>,
+ ),
+ );
+ try {
+ render("a");
+ reportResize(320, 40);
+ render("b");
+ runFrames();
+ expect(host.querySelector('[data-testid="box"]')?.textContent).toBe("300x40");
+ } finally {
+ act(() => root.unmount());
+ host.remove();
+ vi.useRealTimers();
+ vi.restoreAllMocks();
+ globalThis.ResizeObserver = originalResizeObserver;
+ }
+});
+
it("does not re-render the strip when the observer reports the size it already holds", () => {
const originalResizeObserver = globalThis.ResizeObserver;
globalThis.ResizeObserver = MockResizeObserver as unknown as typeof ResizeObserver;
+ const runFrames = nextFrames();
const host = document.createElement("div");
document.body.append(host);
Object.defineProperty(host, "clientWidth", { configurable: true, value: 300 });
@@ -45,14 +161,18 @@ it("does not re-render the strip when the observer reports the size it already h
const settled = stripRenders;
act(() => reportResize(300, 40));
+ runFrames();
expect(stripRenders).toBe(settled);
act(() => reportResize(320, 40));
+ runFrames();
expect(stripRenders).toBe(settled + 1);
expect(host.textContent).toBe("320x40");
} finally {
act(() => root.unmount());
host.remove();
+ vi.useRealTimers();
+ vi.restoreAllMocks();
globalThis.ResizeObserver = originalResizeObserver;
}
});
@@ -60,6 +180,7 @@ it("does not re-render the strip when the observer reports the size it already h
it("measures a strip once a zoom preview ends, not while the preview scales it", () => {
const originalResizeObserver = globalThis.ResizeObserver;
globalThis.ResizeObserver = MockResizeObserver as unknown as typeof ResizeObserver;
+ const runFrames = nextFrames();
const host = document.createElement("div");
document.body.append(host);
let width = 300;
@@ -75,6 +196,7 @@ it("measures a strip once a zoom preview ends, not while the preview scales it",
zoom.previewing = true;
width = 600;
act(() => reportResize(600, 40));
+ runFrames();
expect(host.textContent).toBe("300x40");
zoom.previewing = false;
act(() => zoom.listeners.forEach((listener) => listener()));
@@ -83,6 +205,7 @@ it("measures a strip once a zoom preview ends, not while the preview scales it",
zoom.previewing = false;
act(() => root.unmount());
host.remove();
+ vi.useRealTimers();
globalThis.ResizeObserver = originalResizeObserver;
}
});
diff --git a/packages/studio/src/hooks/useThumbnailStripSize.ts b/packages/studio/src/hooks/useThumbnailStripSize.ts
index ba44f4faa9..76860622a4 100644
--- a/packages/studio/src/hooks/useThumbnailStripSize.ts
+++ b/packages/studio/src/hooks/useThumbnailStripSize.ts
@@ -152,13 +152,20 @@ const measure = (entries: { target: Element; size?: { width: number; height: num
const onPresence = (entries: IntersectionObserverEntry[]) =>
measure(entries.filter((entry) => entry.isIntersecting));
-const onResize = (entries: ResizeObserverEntry[]) =>
- measure(
- entries.map(({ target, contentRect: { width, height } }) => ({
- target,
- size: { width, height },
- })),
- );
+const pendingSizes = new Map<Element, { width: number; height: number }>();
+let pendingSizesFrame = 0;
+const applyPendingSizes = () => {
+ pendingSizesFrame = 0;
+ const entries = [...pendingSizes].map(([target, size]) => ({ target, size }));
+ pendingSizes.clear();
+ measure(entries);
+};
+
+const applyResizesNextFrame = (entries: ResizeObserverEntry[]) => {
+ for (const { target, contentRect } of entries)
+ pendingSizes.set(target, { width: contentRect.width, height: contentRect.height });
+ pendingSizesFrame ||= requestAnimationFrame(applyPendingSizes);
+};
const observeIntersections = (callback: IntersectionObserverCallback) =>
typeof IntersectionObserver === "undefined"
@@ -168,7 +175,7 @@ const observeIntersections = (callback: IntersectionObserverCallback) =>
function acquire() {
if (users++ === 0) {
shared = {
- resize: new ResizeObserver(onResize),
+ resize: new ResizeObserver(applyResizesNextFrame),
presence: observeIntersections(onPresence),
gaps: observeIntersections(
(entries) => entries.some((entry) => entry.isIntersecting) && scheduleRefresh(),
@@ -190,6 +197,9 @@ function release() {
window.removeEventListener("scroll", scheduleRefresh, { capture: true });
cancelAnimationFrame(frame);
frame = 0;
+ cancelAnimationFrame(pendingSizesFrame);
+ pendingSizesFrame = 0;
+ pendingSizes.clear();
}
const watchGap = (gap: HTMLDivElement | null) => {
@@ -234,6 +244,7 @@ export function useThumbnailStripSize() {
resize.unobserve(target);
presence?.unobserve(target);
strips.delete(target);
+ pendingSizes.delete(target);
release();
};
}, []);
diff --git a/packages/studio/src/player/components/CompositionThumbnail.test.ts b/packages/studio/src/player/components/CompositionThumbnail.test.ts
index 88d7826fbc..2ca8f10c1b 100644
--- a/packages/studio/src/player/components/CompositionThumbnail.test.ts
+++ b/packages/studio/src/player/components/CompositionThumbnail.test.ts
@@ -47,6 +47,7 @@ beforeEach(() => {
afterEach(() => {
act(() => root?.unmount());
root = null;
+ vi.useRealTimers();
thumbnailScheduler.invalidateProject("/api/projects/demo/preview");
globalThis.ResizeObserver = originalResizeObserver;
globalThis.Image = originalImage;
@@ -175,7 +176,9 @@ describe("CompositionThumbnail", () => {
await new Promise((resolve) => setTimeout(resolve, 0));
});
+ vi.useFakeTimers({ toFake: ["requestAnimationFrame", "cancelAnimationFrame"] });
act(() => reportResize(500, 40));
+ act(() => vi.advanceTimersToNextFrame());
expect(host.querySelector("img")?.parentElement?.style.width).toBe("108px");
});
diff --git a/packages/studio/src/player/components/ImageThumbnail.test.tsx b/packages/studio/src/player/components/ImageThumbnail.test.tsx
index b8be338ce9..59b0d7faf7 100644
--- a/packages/studio/src/player/components/ImageThumbnail.test.tsx
+++ b/packages/studio/src/player/components/ImageThumbnail.test.tsx
@@ -2,7 +2,7 @@
import React, { act } from "react";
import { createRoot, type Root } from "react-dom/client";
-import { afterEach, beforeEach, describe, expect, it } from "vitest";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { MockResizeObserver, reportResize } from "../../hooks/resizeObserverTestUtils";
import { thumbnailScheduler } from "../lib/thumbnailScheduler";
import { ImageThumbnail } from "./ImageThumbnail";
@@ -64,6 +64,7 @@ beforeEach(() => {
afterEach(() => {
act(() => root?.unmount());
root = null;
+ vi.useRealTimers();
thumbnailScheduler.invalidateProject("p");
globalThis.IntersectionObserver = originalIO;
globalThis.ResizeObserver = originalRO;
@@ -187,7 +188,9 @@ describe("ImageThumbnail", () => {
probe.onload?.();
});
+ vi.useFakeTimers({ toFake: ["requestAnimationFrame", "cancelAnimationFrame"] });
act(() => reportResize(500, 40));
+ act(() => vi.advanceTimersToNextFrame());
expect(host.querySelector("img")?.parentElement?.style.width).toBe("108px");
});
diff --git a/packages/studio/src/player/components/VideoThumbnail.test.tsx b/packages/studio/src/player/components/VideoThumbnail.test.tsx
index d9103555f4..b135011f8f 100644
--- a/packages/studio/src/player/components/VideoThumbnail.test.tsx
+++ b/packages/studio/src/player/components/VideoThumbnail.test.tsx
@@ -23,6 +23,7 @@ beforeEach(() => {
});
afterEach(() => {
+ vi.useRealTimers();
thumbnailScheduler.invalidateProject("p");
vi.clearAllMocks();
});
@@ -243,8 +244,10 @@ describe("VideoThumbnail", () => {
});
await render(0, 0);
+ vi.useFakeTimers({ toFake: ["requestAnimationFrame", "cancelAnimationFrame"] });
+ act(() => reportResize(500, 40));
await act(async () => {
- reportResize(500, 40);
+ vi.advanceTimersToNextFrame();
await Promise.resolve();
});
@@ -264,7 +267,9 @@ describe("VideoThumbnail", () => {
describe("VideoThumbnail during a zoom", () => {
beforeEach(() => {
- vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
+ vi.useFakeTimers({
+ toFake: ["setTimeout", "clearTimeout", "requestAnimationFrame", "cancelAnimationFrame"],
+ });
vi.mocked(decodeVideoThumbnail).mockResolvedValue({
value: { kind: "filmstrip", urls: ["blob:a", "blob:b"], aspect: 16 / 9 },
weight: 256,
@@ -308,6 +313,7 @@ describe("VideoThumbnail during a zoom", () => {
vi.mocked(decodeVideoThumbnail).mockImplementation(() => new Promise(() => {}));
await act(async () => {
reportResize(880, 40);
+ vi.advanceTimersToNextFrame();
await Promise.resolve();
});
await rest();
@@ -326,6 +332,7 @@ describe("VideoThumbnail during a zoom", () => {
vi.mocked(decodeVideoThumbnail).mockImplementation(() => new Promise(() => {}));
await act(async () => {
reportResize(880, 40);
+ vi.advanceTimersToNextFrame();
root.render(clip(undefined, "overscan"));
await Promise.resolve();
});
@@ -371,6 +378,7 @@ describe("VideoThumbnail during a zoom", () => {
vi.mocked(decodeVideoThumbnail).mockImplementation(() => new Promise(() => {}));
await act(async () => {
reportResize(880, 40);
+ vi.advanceTimersToNextFrame();
await Promise.resolve();
});
await rest();
@@ -411,6 +419,7 @@ describe("VideoThumbnail during a zoom", () => {
await render(440);
await act(async () => {
reportResize(50, 40);
+ vi.advanceTimersToNextFrame();
await Promise.resolve();
});
await rest();
@@ -419,6 +428,7 @@ describe("VideoThumbnail during a zoom", () => {
vi.mocked(decodeVideoThumbnail).mockImplementation(() => new Promise(() => {}));
await act(async () => {
reportResize(880, 40);
+ vi.advanceTimersToNextFrame();
await Promise.resolve();
});
await rest();
@@ -431,6 +441,7 @@ describe("VideoThumbnail during a zoom", () => {
await act(async () => {
markTimelineMotion();
reportResize(880, 40);
+ vi.advanceTimersToNextFrame();
await Promise.resolve();
});
expect(decodeVideoThumbnail).not.toHaveBeenCalled();