Center the selected tab in the sugg follows tabbar

This commit is contained in:
Eric Bailey
2025-04-07 14:06:01 -05:00
parent 94fd12ff49
commit 9dd5c6f137
+6 -20
View File
@@ -471,7 +471,6 @@ let Tabs = ({
contentContainerStyle?: StyleProp<ViewStyle> contentContainerStyle?: StyleProp<ViewStyle>
}): React.ReactNode => { }): React.ReactNode => {
const listRef = useRef<ScrollView>(null) const listRef = useRef<ScrollView>(null)
const [scrollX, setScrollX] = useState(0)
const [totalWidth, setTotalWidth] = useState(0) const [totalWidth, setTotalWidth] = useState(0)
const pendingTabOffsets = useRef<{x: number; width: number}[]>([]) const pendingTabOffsets = useRef<{x: number; width: number}[]>([])
const [tabOffsets, setTabOffsets] = useState<{x: number; width: number}[]>([]) const [tabOffsets, setTabOffsets] = useState<{x: number; width: number}[]>([])
@@ -490,24 +489,11 @@ let Tabs = ({
function scrollIntoViewIfNeeded(index: number) { function scrollIntoViewIfNeeded(index: number) {
const btnLayout = tabOffsets[index] const btnLayout = tabOffsets[index]
if (!btnLayout) return if (!btnLayout) return
listRef.current?.scrollTo({
const viewportLeftEdge = scrollX // centered
const viewportRightEdge = scrollX + totalWidth x: btnLayout.x - (totalWidth / 2 - btnLayout.width / 2),
const shouldScrollToLeftEdge = viewportLeftEdge > btnLayout.x animated: true,
const shouldScrollToRightEdge = })
viewportRightEdge < btnLayout.x + btnLayout.width
if (shouldScrollToLeftEdge) {
listRef.current?.scrollTo({
x: btnLayout.x - tokens.space.lg,
animated: true,
})
} else if (shouldScrollToRightEdge) {
listRef.current?.scrollTo({
x: btnLayout.x - totalWidth + btnLayout.width + tokens.space.lg,
animated: true,
})
}
} }
function handleSelectTab(index: number) { function handleSelectTab(index: number) {
@@ -539,7 +525,7 @@ let Tabs = ({
} }
onLayout={evt => setTotalWidth(evt.nativeEvent.layout.width)} onLayout={evt => setTotalWidth(evt.nativeEvent.layout.width)}
scrollEventThrottle={200} // big throttle scrollEventThrottle={200} // big throttle
onScroll={evt => setScrollX(evt.nativeEvent.contentOffset.x)}> >
{interests.map((interest, i) => { {interests.map((interest, i) => {
const active = interest === selectedInterest && !hasSearchText const active = interest === selectedInterest && !hasSearchText
return ( return (