1- import React , { useEffect } from "react" ;
1+ import React , { useEffect , useRef } from "react" ;
22import { StyleSheet } from "react-native" ;
33
44import PropTypes from "prop-types" ;
@@ -14,32 +14,62 @@ import { theme } from "@theme";
1414
1515const springConfig = {
1616 mass : 1 ,
17- overshootClamping : true ,
17+ damping : 18 ,
18+ stiffness : 100 ,
19+ overshootClamping : false ,
1820 restSpeedThreshold : 0.5 ,
1921 restDisplacementThreshold : 0.5 ,
2022} ;
2123
2224const spacing = moderateScale ( 6 ) ;
2325
2426export const Indicator = ( { measure } ) => {
25- const { x, width, height } = measure ;
26-
27+ const firstRender = useRef ( true ) ;
2728 const translateX = useSharedValue ( 0 ) ;
2829 const animatedWidth = useSharedValue ( 0 ) ;
2930
3031 useEffect ( ( ) => {
31- cancelAnimation ( translateX ) ;
32- cancelAnimation ( animatedWidth ) ;
32+ if ( ! measure ) return ;
33+
34+ const { x, width } = measure ;
35+
36+ if ( firstRender . current ) {
37+ if ( translateX . value === 0 && x > 0 ) {
38+ translateX . value = x + spacing / 2 ;
39+ }
40+
41+ if ( animatedWidth . value === 0 && width > 0 ) {
42+ animatedWidth . value = width - spacing ;
43+ }
3344
34- translateX . value = withSpring ( x + spacing / 2 , springConfig ) ;
35- animatedWidth . value = withSpring ( width - spacing , springConfig ) ;
36- } , [ animatedWidth , translateX , width , x ] ) ;
45+ firstRender . current = false ;
46+ } else {
47+ // Cancel any ongoing animations first
48+ cancelAnimation ( translateX ) ;
49+ cancelAnimation ( animatedWidth ) ;
50+
51+ translateX . value = withSpring ( x + spacing / 2 , springConfig ) ;
52+ animatedWidth . value = withSpring ( width - spacing , springConfig ) ;
53+ }
54+ } , [ measure ] ) ;
3755
3856 const tabTranslateAnimatedStyles = useAnimatedStyle ( ( ) => ( {
3957 transform : [ { translateX : translateX . value } ] ,
4058 width : animatedWidth . value ,
4159 } ) ) ;
4260
61+ // Guard against undefined measure
62+ if (
63+ ! measure ||
64+ typeof measure . x !== "number" ||
65+ typeof measure . width !== "number" ||
66+ typeof measure . height !== "number"
67+ ) {
68+ return null ;
69+ }
70+
71+ const { height } = measure ;
72+
4373 return (
4474 < Animated . View
4575 style = { [
0 commit comments