[useScrollTrigger] Fix flip-flopping when a layout resize shifts the scroll position#48801
Open
xxashxx-svg wants to merge 1 commit into
Open
[useScrollTrigger] Fix flip-flopping when a layout resize shifts the scroll position#48801xxashxx-svg wants to merge 1 commit into
xxashxx-svg wants to merge 1 commit into
Conversation
…scroll position When the trigger toggles content inside a sticky AppBar (e.g. a Collapse), the resulting layout resize shifts the scroll position via scroll anchoring. defaultTrigger interpreted that shift as user scrolling, flipping the trigger back and making the AppBar shake. Track the scroll target's scrollHeight between scroll events: if it changed, keep the previous trigger value instead of reading a scroll direction from the event. Also keep the trigger on while scrolling down with hysteresis, since a resize can leave the scroll position below the threshold. Fixes mui#46589 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Deploy previewBundle size
Check out the code infra dashboard for more information about this PR. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #46589
Problem
When
useScrollTriggerdrives content inside aposition="sticky"AppBar (for example aCollapsethat hides a secondary toolbar), the trigger flip-flops and the AppBar visibly shakes while scrolling slowly:trueand the collapse starts.scrollY, which fires scroll events with a decreasing position.defaultTriggerinterprets the decreasing position as the user scrolling up and flips the trigger back tofalse.scrollY, the trigger flips totrue… and the loop repeats.As @abriginets noted in the issue, the resize itself changes
pageYOffset/scrollTop, so the previous-vs-current comparison indefaultTriggerno longer reflects the user's scroll intent during the transition.Fix
defaultTriggernow also records the scroll target'sscrollHeightbetween scroll events (entirely internal state, no API change):Genuine scrolling behaves exactly as before: all existing tests pass unchanged, and the threshold/hysteresis semantics for a stable layout are untouched.
Tests
Added two regression tests (element target and window target) that replay the reported scenario: scroll down past the threshold, shrink the scrollable area while shifting the position up (collapse + scroll anchoring), keep scrolling, scroll up, grow the area back. Both fail on
masterwith the exact oscillation from the issue (trigger flips on every resize frame) and pass with this change.Ran:
pnpm test:unit run useScrollTrigger(node: 16 passed; browser: 3 passed, 13 jsdom-only skipped), eslint, prettier andtsgo -p tsconfig.jsonforpackages/mui-material.Relation to #46751
The earlier attempt #46751 added a user-facing opt-in (
enableReentrantLock+ a duration that had to match the transition timing) and was closed after reviewers asked for the edge case to be fixed internally without new public API (review comment). This PR fixes it insidedefaultTriggerwith no new options, no timers, and no behavior change for stable layouts.🤖 Generated with Claude Code