Skip to content

Commit f70735c

Browse files
feat(Table): useTableColumnsVirtualization windows the unpinned columns
Table.virtualization accepts a columns capability, alone or in a list with at most one rows capability. Table mounts only the unpinned columns near the inline viewport, plus two overscan columns per side, and keeps the pinned start and end runs mounted. Each column's pitch is the width it renders at the table's minimum width. The header row and every body row render the same inaccessible spacer cells for unmounted columns. While columns window, the table carries aria-colcount and mounted cells carry full-table aria-colindex. The server and first client use the same seed from initialViewportWidth. Inline geometry comes from the shared Table viewport measurement. The capability carries the column window, a component, under a shared key. Table renders that component around its table and reads no column planner code of its own, so a Table that never imports the hook carries none of it. Adding or removing the capability remounts the table; a new seed does not. The pinned-run registry moves to its own module, which only the sticky columns plugin and the column window import. A Table whose first render has no rows windows its columns once rows arrive: the empty-state cell no longer counts as a cell that spans columns. Unsupported content or structure renders every column with a development warning. With both capabilities, this slice windows the rows and renders every column. The retained focused column, the first-visible-column anchor and windowing both axes together come in later slices. Adds the hook's CLI example block. This diff was generated with AI assistance (Navi). Tags: ai-generated
1 parent 06b7e61 commit f70735c

26 files changed

Lines changed: 2146 additions & 308 deletions
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@astryxdesign/cli': patch
3+
---
4+
5+
[docs] A new example block shows `useTableColumnsVirtualization`: a 121-column Table with a pinned first column that mounts only the columns near its horizontal scroll position.
6+
7+
@humbertovirtudes
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@astryxdesign/core': patch
3+
---
4+
5+
[feat] Tables can window their columns: pass `useTableColumnsVirtualization({initialViewportWidth})` to `Table.virtualization`. Table keeps every column definition and width, mounts only the unpinned columns near the inline viewport plus two overscan columns per side, keeps pinned columns mounted, and gives mounted cells full-table `aria-colindex` values with `aria-colcount` on the table. When the scroll area is at least as wide as the table's minimum width, every column mounts. `Table.virtualization` also accepts a list with at most one rows and one columns capability; with both, this release windows the rows and renders every column. Unsupported content or structure renders every column with a development warning. Adding or removing the columns capability remounts the table. Tables that do not opt in are unchanged and carry none of the column windowing code.
6+
7+
@humbertovirtudes

‎apps/sandbox/scripts/next-export-routes.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@
256256
"/templates/CollapsibleSingleAccordion/",
257257
"/templates/CollapsibleWithoutCard/",
258258
"/templates/ColumnResizeHookUsage/",
259+
"/templates/ColumnsVirtualizationHookUsage/",
259260
"/templates/CommandPaletteAsyncSearch/",
260261
"/templates/CommandPaletteAutoGrouped/",
261262
"/templates/CommandPaletteCustomFooter/",

