Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix(markdown): wrap long table cells instead of stretching the table (#18357)

John-Mason P. Shackelford5a8635a3f983
diff --git a/.github/pr-assets/table-cell-wrap-01-before.png b/.github/pr-assets/table-cell-wrap-01-before.png
new file mode 100644
index 0000000000..ad317263aa
Binary files /dev/null and b/.github/pr-assets/table-cell-wrap-01-before.png differ
diff --git a/.github/pr-assets/table-cell-wrap-02-after.png b/.github/pr-assets/table-cell-wrap-02-after.png
new file mode 100644
index 0000000000..0db39f83b7
Binary files /dev/null and b/.github/pr-assets/table-cell-wrap-02-after.png differ
diff --git a/__tests__/components/features/markdown/table.test.tsx b/__tests__/components/features/markdown/table.test.tsx
index 958984ddfc..f2c8214b27 100644
--- a/__tests__/components/features/markdown/table.test.tsx
+++ b/__tests__/components/features/markdown/table.test.tsx
@@ -57,6 +57,19 @@ describe("table (markdown)", () => {
expect(table).not.toHaveClass("w-full");
});
+ it("should cap cell width and wrap long text instead of stretching the table", () => {
+ render(<MarkdownRenderer>{GFM_TABLE}</MarkdownRenderer>);
+
+ [
+ ...screen.getAllByRole("columnheader"),
+ ...screen.getAllByRole("cell"),
+ ].forEach((cell) => {
+ expect(cell).toHaveClass("max-w-md");
+ expect(cell).toHaveClass("break-words");
+ expect(cell).not.toHaveClass("whitespace-nowrap");
+ });
+ });
+
it("should render header cells as <th> elements with correct content", () => {
render(<MarkdownRenderer>{GFM_TABLE}</MarkdownRenderer>);
diff --git a/src/components/features/markdown/table.tsx b/src/components/features/markdown/table.tsx
index 33e15b5799..500c10d741 100644
--- a/src/components/features/markdown/table.tsx
+++ b/src/components/features/markdown/table.tsx
@@ -2,6 +2,12 @@ import React from "react";
import { ExtraProps } from "react-markdown";
import { MarkdownTableScroll } from "./markdown-table-scroll";
+// Cells wrap once they reach this width so one long sentence cannot stretch the
+// table far past the viewport. A fixed cap (rather than a share of the chat
+// width) avoids container-query/measurement feedback loops inside shrink-to-fit
+// parents such as the user message bubble.
+const CELL_WIDTH_CLASS = "max-w-md break-words";
+
// Custom component to render <table> in markdown
export function table({
children,
@@ -31,7 +37,9 @@ export function th({
React.ThHTMLAttributes<HTMLTableCellElement> &
ExtraProps) {
return (
- <th className="whitespace-nowrap border-border bg-surface px-3 py-2 text-left font-semibold text-contrast">
+ <th
+ className={`${CELL_WIDTH_CLASS} border-border bg-surface px-3 py-2 text-left font-semibold text-contrast`}
+ >
{children}
</th>
);
@@ -44,7 +52,7 @@ export function td({
React.TdHTMLAttributes<HTMLTableCellElement> &
ExtraProps) {
return (
- <td className="whitespace-nowrap border-border px-3 py-2 align-top">
+ <td className={`${CELL_WIDTH_CLASS} border-border px-3 py-2 align-top`}>
{children}
</td>
);