Skip to content

[modal][dialog] Fix scrollbar compensation in Shadow DOM#48826

Open
ZeeshanTamboli wants to merge 5 commits into
mui:masterfrom
ZeeshanTamboli:issue-39636-dialog-in-shadow-dom-scroll-jump
Open

[modal][dialog] Fix scrollbar compensation in Shadow DOM#48826
ZeeshanTamboli wants to merge 5 commits into
mui:masterfrom
ZeeshanTamboli:issue-39636-dialog-in-shadow-dom-scroll-jump

Conversation

@ZeeshanTamboli

@ZeeshanTamboli ZeeshanTamboli commented Jul 18, 2026

Copy link
Copy Markdown
Member

Fixes #39636

Fixes scrollbar compensation for modals rendered inside a Shadow DOM by applying padding-right to the element being scroll-locked (in this case always document.body). This prevents the page from shifting when the dialog opens inside Shadow DOM.


Before: https://stackblitz.com/edit/github-hnga8rq5?file=src%2FApp.tsx

After: https://stackblitz.com/edit/8k4rqjn2-cs8zgf7u

@ZeeshanTamboli ZeeshanTamboli added type: bug It doesn't behave as expected. scope: dialog Changes related to the dialog. scope: modal Changes related to the modal. labels Jul 18, 2026
@code-infra-dashboard

code-infra-dashboard Bot commented Jul 18, 2026

Copy link
Copy Markdown

Deploy preview

https://deploy-preview-48826--material-ui.netlify.app/
QR code for https://deploy-preview-48826--material-ui.netlify.app/

Bundle size

Bundle Parsed size Gzip size
@mui/material 🔺+23B(0.00%) ▼-14B(-0.01%)
@mui/lab 0B(0.00%) 0B(0.00%)
@mui/private-theming 0B(0.00%) 0B(0.00%)
@mui/system 0B(0.00%) 0B(0.00%)
@mui/utils 0B(0.00%) 0B(0.00%)

Details of bundle changes


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

@ZeeshanTamboli
ZeeshanTamboli marked this pull request as ready for review July 18, 2026 10:09
@ZeeshanTamboli ZeeshanTamboli changed the title [modal][dialog] Fix dialog in shadow DOM creates scroll jump [modal][dialog] Fix scrollbar compensation in Shadow DOM Jul 18, 2026
@silviuaavram
silviuaavram requested a review from Copilot July 21, 2026 07:24

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 addresses scrollbar “jumping” when Dialog/Modal is rendered from within a Shadow DOM by ensuring scrollbar-width compensation (padding-right) is applied to the element that is actually scroll-locked (e.g. document.body in Shadow DOM cases).

Changes:

  • Refactors handleContainer() to compute scrollContainer earlier and apply scrollbar compensation (padding-right) to scrollContainer instead of the original container.
  • Updates the Shadow DOM test to assert padding-right compensation is applied to document.body (and not the Shadow DOM container).

Reviewed changes

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

File Description
packages/mui-material/src/Modal/ModalManager.ts Applies scrollbar compensation to the computed scrollContainer, including Shadow DOM scenarios.
packages/mui-material/src/Modal/ModalManager.test.ts Strengthens Shadow DOM assertions around body scroll-locking and padding compensation.

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

: container;
}

if (isOverflowing(scrollContainer)) {

@ZeeshanTamboli ZeeshanTamboli Jul 21, 2026

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.

Makes sense. But applied a different suggestion f722940. A clearer fix would be to make isOverflowing() understand both viewport elements.

The Copilot's suggestion above is slightly confusing because it checks body even though documentElement (<html> element) is the element being locked.

Comment thread packages/mui-material/src/Modal/ModalManager.test.ts
@silviuaavram

silviuaavram commented Jul 21, 2026

Copy link
Copy Markdown
Member

Do I need some special setup to repro the issue? On my macos chrome, the scrollbar the does not even show when I'm not scrolling, and when it does show, there's no jumping.

Screen.Recording.2026-07-21.at.10.34.27.mov

@ZeeshanTamboli

Copy link
Copy Markdown
Member Author

Do I need some special setup to repro the issue? On my macos chrome, the scrollbar the does not even show when I'm not scrolling, and when it does show, there's no jumping.

Screen.Recording.2026-07-21.at.10.34.27.mov

I am not sure why. Did you try the sandbox preview in full screen?

ZeeshanTamboli and others added 3 commits July 21, 2026 15:18
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Zeeshan Tamboli <zeeshan.tamboli@gmail.com>
@ZeeshanTamboli

Copy link
Copy Markdown
Member Author

Do I need some special setup to repro the issue? On my macos chrome, the scrollbar the does not even show when I'm not scrolling, and when it does show, there's no jumping.

Screen.Recording.2026-07-21.at.10.34.27.mov

@silviuaavram Can you try this:

On macOS, Chrome hides the scrollbar when you aren't scrolling because of the system's "overlay scrollbar" design. To make it permanently visible, open the Apple Menu > System Settings > Appearance, and change the Show scroll bars setting to Always.

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

Labels

scope: dialog Changes related to the dialog. scope: modal Changes related to the modal. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ModalManager] Dialog in Shadow DOM creates scroll jump

3 participants