Skip to content

[utils] Lock body scrolling when html clips the viewport - #6126

Closed
jjenzz wants to merge 3 commits into
mui:masterfrom
jjenzz:codex/base-ui-issue-1499f120-20bf-4c7c-a30d-9fcd65fbc23d
Closed

jjenzz wants to merge 3 commits into
mui:masterfrom
jjenzz:codex/base-ui-issue-1499f120-20bf-4c7c-a30d-9fcd65fbc23d

Conversation

@jjenzz

@jjenzz jjenzz commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

Fixes #5720

  • Cause: If a page sets overflow hidden on <html> and makes <body> its own scroll container (an app-shell pattern some widgets inject), the scroll lock checked only <html>, saw hidden, assumed the page was already locked and waited for that lock to clear. It never clears, so modal popups left <body> scrollable.
  • Fix and rationale: When <html> clips the viewport and <body> establishes its own scroll container, <body> is the element that scrolls the page, so the viewport scroller now resolves to <body>. Detection and locking then hide <body>'s overflow and restore it on close. Pages where <html> scrolls, or where <body>'s overflow propagates, resolve as before, and the handoff to external <html> or <body> locks is unchanged.
  • Considered: Also require body.scrollHeight > body.clientHeight before treating <body> as the scroller — Passed the same real-browser scenarios but adds a layout read and a second condition without changing any verified outcome.
  • Considered: Drop the already-locked handoff and always hide overflow on both <html> and <body> — Snapshots a third-party <html> lock and restores it on close, so the page stayed locked after the other library unlocked.
  • Verification: Chromium, jsdom, ESLint, TypeScript, formatting. Regression fails before and passes after the fix.
  • Scope: Author !important rules on body overflow still override the lock. BrowserStack scenarios ran in Chrome on Windows; Firefox and Safari were not tested.

@jjenzz jjenzz added component: dialog Changes related to the dialog component. package: utils Specific to the utils package. type: bug It doesn't behave as expected. labels Oct 28, 2026 — with ChatGPT Codex Connector
@pkg-pr-new

pkg-pr-new Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

commit: f4f8c25

@jjenzz jjenzz changed the title [utils] Lock the page scroller when clips the viewport [utils] Lock body scrolling when html clips the viewport Oct 28, 2026
@code-infra-dashboard

code-infra-dashboard Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+368B(+0.07%) 🔺+77B(+0.05%)

Details of bundle changes

Performance

No regressions in 36 benchmarks · ran 3m 52s · details


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

@netlify

netlify Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit f4f8c25
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6ae22d5fa1c448ffccfe81a2
😎 Deploy Preview https://deploy-preview-6126--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@jjenzz jjenzz removed the component: dialog Changes related to the dialog component. label Oct 28, 2026
@jjenzz

This comment has been minimized.

@jjenzz

This comment has been minimized.

@jjenzz

jjenzz commented Oct 28, 2026

Copy link
Copy Markdown
Member Author

I reproduced the 16px layout shift with classic body scrollbars. I now preserve the body gutter only when it has an inset scrollbar and restore its previous gutter on unlock; overlay-scrollbar layouts retain their original width.

I added a browser regression with overflowing content and a nonzero body scroll position, checking width and position through lock and unlock. Disabling the gutter preservation makes the width assertion fail.


🤖 Comment posted by Codex

@jjenzz

jjenzz commented Oct 28, 2026

Copy link
Copy Markdown
Member Author

PR review

Nothing here blocks merge. The fixes from earlier rounds are in place in the current code: the body gutter is preserved and restored, the body is only treated as the page scroller when it actually overflows vertically, and body measurements only happen when the body is the element being locked. Two small issues remain. The new gutter regression test probably can't fail in CI's default headless Chromium. And a body-scroller page whose content is short when the dialog opens never gets locked. I only read the code; I didn't run any tests or reproduce anything in a browser.

Bugs (1)

1. 🟡 Body-scroller pages that grow after a dialog opens are never locked

Location: packages/utils/src/useScrollLock.ts:21-24

