Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

feat(Table): structural children mode + XDSTable in Markdown (#2098)

Cindy Zhang953377ad9a56
diff --git a/apps/storybook/stories/Table.stories.tsx b/apps/storybook/stories/Table.stories.tsx
index 6bb65e89dd..6e8b296bd9 100644
--- a/apps/storybook/stories/Table.stories.tsx
+++ b/apps/storybook/stories/Table.stories.tsx
@@ -4,6 +4,9 @@ import {
XDSTable,
XDSTableRow,
XDSTableCell,
+ XDSTableHeaderCell,
+ XDSTableHeader,
+ XDSTableBody,
proportional,
pixel,
} from '@xds/core/Table';
@@ -18,7 +21,7 @@ import {
XDSVStack,
XDSHStack,
} from '@xds/core/Layout';
-import {XDSHeading} from '@xds/core/Text';
+import {XDSHeading, XDSText} from '@xds/core/Text';
import {XDSButton} from '@xds/core/Button';
import {
colorDefaults,
@@ -252,26 +255,35 @@ export const CustomCellRenderer: Story = {
export const ChildrenMode: Story = {
render: () => (
<XDSTable density="balanced" dividers="rows" isStriped hasHover>
- <XDSTableRow>
- <XDSTableCell>Alice</XDSTableCell>
- <XDSTableCell>[email protected]</XDSTableCell>
- <XDSTableCell>Engineer</XDSTableCell>
- </XDSTableRow>
- <XDSTableRow>
- <XDSTableCell>Bob</XDSTableCell>
- <XDSTableCell>[email protected]</XDSTableCell>
- <XDSTableCell>Designer</XDSTableCell>
- </XDSTableRow>
- <XDSTableRow>
- <XDSTableCell>Charlie</XDSTableCell>
- <XDSTableCell>[email protected]</XDSTableCell>
- <XDSTableCell>PM</XDSTableCell>
- </XDSTableRow>
- <XDSTableRow>
- <XDSTableCell>Diana</XDSTableCell>
- <XDSTableCell>[email protected]</XDSTableCell>
- <XDSTableCell>Engineer</XDSTableCell>
- </XDSTableRow>
+ <XDSTableHeader>
+ <XDSTableRow>
+ <XDSTableHeaderCell>Name</XDSTableHeaderCell>
+ <XDSTableHeaderCell>Email</XDSTableHeaderCell>
+ <XDSTableHeaderCell>Role</XDSTableHeaderCell>
+ </XDSTableRow>
+ </XDSTableHeader>
+ <XDSTableBody>
+ <XDSTableRow>
+ <XDSTableCell>Alice</XDSTableCell>
+ <XDSTableCell>[email protected]</XDSTableCell>
+ <XDSTableCell>Engineer</XDSTableCell>
+ </XDSTableRow>
+ <XDSTableRow>
+ <XDSTableCell>Bob</XDSTableCell>
+ <XDSTableCell>[email protected]</XDSTableCell>
+ <XDSTableCell>Designer</XDSTableCell>
+ </XDSTableRow>
+ <XDSTableRow>
+ <XDSTableCell>Charlie</XDSTableCell>
+ <XDSTableCell>[email protected]</XDSTableCell>
+ <XDSTableCell>PM</XDSTableCell>
+ </XDSTableRow>
+ <XDSTableRow>
+ <XDSTableCell>Diana</XDSTableCell>
+ <XDSTableCell>[email protected]</XDSTableCell>
+ <XDSTableCell>Engineer</XDSTableCell>
+ </XDSTableRow>
+ </XDSTableBody>
</XDSTable>
),
};
diff --git a/packages/core/src/Markdown/XDSMarkdown.tsx b/packages/core/src/Markdown/XDSMarkdown.tsx
index 90e8af4900..c9685ed9af 100644
--- a/packages/core/src/Markdown/XDSMarkdown.tsx
+++ b/packages/core/src/Markdown/XDSMarkdown.tsx
@@ -28,6 +28,12 @@ import {XDSCheckboxList} from '../CheckboxList/XDSCheckboxList';
import {XDSCheckboxListItem} from '../CheckboxList/XDSCheckboxListItem';
import {XDSList} from '../List/XDSList';
import {XDSListItem} from '../List/XDSListItem';
+import {XDSTable} from '../Table/XDSTable';
+import {XDSTableRow} from '../Table/XDSTableRow';
+import {XDSTableCell} from '../Table/XDSTableCell';
+import {XDSTableHeaderCell} from '../Table/XDSTableHeaderCell';
+import {XDSTableHeader} from '../Table/XDSTableHeader';
+import {XDSTableBody} from '../Table/XDSTableBody';
import {xdsClassName, mergeProps} from '../utils';
import {useXDSStreamingText} from '../hooks/useXDSStreamingText';
import {XDSCitation} from '../Citation/XDSCitation';
@@ -345,28 +351,6 @@ const styles = stylex.create({
blockIndent: {
marginInline: `calc(-1 * ${spacingVars['--spacing-2']})`,
},
- table: {
- borderCollapse: 'collapse',
- width: 'fit-content',
- maxWidth: '100%',
- },
- th: {
- fontWeight: fontWeightVars['--font-weight-semibold'],
- textAlign: 'left',
- padding: spacingVars['--spacing-2'],
- borderBottomWidth: borderVars['--border-width'],
- borderBottomStyle: 'solid',
- borderBottomColor: colorVars['--color-border-emphasized'],
- },
- td: {
- padding: spacingVars['--spacing-2'],
- borderBottomWidth: borderVars['--border-width'],
- borderBottomStyle: 'solid',
- borderBottomColor: colorVars['--color-border'],
- },
- alignLeft: {textAlign: 'left'},
- alignCenter: {textAlign: 'center'},
- alignRight: {textAlign: 'right'},
// HR
hr: {
borderWidth: 0,
@@ -1362,92 +1346,71 @@ function renderBlock(
);
}
case 'table': {
- const alignStyle = (a: (typeof node.alignments)[number]) =>
- a === 'center'
- ? styles.alignCenter
- : a === 'right'
- ? styles.alignRight
- : styles.alignLeft;
return (
<div
key={index}
{...stylex.props(
styles.tableWrapper,
spacing,
+ styles.blockIndent,
+ contentWidthValue != null
+ ? dynamicStyles.blockWidth(contentWidthValue)
+ : null,
+ BLOCK_ALIGN_MARGIN[contentAlign] != null
+ ? dynamicStyles.blockAlign(BLOCK_ALIGN_MARGIN[contentAlign]!)
+ : null,
isFirst && styles.noMarginBlockStart,
isLast && styles.noMarginBlockEnd,
)}>
- <table
- {...stylex.props(
- styles.table,
- styles.blockIndent,
- contentWidthValue != null
- ? dynamicStyles.blockWidth(contentWidthValue)
- : null,
- BLOCK_ALIGN_MARGIN[contentAlign] != null
- ? dynamicStyles.blockAlign(BLOCK_ALIGN_MARGIN[contentAlign]!)
- : null,
- )}>
- <thead>
- <tr>
+ <XDSTable dividers="rows" textOverflow="wrap">
+ <XDSTableHeader>
+ <XDSTableRow>
{node.headers.map((h, i) => (
- <th
+ <XDSTableHeaderCell
key={i}
- {...stylex.props(
- styles.th,
- alignStyle(node.alignments[i]),
- )}>
+ style={
+ node.alignments[i] === 'center'
+ ? {textAlign: 'center'}
+ : node.alignments[i] === 'right'
+ ? {textAlign: 'right'}
+ : undefined
+ }>
{h.children.map((c, j) =>
- renderInline(
- c,
- j,
- onLinkClick,
- cursor,
- citationCtx,
- linkComponent,
- inlinePlugins,
- components,
- ),
+ renderInline(c, j, onLinkClick, cursor, citationCtx, linkComponent, inlinePlugins, components),
)}
- </th>
+ </XDSTableHeaderCell>
))}
- </tr>
- </thead>
- <tbody>
+ </XDSTableRow>
+ </XDSTableHeader>
+ <XDSTableBody>
{node.rows.map((row, i) => {
const rowIsNew =
cursor.active && cursor.offset >= cursor.boundary;
const cells = row.map((cell, j) => (
- <td
+ <XDSTableCell
key={j}
- {...stylex.props(
- styles.td,
- alignStyle(node.alignments[j]),
- )}>
+ style={
+ node.alignments[j] === 'center'
+ ? {textAlign: 'center'}
+ : node.alignments[j] === 'right'
+ ? {textAlign: 'right'}
+ : undefined
+ }>
{cell.children.map((c, k) =>
- renderInline(
- c,
- k,
- onLinkClick,
- cursor,
- citationCtx,
- linkComponent,
- inlinePlugins,
- components,
- ),
+ renderInline(c, k, onLinkClick, cursor, citationCtx, linkComponent, inlinePlugins, components),
)}
- </td>
+ </XDSTableCell>
));
return (
- <tr
+ <XDSTableRow
key={rowIsNew ? `fade-row-${i}` : i}
{...(rowIsNew ? stylex.props(streamingStyles.fadeIn) : {})}>
{cells}
- </tr>
+ </XDSTableRow>
);
})}
- </tbody>
- </table>
+ </XDSTableBody>
+ </XDSTable>
</div>
);
}
diff --git a/packages/core/src/Table/XDSBaseTable.tsx b/packages/core/src/Table/XDSBaseTable.tsx
index 429add6086..25ed72c393 100644
--- a/packages/core/src/Table/XDSBaseTable.tsx
+++ b/packages/core/src/Table/XDSBaseTable.tsx
@@ -36,6 +36,8 @@ import {
import {XDSTableRow} from './XDSTableRow';
import {XDSTableCell} from './XDSTableCell';
import {XDSTableHeaderCell} from './XDSTableHeaderCell';
+import {XDSTableHeader} from './XDSTableHeader';
+import {XDSTableBody} from './XDSTableBody';
import {xdsClassName, mergeProps} from '../utils';
import {XDSEmptyState} from '../EmptyState';
import {XDSText} from '../Text';
@@ -262,7 +264,6 @@ function XDSBaseTableInner<T extends Record<string, unknown>>({
columns: columnsProp,
idKey,
plugins: pluginsProp,
- components,
children,
tableProps: userTableProps,
textOverflow = 'wrap',
@@ -273,12 +274,9 @@ function XDSBaseTableInner<T extends Record<string, unknown>>({
// Use stable empty array when no plugins provided
const plugins = pluginsProp ?? (EMPTY_PLUGINS as TablePlugin<T>[]);
- const RowComponent = (components?.Row ??
- XDSTableRow) as React.ComponentType<TableRowComponentProps>;
- const CellComponent = (components?.Cell ??
- XDSTableCell) as React.ComponentType<TableCellComponentProps>;
- const HeaderCellComponent = (components?.HeaderCell ??
- XDSTableHeaderCell) as React.ComponentType<TableHeaderCellComponentProps>;
+ const RowComponent = XDSTableRow as React.ComponentType<TableRowComponentProps>;
+ const CellComponent = XDSTableCell as React.ComponentType<TableCellComponentProps>;
+ const HeaderCellComponent = XDSTableHeaderCell as React.ComponentType<TableHeaderCellComponentProps>;
// Resolve columns: explicit > auto-generated from data.
const baseColumns: XDSTableColumn<T>[] =
@@ -421,53 +419,29 @@ function XDSBaseTableInner<T extends Record<string, unknown>>({
stylex.props(...tableRenderProps.styles),
)}
style={tableStyle}>
- {/* thead */}
- {hasColumns && (
- <thead>
- <RowComponent
- {...headerRowRenderProps.htmlProps}
- xstyle={headerRowRenderProps.styles}>
- {headerRowRenderProps.children}
- </RowComponent>
- </thead>
- )}
-
- {/* tbody — data-driven or children mode */}
- <tbody>
- {children
- ? children
- : hasData
- ? data.map((item, rowIndex) => {
- const rowKey =
- idKey == null
- ? rowIndex
- : typeof idKey === 'function'
- ? idKey(item)
- : String(item[idKey]);
-
- return (
- <MemoizedTableRow<T>
- key={rowKey}
- item={item}
- rowIndex={rowIndex}
- rowKey={rowKey}
- columns={resolvedColumns}
- plugins={plugins}
- textOverflow={textOverflow}
- RowComponent={RowComponent}
- CellComponent={CellComponent}
- />
- );
- })
- : data != null &&
- emptyState !== false && (
- <tr>
- <td colSpan={resolvedColumns.length}>
- {emptyState ?? <XDSEmptyState title="No data" isCompact />}
- </td>
- </tr>
- )}
- </tbody>
+ {children
+ ? children
+ : (
+ <>
+ {hasColumns && (
+ <XDSTableHeader>
+ <RowComponent {...headerRowRenderProps.htmlProps} xstyle={headerRowRenderProps.styles}>
+ {headerRowRenderProps.children}
+ </RowComponent>
+ </XDSTableHeader>
+ )}
+ <XDSTableBody>
+ {hasData
+ ? data.map((item, rowIndex) => {
+ const rowKey = idKey == null ? rowIndex : typeof idKey === 'function' ? idKey(item) : String(item[idKey]);
+ return (<MemoizedTableRow<T> key={rowKey} item={item} rowIndex={rowIndex} rowKey={rowKey} columns={resolvedColumns} plugins={plugins} textOverflow={textOverflow} RowComponent={RowComponent} CellComponent={CellComponent} />);
+ })
+ : data != null && emptyState !== false && (
+ <tr><td colSpan={resolvedColumns.length}>{emptyState ?? <XDSEmptyState title="No data" isCompact />}</td></tr>
+ )}
+ </XDSTableBody>
+ </>
+ )}
</table>
);
diff --git a/packages/core/src/Table/XDSTable.tsx b/packages/core/src/Table/XDSTable.tsx
index 7c7351896b..f407771924 100644
--- a/packages/core/src/Table/XDSTable.tsx
+++ b/packages/core/src/Table/XDSTable.tsx
@@ -18,9 +18,6 @@ import {useMemo, type ReactElement, type Ref} from 'react';
import * as stylex from '@stylexjs/stylex';
import {colorVars} from '../theme/tokens.stylex';
import {XDSBaseTable} from './XDSBaseTable';
-import {XDSTableRow} from './XDSTableRow';
-import {XDSTableCell} from './XDSTableCell';
-import {XDSTableHeaderCell} from './XDSTableHeaderCell';
import {XDSTableContext} from './XDSTableContext';
import {useXDSBaseTablePlugins} from './useXDSBaseTablePlugins';
import {xdsClassName, mergeProps} from '../utils';
@@ -29,9 +26,7 @@ import type {
XDSTableVerticalAlign,
TablePlugin,
TableRenderProps,
- TableRowComponentProps,
- TableCellComponentProps,
- TableHeaderCellComponentProps,
+
} from './types';
// =============================================================================
@@ -174,16 +169,7 @@ function buildTableStylePlugin<
};
}
-// Stable component references for the components prop.
-// XDSTable* components accept XDSBaseProps (narrower than the internal
-// Table*ComponentProps interfaces), but they spread ...props on their HTML
-// elements, so extra attributes from the plugin pipeline pass through safely.
-const xdsComponents = {
- Row: XDSTableRow as unknown as React.ComponentType<TableRowComponentProps>,
- Cell: XDSTableCell as unknown as React.ComponentType<TableCellComponentProps>,
- HeaderCell:
- XDSTableHeaderCell as unknown as React.ComponentType<TableHeaderCellComponentProps>,
-};
+
// =============================================================================
// XDSTable Component
@@ -228,7 +214,6 @@ function XDSTableInner<T extends Record<string, unknown>>({
data={data}
columns={columns}
plugins={mergedPlugins}
- components={xdsComponents}
textOverflow={textOverflow}
scrollWrapper={TableScrollWrapper}
{...rest}
diff --git a/packages/core/src/Table/XDSTableBody.tsx b/packages/core/src/Table/XDSTableBody.tsx
new file mode 100644
index 0000000000..0fb40c5e3f
--- /dev/null
+++ b/packages/core/src/Table/XDSTableBody.tsx
@@ -0,0 +1,12 @@
+'use client';
+import type {ReactNode} from 'react';
+import type {StyleXStyles} from '../theme/types';
+import * as stylex from '@stylexjs/stylex';
+import {xdsClassName, mergeProps} from '../utils';
+
+export interface XDSTableBodyProps { children: ReactNode; xstyle?: StyleXStyles; }
+
+export function XDSTableBody({children, xstyle}: XDSTableBodyProps) {
+ return (<tbody {...mergeProps(xdsClassName('table-body'), stylex.props(xstyle))}>{children}</tbody>);
+}
+XDSTableBody.displayName = 'XDSTableBody';
diff --git a/packages/core/src/Table/XDSTableFooter.tsx b/packages/core/src/Table/XDSTableFooter.tsx
new file mode 100644
index 0000000000..1d771d1d0d
--- /dev/null
+++ b/packages/core/src/Table/XDSTableFooter.tsx
@@ -0,0 +1,12 @@
+'use client';
+import type {ReactNode} from 'react';
+import type {StyleXStyles} from '../theme/types';
+import * as stylex from '@stylexjs/stylex';
+import {xdsClassName, mergeProps} from '../utils';
+
+export interface XDSTableFooterProps { children: ReactNode; xstyle?: StyleXStyles; }
+
+export function XDSTableFooter({children, xstyle}: XDSTableFooterProps) {
+ return (<tfoot {...mergeProps(xdsClassName('table-footer'), stylex.props(xstyle))}>{children}</tfoot>);
+}
+XDSTableFooter.displayName = 'XDSTableFooter';
diff --git a/packages/core/src/Table/XDSTableHeader.tsx b/packages/core/src/Table/XDSTableHeader.tsx
new file mode 100644
index 0000000000..6e2b6b1b1e
--- /dev/null
+++ b/packages/core/src/Table/XDSTableHeader.tsx
@@ -0,0 +1,12 @@
+'use client';
+import type {ReactNode} from 'react';
+import type {StyleXStyles} from '../theme/types';
+import * as stylex from '@stylexjs/stylex';
+import {xdsClassName, mergeProps} from '../utils';
+
+export interface XDSTableHeaderProps { children: ReactNode; xstyle?: StyleXStyles; }
+
+export function XDSTableHeader({children, xstyle}: XDSTableHeaderProps) {
+ return (<thead {...mergeProps(xdsClassName('table-header'), stylex.props(xstyle))}>{children}</thead>);
+}
+XDSTableHeader.displayName = 'XDSTableHeader';
diff --git a/packages/core/src/Table/index.ts b/packages/core/src/Table/index.ts
index 8417dae2b9..550f3e498c 100644
--- a/packages/core/src/Table/index.ts
+++ b/packages/core/src/Table/index.ts
@@ -9,11 +9,13 @@
* SYNC: When modified, update this header and /packages/core/src/Table/Table.doc.mjs
*/
-export {XDSBaseTable} from './XDSBaseTable';
export {XDSTable} from './XDSTable';
export {XDSTableRow} from './XDSTableRow';
export {XDSTableCell} from './XDSTableCell';
export {XDSTableHeaderCell} from './XDSTableHeaderCell';
+export {XDSTableHeader} from './XDSTableHeader';
+export {XDSTableBody} from './XDSTableBody';
+export {XDSTableFooter} from './XDSTableFooter';
export {XDSTableContext} from './XDSTableContext';
export {useXDSTableSelection} from './plugins/selection';
export {useXDSTableSelectionState} from './plugins/selection';
@@ -50,9 +52,6 @@ export type {
BodyRowRenderProps,
BodyCellRenderProps,
XDSBaseTableProps,
- TableRowComponentProps,
- TableCellComponentProps,
- TableHeaderCellComponentProps,
} from './types';
export type {
XDSTableProps,
@@ -63,6 +62,9 @@ export type {
export type {XDSTableRowProps} from './XDSTableRow';
export type {XDSTableCellProps} from './XDSTableCell';
export type {XDSTableHeaderCellProps} from './XDSTableHeaderCell';
+export type {XDSTableHeaderProps} from './XDSTableHeader';
+export type {XDSTableBodyProps} from './XDSTableBody';
+export type {XDSTableFooterProps} from './XDSTableFooter';
export type {XDSTableContextValue} from './XDSTableContext';
export type {UseXDSTableSelectionConfig} from './plugins/selection';
export type {
diff --git a/packages/core/src/Table/types.ts b/packages/core/src/Table/types.ts
index a80812d226..452dca116e 100644
--- a/packages/core/src/Table/types.ts
+++ b/packages/core/src/Table/types.ts
@@ -373,16 +373,7 @@ export interface XDSBaseTableProps<T extends Record<string, unknown>> {
idKey?: (keyof T & string) | ((item: T) => string | number);
/** Plugins to transform render props at each level */
plugins?: TablePlugin<T>[];
- /**
- * Component overrides for table elements.
- * When provided, these components are rendered instead of raw HTML elements.
- * Components receive `xstyle` from plugin transforms.
- */
- components?: {
- Row?: ComponentType<TableRowComponentProps>;
- Cell?: ComponentType<TableCellComponentProps>;
- HeaderCell?: ComponentType<TableHeaderCellComponentProps>;
- };
+
/** Children mode — render `<tr>`/`<td>` directly instead of data-driven */
children?: ReactNode;
/** Additional HTML attributes for the `<table>` element */