Skip to content
Open
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
248 changes: 126 additions & 122 deletions src/DataGrid.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
Activity,
useCallback,
useImperativeHandle,
useLayoutEffect,
Expand Down Expand Up @@ -353,7 +354,8 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
[columnWidths]
);

const [gridRef, gridWidth, gridHeight, horizontalScrollbarHeight] = useGridDimensions();
const [gridRef, activityMode, gridWidth, gridHeight, horizontalScrollbarHeight] =
useGridDimensions();
const {
columns,
colSpanColumns,
Expand Down Expand Up @@ -1229,134 +1231,136 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
data-testid={testId}
data-cy={dataCy}
>
<DataGridDefaultRenderersContext value={defaultGridComponents}>
<HeaderRowSelectionChangeContext value={selectHeaderRowLatest}>
<HeaderRowSelectionContext value={headerSelectionValue}>
{Array.from({ length: groupedColumnHeaderRowsCount }, (_, index) => (
<GroupedColumnHeaderRow
key={index}
rowIdx={index + 1}
level={-groupedColumnHeaderRowsCount + index}
columns={getRowViewportColumns(minRowIdx + index)}
<Activity mode={activityMode}>
<DataGridDefaultRenderersContext value={defaultGridComponents}>
<HeaderRowSelectionChangeContext value={selectHeaderRowLatest}>
<HeaderRowSelectionContext value={headerSelectionValue}>
{Array.from({ length: groupedColumnHeaderRowsCount }, (_, index) => (
<GroupedColumnHeaderRow
key={index}
rowIdx={index + 1}
level={-groupedColumnHeaderRowsCount + index}
columns={getRowViewportColumns(minRowIdx + index)}
selectedCellIdx={
selectedPosition.rowIdx === minRowIdx + index ? selectedPosition.idx : undefined
}
selectCell={selectHeaderCellLatest}
/>
))}
<HeaderRow
headerRowClass={headerRowClass}
rowIdx={headerRowsCount}
columns={getRowViewportColumns(mainHeaderRowIdx)}
onColumnResize={handleColumnResizeLatest}
onColumnResizeEnd={handleColumnResizeEndLatest}
onColumnsReorder={onColumnsReorderLastest}
sortColumns={sortColumns}
onSortColumnsChange={onSortColumnsChangeLatest}
lastFrozenColumnIndex={lastFrozenColumnIndex}
selectedCellIdx={
selectedPosition.rowIdx === minRowIdx + index ? selectedPosition.idx : undefined
selectedPosition.rowIdx === mainHeaderRowIdx ? selectedPosition.idx : undefined
}
selectCell={selectHeaderCellLatest}
shouldFocusGrid={!selectedCellIsWithinSelectionBounds}
direction={direction}
/>
))}
<HeaderRow
headerRowClass={headerRowClass}
rowIdx={headerRowsCount}
columns={getRowViewportColumns(mainHeaderRowIdx)}
onColumnResize={handleColumnResizeLatest}
onColumnResizeEnd={handleColumnResizeEndLatest}
onColumnsReorder={onColumnsReorderLastest}
sortColumns={sortColumns}
onSortColumnsChange={onSortColumnsChangeLatest}
lastFrozenColumnIndex={lastFrozenColumnIndex}
selectedCellIdx={
selectedPosition.rowIdx === mainHeaderRowIdx ? selectedPosition.idx : undefined
}
selectCell={selectHeaderCellLatest}
shouldFocusGrid={!selectedCellIsWithinSelectionBounds}
direction={direction}
/>
</HeaderRowSelectionContext>
</HeaderRowSelectionChangeContext>
{rows.length === 0 && noRowsFallback ? (
noRowsFallback
) : (
<>
{topSummaryRows?.map((row, rowIdx) => {
const gridRowStart = headerRowsCount + 1 + rowIdx;
const summaryRowIdx = mainHeaderRowIdx + 1 + rowIdx;
const isSummaryRowSelected = selectedPosition.rowIdx === summaryRowIdx;
const top = headerRowsHeight + summaryRowHeight * rowIdx;

return (
<SummaryRow
key={rowIdx}
aria-rowindex={gridRowStart}
rowIdx={summaryRowIdx}
gridRowStart={gridRowStart}
row={row}
top={top}
bottom={undefined}
viewportColumns={getRowViewportColumns(summaryRowIdx)}
lastFrozenColumnIndex={lastFrozenColumnIndex}
selectedCellIdx={isSummaryRowSelected ? selectedPosition.idx : undefined}
isTop
selectCell={selectCellLatest}
/>
);
})}
<RowSelectionChangeContext value={selectRowLatest}>
{getViewportRows()}
</RowSelectionChangeContext>
{bottomSummaryRows?.map((row, rowIdx) => {
const gridRowStart = headerAndTopSummaryRowsCount + rows.length + rowIdx + 1;
const summaryRowIdx = rows.length + rowIdx;
const isSummaryRowSelected = selectedPosition.rowIdx === summaryRowIdx;
const top =
clientHeight > totalRowHeight
? gridHeight - summaryRowHeight * (bottomSummaryRows.length - rowIdx)
: undefined;
const bottom =
top === undefined
? summaryRowHeight * (bottomSummaryRows.length - 1 - rowIdx)
: undefined;

return (
<SummaryRow
aria-rowindex={ariaRowCount - bottomSummaryRowsCount + rowIdx + 1}
key={rowIdx}
rowIdx={summaryRowIdx}
gridRowStart={gridRowStart}
row={row}
top={top}
bottom={bottom}
viewportColumns={getRowViewportColumns(summaryRowIdx)}
lastFrozenColumnIndex={lastFrozenColumnIndex}
selectedCellIdx={isSummaryRowSelected ? selectedPosition.idx : undefined}
isTop={false}
selectCell={selectCellLatest}
/>
);
</HeaderRowSelectionContext>
</HeaderRowSelectionChangeContext>
{rows.length === 0 && noRowsFallback ? (
noRowsFallback
) : (
<>
{topSummaryRows?.map((row, rowIdx) => {
const gridRowStart = headerRowsCount + 1 + rowIdx;
const summaryRowIdx = mainHeaderRowIdx + 1 + rowIdx;
const isSummaryRowSelected = selectedPosition.rowIdx === summaryRowIdx;
const top = headerRowsHeight + summaryRowHeight * rowIdx;

return (
<SummaryRow
key={rowIdx}
aria-rowindex={gridRowStart}
rowIdx={summaryRowIdx}
gridRowStart={gridRowStart}
row={row}
top={top}
bottom={undefined}
viewportColumns={getRowViewportColumns(summaryRowIdx)}
lastFrozenColumnIndex={lastFrozenColumnIndex}
selectedCellIdx={isSummaryRowSelected ? selectedPosition.idx : undefined}
isTop
selectCell={selectCellLatest}
/>
);
})}
<RowSelectionChangeContext value={selectRowLatest}>
{getViewportRows()}
</RowSelectionChangeContext>
{bottomSummaryRows?.map((row, rowIdx) => {
const gridRowStart = headerAndTopSummaryRowsCount + rows.length + rowIdx + 1;
const summaryRowIdx = rows.length + rowIdx;
const isSummaryRowSelected = selectedPosition.rowIdx === summaryRowIdx;
const top =
clientHeight > totalRowHeight
? gridHeight - summaryRowHeight * (bottomSummaryRows.length - rowIdx)
: undefined;
const bottom =
top === undefined
? summaryRowHeight * (bottomSummaryRows.length - 1 - rowIdx)
: undefined;

return (
<SummaryRow
aria-rowindex={ariaRowCount - bottomSummaryRowsCount + rowIdx + 1}
key={rowIdx}
rowIdx={summaryRowIdx}
gridRowStart={gridRowStart}
row={row}
top={top}
bottom={bottom}
viewportColumns={getRowViewportColumns(summaryRowIdx)}
lastFrozenColumnIndex={lastFrozenColumnIndex}
selectedCellIdx={isSummaryRowSelected ? selectedPosition.idx : undefined}
isTop={false}
selectCell={selectCellLatest}
/>
);
})}
</>
)}
</DataGridDefaultRenderersContext>

{getDragHandle()}

{/* render empty cells that span only 1 column so we can safely measure column widths, regardless of colSpan */}
{renderMeasuringCells(viewportColumns)}

{/* extra div is needed for row navigation in a treegrid */}
{isTreeGrid && (
<div
ref={focusSinkRef}
tabIndex={isGroupRowFocused ? 0 : -1}
className={classnames(focusSinkClassname, {
[focusSinkHeaderAndSummaryClassname]: !isRowIdxWithinViewportBounds(
selectedPosition.rowIdx
),
[rowSelected]: isGroupRowFocused,
[rowSelectedWithFrozenCell]: isGroupRowFocused && lastFrozenColumnIndex !== -1
})}
</>
style={{
gridRowStart: selectedPosition.rowIdx + headerAndTopSummaryRowsCount + 1
}}
/>
)}
</DataGridDefaultRenderersContext>

{getDragHandle()}

{/* render empty cells that span only 1 column so we can safely measure column widths, regardless of colSpan */}
{renderMeasuringCells(viewportColumns)}

{/* extra div is needed for row navigation in a treegrid */}
{isTreeGrid && (
<div
ref={focusSinkRef}
tabIndex={isGroupRowFocused ? 0 : -1}
className={classnames(focusSinkClassname, {
[focusSinkHeaderAndSummaryClassname]: !isRowIdxWithinViewportBounds(
selectedPosition.rowIdx
),
[rowSelected]: isGroupRowFocused,
[rowSelectedWithFrozenCell]: isGroupRowFocused && lastFrozenColumnIndex !== -1
})}
style={{
gridRowStart: selectedPosition.rowIdx + headerAndTopSummaryRowsCount + 1
}}
/>
)}

{scrollToPosition !== null && (
<ScrollToCell
scrollToPosition={scrollToPosition}
setScrollToCellPosition={setScrollToPosition}
gridRef={gridRef}
/>
)}
{scrollToPosition !== null && (
<ScrollToCell
scrollToPosition={scrollToPosition}
setScrollToCellPosition={setScrollToPosition}
gridRef={gridRef}
/>
)}
</Activity>
</div>
);
}
Expand Down
41 changes: 32 additions & 9 deletions src/hooks/useGridDimensions.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
import { useLayoutEffect, useRef, useState } from 'react';
import { useLayoutEffect, useRef, useState, type ActivityProps } from 'react';
import { flushSync } from 'react-dom';

export function useGridDimensions() {
const gridRef = useRef<HTMLDivElement>(null);
const [inlineSize, setInlineSize] = useState(1);
const [blockSize, setBlockSize] = useState(1);
const [horizontalScrollbarHeight, setHorizontalScrollbarHeight] = useState(0);
const [isMeasured, setIsMeasured] = useState(false);
const [isVisible, setIsVisible] = useState(true);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests break if I do not leave this initial state as true.
Should be fine once we move all our tests to use locators instead.

const [isDocumentVisible, setIsDocumentVisible] = useState(false);
const activityMode: NonNullable<ActivityProps['mode']> =
isMeasured && isVisible && isDocumentVisible ? 'visible' : 'hidden';

useLayoutEffect(() => {
const { ResizeObserver } = window;
const { ResizeObserver, IntersectionObserver } = window;

// don't break in Node.js (SSR), jsdom, and browsers that don't support ResizeObserver
Copy link
Collaborator Author

@nstepien nstepien Feb 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SSR environments do not run useLayoutEffect, not sure that part of the comment was ever applicable.

// don't break in environments like JSDOM that do not support observers
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (ResizeObserver == null) return;
if (ResizeObserver == null || IntersectionObserver == null) return;

const { clientWidth, clientHeight, offsetWidth, offsetHeight } = gridRef.current!;
const { width, height } = gridRef.current!.getBoundingClientRect();
const grid = gridRef.current!;
const { clientWidth, clientHeight, offsetWidth, offsetHeight } = grid;
const { width, height } = grid.getBoundingClientRect();
const initialHorizontalScrollbarHeight = offsetHeight - clientHeight;
const initialWidth = width - offsetWidth + clientWidth;
const initialHeight = height - initialHorizontalScrollbarHeight;

setInlineSize(initialWidth);
setBlockSize(initialHeight);
setHorizontalScrollbarHeight(initialHorizontalScrollbarHeight);
setIsMeasured(true);

const resizeObserver = new ResizeObserver((entries) => {
const size = entries[0].contentBoxSize[0];
const { clientHeight, offsetHeight } = gridRef.current!;
const { clientHeight, offsetHeight } = grid;

// we use flushSync here to avoid flashing scrollbars
flushSync(() => {
Expand All @@ -35,12 +42,28 @@ export function useGridDimensions() {
setHorizontalScrollbarHeight(offsetHeight - clientHeight);
});
});
resizeObserver.observe(gridRef.current!);

const intersectionObserver = new IntersectionObserver((entries) => {
flushSync(() => {
setIsVisible(entries[0].isIntersecting);
});
});

function onVisibilityChange() {
setIsDocumentVisible(!document.hidden);
}

resizeObserver.observe(grid);
intersectionObserver.observe(grid);
document.addEventListener('visibilitychange', onVisibilityChange);
onVisibilityChange();

return () => {
resizeObserver.disconnect();
intersectionObserver.disconnect();
document.removeEventListener('visibilitychange', onVisibilityChange);
};
}, []);

return [gridRef, inlineSize, blockSize, horizontalScrollbarHeight] as const;
return [gridRef, activityMode, inlineSize, blockSize, horizontalScrollbarHeight] as const;
}
4 changes: 2 additions & 2 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function getInstances(): BrowserInstanceOption[] {
{
browser: 'chromium',
provider: playwright({
actionTimeout: 1000,
actionTimeout: 2000,
contextOptions: {
viewport
},
Expand All @@ -81,7 +81,7 @@ function getInstances(): BrowserInstanceOption[] {
{
browser: 'firefox',
provider: playwright({
actionTimeout: 1000,
actionTimeout: 2000,
contextOptions: {
viewport
}
Expand Down