1- import { Pressable , StyleSheet , Text } from 'react-native' ;
1+ import { useEffect } from 'react' ;
2+ import { Pressable , StyleSheet } from 'react-native' ;
3+ import Animated , {
4+ interpolate ,
5+ useAnimatedStyle ,
6+ useSharedValue ,
7+ withSpring ,
8+ } from 'react-native-reanimated' ;
29
310export interface TabBarButtonProps {
411 label : string ;
512 isFocused : boolean ;
613 onPress : ( ) => void ;
7- onLongPress : ( ) => void ;
814 tabBarIcon ?: ( props : {
915 focused : boolean ;
1016 color : string ;
@@ -18,36 +24,65 @@ const TabBarButton = ({
1824 label,
1925 isFocused,
2026 onPress,
21- onLongPress,
2227 tabBarIcon,
2328 tabBarActiveTintColor,
2429 tabBarInactiveTintColor,
2530} : TabBarButtonProps ) => {
31+ const scale = useSharedValue ( 1 ) ;
32+
33+ useEffect ( ( ) => {
34+ scale . value = withSpring (
35+ typeof isFocused === 'boolean' ? ( isFocused ? 1 : 0 ) : isFocused ,
36+ { duration : 350 } ,
37+ ) ;
38+ } , [ scale , isFocused ] ) ;
39+
40+ const animatedTextStyle = useAnimatedStyle ( ( ) => {
41+ const opacity = interpolate ( scale . value , [ 0 , 1 ] , [ 1 , 0 ] ) ;
42+
43+ return {
44+ opacity,
45+ } ;
46+ } ) ;
47+
48+ const animatedIconStyle = useAnimatedStyle ( ( ) => {
49+ const scaleValue = interpolate ( scale . value , [ 0 , 1 ] , [ 1 , 1.2 ] ) ;
50+ const top = interpolate ( scale . value , [ 0 , 1 ] , [ 0 , 10 ] ) ;
51+
52+ return {
53+ transform : [ { scale : scaleValue } ] ,
54+ top,
55+ } ;
56+ } ) ;
57+
2658 return (
2759 < Pressable
2860 onPress = { onPress }
29- onLongPress = { onLongPress }
3061 android_ripple = { null }
3162 style = { [ styles . tab , isFocused && styles . activeTab ] }
3263 >
33- { tabBarIcon &&
34- tabBarIcon ( {
35- color : isFocused ? tabBarActiveTintColor : tabBarInactiveTintColor ,
36- size : 24 ,
37- focused : isFocused ,
38- } ) }
64+ { tabBarIcon && (
65+ < Animated . View style = { [ animatedIconStyle ] } >
66+ { tabBarIcon ( {
67+ color : isFocused ? tabBarActiveTintColor : tabBarInactiveTintColor ,
68+ size : 24 ,
69+ focused : isFocused ,
70+ } ) }
71+ </ Animated . View >
72+ ) }
3973
40- < Text
74+ < Animated . Text
4175 style = { [
4276 {
4377 color : isFocused ? tabBarActiveTintColor : tabBarInactiveTintColor ,
4478 } ,
4579 styles . tabText ,
4680 isFocused && styles . activeTabText ,
81+ animatedTextStyle ,
4782 ] }
4883 >
4984 { label }
50- </ Text >
85+ </ Animated . Text >
5186 </ Pressable >
5287 ) ;
5388} ;
@@ -66,7 +101,7 @@ const styles = StyleSheet.create({
66101 marginTop : 4 ,
67102 } ,
68103 activeTab : {
69- backgroundColor : '#e6f0ff ' ,
104+ backgroundColor : '#E6F0FF ' ,
70105 } ,
71106 activeTabText : {
72107 color : '#4B6BFB' ,
0 commit comments