From 9af8d11ebc5516a7649af967af8d77735a03301d Mon Sep 17 00:00:00 2001 From: mike Date: Sat, 14 Nov 2026 15:39:35 -0500 Subject: [PATCH 1/2] feat(web): wide chat tables stay readable A 7-column table in a reply was squeezed into the 46rem reading column, so its cells wrapped into tall, narrow blocks (3102px tall at 1920px wide). - Each column keeps a floor of its longest cell, up to 16ch, so a table with many long columns scrolls instead of collapsing. Tables that fit are unchanged. - In assistant replies, a wide table grows past the reading column into the free pane width, centred on it. Prose keeps the chat width. - A scrolled table keeps its first column in view. - Tables that scroll get an Expand button that opens a near full-screen view with a sticky header row, a sticky first column, and the copy menu. Co-Authored-By: Claude Opus 5.5 --- apps/web/src/components/ChatMarkdown.tsx | 5 +- .../web/src/components/chat/MarkdownTable.tsx | 128 ++++++++++++++---- .../components/chat/TimelineAssistantRows.tsx | 2 +- .../chat/markdownTableLayout.logic.test.ts | 56 ++++++++ .../components/chat/markdownTableLayout.ts | 47 +++++++ apps/web/src/index.css | 82 ++++++++++- 6 files changed, 289 insertions(+), 31 deletions(-) create mode 100644 apps/web/src/components/chat/markdownTableLayout.logic.test.ts create mode 100644 apps/web/src/components/chat/markdownTableLayout.ts diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsx index 7e0b87e11ad39..edc90c946d519 100644 --- a/apps/web/src/components/ChatMarkdown.tsx +++ b/apps/web/src/components/ChatMarkdown.tsx @@ -203,6 +203,7 @@ import { createMarkdownRenderer } from "./chat/markdownRenderer"; import { splitMarkdownBlocks } from "./chat/markdownStreamBlocks"; import { MarkdownMermaidCodeBlock } from "./chat/MarkdownMermaidCodeBlock"; import { MarkdownTable } from "./chat/MarkdownTable"; +import { markdownTableMinWidth } from "./chat/markdownTableLayout"; import { MarkdownDetails } from "./chat/MarkdownDetails"; import { authoredImageSizeStyle, @@ -1664,8 +1665,8 @@ const CHAT_MARKDOWN_COMPONENTS = { } return ; }, - table: function MarkdownTableRenderer({ node: _node, ...props }) { - return ; + table: function MarkdownTableRenderer({ node, ...props }) { + return ; }, details: function MarkdownDetailsRenderer({ node: _node, children, open: detailsOpen }) { return {children}; diff --git a/apps/web/src/components/chat/MarkdownTable.tsx b/apps/web/src/components/chat/MarkdownTable.tsx index 6bb72ff5ca578..ffe33d866e9cb 100644 --- a/apps/web/src/components/chat/MarkdownTable.tsx +++ b/apps/web/src/components/chat/MarkdownTable.tsx @@ -1,5 +1,5 @@ -import { CheckIcon, CopyIcon } from "lucide-react"; -import { useRef, type ComponentProps } from "react"; +import { CheckIcon, CopyIcon, Maximize2Icon } from "lucide-react"; +import { useRef, useState, type ComponentProps, type CSSProperties } from "react"; import { useCopyToClipboard } from "../../hooks/useCopyToClipboard"; import { @@ -7,13 +7,25 @@ import { serializeTableElementToMarkdown, } from "../../markdown-clipboard"; import { Button } from "../ui/button"; +import { Dialog, DialogPopup, DialogTitle } from "../ui/dialog"; import { Menu, MenuItem, MenuPopup, MenuTrigger } from "../ui/menu"; import { ScrollArea } from "../ui/scroll-area"; import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; import { reportMarkdownActionFailure } from "./markdownActionFailure"; +import type { markdownTableMinWidth } from "./markdownTableLayout"; -export function MarkdownTable({ children, ...props }: ComponentProps<"table">) { - const containerRef = useRef(null); +type MarkdownTableProps = ComponentProps<"table"> & { + minWidth?: ReturnType; +}; + +function TableCopyMenu({ + getTable, + labeled = false, +}: { + getTable: () => HTMLTableElement | null | undefined; + /** A text button instead of an icon. Inside the expanded dialog a tooltip would take one Escape press of its own. */ + labeled?: boolean; +}) { const { copyToClipboard, isCopied: copied } = useCopyToClipboard<"markdown" | "csv">({ timeout: 1200, onError: (cause, format) => @@ -22,7 +34,7 @@ export function MarkdownTable({ children, ...props }: ComponentProps<"table">) { const copyLabel = copied ? "Copied" : "Copy table"; const handleCopy = (format: "markdown" | "csv") => { - const table = containerRef.current?.querySelector("table"); + const table = getTable(); if (!table) return; copyToClipboard( format === "markdown" @@ -32,39 +44,103 @@ export function MarkdownTable({ children, ...props }: ComponentProps<"table">) { ); }; + const icon = copied ? : ; + return ( + + {labeled ? ( + }> + {icon} + {copied ? "Copied" : "Copy"} + + ) : ( + + + } + /> + } + > + {icon} + + {copyLabel} + + )} + + handleCopy("markdown")}>Copy as Markdown + handleCopy("csv")}>Copy as CSV + + + ); +} + +export function MarkdownTable({ children, minWidth, style, ...props }: MarkdownTableProps) { + const containerRef = useRef(null); + const expandedRef = useRef(null); + const [expanded, setExpanded] = useState(false); + const tableStyle = minWidth + ? ({ + ...style, + "--markdown-table-floor": `calc(${minWidth.ch}ch + ${minWidth.columns - 1} * 1.5rem)`, + } as CSSProperties) + : style; + return (
- - {children}
-
+
+ + + {children} +
+
+
{/* Stays within the row's px-1: the chat lane clips sideways overflow. */} -
- +
+ - } +
+ + containerRef.current?.querySelector("table")} />
+ + +
+ Table +
+ expandedRef.current?.querySelector("table")} /> +
+
+
+ + {children} +
+
+
+
); } diff --git a/apps/web/src/components/chat/TimelineAssistantRows.tsx b/apps/web/src/components/chat/TimelineAssistantRows.tsx index 1d03161e83014..429192e4a8af3 100644 --- a/apps/web/src/components/chat/TimelineAssistantRows.tsx +++ b/apps/web/src/components/chat/TimelineAssistantRows.tsx @@ -48,7 +48,7 @@ export function AssistantTimelineRow({ row }: { row: Extract
T3 Code -
+
node.type === "element" && node.tagName === "table", + ); + if (!table) throw new Error("no table"); + return table; +} + +describe("markdownTableMinWidth", () => { + it("keeps short columns at their longest cell and caps long ones at the floor", () => { + const table = tableFrom( + [ + "| # | Approach | Notes |", + "| - | --- | --- |", + "| 1 | Breakout | Grows past the reading column into the free pane width |", + "| 12 | Scroll | **Keeps** the column |", + ].join("\n"), + ); + expect(markdownTableMinWidth(table)).toEqual({ + ch: 2 + "Breakout".length + MARKDOWN_TABLE_COLUMN_FLOOR_CH, + columns: 3, + }); + }); + + it("sums a floor per column, so many long columns outgrow a narrow lane", () => { + const cell = "a long explanation that would otherwise wrap into a tall block"; + const row = `| ${Array.from({ length: 7 }, () => cell).join(" | ")} |`; + const table = tableFrom( + [ + `| ${Array.from({ length: 7 }, (_, index) => `Column ${index}`).join(" | ")} |`, + `| ${Array.from({ length: 7 }, () => "---").join(" | ")} |`, + row, + ].join("\n"), + ); + expect(markdownTableMinWidth(table)).toEqual({ + ch: 7 * MARKDOWN_TABLE_COLUMN_FLOOR_CH, + columns: 7, + }); + }); + + it("returns null without a table", () => { + expect(markdownTableMinWidth(undefined)).toBeNull(); + }); +}); diff --git a/apps/web/src/components/chat/markdownTableLayout.ts b/apps/web/src/components/chat/markdownTableLayout.ts new file mode 100644 index 0000000000000..ed6f8c9bc12ef --- /dev/null +++ b/apps/web/src/components/chat/markdownTableLayout.ts @@ -0,0 +1,47 @@ +import type { Element, ElementContent } from "hast"; + +/** Widest a column's floor gets, in characters; longer cells wrap above it. */ +export const MARKDOWN_TABLE_COLUMN_FLOOR_CH = 16; + +function textLength(node: ElementContent): number { + if (node.type === "text") return node.value.trim().length; + if (node.type !== "element") return 0; + let length = 0; + for (const child of node.children) length += textLength(child); + return length; +} + +function* tableRows(node: Element): Generator { + for (const child of node.children) { + if (child.type !== "element") continue; + if (child.tagName === "tr") yield child; + else if (child.tagName === "thead" || child.tagName === "tbody" || child.tagName === "tfoot") { + yield* tableRows(child); + } + } +} + +/** + * The narrowest a table can get before its cells turn into tall, hard-to-scan + * blocks: each column keeps room for its longest cell, up to the floor. Narrow + * columns such as numbers keep their natural width. Returns the summed floor in + * `ch` and the column count, or null when the table has no cells. + */ +export function markdownTableMinWidth( + table: Element | undefined, +): { readonly ch: number; readonly columns: number } | null { + if (!table) return null; + const longest: number[] = []; + for (const row of tableRows(table)) { + let column = 0; + for (const cell of row.children) { + if (cell.type !== "element" || (cell.tagName !== "th" && cell.tagName !== "td")) continue; + longest[column] = Math.max(longest[column] ?? 0, textLength(cell)); + column += 1; + } + } + if (longest.length === 0) return null; + let ch = 0; + for (const length of longest) ch += Math.min(length, MARKDOWN_TABLE_COLUMN_FLOOR_CH); + return { ch, columns: longest.length }; +} diff --git a/apps/web/src/index.css b/apps/web/src/index.css index 0f9942e3295ee..4987e047dfbc8 100644 --- a/apps/web/src/index.css +++ b/apps/web/src/index.css @@ -2258,10 +2258,12 @@ code { /* Diagnostics-style tables: row separators only, and a scroll-fade container for horizontal overflow. Cells wrap and the table fits its lane. The root chat-markdown wrapping rules (overflow-wrap: anywhere) would let columns - shrink to single characters, so restore word-boundary wrapping: the min - column width is the longest word, and only wider content scrolls. */ + shrink to single characters, so restore word-boundary wrapping. Each column + also keeps a floor (MarkdownTable's --markdown-table-floor) so many long + columns scroll instead of collapsing into tall, narrow blocks. */ .chat-markdown table { width: 100%; + min-width: var(--markdown-table-floor, 0); border-collapse: collapse; overflow-wrap: normal; word-break: normal; @@ -2308,6 +2310,82 @@ code { max-width: none; } +/* Expand only helps a table that does not fit. */ +.chat-markdown-table-container:not(:has([data-has-overflow-x])) .chat-markdown-table-expand { + display: none; +} + +/* A scrolled table keeps its first column in view. The left edge fade would + dim that column, so only the right edge fades. */ +.chat-markdown-table-container [data-has-overflow-x] { + --tw-mask-left: linear-gradient(#000, #000); +} + +.chat-markdown-table-container [data-has-overflow-x] :is(th, td):first-child, +.chat-markdown-table-expanded :is(th, td):first-child { + position: sticky; + inset-inline-start: 0; + z-index: 1; + background: var(--background); + box-shadow: inset -1px 0 0 color-mix(in srgb, var(--contrast-border) 60%, transparent); +} + +.chat-markdown-table-expanded thead th { + position: sticky; + top: 0; + z-index: 2; + background: var(--background); + box-shadow: inset 0 -1px 0 color-mix(in srgb, var(--contrast-border) 60%, transparent); +} + +.chat-markdown-table-expanded thead th:first-child { + z-index: 3; + box-shadow: + inset -1px 0 0 color-mix(in srgb, var(--contrast-border) 60%, transparent), + inset 0 -1px 0 color-mix(in srgb, var(--contrast-border) 60%, transparent); +} + +/* Edge padding lives on the cells: sticky cells pin to the scroller's padding + edge, so scroller padding would let scrolled columns show beside them. */ +.chat-markdown-table-expanded :is(th, td) { + max-width: 32rem; +} + +.chat-markdown-table-expanded :is(th, td):first-child { + padding-inline-start: 1rem; +} + +.chat-markdown-table-expanded :is(th, td):last-child { + padding-inline-end: 1rem; +} + +/* Assistant replies let a wide table grow past the reading column, centred on + it, up to the timeline row (the pane minus its gutters). Prose stays at the + chat width. Past the row width the table wraps and then scrolls as before. */ +.messages-timeline-row-frame { + container: chat-row / inline-size; +} + +@container chat-row (width > 0) { + .chat-content-lane:has([data-chat-wide-content] .chat-markdown-table-breakout) { + overflow-x: visible; + } + + [data-chat-wide-content] .chat-markdown > .chat-markdown-table-container { + .chat-markdown-table-breakout { + display: flex; + justify-content: center; + } + + .chat-markdown-table-breakout > :first-child { + flex: none; + width: max-content; + min-width: 100%; + max-width: 100cqw; + } + } +} + /* Keep overflow discoverable when the edge fade falls between columns. */ .chat-markdown .chat-markdown-table-container From d15185146473fadded0e964ddf997dfa6e188486 Mon Sep 17 00:00:00 2001 From: Yordis Prieto Date: Mon, 16 Nov 2026 23:28:12 -0500 Subject: [PATCH 2/2] fix(web): table column floors count spaces between formatted words Each inline text node was trimmed on its own, so the spaces between bold or linked words were dropped and the column floor came out too narrow. Signed-off-by: Yordis Prieto --- .../chat/markdownTableLayout.logic.test.ts | 2 ++ .../web/src/components/chat/markdownTableLayout.ts | 14 +++++++------- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/apps/web/src/components/chat/markdownTableLayout.logic.test.ts b/apps/web/src/components/chat/markdownTableLayout.logic.test.ts index 3bce1a5117e36..a3d177d92cfa2 100644 --- a/apps/web/src/components/chat/markdownTableLayout.logic.test.ts +++ b/apps/web/src/components/chat/markdownTableLayout.logic.test.ts @@ -32,6 +32,8 @@ describe("markdownTableMinWidth", () => { ch: 2 + "Breakout".length + MARKDOWN_TABLE_COLUMN_FLOOR_CH, columns: 3, }); + const spaced = tableFrom(["| A |", "| - |", "| **a** **b** **c** **d** |"].join("\n")); + expect(markdownTableMinWidth(spaced)).toEqual({ ch: "a b c d".length, columns: 1 }); }); it("sums a floor per column, so many long columns outgrow a narrow lane", () => { diff --git a/apps/web/src/components/chat/markdownTableLayout.ts b/apps/web/src/components/chat/markdownTableLayout.ts index ed6f8c9bc12ef..8a5e9776141c3 100644 --- a/apps/web/src/components/chat/markdownTableLayout.ts +++ b/apps/web/src/components/chat/markdownTableLayout.ts @@ -3,12 +3,12 @@ import type { Element, ElementContent } from "hast"; /** Widest a column's floor gets, in characters; longer cells wrap above it. */ export const MARKDOWN_TABLE_COLUMN_FLOOR_CH = 16; -function textLength(node: ElementContent): number { - if (node.type === "text") return node.value.trim().length; - if (node.type !== "element") return 0; - let length = 0; - for (const child of node.children) length += textLength(child); - return length; +function textContent(node: ElementContent): string { + if (node.type === "text") return node.value; + if (node.type !== "element") return ""; + let text = ""; + for (const child of node.children) text += textContent(child); + return text; } function* tableRows(node: Element): Generator { @@ -36,7 +36,7 @@ export function markdownTableMinWidth( let column = 0; for (const cell of row.children) { if (cell.type !== "element" || (cell.tagName !== "th" && cell.tagName !== "td")) continue; - longest[column] = Math.max(longest[column] ?? 0, textLength(cell)); + longest[column] = Math.max(longest[column] ?? 0, textContent(cell).trim().length); column += 1; } }