Skip to content

[modal] Fix the scroll lock of stacked modals with disableScrollLock - #49915

Open
LukasTy wants to merge 6 commits into
mui:masterfrom
LukasTy:claude/modal-scroll-lock-owners
Open

LukasTy wants to merge 6 commits into
mui:masterfrom
LukasTy:claude/modal-scroll-lock-owners

Conversation

@LukasTy

@LukasTy LukasTy commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Fixes the scroll lock when stacked modals on one container differ in disableScrollLock.

Before, the first modal that mounted on a container decided the lock for every modal on top of it, and the lock lasted until the last modal closed:

  • A Dialog with disableScrollLock is open, and a Menu, Select, Popover, or Dialog opens on top. The page does not lock.
  • A Dialog is open, and a Popover with disableScrollLock opens on top. The Dialog closes first. The page stays locked until the Popover closes.

Now each modal that mounts without disableScrollLock owns the lock. The first owner locks the scroll container, and the last owner restores it, even when modals without the lock stay open.

Changes

  • ModalManager tracks the lock owners for each scroll container, not for each container. Modals in different containers that resolve to the same scroll container share one lock, for example a body modal and a modal in a shadow root. Before, each container saved its own copy of the styles, so a close in the wrong order left the page locked.
  • Each document keeps one copy of the .mui-fixed padding for all its scroll locks. The first lock pads the elements, and the last lock restores them.
  • handleContainer is now getScrollContainer, lockScroll, and padFixedElements. Each container keeps its scroll container while it holds the lock, because the lock changes the styles that the resolution reads. The next lock resolves it again, for example after a media query changes the overflow-y of html.
  • useModal mounts every open modal from its portal ref, not only the top one. Before, a modal below another one that opened in the same render never mounted. This did not matter while the lock lasted until the last modal closed.
  • ModalManager.mount() ignores a modal that is not added yet. With disablePortal, the portal ref runs before add().
  • useModal updates the lock when disableScrollLock changes while the modal is open. ModalManager.mount() with disableScrollLock now releases the lock of that modal. Before, a change to the prop had no effect until the modal closed.

Behavior change

A Menu, Select, Popover, or temporary Drawer inside a Dialog or Drawer with disableScrollLock now locks the page while it is open, the same as outside the Dialog. With classic scrollbars, the parent Dialog or Drawer moves by up to the scrollbar width while the child holds the lock. The lock pads only the scroll container and the .mui-fixed elements. To keep the page scrollable, set disableScrollLock on the child too, for example MenuProps={{ disableScrollLock: true }} on Select. The v10 migration guide has an entry for this change.

With two overflowing containers open at the same time, .mui-fixed elements now get the scrollbar padding one time, not two times.

ModalManager is a public export. Its signatures do not change. A modal that is added but never mounted no longer keeps the lock.

Found in the Menu v2 review: #48823

Merge order: merge this PR after Menu v2 and its v9 cherry-pick. This PR is for v10 only. Before the merge, it moves the Menu v2 lockScroll() to the lock owners.

Generated with Claude Code

Each modal that mounts without disableScrollLock now owns the scroll lock. The first owner locks the container, and the last owner restores it, even when modals without the lock stay open.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@code-infra-dashboard

code-infra-dashboard Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Deploy preview

Bundle size

Bundle Parsed size Gzip size
@mui/material 🔺+803B(+0.14%) 🔺+264B(+0.16%)
@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

Performance

No regressions in 32 benchmarks · precise to ±9.0% (±13.9% at worst) · ran 4m 06s · details


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

@LukasTy LukasTy added type: bug It doesn't behave as expected. scope: modal Changes related to the modal. labels Nov 25, 2026
LukasTy and others added 4 commits November 25, 2026 17:23
The modal did not register as a lock owner when the prop changed to false after it opened. When the dialog below it closed, the page unlocked behind it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Modals in different containers can lock the same scroll container, for example a body modal and a modal in a shadow root. Each container saved its own copy of the styles, so a close in the wrong order left the page locked. The `.mui-fixed` padding had the same problem across overflowing containers.

Now each scroll container has one lock for all its owners, and each document has one copy of the `.mui-fixed` padding. The first owner applies the styles, and the last owner restores them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A lock that measured no page scrollbar created the shared `.mui-fixed` record with a zero size. A later lock that measured a scrollbar then did not pad the fixed elements.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A container kept its first scroll container while it existed. A modal without the lock keeps the container, so a later lock used the old element. For example, after a media query set `overflow-y: scroll` on html, the lock went to body, and the page scrolled.

Now the container keeps the scroll container only while one of its modals holds the lock.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@LukasTy

LukasTy commented Nov 26, 2026

Copy link
Copy Markdown
Member Author

GPT Astra review

Looks good to merge at 0148efd0de. Each open modal now controls whether it needs a scroll lock, and the last modal using a scroll container restores its styles. For example, a Menu opened inside a Dialog with disableScrollLock now locks the page while the Menu is open. To keep the page scrollable throughout, set disableScrollLock on the Menu too.

The review covered closing the lower modal first, changing disableScrollLock while open, and body and shadow-root portals sharing a scroll lock. I also verified in Chromium that reopening a modal after a breakpoint changes the page’s scroll container locks the correct element and prevents background scrolling.