‎apps/storybook/stories/TableVirtualization.stories.tsx‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import {
44
Card,
55
Table,
66
VStack,
7+
pixel,
8+
useTableColumnsVirtualization,
79
useTableRowsVirtualization,
10+
useTableStickyColumns,
11+
type TableColumn,
812
} from '@astryxdesign/core';
913
const rows = Array.from({length: 2000}, (_, index) => ({
1014
id: String(index),
@@ -65,3 +69,44 @@ function InCard() {
6569
export const InSizedCard: StoryObj = {
6670
render: () => <InCard />,
6771
};
72+
73+
type WideRow = Record<string, string> & {id: string};
74+
const wideColumns: TableColumn<WideRow>[] = [
75+
{key: 'name', header: 'Name', width: pixel(160)},
76+
...Array.from({length: 299}, (_, index) => ({
77+
key: `metric${index + 1}`,
78+
header: `Metric ${index + 1}`,
79+
width: pixel(120),
80+
})),
81+
];
82+
const wideRows: WideRow[] = Array.from({length: 200}, (_, row) => {
83+
const item: WideRow = {id: String(row), name: `Account ${row + 1}`};
84+
for (let index = 1; index < 300; index++) {
85+
item[`metric${index}`] = `${row + 1}.${index}`;
86+
}
87+
return item;
88+
});
89+
90+
function ColumnWindowingExample() {
91+
const virtualization = useTableColumnsVirtualization({
92+
initialViewportWidth: 900,
93+
});
94+
const sticky = useTableStickyColumns<WideRow>({startKeys: ['name']});
95+
// 300 columns and 200 rows: only the columns near the viewport mount.
96+
return (
97+
<VStack width={900}>
98+
<Table
99+
aria-label="Account metrics"
100+
data={wideRows}
101+
columns={wideColumns}
102+
idKey="id"
103+
plugins={{sticky}}
104+
virtualization={virtualization}
105+
dividers="grid"
106+
/>
107+
</VStack>
108+
);
109+
}
110+
export const ColumnWindowing: StoryObj = {
111+
render: () => <ColumnWindowingExample />,
112+
};

‎internal/shadcn-registry/routes.lock.json‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3966,6 +3966,11 @@
39663966
"path": "examples/use-table-column-settings-state/column-visibility-picker",
39673967
"aliases": []
39683968
},
3969+
{
3970+
"name": "example-use-table-columns-virtualization-wide-table",
3971+
"path": "examples/use-table-columns-virtualization/wide-table",
3972+
"aliases": []
3973+
},
39693974
{
39703975
"name": "example-use-table-grouped-rows-collapsible-groups",
39713976
"path": "examples/use-table-grouped-rows/collapsible-groups",
@@ -4381,6 +4386,11 @@
43814386
"path": "hooks/use-table-column-settings-state",
43824387
"aliases": []
43834388
},
4389+
{
4390+
"name": "hook-use-table-columns-virtualization",
4391+
"path": "hooks/use-table-columns-virtualization",
4392+
"aliases": []
4393+
},
43844394
{
43854395
"name": "hook-use-table-filter-state",
43864396
"path": "hooks/use-table-filter-state",
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/** @type {import('@astryxdesign/cli/authoring').TemplateDoc} */
4+
export const doc = {
5+
type: 'block',
6+
exampleFor: 'useTableColumnsVirtualization',
7+
name: 'useTableColumnsVirtualization - Wide Table',
8+
displayName: 'useTableColumnsVirtualization - Wide Table',
9+
description:
10+
'A Table with 121 columns: a pinned Sensor column and 120 monthly readings at fixed widths. Scroll horizontally to mount the columns near the scroll position; the pinned column always renders. initialViewportWidth sizes only the first window. The caller keeps all data and columns; this hook does not virtualize rows.',
11+
isReady: true,
12+
aspectRatio: 16 / 9,
13+
componentsUsed: ['Table'],
14+
};
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
'use client';
4+
5+
import {
6+
Table,
7+
pixel,
8+
useTableColumnsVirtualization,
9+
useTableStickyColumns,
10+
type TableColumn,
11+
} from '@astryxdesign/core/Table';
12+
13+
interface Reading extends Record<string, unknown> {
14+
id: string;
15+
sensor: string;
16+
}
17+
18+
const MONTHS = 120;
19+
const readings: Reading[] = Array.from({length: 40}, (_, row) => {
20+
const reading: Reading = {id: `sensor-${row}`, sensor: `Sensor ${row + 1}`};
21+
for (let month = 0; month < MONTHS; month++) {
22+
reading[`m${month}`] = ((row * 7 + month * 3) % 90) + 10;
23+
}
24+
return reading;
25+
});
26+
const columns: TableColumn<Reading>[] = [
27+
{key: 'sensor', header: 'Sensor', width: pixel(160)},
28+
...Array.from({length: MONTHS}, (_, month): TableColumn<Reading> => ({
29+
key: `m${month}`,
30+
header: `Month ${month + 1}`,
31+
width: pixel(110),
32+
align: 'end',
33+
})),
34+
];
35+
36+
export default function ColumnsVirtualizationHookUsage() {
37+
const virtualization = useTableColumnsVirtualization({
38+
initialViewportWidth: 960,
39+
});
40+
// The pinned Sensor column always renders. Only the unpinned columns near
41+
// the horizontal scroll position mount. initialViewportWidth sizes only the
42+
// first window, before the Table measures its scroll area.
43+
const sticky = useTableStickyColumns<Reading>({startKeys: ['sensor']});
44+
return (
45+
<Table
46+
data={readings}
47+
columns={columns}
48+
idKey="id"
49+
plugins={{sticky}}
50+
virtualization={virtualization}
51+
/>
52+
);
53+
}

0 commit comments

Comments
 (0)