iterate.com / ui

git clone https://git.iterate.com/repos/ui

Agent chat: a picture's box keeps the picture's proportions in a narrow chat #5

merged dash-scroll-images-2 was squashed into main as 9dadff7 by jonas, 2026-11-28 00:14 UTC. jonas opened it 2026-11-28 00:14 UTC.

Follow-up of #4. The box was the picture's height with an aspect ratio, so a wide screenshot in a narrow chat got bands over and under it (seen live after the pin moved: a 2184x552 picture in a box of 834x254). Now the box is the picture's own width and height attributes, scaled to at most 256 px high.

Checked in a headless Chromium at 834 px for 5 sizes: the box before the load, after it, and the look before #4 are the same. pnpm check passes on d10c237 (1,253 tests).

Card: tasks/make-the-dash-scroll-and-render-smoothly-on-large-logs-like.md of /repos/task-board.
Session: claude e7bfeefe-1067-494b-8f63-82177cb1beb9 on beelink

๐Ÿค– Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_0158EjAGDfCugWqZ9a2JTTgL

Conversation

Nobody has said anything yet.

Sign in to comment or to merge.

Files

2 files changed

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;