Skip to content

Commit 03e72a2

Browse files
Matej Ukmarclaude
andcommitted
fix(web): chat videos keep their ratio under a one-sided Markdown size
A Markdown width or height alone now only bounds the player, so the video keeps its own ratio instead of falling back to 16:9. A loaded size belongs to the video it came from, so a different video in the same slot starts from its own size. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 079f45b commit 03e72a2

2 files changed

Lines changed: 63 additions & 6 deletions

File tree

‎apps/web/src/components/chat/ChatMarkdownMedia.browser.test.tsx‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import { describe, expect, it, vi } from "vite-plus/test";
44

55
import { act, render, stubGlobal } from "../../../test/render";
66
import "../../index.css";
7-
import { ChatMarkdownAssetImage, ChatMarkdownVideo } from "./ChatMarkdownMedia";
7+
import {
8+
authoredImageSizeStyle,
9+
ChatMarkdownAssetImage,
10+
ChatMarkdownVideo,
11+
} from "./ChatMarkdownMedia";
812

913
// Every evaluation of the URL query is one mint: the first read, then one per refresh.
1014
const assetQuery = vi.hoisted(() => ({ mints: 0 }));
@@ -120,5 +124,36 @@ describe("ChatMarkdownVideo", () => {
120124

121125
const box = video.getBoundingClientRect();
122126
expect({ width: box.width, height: box.height }).toEqual({ width: 270, height: 480 });
127+
128+
// A different video in the same slot starts from its own size, not the last one's.
129+
await screen.rerender(
130+
<ChatMarkdownVideo
131+
src="/asset/landscape.mp4"
132+
alt="landscape"
133+
copyMarkdown={undefined}
134+
knownSize={{ width: 1920, height: 1080 }}
135+
/>,
136+
);
137+
const next = screen.getByLabelText("landscape").element().getBoundingClientRect();
138+
expect({ width: next.width, height: next.height }).toEqual({ width: 414, height: 232.875 });
139+
});
140+
141+
it("keeps a video's own ratio when Markdown gives only a width", async () => {
142+
using _observer = stubGlobal("IntersectionObserver", HiddenIntersectionObserver);
143+
await using screen = await render(
144+
<ChatMarkdownVideo
145+
src="/asset/portrait.mp4"
146+
alt="portrait"
147+
copyMarkdown={undefined}
148+
style={authoredImageSizeStyle(200, undefined)}
149+
knownSize={{ width: 1080, height: 1920 }}
150+
/>,
151+
);
152+
153+
const box = screen.getByLabelText("portrait").element().getBoundingClientRect();
154+
expect({ width: box.width, height: Math.round(box.height) }).toEqual({
155+
width: 200,
156+
height: 356,
157+
});
123158
});
124159
});

‎apps/web/src/components/chat/ChatMarkdownMedia.tsx‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,24 @@ export function ChatMarkdownImage(props: {
275275
);
276276
}
277277

278+
/** A Markdown width or height alone only bounds a video; both together replace its own ratio. */
279+
function videoSizeStyle(
280+
size: { readonly width: number; readonly height: number } | undefined,
281+
authored: CSSProperties | undefined,
282+
): CSSProperties | undefined {
283+
if (!size) return authored;
284+
const own = authoredImageSizeStyle(size.width, size.height);
285+
if (!own || authored?.aspectRatio !== undefined) return authored ?? own;
286+
const bounds = [
287+
own.maxWidth,
288+
authored?.maxWidth,
289+
authored?.maxHeight === undefined
290+
? undefined
291+
: `calc(${authored.maxHeight} * ${size.width / size.height})`,
292+
].filter((bound) => bound !== undefined);
293+
return { ...own, maxWidth: `min(${bounds.join(", ")})` };
294+
}
295+
278296
export function ChatMarkdownVideo(props: {
279297
readonly src: string | null;
280298
readonly alt: string;
@@ -288,18 +306,22 @@ export function ChatMarkdownVideo(props: {
288306
/** The server's read of the file header; the loaded video's own size replaces it. */
289307
readonly knownSize?: { readonly width: number; readonly height: number } | undefined;
290308
}) {
291-
const [videoSize, setVideoSize] = useState<{ width: number; height: number } | null>(null);
292-
const size = videoSize ?? props.knownSize;
293-
const style = props.style ?? (size ? authoredImageSizeStyle(size.width, size.height) : undefined);
309+
const identity = props.mediaIdentity ?? props.copyMarkdown ?? props.src;
310+
const [loaded, setLoaded] = useState<{
311+
identity: typeof identity;
312+
size: { width: number; height: number };
313+
} | null>(null);
314+
const size = (loaded?.identity === identity ? loaded.size : undefined) ?? props.knownSize;
315+
const style = videoSizeStyle(size, props.style);
294316
return (
295317
<MediaVideoPlayer
296-
key={props.mediaIdentity ?? props.copyMarkdown ?? props.src}
318+
key={identity}
297319
src={props.src}
298320
sourceFailed={props.sourceFailed}
299321
label={props.alt}
300322
originalUrl={props.originalUrl}
301323
style={style}
302-
onVideoSize={setVideoSize}
324+
onVideoSize={(videoSize) => setLoaded({ identity, size: videoSize })}
303325
copyMarkdown={props.copyMarkdown}
304326
className={cn(
305327
CHAT_MARKDOWN_MEDIA_LAYOUT_CLASS_NAME,

0 commit comments

Comments
 (0)