[Tabs] Fix selected tab not scrolled into view when scroll buttons appear (auto mode)#48489
Open
starboyvarun wants to merge 3 commits intomui:masterfrom
Open
[Tabs] Fix selected tab not scrolled into view when scroll buttons appear (auto mode)#48489starboyvarun wants to merge 3 commits intomui:masterfrom
starboyvarun wants to merge 3 commits intomui:masterfrom
Conversation
…pear in auto mode When scrollButtons="auto", the IntersectionObserver fires asynchronously after the initial render to show scroll buttons. This shrinks the scroller, which can push the selected tab out of view. Since the tab's indicatorStyle doesn't change when the scroller resizes, scrollSelectedIntoView was never re-triggered. Add a ResizeObserver on the scroller element that calls scrollSelectedIntoView (without animation) whenever the scroller's size changes. This covers the scroll-buttons-appear case on initial mount without introducing the side effects of the reverted PR mui#46869 (animating on mount, re-scrolling on button clicks). Fixes mui#44211
Deploy previewhttps://deploy-preview-48489--material-ui.netlify.app/ Bundle size
Check out the code infra dashboard for more information about this PR. |
Member
|
This fix doesn't seem correct yet, the failing tests in CI look directly related |
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 #44211
Problem
When
variant="scrollable"andscrollButtons="auto", theIntersectionObserverfires asynchronously after the initial render to determine whether the first/last tabs are visible. When it fires, React shows the scroll buttons, which shrinks the scroller.The
scrollSelectedIntoViewfunction is only triggered whenindicatorStylechanges. But when the scroller shrinks (scroll buttons appear), the selected tab's indicator position relative to the scroller does not change — the tab and the scroller both shift by the same amount. SoindicatorStylestays the same,scrollSelectedIntoViewis never called, and the selected tab ends up partially out of view.This was the root cause behind the reverted PR #46869. That fix re-ran
scrollSelectedIntoViewwheneverdisplayStartScroll/displayEndScrollchanged — but this also fired when scroll buttons were clicked (causing the view to snap back to the selected tab instead of scrolling per the button click) and caused animation on initial render.Solution
Add a
ResizeObserveron the scroller element (tabsRef.current) that callsscrollSelectedIntoView(false)(no animation) whenever the scroller's size changes.scrollSelectedIntoView(false)is passedanimation = false. ✅scrollerResizeObserver?.disconnect()on unmount. ✅Changes
Tabs.js— addscrollerResizeObserverthat observes the scroller and callsscrollSelectedIntoView(false)on resize; addscrollable,scrollButtons,scrollSelectedIntoViewto theuseEffectdep arrayTabs.test.js— add test: mockResizeObserver, trigger the scroller callback, verifyscrollLeftis corrected