Skip to content

DataGrid: split scroll/scroll to position/should focus position handling into hooks#3986

Open
nstepien wants to merge 4 commits intomainfrom
split-grid
Open

DataGrid: split scroll/scroll to position/should focus position handling into hooks#3986
nstepien wants to merge 4 commits intomainfrom
split-grid

Conversation

@nstepien
Copy link
Collaborator

@nstepien nstepien commented Feb 26, 2026

src/DataGrid.tsx is quite fat, so I'm trying to slim it down.
This improves co-locality too!

@nstepien nstepien self-assigned this Feb 26, 2026
return scrollStateMap.get(gridRef) ?? initialScrollState;
}, [gridRef]);

return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Scroll state now uses useSyncExternalStore instead of 2xuseState+flushSync

gridRef: React.RefObject<HTMLDivElement | null>;
selectedPosition: { idx: number; rowIdx: number };
}) {
const shouldFocusPositionRef = useRef(false);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This used to be a useState, but an eslint rule complained about setting state in effect.
Strangely it did not complain when the same code was in DataGrid, probably because it's too complicated for static analysis.
Though to make sure the effect works, I had to change the selectedPosition.idx dependency to selectedPosition.
But now we 1 fewer state that can trigger re-renders!

@nstepien nstepien marked this pull request as ready for review February 26, 2026 20:57
@codecov-commenter
Copy link

codecov-commenter commented Feb 27, 2026

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.57%. Comparing base (a80e036) to head (140c714).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3986      +/-   ##
==========================================
+ Coverage   97.53%   97.57%   +0.03%     
==========================================
  Files          36       38       +2     
  Lines        1463     1486      +23     
  Branches      472      478       +6     
==========================================
+ Hits         1427     1450      +23     
  Misses         36       36              
Files with missing lines Coverage Δ
src/DataGrid.tsx 98.92% <100.00%> (-0.07%) ⬇️
src/hooks/useGridDimensions.ts 100.00% <100.00%> (ø)
src/hooks/useScrollState.ts 100.00% <100.00%> (ø)
src/hooks/useScrollToPosition.tsx 100.00% <100.00%> (ø)
src/hooks/useShouldFocusPosition.ts 100.00% <100.00%> (ø)
src/utils/domUtils.ts 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

setScrollToPosition,
scrollToPositionElement: scrollToPosition && (
<div
ref={(div) => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

before: useRef+useLayoutEffect
now: 1 function
Seems to work just as well.
Since it doesn't need any hooks anymore, I've also inlined the component

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants