Skip to content

Commit

Permalink
fix: horizontal scrolling back to 0 on load
Browse files Browse the repository at this point in the history
For TabViews where scrollEnabled is set to true and a tab that is off-screen is selected, if that tab has async content, the horizontal scroll position of the tab bar was resetting to the start once the content was loaded.  Just added a check to only update  the layout if we don't already have a layout for the selected tab
  • Loading branch information
gezquinndesign authored Dec 19, 2024
1 parent 88000cc commit ed56eac
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/MaterialTabBar/TabBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,22 @@ const MaterialTabBar = <T extends TabName = TabName>({
if (!event.nativeEvent?.layout) return
const { width, x } = event.nativeEvent.layout

itemLayoutGathering.current.set(name, {
width,
x,
})

// pick out the layouts for the tabs we know about (in case they changed dynamically)
const layout = Array.from(itemLayoutGathering.current.entries())
.filter(([tabName]) => tabNames.includes(tabName))
.map(([, layout]) => layout)
.sort((a, b) => a.x - b.x)

if (layout.length === tabNames.length) {
setItemsLayout(layout)
// Only update if we don't already have a layout for this tab
if (!itemLayoutGathering.current.has(name)) {
itemLayoutGathering.current.set(name, {
width,
x,
})

// pick out the layouts for the tabs we know about (in case they changed dynamically)
const layout = Array.from(itemLayoutGathering.current.entries())
.filter(([tabName]) => tabNames.includes(tabName))
.map(([, layout]) => layout)
.sort((a, b) => a.x - b.x)

if (layout.length === tabNames.length) {
setItemsLayout(layout)
}
}
}
},
Expand Down

0 comments on commit ed56eac

Please sign in to comment.