One visual tradeoff remains. This PR fixes double padding for a .mui-fixed toolbar that already exists when a panel modal opens. If the toolbar mounts after the panel modal opens, opening a second dialog on document.body can shift the toolbar by one scrollbar width. It stayed aligned on the PR parent and moved 15px in the Chromium reproduction. Closing the dialog restores its position. I’m comfortable accepting that edge case in this PR.

The Modal and ModalManager jsdom suites passed locally (95 passed, 3 skipped). Unit, browser, and legacy browser CI passed on the reviewed commit.

@LukasTy
LukasTy marked this pull request as ready for review November 26, 2026 10:12

@aarongarciah aarongarciah 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.

Reviewed at 0148efd0de. I found no correctness issues. One request about how the behavior change is communicated, and one missing test.

What I checked

  • Modal, Dialog, Menu, Popover, Select, and Drawer tests pass locally in jsdom and Chromium.
  • Extra cases, all passing: StrictMode, keepMounted with disableScrollLock toggled while closed and while open, reopening during a closeAfterTransition exit, changing container while open, two disablePortal modals opening together, and a Dialog and its Menu closing together.
  • One more case fails on master and passes here: a locking modal and a non-locking modal above it that open in the same commit. On master the page never locks.

1. The behavior change moves the parent overlay, and the v10 guide doesn't mention it

With classic scrollbars (Windows, Linux, or macOS with scrollbars always shown), the parent Dialog or Drawer moves while the inner Menu, Select, or Popover holds the lock. The lock pads the scroll container and .mui-fixed elements only, so the parent becomes one of the "fixed positioned elements" from the FAQ.

Measured in Chromium with a 15px scrollbar, when a Select inside the parent opens:

Parent master this PR
<Dialog disableScrollLock> paper no move moves 7.5px
<Drawer anchor="right" ModalProps={{ disableScrollLock: true }}> paper no move moves 15px

The new rule (each modal decides for itself) makes sense to me. But the PR has no breaking change label and upgrade-to-v10.md has no entry, so users who relied on the parent's disableScrollLock won't find the fix. Could you add both? #49110 is a similar default change that alpha.0 lists under Breaking Changes. Something like:

### Scroll lock of stacked modals

Each modal now decides its own scroll lock.
Before, the first modal that opened in a container decided for all the modals above it.

A Menu, Select, Popover, or temporary Drawer inside a Dialog or Drawer with `disableScrollLock` now locks the page while it is open.
With classic scrollbars, the parent can move by up to the scrollbar width while the lock is held.

To keep the page scrollable, set `disableScrollLock` on the child too:

```diff
 <Dialog disableScrollLock>
-  <Select>
+  <Select MenuProps={{ disableScrollLock: true }}>
```

2. The scroll container cache has no test

The description explains why each container keeps its scroll container while it holds the lock, and the reasoning is right. But no test covers it: with containerInfo.scrollContainer ??= … changed to resolve every time, all the Modal tests still pass. In that case, with html { overflow-y: scroll } and two modals on body, the second modal resolves to body, and <html> stays locked after both close.

This test passes here and fails without the cache, in jsdom and Chromium:

it('should keep the scroll container while a modal holds the lock', () => {
  const modal1 = getDummyModal();
  const modal2 = getDummyModal();
  document.documentElement.style.overflowY = 'scroll';
  modalManager.add(modal1, document.body);
  modalManager.mount(modal1, {});
  // The lock sets `overflow: hidden` on html, so a new resolution would return body.
  modalManager.add(modal2, document.body);
  modalManager.mount(modal2, {});
  expect(document.documentElement.style.overflow).to.equal('hidden');
  expect(document.body.style.overflow).to.equal('');

  modalManager.remove(modal1);
  modalManager.remove(modal2);
  expect(document.documentElement.style.overflow).to.equal('');
  expect(document.documentElement.style.overflowY).to.equal('scroll');
  document.documentElement.style.removeProperty('overflow-y');
});

@LukasTy LukasTy added breaking change Introduces changes that are not backward compatible. v10.x labels Nov 27, 2026
The guide entry explains that a child overlay inside a parent with `disableScrollLock` now takes the lock, and that the parent can move with classic scrollbars.

The test checks that a container keeps its scroll container while it holds the lock. Without the cache, the second modal locks body, and html stays locked after both modals close.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@LukasTy

LukasTy commented Nov 27, 2026

Copy link
Copy Markdown
Member Author

Thanks for the review, @aarongarciah.

  1. I added both in 93ebf69. The PR now has the breaking change and v10.x labels, and the v10 guide has your entry, with small wording changes. The "Behavior change" section of the PR body now also says that the parent moves.

  2. I added your test in 93ebf69. With ??= changed to =, it fails in jsdom and Chromium. With the cache, it passes in jsdom, Chromium, WebKit, and Firefox.

Merge order: this PR now waits for Menu v2 and its v9 cherry-pick, and it is for v10 only. Menu v2 has its own lock on the current ModalManager, so its v9 cherry-pick needs no changes. Before the merge, this PR moves the Menu v2 lockScroll() to the lock owners.

@LukasTy LukasTy added the on hold There is a blocker, we need to wait. label Nov 27, 2026

This branch has not been deployed

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

Labels

breaking change Introduces changes that are not backward compatible. on hold There is a blocker, we need to wait. scope: modal Changes related to the modal. type: bug It doesn't behave as expected. v10.x

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants