import React, { memo, useEffect } from 'react'; import { StyleProp, TouchableOpacity, View } from 'react-native'; import RNReanimated, { interpolate, useAnimatedStyle, useSharedValue, withSpring, WithSpringConfig, } from 'react-native-reanimated'; import { BottomTabNavigationOptions } from '@react-navigation/bottom-tabs'; import { style } from '../styles/tab.bar.button.styles'; interface Props { mode: 'default' | 'square'; index: number; isFocused: boolean; onPress: () => void; onLongPress: () => void; options: BottomTabNavigationOptions; inactiveTintColor?: string; activeTintColor?: string; springConfig?: WithSpringConfig; focusedButtonStyle?: StyleProp; } export const defaultSpringConfig: WithSpringConfig = { damping: 30, mass: 0.7, stiffness: 250, }; export const BarButton: React.FC = memo( ({ isFocused, options, onPress, onLongPress, inactiveTintColor, focusedButtonStyle, springConfig, }) => { const animationValueThreshold = useSharedValue(0); useEffect(() => { if (isFocused) { animationValueThreshold.value = withSpring( 0, springConfig || defaultSpringConfig ); } else { animationValueThreshold.value = withSpring( 1, springConfig || defaultSpringConfig ); } }, [isFocused, animationValueThreshold, springConfig]); const animatedStyles = useAnimatedStyle(() => { return { opacity: animationValueThreshold.value, transform: [ { scale: animationValueThreshold.value, }, ], }; }); const textAnimatedStyle = useAnimatedStyle(() => { return { opacity: interpolate(animationValueThreshold.value, [0.5, 1], [0, 1]), }; }); return ( {options.tabBarIcon && !isFocused ? ( options.tabBarIcon({ focused: isFocused, color: inactiveTintColor || 'white', size: 28, }) ) : ( )} {options.tabBarLabel && ( {options.tabBarLabel} )} ); } ); export const TabBarButton: React.FC = memo( ({ isFocused, options, onPress, onLongPress, activeTintColor, springConfig, focusedButtonStyle, mode, }) => { const animationValueThreshold = useSharedValue(0); useEffect(() => { if (isFocused) { animationValueThreshold.value = withSpring( 0, springConfig || defaultSpringConfig ); } else { animationValueThreshold.value = withSpring( 1, springConfig || defaultSpringConfig ); } }, [isFocused, animationValueThreshold, springConfig]); const animatedStyles = useAnimatedStyle(() => { return { transform: [ { translateY: interpolate( animationValueThreshold.value, [0, 1], [-18, 100] ), }, ], }; }); return ( {options.tabBarIcon ? options.tabBarIcon({ focused: isFocused, color: 'white', size: 28, }) : null} ); } ); export default TabBarButton;