11import { GlassView } from "expo-glass-effect" ;
2- import { useState , type ReactNode } from "react" ;
2+ import { useEffect , useRef , useState , type ComponentRef , type ReactNode } from "react" ;
33import { Pressable , ScrollView , View } from "react-native" ;
44import Animated , { useAnimatedStyle , useReducedMotion , withTiming } from "react-native-reanimated" ;
55import { withUniwind } from "uniwind" ;
@@ -27,8 +27,13 @@ export interface TabsProps<Value extends string> {
2727export function Tabs < Value extends string > ( props : TabsProps < Value > ) {
2828 const { themeAppearance } = useAppearancePreferences ( ) ;
2929 const reduceMotion = useReducedMotion ( ) ;
30+ const scrollView = useRef < ComponentRef < typeof ScrollView > > ( null ) ;
3031 const [ frames , setFrames ] = useState < Partial < Record < Value , { x : number ; width : number } > > > ( { } ) ;
3132 const frame = frames [ props . selected ] ;
33+ // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- keeps the selected tab visible in the native scroll viewport
34+ useEffect ( ( ) => {
35+ if ( frame ) scrollView . current ?. scrollTo ( { x : frame . x , animated : false } ) ;
36+ } , [ frame ] ) ;
3237 const duration = reduceMotion ? 0 : 220 ;
3338 const indicatorStyle = useAnimatedStyle ( ( ) => ( {
3439 width : withTiming ( frame ?. width ?? 0 , { duration } ) ,
@@ -37,6 +42,7 @@ export function Tabs<Value extends string>(props: TabsProps<Value>) {
3742
3843 return (
3944 < ScrollView
45+ ref = { scrollView }
4046 horizontal
4147 showsHorizontalScrollIndicator = { false }
4248 className = { cn ( "grow-0" , props . className ) }
0 commit comments