Skip to content

[popups] Lock full-width touch popups - #3100

Merged
atomiks merged 17 commits into
mui:masterfrom
atomiks:fix/scroll-lock-touch
Apr 9, 2026
Merged

atomiks merged 17 commits into
mui:masterfrom
atomiks:fix/scroll-lock-touch

Conversation

@atomiks

@atomiks atomiks commented Oct 30, 2025 •

Copy link
Copy Markdown
Contributor

Fixes #3098

Touch-opened popups intentionally skip scroll lock so swipe-outside dismissal still works, but that breaks modal popups that effectively fill the viewport. This change only re-enables scroll lock for the touch-open path when the positioned popup is effectively full-width, so smaller anchored popups keep the existing outside-swipe behavior.

Changes

  • Re-enable scroll lock for touch-opened Menu, Popover, Select, and Combobox popups only when the positioner width is within 20px of the full viewport width.
  • Run the touch scroll-lock check from the positioners instead of the roots for internal consistency (+mount perf)

Preview: https://deploy-preview-3100--base-ui.netlify.app/react/components/select

@atomiks atomiks added component: select Changes related to the select component. component: menu Changes related to the menu component. type: bug It doesn't behave as expected. component: popover Changes related to the popover component. component: autocomplete Changes related to the autocomplete component. component: combobox Changes related to the combobox component. labels Oct 30, 2025
@pkg-pr-new

pkg-pr-new Bot commented Oct 30, 2025 •

Copy link
Copy Markdown
  • base-ui-tanstack-start

  • vite-css-base-ui-example

    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/react@3100
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@3100
    

commit: 07e348c

@mui-bot

mui-bot commented Oct 30, 2025 •

Copy link
Copy Markdown

Bundle size report

Bundle Parsed size Gzip size
@base-ui/react 🔺+196B(+0.04%) 🔺+55B(+0.04%)

Details of bundle changes


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

@netlify

netlify Bot commented Oct 30, 2025 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 07e348c
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/69d70ed8de308500080b5f56
😎 Deploy Preview https://deploy-preview-3100--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

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

@atomiks
atomiks marked this pull request as ready for review October 30, 2025 07:45
@github-actions github-actions Bot added PR: out-of-date The pull request has merge conflicts and can't be merged. and removed PR: out-of-date The pull request has merge conflicts and can't be merged. labels Oct 31, 2025
@github-actions github-actions Bot added PR: out-of-date The pull request has merge conflicts and can't be merged. and removed PR: out-of-date The pull request has merge conflicts and can't be merged. labels Nov 10, 2025
@atomiks atomiks mentioned this pull request Nov 11, 2025
1 task done
@github-actions github-actions Bot added PR: out-of-date The pull request has merge conflicts and can't be merged. and removed PR: out-of-date The pull request has merge conflicts and can't be merged. labels Nov 12, 2025
@atomiks
atomiks marked this pull request as draft November 12, 2025 03:46
@atomiks

This comment was marked as outdated.

@github-actions github-actions Bot added the PR: out-of-date The pull request has merge conflicts and can't be merged. label Nov 13, 2025
@atomiks atomiks mentioned this pull request Apr 8, 2026
1 task done
@atomiks
atomiks force-pushed the fix/scroll-lock-touch branch from 6d7cf81 to aef76e0 Compare April 8, 2026 09:21
@github-actions github-actions Bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged. label Apr 8, 2026
@atomiks atomiks changed the title [popups] Enable scroll lock on touch when modal [popups] Lock full-width touch popups Apr 8, 2026

@atomiks atomiks left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Codex Review (GPT-5.4)

Reviewed against the refreshed full branch range from upstream/master to the current PR head. Dominant type: bug-fix with secondary tests and react-ui; the earlier validation gap is now addressed, and I did not find any remaining actionable issues in the amended branch.

1. Bugs / Issues (None)

Previous Review Issues Status

The previously missing direct regression coverage for the new touch-lock path is now addressed. PopoverRoot.test.tsx and ComboboxRoot.test.tsx both add explicit Chromium assertions for touch-opened, effectively full-width popups, which closes the only branch-relevant gap I had in the prior pass.

Root Cause & Patch Assessment

