Skip to content

Commit

Permalink
Fix the "Scroll to active tab" behavior on mobile devices (#2214)
Browse files Browse the repository at this point in the history
* fix scroll left position calculation when changing the tab

* do not scroll if the selected tab is on the first page
  • Loading branch information
tom2drum authored Sep 6, 2024
1 parent 53ddeb5 commit d37205b
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions ui/shared/Tabs/useScrollToActiveTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,22 @@ export default function useScrollToActiveTab({ activeTabIndex, tabsRefs, listRef
const activeTabRef = tabsRefs[activeTabIndex];

if (activeTabRef.current && listRef.current) {
const activeTabRect = activeTabRef.current.getBoundingClientRect();
const containerWidth = listRef.current.getBoundingClientRect().width;
const activeTabWidth = activeTabRef.current.getBoundingClientRect().width;
const left = tabsRefs.slice(0, activeTabIndex)
.map((tab) => tab.current?.getBoundingClientRect())
.filter(Boolean)
.map((rect) => rect.width)
.reduce((result, item) => result + item, 0);

const isWithinFirstPage = containerWidth > left + activeTabWidth;

if (isWithinFirstPage) {
return;
}

listRef.current.scrollTo({
left: activeTabRect.left,
left,
behavior: 'smooth',
});
}
Expand Down

0 comments on commit d37205b

Please sign in to comment.