An open source design system that's fully customizable and agent ready
TypeScript
fix(table): truncate overflowing cell text with ellipsis (#933)
Cindy Zhangc328e74f5b3c
diff --git a/apps/storybook/stories/Table.stories.tsx b/apps/storybook/stories/Table.stories.tsxindex e40276a9c4..b084f83d76 100644--- a/apps/storybook/stories/Table.stories.tsx+++ b/apps/storybook/stories/Table.stories.tsx@@ -298,3 +298,81 @@ export const KitchenSink: Story = {hasHover: true,},};++// =============================================================================+// Overflow & Text Wrapping+// =============================================================================++interface OverflowRow extends Record<string, unknown> {+ column: string;+ truncated: string;+ wrapped: string;+}++const overflowData: OverflowRow[] = [+ {+ column: 'Default (truncate)',+ truncated:+ 'a_very_long_string_like_this_that_overflows_the_column_without_spaces',+ wrapped:+ 'a_very_long_string_like_this_that_overflows_the_column_without_spaces',+ },+ {+ column: 'Normal prose',+ truncated:+ 'This is a longer sentence that will also get clipped by the ellipsis when it runs out of room.',+ wrapped:+ 'This is a longer sentence that wraps naturally onto the next line when it runs out of room.',+ },+];++/**+ * Cells truncate overflow with an ellipsis by default.+ * The default renderer adds a native title tooltip so truncated text+ * is still accessible on hover.+ *+ * For the full XDS tooltip experience (only shows when actually truncated),+ * use renderCell with <XDSText maxLines={1}>.+ *+ * The "Wrapped" column shows how consumers can opt into wrapping+ * via renderCell with white-space: normal and overflow: visible.+ */+export const OverflowBehavior: Story = {+ render: () => {+ const cols: XDSTableColumn<OverflowRow>[] = [+ {key: 'column', header: 'Row', width: pixel(140)},+ {+ key: 'truncated',+ header: 'Truncated (default)',+ width: proportional(1),+ },+ {+ key: 'wrapped',+ header: 'Wrapped (xstyle override)',+ width: proportional(1),+ renderCell: item => (+ <span+ style={{+ whiteSpace: 'normal',+ overflow: 'visible',+ wordBreak: 'break-word',+ display: 'block',+ }}>+ {item.wrapped}+ </span>+ ),+ },+ ];++ return (+ <div style={{width: '640px'}}>+ <XDSTable+ data={overflowData}+ columns={cols}+ dividers="grid"+ density="balanced"+ />+ </div>+ );+ },+};diff --git a/packages/core/src/Table/XDSBaseTable.tsx b/packages/core/src/Table/XDSBaseTable.tsxindex 85bc847070..0ab81cfd13 100644--- a/packages/core/src/Table/XDSBaseTable.tsx+++ b/packages/core/src/Table/XDSBaseTable.tsx@@ -106,10 +106,19 @@ function TableRowInner<T extends Record<string, unknown>>({? col.renderCell(item): defaultCellRenderer(item, col.key);+ // Default renderer: add title so truncated text is accessible on hover.+ // For the full XDS tooltip experience, use renderCell with+ // <XDSText maxLines={1}> — the title attr is the zero-cost safety net.+ const titleProp =+ !col.renderCell && typeof content === 'string' && content.length > 0+ ? {title: content}+ : {};+return (<CellComponentkey={col.key}{...cellRenderProps.htmlProps}+ {...titleProp}xstyle={cellRenderProps.styles}>{content}</CellComponent>@@ -242,12 +251,19 @@ function XDSBaseTableInner<T extends Record<string, unknown>>({}: cellRenderProps.htmlProps;+ const headerContent = col.header ?? col.key;+ const headerTitleProp =+ typeof headerContent === 'string' && headerContent.length > 0+ ? {title: headerContent}+ : {};+return (<HeaderCellComponentkey={col.key}{...mergedHtmlProps}+ {...headerTitleProp}xstyle={cellRenderProps.styles}>- {col.header ?? col.key}+ {headerContent}</HeaderCellComponent>);});diff --git a/packages/core/src/Table/XDSTable.test.tsx b/packages/core/src/Table/XDSTable.test.tsxindex 00908e3ce1..c232ade583 100644--- a/packages/core/src/Table/XDSTable.test.tsx+++ b/packages/core/src/Table/XDSTable.test.tsx@@ -664,6 +664,60 @@ describe('XDSTable', () => {expect(idKey).toHaveBeenCalledWith(users[1]);expect(idKey).toHaveBeenCalledWith(users[2]);});++ it('applies overflow truncation styles to body cells', () => {+ // text-overflow: ellipsis + overflow: hidden + white-space: nowrap are applied+ // via StyleX class names. We verify a class is present on the cell; the actual+ // CSS rendering is covered by visual/e2e tests (jsdom doesn't compute layout).+ const longData = [+ {+ name: 'a_very_long_string_without_spaces_that_would_overflow_a_fixed_width_column',+ value: '42',+ },+ ];+ render(<XDSTable data={longData} />);+ const cell = screen.getAllByRole('cell')[0];+ // Cell should have at least one StyleX-generated class applied+ expect(cell.className.length).toBeGreaterThan(0);+ // Text content is present in the DOM (truncation is purely visual)+ expect(cell).toHaveTextContent(+ 'a_very_long_string_without_spaces_that_would_overflow_a_fixed_width_column',+ );+ });++ it('sets title attribute on default-rendered cells for overflow accessibility', () => {+ render(<XDSTable data={users} columns={columns} />);+ const cells = screen.getAllByRole('cell');+ // Default renderer adds title for native hover tooltip on truncated text+ expect(cells[0]).toHaveAttribute('title', 'Alice');+ expect(cells[1]).toHaveAttribute('title', '30');+ });++ it('does not set title attribute on renderCell columns', () => {+ const cols: XDSTableColumn<User>[] = [+ {+ key: 'name',+ header: 'Name',+ renderCell: item => <span>{item.name}</span>,+ },+ {key: 'email', header: 'Email'},+ ];+ render(<XDSTable data={users} columns={cols} />);+ const cells = screen.getAllByRole('cell');+ // renderCell column: consumer owns disclosure+ expect(cells[0]).not.toHaveAttribute('title');+ // default column: title present+ expect(cells[1]).toHaveAttribute('title', users[0].email);+ });++ it('sets title attribute on string header cells', () => {+ render(<XDSTable data={users} columns={columns} />);+ const headers = screen.getAllByRole('columnheader');+ // columns fixture order: name, age, email+ expect(headers[0]).toHaveAttribute('title', 'Name');+ expect(headers[1]).toHaveAttribute('title', 'Age');+ expect(headers[2]).toHaveAttribute('title', 'Email');+ });});// =============================================================================diff --git a/packages/core/src/Table/XDSTableCell.tsx b/packages/core/src/Table/XDSTableCell.tsxindex e54127f31e..a765298757 100644--- a/packages/core/src/Table/XDSTableCell.tsx+++ b/packages/core/src/Table/XDSTableCell.tsx@@ -22,6 +22,7 @@ import {} from '../theme/tokens.stylex';import type {StyleXStyles} from '../theme/types';import {XDSTableContext} from './XDSTableContext';+import {overflowStyles} from './table.stylex';import {xdsClassName, mergeProps} from '../utils';/** Props for XDSTableCell — thin `<td>` wrapper */@@ -80,6 +81,8 @@ const dividerColumnStyles = stylex.create({},});+// Shared overflow styles — see table.stylex.ts for rationale+/*** XDSTableCell — a `<td>` wrapper for children/streaming mode.*@@ -113,7 +116,10 @@ export function XDSTableCell({);}- const cellStyles: StyleXStyles[] = [densityStyles[ctx.density]];+ const cellStyles: StyleXStyles[] = [+ densityStyles[ctx.density],+ overflowStyles.cell,+ ];if (ctx.dividers === 'rows' || ctx.dividers === 'grid') {cellStyles.push(dividerRowStyles.cell);diff --git a/packages/core/src/Table/XDSTableHeaderCell.tsx b/packages/core/src/Table/XDSTableHeaderCell.tsxindex 92e44794e5..22d17edbac 100644--- a/packages/core/src/Table/XDSTableHeaderCell.tsx+++ b/packages/core/src/Table/XDSTableHeaderCell.tsx@@ -23,6 +23,7 @@ import {} from '../theme/tokens.stylex';import type {StyleXStyles} from '../theme/types';import {XDSTableContext} from './XDSTableContext';+import {overflowStyles} from './table.stylex';import {xdsClassName, mergeProps} from '../utils';/** Props for XDSTableHeaderCell — `<th>` wrapper with context-aware styling */@@ -87,6 +88,8 @@ const dividerColumnStyles = stylex.create({},});+// Shared overflow styles — see table.stylex.ts for rationale+/*** XDSTableHeaderCell — a `<th>` wrapper for header cells.*@@ -136,6 +139,7 @@ export function XDSTableHeaderCell({headerStyles.cell,densityStyles[ctx.density],headerDividerStyles.cell,+ overflowStyles.cell,];if (ctx.dividers === 'columns' || ctx.dividers === 'grid') {diff --git a/packages/core/src/Table/table.stylex.ts b/packages/core/src/Table/table.stylex.tsnew file mode 100644index 0000000000..f395abaa7a--- /dev/null+++ b/packages/core/src/Table/table.stylex.ts@@ -0,0 +1,25 @@+/**+ * @file table.stylex.ts+ * @input StyleX, theme tokens+ * @output Shared table styles used by XDSTableCell and XDSTableHeaderCell+ * @position Utility styles; consumed by cell components+ */++import * as stylex from '@stylexjs/stylex';++/**+ * Overflow truncation for table cells.+ *+ * Applied at the <td>/<th> level as a CSS safety net. For data-driven+ * tables, the default renderer also adds a title attribute so truncated+ * text is accessible on hover. For the full XDS tooltip experience,+ * use renderCell with <XDSText maxLines={1}>.+ */+export const overflowStyles = stylex.create({+ cell: {+ overflow: 'hidden',+ textOverflow: 'ellipsis',+ whiteSpace: 'nowrap',+ maxWidth: '0',+ },+});