Agent chat: a picture's box keeps the picture's proportions in a narrow chat
Jonas Templestein, 2026-11-28 00:13 UTC · d10c237587f3893e77591fd60d89972562e2c790 · parent bb2dff4
2 files changed
- M src/components/agent-chat/agent-feed-attachment.test.tsx
- M src/components/agent-chat/agent-feed.tsx
M src/components/agent-chat/agent-feed-attachment.test.tsx
@@ -5,7 +5,7 @@ import { renderToStaticMarkup } from "react-dom/server"; import { expect, test } from "vitest"; -import { attachmentBoxHeight, UserMessageBody } from "./agent-feed.tsx"; +import { attachmentBox, UserMessageBody } from "./agent-feed.tsx"; const picture = (path: string) => ({ kind: "user" as const, @@ -16,10 +16,11 @@ files: [{ contentType: "image/png", filename: "shot.png", path, size: 10 }], }); -test("a picture's box is 256 px high until its size is known, then its own height at most 256", () => { - expect(attachmentBoxHeight({})).toBe(256); - expect(attachmentBoxHeight({ width: 400, height: 91 })).toBe(91); - expect(attachmentBoxHeight({ width: 4000, height: 3000 })).toBe(256); +test("a picture's box is its own size, at most 256 px high in its proportions; none while unknown", () => { + expect(attachmentBox({})).toBeUndefined(); + expect(attachmentBox({ width: 400, height: 91 })).toEqual({ width: 400, height: 91 }); + expect(attachmentBox({ width: 2184, height: 552 })).toEqual({ width: 1013, height: 256 }); + expect(attachmentBox({ width: 800, height: 1200 })).toEqual({ width: 171, height: 256 }); }); test("before its address is signed a picture already has its box, and no file chip", () => { @@ -48,11 +49,16 @@ Object.defineProperty(first, "naturalWidth", { value: 400 }); Object.defineProperty(first, "naturalHeight", { value: 91 }); await act(async () => void first.dispatchEvent(new Event("load"))); - expect(host.querySelector("img")!.style.height).toBe("91px"); + const sized = host.querySelector("img")!; + expect([sized.getAttribute("width"), sized.getAttribute("height"), sized.style.height]).toEqual([ + "400", + "91", + "", + ]); await act(async () => root.render(null)); const again = renderToStaticMarkup(<UserMessageBody item={item} signedUrl={signedUrl} />); expect(again).toContain("<img"); - expect(again).toContain("height:91px"); + expect(again).toContain('width="400" height="91"'); expect(signings).toBe(1); });
M src/components/agent-chat/agent-feed.tsx
@@ -1190,9 +1190,20 @@ if (!seen) attachmentsSeen.set(path, (seen = {})); return seen; } -/** The height of a picture's box: its own, at most `ATTACHMENT_MOST_PX`; that most while unknown. */ -export function attachmentBoxHeight(size: { width?: number; height?: number }): number { - return size.height ? Math.min(ATTACHMENT_MOST_PX, size.height) : ATTACHMENT_MOST_PX; +/** A picture's box, from its own size: at most `ATTACHMENT_MOST_PX` high, in its own proportions. + * These are the picture's `width` and `height` attributes, so the browser knows the box before + * the picture loads and makes it narrower (and lower) in a narrow chat. Nothing while the size is + * unknown: the box is then `ATTACHMENT_MOST_PX` high. */ +export function attachmentBox(size: { + width?: number; + height?: number; +}): { width: number; height: number } | undefined { + if (!size.width || !size.height) return undefined; + const scale = Math.min(1, ATTACHMENT_MOST_PX / size.height); + return { + width: Math.max(1, Math.round(size.width * scale)), + height: Math.max(1, Math.round(size.height * scale)), + }; } function MessageAttachment({ @@ -1224,18 +1235,22 @@ return () => void (live = false); }, [file.path, signedUrl]); if (file.contentType.startsWith("image/")) { - const box = { - height: attachmentBoxHeight(size), - ...(size.width && - size.height && { aspectRatio: `${String(size.width)} / ${String(size.height)}` }), - }; - const className = "max-w-full rounded-lg border border-border/60 bg-background object-contain"; + const box = attachmentBox(size); + const className = cn( + "max-w-full rounded-lg border border-border/60 bg-background object-contain", + box && "h-auto", + ); + const unknown = box ? undefined : { height: ATTACHMENT_MOST_PX }; if (!url) return ( <div role="img" aria-label={file.filename} - style={{ ...box, width: box.height }} + style={ + box + ? { width: box.width, aspectRatio: `${String(box.width)} / ${String(box.height)}` } + : { ...unknown, width: ATTACHMENT_MOST_PX } + } className={className} /> ); @@ -1244,7 +1259,9 @@ <img src={url} alt={file.filename} - style={box} + width={box?.width} + height={box?.height} + style={unknown} className={className} onLoad={(event) => { const { naturalWidth: width, naturalHeight: height } = event.currentTarget;