-
-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathBubbleTabBarItem.tsx
More file actions
164 lines (155 loc) 路 4.41 KB
/
Copy pathBubbleTabBarItem.tsx
File metadata and controls
164 lines (155 loc) 路 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import React, { useMemo, memo } from 'react';
import { View, Text, LayoutChangeEvent } from 'react-native';
import Animated from 'react-native-reanimated';
import { interpolateColor, useValue } from 'react-native-redash/lib/module/v1';
// @ts-ignore 馃槥
import isEqual from 'lodash.isequal';
import { interpolate } from '../../../utilities';
import type { BubbleTabBarItemProps } from '../types';
import { styles } from './styles';
const { add } = Animated;
const BubbleTabBarItemComponent = ({
animatedFocus,
label,
icon,
background,
labelStyle: labelStyleOverride,
labelAllowFontScaling,
spacing,
iconSize,
isRTL,
reverseAnimatedFocus,
}: BubbleTabBarItemProps) => {
//#region extract props
const {
innerVerticalSpace,
innerHorizontalSpace,
outerVerticalSpace,
outerHorizontalSpace,
} = spacing;
//#endregion
//#region variables
const labelWidth = useValue<number>(0);
/**
* @DEV
* min width is calculated by adding outer & inner spaces
* with the icon size.
*/
const minWidth = useMemo(() => {
return innerHorizontalSpace * 2 + iconSize + outerHorizontalSpace * 2;
}, [innerHorizontalSpace, outerHorizontalSpace, iconSize]);
/**
* @DEV
* max width is calculated by adding inner space with label width
*/
const maxWidth = add(labelWidth, innerHorizontalSpace, minWidth);
//#endregion
//#region styles
const animatedIconColor = interpolateColor(animatedFocus, {
inputRange: [0, 1],
outputRange: [icon.inactiveColor, icon.activeColor],
});
const animatedIconSecondColor = useMemo(() => {
if (!icon.secondColor) return undefined;
return interpolateColor(animatedFocus, {
inputRange: [0, 1],
outputRange: [
icon.secondColor.inactiveColor,
icon.secondColor.activeColor,
],
});
}, [animatedFocus, icon.secondColor]);
const containerStyle = [
styles.container,
{
paddingHorizontal: outerHorizontalSpace,
paddingVertical: outerVerticalSpace,
width: interpolate(animatedFocus, {
inputRange: [0, 1],
outputRange: [minWidth, maxWidth],
}),
},
];
const contentContainerStyle = [
styles.contentContainer,
{
flexDirection: isRTL ? 'row-reverse' : 'row',
paddingHorizontal: innerHorizontalSpace,
paddingVertical: innerVerticalSpace,
borderRadius: innerVerticalSpace * 2 + iconSize,
backgroundColor: interpolateColor(animatedFocus, {
inputRange: [0, 1],
outputRange: [background.inactiveColor, background.activeColor],
}),
},
];
const labelContainerStyle = [
styles.labelContainer,
{
opacity: interpolate(animatedFocus, {
inputRange: [0.33, 1],
outputRange: [0, 1],
}),
[isRTL ? 'left' : 'right']: interpolate(animatedFocus, {
inputRange: [0, 1],
outputRange: [0, innerHorizontalSpace + outerHorizontalSpace],
}),
},
];
const iconContainerStyle = [
styles.iconContainer,
{
minHeight: iconSize,
minWidth: iconSize,
},
];
const labelStyle = [styles.label, labelStyleOverride];
//#endregion
// callbacks
const handleTextLayout = ({
nativeEvent: {
layout: { width },
},
}: LayoutChangeEvent) =>
requestAnimationFrame(() => labelWidth.setValue(width));
// render
const renderIcon = () => {
const IconComponent: any = icon.component;
return typeof IconComponent === 'function' ? (
IconComponent({
animatedFocus,
color: animatedIconColor,
secondColor: animatedIconSecondColor,
size: iconSize,
reverseAnimatedFocus,
})
) : (
<IconComponent
animatedFocus={animatedFocus}
color={animatedIconColor}
secondColor={animatedIconSecondColor}
size={iconSize}
reverseAnimatedFocus={reverseAnimatedFocus}
/>
);
};
return (
<Animated.View style={containerStyle}>
<Animated.View style={contentContainerStyle}>
<View style={iconContainerStyle}>{renderIcon()}</View>
</Animated.View>
<Animated.View style={labelContainerStyle}>
<Text
allowFontScaling={labelAllowFontScaling}
onLayout={handleTextLayout}
style={labelStyle}
numberOfLines={1}
>
{label}
</Text>
</Animated.View>
</Animated.View>
);
};
const BubbleTabBarItem = memo(BubbleTabBarItemComponent, isEqual);
export default BubbleTabBarItem;