[Tabs] Fix selected tab not scrolled into view when scroll buttons appear (auto mode)#48489
[Tabs] Fix selected tab not scrolled into view when scroll buttons appear (auto mode)#48489starboyvarun wants to merge 4 commits into
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. |
|
This fix doesn't seem correct yet, the failing tests in CI look directly related |
The ResizeObserver on the scroller was attached for any scrollButtons !== false, which included scrollButtons=true. In real browsers the observer fires an initial entry after observation starts, calling scrollSelectedIntoView unexpectedly and breaking existing browser tests. The scroller only resizes asynchronously (due to IntersectionObserver toggling scroll buttons) in auto mode, so scope the observer to scrollButtons === 'auto'.
|
@mj12albert The scroller only resizes asynchronously (when the IntersectionObserver toggles scroll button visibility) in |
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