Skip to content

[DataGrid] Wait for rows before reacting on autosizeOnMount#22698

Merged
MBilalShafi merged 7 commits into
mui:masterfrom
MBilalShafi:codex/fix-22505-autosize
Jun 18, 2026
Merged

[DataGrid] Wait for rows before reacting on autosizeOnMount#22698
MBilalShafi merged 7 commits into
mui:masterfrom
MBilalShafi:codex/fix-22505-autosize

Conversation

@MBilalShafi

@MBilalShafi MBilalShafi commented Jun 5, 2026

Copy link
Copy Markdown
Member

Delay autosizeOnMount until the initial render context is ready and committed, so fitting rows are measured before column widths are extracted.

Add a browser regression test covering a wide value in the last rendered row.

Fixes #22505

Closes #20551

Delay autosizeOnMount until the initial render context is ready and committed, so fitting rows are measured before column widths are extracted.

Add a browser regression test covering a wide value in the last rendered row.
@MBilalShafi MBilalShafi added type: bug It doesn't behave as expected. scope: data grid Changes related to the data grid. feature: Column resize labels Jun 5, 2026
@code-infra-dashboard

code-infra-dashboard Bot commented Jun 5, 2026

Copy link
Copy Markdown

Deploy preview

Bundle size

Bundle Parsed size Gzip size
@mui/x-data-grid 🔺+480B(+0.12%) 🔺+142B(+0.12%)
@mui/x-data-grid-pro 🔺+480B(+0.09%) 🔺+151B(+0.10%)
@mui/x-data-grid-premium 🔺+480B(+0.07%) 🔺+151B(+0.07%)
@mui/x-charts 0B(0.00%) 0B(0.00%)
@mui/x-charts-pro 0B(0.00%) 0B(0.00%)
@mui/x-charts-premium 0B(0.00%) 0B(0.00%)
@mui/x-date-pickers 0B(0.00%) 0B(0.00%)
@mui/x-date-pickers-pro 0B(0.00%) 0B(0.00%)
@mui/x-tree-view 0B(0.00%) 0B(0.00%)
@mui/x-tree-view-pro 0B(0.00%) 0B(0.00%)
@mui/x-license 0B(0.00%) 0B(0.00%)

Details of bundle changes


Check out the code infra dashboard for more information about this PR.

@MBilalShafi
MBilalShafi marked this pull request as ready for review June 5, 2026 16:35
@MBilalShafi
MBilalShafi requested review from a team and Copilot June 5, 2026 16:35
@MBilalShafi MBilalShafi added needs cherry-pick The PR should be cherry-picked to master after merge. v8.x labels Jun 5, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR adjusts the DataGrid autosizeOnMount behavior so it runs only after the initial render context is ready/committed, ensuring row cells are actually measurable before column widths are extracted (fixing the “last row wide value not considered” bug from #22505).

Changes:

  • Add a render-context readiness gate for autosizeOnMount, driven by renderedRowsIntervalChange/stateChange, and schedule autosize on the next animation frame once ready.
  • Add a browser regression test that reproduces the issue by delaying the final row’s rendered cell content, then asserts the wide value is fully visible after autosizing.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
packages/x-data-grid/src/hooks/features/columnResize/useGridColumnResize.tsx Delays autosizeOnMount until dimensions + row render context indicate rows are committed/ready for measurement.
packages/x-data-grid-pro/src/tests/columns.DataGridPro.test.tsx Adds a regression test for autosize measuring the last rendered row’s wide value.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/x-data-grid-pro/src/tests/columns.DataGridPro.test.tsx
@siriwatknp
siriwatknp self-requested a review June 15, 2026 08:28

@siriwatknp siriwatknp left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, this looks solid. A few nitpicks, none blocking:

  1. isRenderContextReadyForAutosizeOnMount no-scroll vs scroll branch asymmetry. Maybe add a one-line comment.

  2. Autosize now fires only when the readiness guard passes. Worth confirming a layout cannot get stuck.

}
};

unsubscribeStateChange = apiRef.current.subscribeEvent('stateChange', checkRenderContext);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The old code fired autosize once unconditionally. Now it fires only when the readiness guard passes. In a layout where the render context never commits, autosize never fires and the listeners stay subscribed until unmount.

Risk looks low (empty rows hit the early return, mounted grids emit renderedRowsIntervalChange), but there is no timeout fallback. Could a real layout get stuck here? If yes, maybe add a last-resort fallback to keep the old guaranteed-once behavior.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I checked this again. I think we should avoid adding a timeout fallback for now because it would preserve the old “always run once” behavior, but it could also reintroduce the bug by measuring before the initial rows have committed.

The guard runs immediately on mount, then again on stateChange and renderedRowsIntervalChange. Empty rows return ready, and mounted grids should publish a render context once dimensions/rows are committed. So I don’t see a real layout that should get stuck here unless the grid never commits a render context at all, in which case measuring would likely be unreliable anyway.

In b193b65, I added a short comment to explain the scroll/no-scroll asymmetry and a expanded more in docs for the row virtualization limitation: when rows overflow the viewport, autosizing can still only measure the currently rendered row window.

@MBilalShafi
MBilalShafi enabled auto-merge (squash) June 18, 2026 12:36
@MBilalShafi MBilalShafi mentioned this pull request Jun 18, 2026
13 tasks
@MBilalShafi
MBilalShafi disabled auto-merge June 18, 2026 13:17
@MBilalShafi
MBilalShafi enabled auto-merge (squash) June 18, 2026 14:14
@MBilalShafi
MBilalShafi merged commit 33a6002 into mui:master Jun 18, 2026
21 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

Cherry-pick PRs will be created targeting branches: v8.x

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

Labels

feature: Column resize needs cherry-pick The PR should be cherry-picked to master after merge. scope: data grid Changes related to the data grid. type: bug It doesn't behave as expected. v8.x

Projects

None yet

3 participants