Skip to content
This repository was archived by the owner on May 5, 2025. It is now read-only.

Commit 516bc9d

Browse files
committed
refactor: enhance TabBarButton with animated styles and remove onLongPress handler
1 parent b241577 commit 516bc9d

File tree

2 files changed

+49
-21
lines changed

2 files changed

+49
-21
lines changed

src/components/tabbar/BottomTabBar.tsx

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,11 @@ const BottomTabBar = ({
3131
}
3232
};
3333

34-
const onLongPress = () => {
35-
navigation.emit({
36-
type: 'tabLongPress',
37-
target: route.key,
38-
});
39-
};
40-
4134
return (
4235
<TabBarButton
4336
label={label}
4437
isFocused={isFocused}
4538
onPress={onPress}
46-
onLongPress={onLongPress}
4739
tabBarIcon={tabBarIcon}
4840
tabBarActiveTintColor={options.tabBarActiveTintColor!}
4941
tabBarInactiveTintColor={options.tabBarInactiveTintColor!}
@@ -62,6 +54,7 @@ const styles = StyleSheet.create({
6254
paddingVertical: 14,
6355
borderTopWidth: 2,
6456
borderTopColor: '#EEEEEE',
57+
gap: 10,
6558
},
6659
});
6760

src/components/tabbar/TabBarButton.tsx

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
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

310
export 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

Comments
 (0)