Moving the touch-specific scroll-lock decision into the positioners still looks like the right fix. The width heuristic now lives next to the positioned element that owns --available-width, and the shared helper keeps the four updated call sites aligned without changing the non-touch path.

Test Coverage Assessment

I ran pnpm test:chromium MenuRoot --no-watch, pnpm test:chromium SelectRoot --no-watch, pnpm test:chromium PopoverRoot --no-watch, pnpm test:chromium ComboboxRoot --no-watch, and pnpm typescript; all passed. With the added Popover and Combobox regressions, the amended branch now directly covers the touch-lock behavior across all four updated positioners.

Recommendation

Approve ✅

The runtime change remains targeted, the newly added regressions close the earlier coverage hole, and the validation I ran is green on the current head.

@atomiks
atomiks marked this pull request as ready for review April 8, 2026 12:38

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

PR #3100 Review: [popups] Lock full-width touch popups

PR: #3100
Author: atomiks
Fixes: #3098

Summary

Touch-opened popups intentionally skip scroll lock so swipe-outside dismissal still works, but that breaks modal popups that effectively fill the viewport. This PR re-enables scroll lock for the touch-open path only when the positioner width is within 20px of full viewport width. It also moves scroll lock logic from Root components to Positioner components for Menu and Popover.

Overall Assessment

Well-structured PR that follows project conventions. The shared useTouchOpenScrollLock utility avoids logic duplication across 4 components (per AGENTS.md guidelines). The hook correctly uses useIsoLayoutEffect, ownerDocument, and the existing useScrollLock primitive. Tests are properly gated with describe.skipIf(isJSDOM).


Critical Issues (0)

None found.


Important Issues (3)

1. Layout effect timing may read stale positioner dimensions

File: packages/react/src/utils/useTouchOpenScrollLock.ts (lines 18-32)

useIsoLayoutEffect fires synchronously after DOM mutations but before paint. At this point, positionerElement.offsetWidth reflects the element's current layout. However, Floating UI's positioning logic also runs in layout effects. The dependency array is [enabled, touchOpen, positionerElement] -- it does not include any signal that positioning has completed.

Race condition scenario:

  1. Popup opens, positionerElement is set (triggers the effect)
  2. Effect reads offsetWidth, but Floating UI hasn't applied final position/size yet
  3. Width measurement is stale/zero, touchOpenShouldLockScroll = false
  4. Floating UI applies final width, but the effect doesn't re-run (no dependency changed)

In practice, tests use explicit CSS widths (style={{ width: 'calc(100vw - 10px)' }}), which are applied before the layout effect, masking this timing issue. For dynamically-sized popups whose width depends on Floating UI computation, this could silently produce the wrong scroll lock decision.

Suggestion: Document the assumption that the positioner width must be deterministic at layout-effect time (e.g., set via CSS), or consider depending on a positioning-complete signal.

2. Reference element changed from positionerElement to triggerElement

Files: PopoverRoot.tsx, MenuRoot.tsx (removed), PopoverPositioner.tsx, MenuPositioner.tsx (added)

Previously, useScrollLock was called in the Root with positionerElement as the reference. Now useTouchOpenScrollLock passes triggerElement as referenceElement to useScrollLock. The reference element is used by ScrollLocker to resolve ownerDocument and ownerWindow.

If the trigger and positioner are in different documents (e.g., positioner rendered in a portal to a different iframe), scroll lock would target the wrong document. This is an edge case, but it's a silent behavioral change.

Suggestion: Verify this is intentional. Consider adding a brief comment on why triggerElement is preferred over positionerElement.

3. Inconsistent enabled conditions across call sites

Component enabled parameter
Combobox open && modal
Menu open && modal && lastOpenChangeReason !== REASONS.triggerHover
Popover open && modal === true && openReason !== REASONS.triggerHover
Select (alignItemWithTriggerActive || modal) && open

Combobox does not exclude triggerHover, while Menu and Popover do. Popover uses strict equality (modal === true) while others use truthy checks. These differences likely reflect legitimate behavioral differences, but the inconsistency increases the risk of a wrong condition at one call site.

Suggestion: Add a brief comment at each call site explaining why its enabled condition differs from the others.


