Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/table-columns-virtualization-cli-example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/cli': patch
---

[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.

@humbertovirtudes
7 changes: 7 additions & 0 deletions .changeset/table-columns-virtualization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/core': patch
---

[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. In this release, focus in an unpinned column is lost when the column scrolls out of the window, and column changes do not keep the first visible column in place. 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 render as before; their Table import carries about 0.4 KB gzip of column windowing wiring, and the column planner loads only with the hook.

@humbertovirtudes
1 change: 1 addition & 0 deletions apps/sandbox/scripts/next-export-routes.json
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@
"/templates/CollapsibleSingleAccordion/",
"/templates/CollapsibleWithoutCard/",
"/templates/ColumnResizeHookUsage/",
"/templates/ColumnsVirtualizationHookUsage/",
"/templates/CommandPaletteAsyncSearch/",
"/templates/CommandPaletteAutoGrouped/",
"/templates/CommandPaletteCustomFooter/",
Expand Down
45 changes: 45 additions & 0 deletions apps/storybook/stories/TableVirtualization.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import {
Card,
Table,
VStack,
pixel,
useTableColumnsVirtualization,
useTableRowsVirtualization,
useTableStickyColumns,
type TableColumn,
} from '@astryxdesign/core';
const rows = Array.from({length: 2000}, (_, index) => ({
id: String(index),
Expand Down Expand Up @@ -65,3 +69,44 @@ function InCard() {
export const InSizedCard: StoryObj = {
render: () => <InCard />,
};

type WideRow = Record<string, string> & {id: string};
const wideColumns: TableColumn<WideRow>[] = [
{key: 'name', header: 'Name', width: pixel(160)},
...Array.from({length: 299}, (_, index) => ({
key: `metric${index + 1}`,
header: `Metric ${index + 1}`,
width: pixel(120),
})),
];
const wideRows: WideRow[] = Array.from({length: 200}, (_, row) => {
const item: WideRow = {id: String(row), name: `Account ${row + 1}`};
for (let index = 1; index < 300; index++) {
item[`metric${index}`] = `${row + 1}.${index}`;
}
return item;
});

function ColumnWindowingExample() {
const virtualization = useTableColumnsVirtualization({
initialViewportWidth: 900,
});
const sticky = useTableStickyColumns<WideRow>({startKeys: ['name']});
// 300 columns and 200 rows: only the columns near the viewport mount.
return (
<VStack width={900}>
<Table
aria-label="Account metrics"
data={wideRows}
columns={wideColumns}
idKey="id"
plugins={{sticky}}
virtualization={virtualization}
dividers="grid"
/>
</VStack>
);
}
export const ColumnWindowing: StoryObj = {
render: () => <ColumnWindowingExample />,
};
10 changes: 10 additions & 0 deletions internal/shadcn-registry/routes.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -3966,6 +3966,11 @@
"path": "examples/use-table-column-settings-state/column-visibility-picker",
"aliases": []
},
{
"name": "example-use-table-columns-virtualization-wide-table",
"path": "examples/use-table-columns-virtualization/wide-table",
"aliases": []
},
{
"name": "example-use-table-grouped-rows-collapsible-groups",
"path": "examples/use-table-grouped-rows/collapsible-groups",
Expand Down Expand Up @@ -4386,6 +4391,11 @@
"path": "hooks/use-table-column-settings-state",
"aliases": []
},
{
"name": "hook-use-table-columns-virtualization",
"path": "hooks/use-table-columns-virtualization",
"aliases": []
},
{
"name": "hook-use-table-filter-state",
"path": "hooks/use-table-filter-state",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.

/** @type {import('@astryxdesign/cli/authoring').TemplateDoc} */
export const doc = {
type: 'block',
exampleFor: 'useTableColumnsVirtualization',
name: 'useTableColumnsVirtualization - Wide Table',
displayName: 'useTableColumnsVirtualization - Wide Table',
description:
'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.',
isReady: true,
aspectRatio: 16 / 9,
componentsUsed: ['Table'],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.

'use client';

import {
Table,
pixel,
useTableColumnsVirtualization,
useTableStickyColumns,
type TableColumn,
} from '@astryxdesign/core/Table';

interface Reading extends Record<string, unknown> {
id: string;
sensor: string;
}

const MONTHS = 120;
const readings: Reading[] = Array.from({length: 40}, (_, row) => {
const reading: Reading = {id: `sensor-${row}`, sensor: `Sensor ${row + 1}`};
for (let month = 0; month < MONTHS; month++) {
reading[`m${month}`] = ((row * 7 + month * 3) % 90) + 10;
}
return reading;
});
const columns: TableColumn<Reading>[] = [
{key: 'sensor', header: 'Sensor', width: pixel(160)},
...Array.from({length: MONTHS}, (_, month): TableColumn<Reading> => ({
key: `m${month}`,
header: `Month ${month + 1}`,
width: pixel(110),
align: 'end',
})),
];

export default function ColumnsVirtualizationHookUsage() {
const virtualization = useTableColumnsVirtualization({
initialViewportWidth: 960,
});
// The pinned Sensor column always renders. Only the unpinned columns near
// the horizontal scroll position mount. initialViewportWidth sizes only the
// first window, before the Table measures its scroll area.
const sticky = useTableStickyColumns<Reading>({startKeys: ['sensor']});
return (
<Table
data={readings}
columns={columns}
idKey="id"
plugins={{sticky}}
virtualization={virtualization}
/>
);
}
Loading
Loading