Skip to content

Commit

Permalink
do not scroll if the selected tab is on the first page
Browse files Browse the repository at this point in the history
  • Loading branch information
tom2drum committed Sep 5, 2024
1 parent ff15c87 commit 8b785e6
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ui/shared/Tabs/useScrollToActiveTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@ export default function useScrollToActiveTab({ activeTabIndex, tabsRefs, listRef
const activeTabRef = tabsRefs[activeTabIndex];

if (activeTabRef.current && listRef.current) {
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,
behavior: 'smooth',
Expand Down

0 comments on commit 8b785e6

Please sign in to comment.