diff --git a/apps/web/src/components/media/MediaPlayer.test.tsx b/apps/web/src/components/media/MediaPlayer.test.tsx index 0be2403..c1d9f5a 100644 --- a/apps/web/src/components/media/MediaPlayer.test.tsx +++ b/apps/web/src/components/media/MediaPlayer.test.tsx @@ -66,6 +66,21 @@ describe("MediaPlayer", () => { expect(video.hasAttribute("controls")).toBe(true); }); + it("sizes the frame from the file when the block has no dimensions", async () => { + await act(async () => + root.render(), + ); + const frame = container.querySelector('[data-testid="media-video"]')!; + expect(frame.style.width).toBe("420px"); + const video = container.querySelector("video")!; + Object.defineProperty(video, "videoWidth", { value: 1080 }); + Object.defineProperty(video, "videoHeight", { value: 1920 }); + await act(async () => { + video.dispatchEvent(new Event("loadedmetadata")); + }); + expect(frame.style.width).toBe("270px"); + }); + it("falls back to a download link when the browser cannot play the file", async () => { await act(async () => root.render(), diff --git a/apps/web/src/components/media/MediaPlayer.tsx b/apps/web/src/components/media/MediaPlayer.tsx index d793ba4..0df489d 100644 --- a/apps/web/src/components/media/MediaPlayer.tsx +++ b/apps/web/src/components/media/MediaPlayer.tsx @@ -28,6 +28,7 @@ export function MediaPlayer({ const container = useRef(null); const near = useNearViewport(container); const [failed, setFailed] = useState(false); + const [frame, setFrame] = useState<{ width: number; height: number } | null>(null); const src = near ? artifactUrl(artifactId) : undefined; if (failed) return ; @@ -48,7 +49,8 @@ export function MediaPlayer({ ); } - const box = videoBox(width, height); + // Uploads carry no dimensions; size the frame from the file once its metadata loads. + const box = videoBox(width ?? frame?.width, height ?? frame?.height); return (
setFailed(true)} + onLoadedMetadata={(event) => { + const { videoWidth, videoHeight } = event.currentTarget; + if (videoWidth > 0 && videoHeight > 0) { + setFrame({ width: videoWidth, height: videoHeight }); + } + }} />
);