Skip to content

Commit b25866c

Browse files
authored
Merge pull request #1 from mikalyh/gen-1
update: colorful fix item with long label
2 parents 98271c2 + 861f96c commit b25866c

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

src/ColorfulTabBar/ColorfulTabBar.tsx

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import React, { useEffect, useRef } from 'react';
1+
import React, { useEffect, useRef, useState } from 'react';
22
import {
33
View,
44
Text,
55
Animated,
66
TouchableOpacity,
77
Easing,
88
SafeAreaView,
9+
LayoutChangeEvent,
910
} from 'react-native';
1011
import { main_colors } from '../assets/TabColor';
1112
import { ColorfulStyle } from '../assets/TabStyle';
@@ -49,6 +50,8 @@ const ColorfulTabBar = ({
4950
>
5051
{state.routes.map((route, index) => {
5152
const focusAnimation = useRef(new Animated.Value(0)).current;
53+
const [width_text, setWidthText] = useState(0)
54+
const [width_icon, setWidthIcon] = useState(0)
5255

5356
const { options } = descriptors[route.key];
5457
const label =
@@ -166,9 +169,17 @@ const ColorfulTabBar = ({
166169
})`,
167170
],
168171
});
169-
const marginLeftText = focusAnimation.interpolate({
172+
173+
const HALF_ICON_WIDTH = width_icon/2
174+
const TEXT_ICON_WIDTH = width_text+HALF_ICON_WIDTH
175+
176+
const translateXIcon = focusAnimation.interpolate({
170177
inputRange: [0, 1],
171-
outputRange: [-50, 7],
178+
outputRange: [0, -(TEXT_ICON_WIDTH)/2],
179+
});
180+
const translateXText = focusAnimation.interpolate({
181+
inputRange: [0, 1],
182+
outputRange: [0, (HALF_ICON_WIDTH)+5],
172183
});
173184
const opacityText = focusAnimation.interpolate({
174185
inputRange: [0.3, 1],
@@ -202,22 +213,40 @@ const ColorfulTabBar = ({
202213
},
203214
]}
204215
>
205-
<View>{renderIcon(isFocused)}</View>
216+
<Animated.View style={{
217+
position: 'absolute',
218+
transform: [
219+
{translateX: translateXIcon}
220+
]
221+
}}
222+
onLayout={(e: LayoutChangeEvent) => {
223+
const {width} = e.nativeEvent.layout
224+
setWidthIcon(width)
225+
}}>
226+
{renderIcon(isFocused)}
227+
</Animated.View>
206228

207229
<Animated.View
208230
style={{
209-
marginLeft: marginLeftText,
231+
position: 'absolute',
210232
opacity: opacityText,
233+
transform: [
234+
{translateX: translateXText}
235+
],
236+
}}
237+
onLayout={(e: LayoutChangeEvent) => {
238+
const {width} = e.nativeEvent.layout
239+
setWidthText(width)
211240
}}
212241
>
213242
<Text
214243
numberOfLines={1}
215244
style={[
216245
ColorfulStyle.itemText,
217-
labelStyle,
218246
{
219247
color: darkMode ? FOREGROUND_COLOR : color,
220248
},
249+
labelStyle,
221250
]}
222251
>
223252
{label}

src/assets/TabStyle.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export const ColorfulStyle = StyleSheet.create({
3636
},
3737
itemText: {
3838
fontWeight: 'bold',
39+
maxWidth: 90
3940
},
4041
});
4142

0 commit comments

Comments
 (0)