Skip to content

Commit f8b06c5

Browse files
fix(web): wide chat tables fit the lane when cells wrap (pingdotgg#20878)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 321b86b commit f8b06c5

4 files changed

Lines changed: 81 additions & 26 deletions

File tree

‎apps/web/src/components/ChatMarkdown.browser.test.tsx‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { createRoot, type Root } from "react-dom/client";
77
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
88
import { page, userEvent } from "vite-plus/test/browser";
99

10+
import "../index.css";
11+
1012
import { filesystemEnvironment } from "../state/filesystem";
1113
import { useRightPanelStore } from "../rightPanelStore";
1214
import { getSyntaxHighlighterPromise } from "../lib/syntaxHighlighting";
@@ -129,6 +131,64 @@ function fileOptions(container: HTMLElement) {
129131
.filter((label) => label?.startsWith("File options for "));
130132
}
131133

134+
describe("ChatMarkdown table layout", () => {
135+
it.each([
136+
{ width: 36, timeline: true, fits: true },
137+
{ width: 48, timeline: true, fits: true },
138+
{ width: 35, timeline: true, fits: false },
139+
{ width: 48, timeline: false, fits: false },
140+
])(
141+
"wraps expanded cells with fit=$fits in a $width rem container (timeline=$timeline)",
142+
async ({ width, timeline, fits }) => {
143+
const { container } = await render(
144+
<div style={{ width: `${width}rem` }}>
145+
<div className={timeline ? "@container/chat-lane chat-content-lane overflow-x-clip" : ""}>
146+
<ChatMarkdown
147+
cwd={undefined}
148+
text={[
149+
"| Change summary | Reason details | Verification status | Release impact | Followup actions |",
150+
"| --- | --- | --- | --- | --- |",
151+
"| 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. |",
152+
].join("\n")}
153+
/>
154+
</div>
155+
</div>,
156+
);
157+
const table = container.querySelector("table")!;
158+
const viewport = table.closest<HTMLElement>('[data-slot="scroll-area-viewport"]')!;
159+
const cell = table.tBodies[0]!.rows[0]!.cells[0]!;
160+
const collapse = () => buttonsLabelled(container, "Collapse table cells")[0];
161+
if (collapse()) await act(async () => collapse()!.click());
162+
163+
const collapsedWidth = table.getBoundingClientRect().width;
164+
const collapsedHeight = cell.getBoundingClientRect().height;
165+
expect(viewport.clientWidth).toBeGreaterThan(0);
166+
expect(viewport.scrollWidth).toBeGreaterThan(viewport.clientWidth);
167+
expect(cell.scrollWidth).toBeGreaterThan(cell.clientWidth);
168+
169+
await act(async () => buttonsLabelled(container, "Expand table cells")[0]!.click());
170+
const expandedWidth = table.getBoundingClientRect().width;
171+
const expandedHeight = cell.getBoundingClientRect().height;
172+
if (fits) {
173+
expect(viewport.scrollWidth).toBeLessThanOrEqual(viewport.clientWidth + 1);
174+
expect(expandedWidth).toBeLessThanOrEqual(viewport.clientWidth + 1);
175+
} else {
176+
expect(viewport.scrollWidth).toBeGreaterThan(viewport.clientWidth);
177+
viewport.scrollLeft = 100;
178+
expect(viewport.scrollLeft).toBeGreaterThan(0);
179+
}
180+
expect(expandedHeight).toBeGreaterThan(collapsedHeight);
181+
182+
await act(async () => collapse()!.click());
183+
expect(table.getBoundingClientRect().width).toBeCloseTo(collapsedWidth, 0);
184+
expect(cell.getBoundingClientRect().height).toBeCloseTo(collapsedHeight, 0);
185+
await act(async () => buttonsLabelled(container, "Expand table cells")[0]!.click());
186+
expect(table.getBoundingClientRect().width).toBeCloseTo(expandedWidth, 0);
187+
expect(cell.getBoundingClientRect().height).toBeCloseTo(expandedHeight, 0);
188+
},
189+
);
190+
});
191+
132192
describe("ChatMarkdown bare anchor placeholders", () => {
133193
it.each(["<A>", "<a>", "<a >", "<a/>", "<A/>", "<a />"])(
134194
"preserves unmatched %s without linking later blocks",

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -639,30 +639,6 @@ function MarkdownTable({ children, ...props }: React.ComponentProps<"table">) {
639639
return observeResize(table, measureClippedCells);
640640
}, [measureClippedCells]);
641641

642-
function toggleExpanded() {
643-
const table = tableRef.current;
644-
if (!table) return;
645-
646-
if (!expanded) {
647-
const rows = [...table.rows];
648-
const columnWidths = rows.reduce<number[]>((widths, row) => {
649-
[...row.cells].forEach((cell, columnIndex) => {
650-
widths[columnIndex] = Math.max(
651-
widths[columnIndex] ?? 0,
652-
cell.getBoundingClientRect().width,
653-
);
654-
});
655-
return widths;
656-
}, []);
657-
658-
[...(table.tHead?.rows[0]?.cells ?? [])].forEach((cell, columnIndex) => {
659-
cell.style.minWidth = `${columnWidths[columnIndex] ?? cell.getBoundingClientRect().width}px`;
660-
});
661-
}
662-
663-
setExpanded((value) => !value);
664-
}
665-
666642
const handleCopy = (format: "markdown" | "csv") => {
667643
const table = containerRef.current?.querySelector("table");
668644
if (!table || typeof navigator === "undefined" || navigator.clipboard == null) {
@@ -720,7 +696,7 @@ function MarkdownTable({ children, ...props }: React.ComponentProps<"table">) {
720696
variant={expanded ? "secondary" : "ghost-muted"}
721697
size="icon-xs"
722698
aria-pressed={expanded}
723-
onClick={toggleExpanded}
699+
onClick={() => setExpanded((value) => !value)}
724700
aria-label={expandLabel}
725701
/>
726702
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1425,7 +1425,10 @@ const ConversationTimeline = memo(function ConversationTimeline({
14251425
const renderItem = ({ item }: { item: MessagesTimelineRow }) => (
14261426
<TimelineRowCtx value={sharedState}>
14271427
<div className="messages-timeline-row-frame">
1428-
<div className="chat-content-lane overflow-x-clip" data-timeline-root="true">
1428+
<div
1429+
className="@container/chat-lane chat-content-lane overflow-x-clip"
1430+
data-timeline-root="true"
1431+
>
14291432
<TimelineRowContent row={item} />
14301433
</div>
14311434
</div>

‎apps/web/src/index.css‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2147,6 +2147,22 @@ code {
21472147
overflow-wrap: anywhere;
21482148
}
21492149

2150+
/* Fit expanded tables to wide timeline lanes; narrow lanes keep horizontal
2151+
scrolling so prose columns remain readable. */
2152+
@container chat-lane (min-width: 36rem) {
2153+
.chat-markdown .chat-markdown-table-container[data-expanded="true"] table {
2154+
min-width: 0;
2155+
}
2156+
2157+
.chat-markdown .chat-markdown-table-container[data-expanded="true"] th {
2158+
white-space: normal;
2159+
}
2160+
2161+
.chat-markdown .chat-markdown-table-container[data-expanded="true"] td {
2162+
overflow-wrap: break-word;
2163+
}
2164+
}
2165+
21502166
/* Formula cells keep their natural width and scroll with the table. */
21512167
.chat-markdown .chat-markdown-table-container td:has([data-markdown-math]) {
21522168
max-width: none;

0 commit comments

Comments
 (0)