const htmlClipsViewport = /hidden|clip/.test(ownerWindow(html).getComputedStyle(html).overflowY);
const bodyScrolls =
  htmlClipsViewport && isOverflowElement(body) && body.scrollHeight > body.clientHeight;
return bodyScrolls ? body : html;

Take the layout this PR targets: html { overflow: hidden } plus body { height: 100%; overflow-y: auto }. If the body's content is shorter than its height when the deferred lock runs, getViewportScroller returns <html>. isPageScrollLocked then sees overflow-y: hidden on <html> and treats the page as externally locked, so it waits on a MutationObserver. That observer only watches attributes on <html> and <body> (useScrollLock.ts:303-306). When body content grows, no attribute changes, so the lock is never applied while the dialog is open.

The base version behaved the same way, so this isn't a regression. But the new scrollHeight guard is what leaves the gap in place, and these are exactly the layouts the PR is meant to support. The guard is needed for the external-<html>-lock case, so it should stay.

Failure scenario: An app uses the html-clip/body-scroll layout and shows a short loading skeleton. A modal dialog opens, then data arrives and the body starts overflowing. The page behind the modal scrolls with the wheel or touch.

Fix: When <html> clips and <body> is an overflow element (overflow-y of auto or scroll, not just overflow-x: hidden), lock the body even if it doesn't overflow yet. Keep the scrollHeight check only for telling an external <html> lock apart from a body that merely clips horizontally. Alternatively, have the observer also re-check on body resize.

Tests (1)

1. 🟡 The gutter-preservation regression test can pass without the fix in CI

Location: packages/react/src/dialog/root/DialogRoot.test.tsx:2103-2145

expect(document.body.scrollHeight).toBeGreaterThan(document.body.clientHeight);
document.body.scrollTop = 100;
expect(document.body.scrollTop).toBe(100);
const contentWidth = document.body.clientWidth;

The width assertion only means something when the body draws a classic scrollbar that takes up space. The browser config (vitest.shared.mts:37-44) uses Playwright's default headless Chromium, and Playwright launches it with --hide-scrollbars by default. In that setup body.offsetWidth - body.clientWidth is 0, so hasBodyScrollbar is false and the new gutter branch never runs. The earlier verification notes agree: the 398→414 failure only reproduced after temporarily changing the launch config. The test never checks that a real gutter exists, so in CI the clientWidth assertions pass whether or not the fix is there. The scroll-position and inline-gutter-restoration assertions still provide some coverage.

Failure scenario: Someone removes or breaks the scrollbarGutter = 'stable' assignment, and CI stays green because the body has no scrollbar width to lose.

Fix: Before locking, assert the precondition document.body.offsetWidth - document.body.clientWidth > 0, so the test fails loudly instead of passing for nothing. Alternatively, skip the test when there's no gutter, or launch the Chromium instance with scrollbars enabled.

Verdict

Approve after nits - the remaining items are a narrow late-overflow gap and a regression test that CI's default headless setup may not exercise; there are no blocking regressions.


🤖 Review generated with Claude Code

@jjenzz

jjenzz commented Oct 28, 2026

Copy link
Copy Markdown
Member Author

I reproduced the premature handoff with body { overflow-x: hidden }: the body was locked while the external <html> lock was still active. I now select the body only when it actually overflows vertically, and the new regression verifies waiting for the external lock and restoring the body styles on close.

I also moved the body gutter measurements inside the body-lock branch. The earlier style-only test is covered by the overflowing-body regression, which checks width and scroll position through lock and unlock.


🤖 Comment posted by Codex

@jjenzz

jjenzz commented Nov 10, 2026

Copy link
Copy Markdown
Member Author

I’m not sure if this is something we should fix. I’m wondering if there is a way we can provide an API or a workaround for people to handle this themselves. It seems a bit of an edge-case.

@jjenzz jjenzz closed this Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

package: utils Specific to the utils package. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[dialog] Scroll lock is skipped when the page scroller is <body> under an overflow:hidden <html>

2 participants