Suggestions (4)

1. Accidental blank line removal in PopoverPositioner.tsx

File: packages/react/src/popover/positioner/PopoverPositioner.tsx

A blank line was removed between the useMemo block and the state declaration. The other positioners keep this separator. Minor formatting inconsistency.

2. JSDoc is incomplete for the new hook

File: packages/react/src/utils/useTouchOpenScrollLock.ts

The JSDoc says "Enables scroll lock for touch-opened popups when their positioned width is effectively viewport-sized", but the hook also handles the non-touch case by delegating to useScrollLock(enabled && !touchOpen). It's a unified scroll lock entry point, not just a touch-open handler.

Suggestion: Expand to: "Manages scroll lock for popup open states. For non-touch opens, scroll lock is applied when enabled. For touch opens, scroll lock is applied only when the positioner width is effectively viewport-sized."

3. The 20px tolerance constant lacks rationale

File: packages/react/src/utils/useTouchOpenScrollLock.ts, line 9

VIEWPORT_WIDTH_TOLERANCE_PX = 20 has no comment explaining why 20px was chosen. Does it account for scrollbar width? Mobile safe areas? Sub-pixel rounding?

Suggestion: Add a comment explaining what the tolerance accounts for.

4. Module-level comment conflates scroll lock with dismissal

File: packages/react/src/utils/useTouchOpenScrollLock.ts, lines 7-8

"Touch-opened popups normally avoid scroll locking so users can still swipe outside to dismiss."

Dismissal is handled by useDismiss, not scroll lock. The actual reason is so users can still scroll the page.

Suggestion: Rewrite to: "Touch-opened popups normally avoid scroll locking so the user can still scroll the page. This hook re-enables scroll lock only when the popup is effectively full-width."


Test Coverage

What's covered

  • Full-width touch popup applies scroll lock (all 4 components)
  • Narrow touch popup does NOT apply scroll lock (all 4 components)
  • Existing tests for mouse-open scroll lock preserved
  • Existing tests for touch interaction type tracking preserved (Select)
  • All new tests properly gated with describe.skipIf(isJSDOM)

Gaps

  • No boundary test: No test at exactly the 20px threshold (e.g., calc(100vw - 20px) and calc(100vw - 21px)) to verify the tolerance precisely
  • No scroll lock cleanup test: No test verifying scroll lock is removed when the popup closes
  • No non-modal test: No explicit test verifying non-modal touch popups are unaffected
  • Test constant coupling: The test value calc(100vw - 10px) is coupled to the 20px constant but the relationship is not documented. A future change to the tolerance could silently break tests.

Strengths

  • Clean extraction of shared logic into useTouchOpenScrollLock -- avoids 4x duplication
  • Correct use of project utilities (useIsoLayoutEffect, ownerDocument)
  • Defensive guards (viewportWidth > 0 && popupWidth > 0) prevent false positives from unmounted elements
  • Moving scroll lock to positioners is architecturally sound -- the positioner has direct access to the element needed for width comparison
  • Adding openMethod to MenuStore is the right approach for making the value available in the positioner

@atomiks

atomiks commented Apr 9, 2026

Copy link
Copy Markdown
Contributor Author
  1. Layout effect timing may read stale positioner dimensions

Yes, but not exactly relevant here as this is a simple heuristic where it's unlikely for a ~full-width popup to need to wait for measurement

Addressed the others

@code-infra-dashboard

code-infra-dashboard Bot commented Apr 9, 2026 •

Copy link
Copy Markdown

Bundle size report

Bundle Parsed size Gzip size
@base-ui/react 🔺+196B(+0.04%) 🔺+55B(+0.04%)

Details of bundle changes


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

@atomiks
atomiks merged commit b13c80e into mui:master Apr 9, 2026
7 checks passed
@atomiks
atomiks deleted the fix/scroll-lock-touch branch April 9, 2026 02:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: autocomplete Changes related to the autocomplete component. component: combobox Changes related to the combobox component. component: menu Changes related to the menu component. component: popover Changes related to the popover component. component: select Changes related to the select component. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[popover] Modal mode doesn't lock scroll on "touch" events

3 participants