TypeScript
fix(web): wide chat tables fit the lane when cells wrap (#20878)
Mantas Kantautasf8b06c5fd0b4
diff --git a/apps/web/src/components/ChatMarkdown.browser.test.tsx b/apps/web/src/components/ChatMarkdown.browser.test.tsxindex f29ee18bd92..ed0822530e6 100644--- a/apps/web/src/components/ChatMarkdown.browser.test.tsx+++ b/apps/web/src/components/ChatMarkdown.browser.test.tsx@@ -7,6 +7,8 @@ import { createRoot, type Root } from "react-dom/client";import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";import { page, userEvent } from "vite-plus/test/browser";+import "../index.css";+import { filesystemEnvironment } from "../state/filesystem";import { useRightPanelStore } from "../rightPanelStore";import { getSyntaxHighlighterPromise } from "../lib/syntaxHighlighting";@@ -129,6 +131,64 @@ function fileOptions(container: HTMLElement) {.filter((label) => label?.startsWith("File options for "));}+describe("ChatMarkdown table layout", () => {+ it.each([+ { width: 36, timeline: true, fits: true },+ { width: 48, timeline: true, fits: true },+ { width: 35, timeline: true, fits: false },+ { width: 48, timeline: false, fits: false },+ ])(+ "wraps expanded cells with fit=$fits in a $width rem container (timeline=$timeline)",+ async ({ width, timeline, fits }) => {+ const { container } = await render(+ <div style={{ width: `${width}rem` }}>+ <div className={timeline ? "@container/chat-lane chat-content-lane overflow-x-clip" : ""}>+ <ChatMarkdown+ cwd={undefined}+ text={[+ "| Change summary | Reason details | Verification status | Release impact | Followup actions |",+ "| --- | --- | --- | --- | --- |",+ "| Keep the complete explanation readable when the table is expanded in the conversation. | Allow each column to wrap its prose within the available space in the current chat lane. | Collapse and expand the table again to check that the text still fits and remains readable. | Keep the existing layout for narrow chat lanes and file previews. | Check the table again after resizing the conversation lane. |",+ ].join("\n")}+ />+ </div>+ </div>,+ );+ const table = container.querySelector("table")!;+ const viewport = table.closest<HTMLElement>('[data-slot="scroll-area-viewport"]')!;+ const cell = table.tBodies[0]!.rows[0]!.cells[0]!;+ const collapse = () => buttonsLabelled(container, "Collapse table cells")[0];+ if (collapse()) await act(async () => collapse()!.click());++ const collapsedWidth = table.getBoundingClientRect().width;+ const collapsedHeight = cell.getBoundingClientRect().height;+ expect(viewport.clientWidth).toBeGreaterThan(0);+ expect(viewport.scrollWidth).toBeGreaterThan(viewport.clientWidth);+ expect(cell.scrollWidth).toBeGreaterThan(cell.clientWidth);++ await act(async () => buttonsLabelled(container, "Expand table cells")[0]!.click());+ const expandedWidth = table.getBoundingClientRect().width;+ const expandedHeight = cell.getBoundingClientRect().height;+ if (fits) {+ expect(viewport.scrollWidth).toBeLessThanOrEqual(viewport.clientWidth + 1);+ expect(expandedWidth).toBeLessThanOrEqual(viewport.clientWidth + 1);+ } else {+ expect(viewport.scrollWidth).toBeGreaterThan(viewport.clientWidth);+ viewport.scrollLeft = 100;+ expect(viewport.scrollLeft).toBeGreaterThan(0);+ }+ expect(expandedHeight).toBeGreaterThan(collapsedHeight);++ await act(async () => collapse()!.click());+ expect(table.getBoundingClientRect().width).toBeCloseTo(collapsedWidth, 0);+ expect(cell.getBoundingClientRect().height).toBeCloseTo(collapsedHeight, 0);+ await act(async () => buttonsLabelled(container, "Expand table cells")[0]!.click());+ expect(table.getBoundingClientRect().width).toBeCloseTo(expandedWidth, 0);+ expect(cell.getBoundingClientRect().height).toBeCloseTo(expandedHeight, 0);+ },+ );+});+describe("ChatMarkdown bare anchor placeholders", () => {it.each(["<A>", "<a>", "<a >", "<a/>", "<A/>", "<a />"])("preserves unmatched %s without linking later blocks",diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsxindex e7cbf2aea36..67fc934e840 100644--- a/apps/web/src/components/ChatMarkdown.tsx+++ b/apps/web/src/components/ChatMarkdown.tsx@@ -639,30 +639,6 @@ function MarkdownTable({ children, ...props }: React.ComponentProps<"table">) {return observeResize(table, measureClippedCells);}, [measureClippedCells]);- function toggleExpanded() {- const table = tableRef.current;- if (!table) return;-- if (!expanded) {- const rows = [...table.rows];- const columnWidths = rows.reduce<number[]>((widths, row) => {- [...row.cells].forEach((cell, columnIndex) => {- widths[columnIndex] = Math.max(- widths[columnIndex] ?? 0,- cell.getBoundingClientRect().width,- );- });- return widths;- }, []);-- [...(table.tHead?.rows[0]?.cells ?? [])].forEach((cell, columnIndex) => {- cell.style.minWidth = `${columnWidths[columnIndex] ?? cell.getBoundingClientRect().width}px`;- });- }-- setExpanded((value) => !value);- }-const handleCopy = (format: "markdown" | "csv") => {const table = containerRef.current?.querySelector("table");if (!table || typeof navigator === "undefined" || navigator.clipboard == null) {@@ -720,7 +696,7 @@ function MarkdownTable({ children, ...props }: React.ComponentProps<"table">) {variant={expanded ? "secondary" : "ghost-muted"}size="icon-xs"aria-pressed={expanded}- onClick={toggleExpanded}+ onClick={() => setExpanded((value) => !value)}aria-label={expandLabel}/>}diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsxindex 6f62eecd27e..8cdd28652e9 100644--- a/apps/web/src/components/chat/MessagesTimeline.tsx+++ b/apps/web/src/components/chat/MessagesTimeline.tsx@@ -1425,7 +1425,10 @@ const ConversationTimeline = memo(function ConversationTimeline({const renderItem = ({ item }: { item: MessagesTimelineRow }) => (<TimelineRowCtx value={sharedState}><div className="messages-timeline-row-frame">- <div className="chat-content-lane overflow-x-clip" data-timeline-root="true">+ <div+ className="@container/chat-lane chat-content-lane overflow-x-clip"+ data-timeline-root="true"+ ><TimelineRowContent row={item} /></div></div>diff --git a/apps/web/src/index.css b/apps/web/src/index.cssindex 299fff4cbf3..e1096171a09 100644--- a/apps/web/src/index.css+++ b/apps/web/src/index.css@@ -2147,6 +2147,22 @@ code {overflow-wrap: anywhere;}+/* Fit expanded tables to wide timeline lanes; narrow lanes keep horizontal+ scrolling so prose columns remain readable. */+@container chat-lane (min-width: 36rem) {+ .chat-markdown .chat-markdown-table-container[data-expanded="true"] table {+ min-width: 0;+ }++ .chat-markdown .chat-markdown-table-container[data-expanded="true"] th {+ white-space: normal;+ }++ .chat-markdown .chat-markdown-table-container[data-expanded="true"] td {+ overflow-wrap: break-word;+ }+}+/* Formula cells keep their natural width and scroll with the table. */.chat-markdown .chat-markdown-table-container td:has([data-markdown-math]) {max-width: none;