Skip to content

Commit 9f56276

Browse files
PR Batch Testerclaude
andcommitted
fix(web): table wrap button only shows when a cell is cut off
The button under every chat table showed even on short tables, where clicking it changed nothing but its own icon. It now appears only when a cell is actually clipped, and uses the same wrap icon and wording as the code block wrap button instead of fullscreen arrows. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 339179f commit 9f56276

2 files changed

Lines changed: 60 additions & 21 deletions

File tree

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

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import {
4545
WrapTextIcon,
4646
type LucideIcon,
4747
} from "lucide-react";
48-
import { Check, Copy, Maximize2, Minimize2 } from "lucide";
48+
import { Check, Copy } from "lucide";
4949
import {
5050
AuthPreviewOperateScope,
5151
type AssetResource,
@@ -88,6 +88,7 @@ import React, {
8888
useCallback,
8989
memo,
9090
useEffect,
91+
useLayoutEffect,
9192
useMemo,
9293
useRef,
9394
useState,
@@ -201,6 +202,7 @@ import {
201202
resolvePullRequestPreviewTarget,
202203
useOpenChangeRequestLink,
203204
} from "~/lib/openPullRequestLink";
205+
import { observeResize } from "~/lib/observeResize";
204206
import { useOpenLink } from "../browser/useOpenLink";
205207
import { writeTextToClipboard } from "../hooks/useCopyToClipboard";
206208
import { isPreviewAvailableFor } from "../browser/previewRuntime";
@@ -559,15 +561,47 @@ function readInitialWordWrapSetting(): boolean {
559561
return getClientSettings().wordWrap;
560562
}
561563

564+
/**
565+
* Whether any cell is clipped in the unwrapped layout. Only then does the wrap
566+
* toggle change anything, so short tables hide it. Measures with the container
567+
* briefly unwrapped and restores it before the browser paints.
568+
*/
569+
function tableHasClippedCells(container: HTMLElement, table: HTMLTableElement): boolean {
570+
const previous = container.dataset.expanded;
571+
container.dataset.expanded = "false";
572+
try {
573+
for (const row of table.rows) {
574+
for (const cell of row.cells) {
575+
if (cell.scrollWidth > cell.clientWidth) return true;
576+
}
577+
}
578+
return false;
579+
} finally {
580+
if (previous === undefined) delete container.dataset.expanded;
581+
else container.dataset.expanded = previous;
582+
}
583+
}
584+
562585
function MarkdownTable({ children, ...props }: React.ComponentProps<"table">) {
563586
const containerRef = useRef<HTMLDivElement | null>(null);
564587
const tableRef = useRef<HTMLTableElement | null>(null);
565588
const [expanded, setExpanded] = useState(readInitialWordWrapSetting);
589+
const [hasClippedCells, setHasClippedCells] = useState(false);
566590
const [copied, setCopied] = useState(false);
567591
const copiedTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
568-
const expandLabel = expanded ? "Collapse table cells" : "Expand table cells";
592+
const wrapLabel = expanded ? "Disable cell wrap" : "Wrap long cells";
569593
const copyLabel = copied ? "Copied" : "Copy table";
570594

595+
// Streaming rows, font loads, and column width changes all resize the table.
596+
useLayoutEffect(() => {
597+
const container = containerRef.current;
598+
const table = tableRef.current;
599+
if (!container || !table) return;
600+
const measure = () => setHasClippedCells(tableHasClippedCells(container, table));
601+
measure();
602+
return observeResize(table, measure);
603+
}, []);
604+
571605
function toggleExpanded() {
572606
const table = tableRef.current;
573607
if (!table) return;
@@ -640,23 +674,27 @@ function MarkdownTable({ children, ...props }: React.ComponentProps<"table">) {
640674
</table>
641675
</ScrollArea>
642676
<div className="mt-0.5 flex items-center justify-between select-none">
643-
<Tooltip>
644-
<TooltipTrigger
645-
render={
646-
<Button
647-
type="button"
648-
variant={expanded ? "secondary" : "ghost-muted"}
649-
size="icon-xs"
650-
aria-pressed={expanded}
651-
onClick={toggleExpanded}
652-
aria-label={expandLabel}
653-
/>
654-
}
655-
>
656-
<MorphIcon className="size-3" icon={expanded ? Minimize2 : Maximize2} />
657-
</TooltipTrigger>
658-
<TooltipPopup side="top">{expandLabel}</TooltipPopup>
659-
</Tooltip>
677+
{hasClippedCells ? (
678+
<Tooltip>
679+
<TooltipTrigger
680+
render={
681+
<Button
682+
type="button"
683+
variant={expanded ? "secondary" : "ghost-muted"}
684+
size="icon-xs"
685+
aria-pressed={expanded}
686+
onClick={toggleExpanded}
687+
aria-label={wrapLabel}
688+
/>
689+
}
690+
>
691+
<WrapTextIcon className="size-3" />
692+
</TooltipTrigger>
693+
<TooltipPopup side="top">{wrapLabel}</TooltipPopup>
694+
</Tooltip>
695+
) : (
696+
<span />
697+
)}
660698
<Menu>
661699
<Tooltip>
662700
<TooltipTrigger

‎apps/web/src/index.css‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2059,8 +2059,9 @@ code {
20592059
border-bottom: 1px solid color-mix(in srgb, var(--contrast-border) 60%, transparent);
20602060
}
20612061

2062-
/* Collapsed (default): single-line cells truncate so arbitrary chat content
2063-
cannot blow the column out; the footer toggle expands cells to wrap. */
2062+
/* Collapsed: single-line cells truncate so arbitrary chat content
2063+
cannot blow the column out; the footer toggle, shown only when a cell is
2064+
clipped, wraps them instead. */
20642065
.chat-markdown .chat-markdown-table-container[data-expanded="false"] th,
20652066
.chat-markdown .chat-markdown-table-container[data-expanded="false"] td {
20662067
overflow: hidden;

0 commit comments

Comments
 (0)