Repository navigation
Conversation
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>
Deploy previewBundle size
PerformanceNo 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. |
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>
GPT Astra reviewLooks good to merge at The review covered closing the lower modal first, changing One visual tradeoff remains. This PR fixes double padding for a The Modal and ModalManager jsdom suites passed locally (95 passed, 3 skipped). Unit, browser, and legacy browser CI passed on the reviewed commit. |
aarongarciah
left a comment
There was a problem hiding this comment.
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,
keepMountedwithdisableScrollLocktoggled while closed and while open, reopening during acloseAfterTransitionexit, changingcontainerwhile open, twodisablePortalmodals 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');
});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>
|
Thanks for the review, @aarongarciah.
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 |
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:
disableScrollLockis open, and a Menu, Select, Popover, or Dialog opens on top. The page does not lock.disableScrollLockopens on top. The Dialog closes first. The page stays locked until the Popover closes.Now each modal that mounts without
disableScrollLockowns the lock. The first owner locks the scroll container, and the last owner restores it, even when modals without the lock stay open.Changes
ModalManagertracks 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..mui-fixedpadding for all its scroll locks. The first lock pads the elements, and the last lock restores them.handleContaineris nowgetScrollContainer,lockScroll, andpadFixedElements. 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 theoverflow-yofhtml.useModalmounts 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. WithdisablePortal, the portal ref runs beforeadd().useModalupdates the lock whendisableScrollLockchanges while the modal is open.ModalManager.mount()withdisableScrollLocknow 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
disableScrollLocknow 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-fixedelements. To keep the page scrollable, setdisableScrollLockon the child too, for exampleMenuProps={{ disableScrollLock: true }}on Select. The v10 migration guide has an entry for this change.With two overflowing containers open at the same time,
.mui-fixedelements now get the scrollbar padding one time, not two times.ModalManageris 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