-
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
BottomTabItem.tsx
305 lines (288 loc) · 6.68 KB
/
BottomTabItem.tsx
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import { getLabel, Label, PlatformPressable } from '@react-navigation/elements';
import { Route, useTheme } from '@react-navigation/native';
import Color from 'color';
import React from 'react';
import {
GestureResponderEvent,
Platform,
StyleProp,
StyleSheet,
TextStyle,
ViewStyle,
} from 'react-native';
import type {
BottomTabBarButtonProps,
BottomTabDescriptor,
LabelPosition,
} from '../types';
import { TabBarIcon } from './TabBarIcon';
type Props = {
/**
* The route object which should be specified by the tab.
*/
route: Route<string>;
/**
* The `href` to use for the anchor tag on web
*/
href?: string;
/**
* Whether the tab is focused.
*/
focused: boolean;
/**
* The descriptor object for the route.
*/
descriptor: BottomTabDescriptor;
/**
* The label text of the tab.
*/
label:
| string
| ((props: {
focused: boolean;
color: string;
position: LabelPosition;
children: string;
}) => React.ReactNode);
/**
* Icon to display for the tab.
*/
icon: (props: {
focused: boolean;
size: number;
color: string;
}) => React.ReactNode;
/**
* Text to show in a badge on the tab icon.
*/
badge?: number | string;
/**
* Custom style for the badge.
*/
badgeStyle?: StyleProp<TextStyle>;
/**
* The button for the tab. Uses a `TouchableWithoutFeedback` by default.
*/
button?: (props: BottomTabBarButtonProps) => React.ReactNode;
/**
* The accessibility label for the tab.
*/
accessibilityLabel?: string;
/**
* An unique ID for testing for the tab.
*/
testID?: string;
/**
* Function to execute on press in React Native.
* On the web, this will use onClick.
*/
onPress: (
e: React.MouseEvent<HTMLElement, MouseEvent> | GestureResponderEvent
) => void;
/**
* Function to execute on long press.
*/
onLongPress: (e: GestureResponderEvent) => void;
/**
* Whether the label should be aligned with the icon horizontally.
*/
horizontal: boolean;
/**
* Color for the icon and label when the item is active.
*/
activeTintColor?: string;
/**
* Color for the icon and label when the item is inactive.
*/
inactiveTintColor?: string;
/**
* Background color for item when its active.
*/
activeBackgroundColor?: string;
/**
* Background color for item when its inactive.
*/
inactiveBackgroundColor?: string;
/**
* Whether to show the label text for the tab.
*/
showLabel?: boolean;
/**
* Whether to allow scaling the font for the label for accessibility purposes.
*/
allowFontScaling?: boolean;
/**
* Style object for the label element.
*/
labelStyle?: StyleProp<TextStyle>;
/**
* Style object for the icon element.
*/
iconStyle?: StyleProp<ViewStyle>;
/**
* Style object for the wrapper element.
*/
style?: StyleProp<ViewStyle>;
};
export function BottomTabItem({
route,
href,
focused,
descriptor,
label,
icon,
badge,
badgeStyle,
button = ({
href,
children,
style,
onPress,
accessibilityRole,
...rest
}: BottomTabBarButtonProps) => {
return (
<PlatformPressable
{...rest}
android_ripple={{ borderless: true }}
pressOpacity={1}
href={href}
accessibilityRole={accessibilityRole}
onPress={onPress}
style={style}
>
{children}
</PlatformPressable>
);
},
accessibilityLabel,
testID,
onPress,
onLongPress,
horizontal,
activeTintColor: customActiveTintColor,
inactiveTintColor: customInactiveTintColor,
activeBackgroundColor = 'transparent',
inactiveBackgroundColor = 'transparent',
showLabel = true,
allowFontScaling,
labelStyle,
iconStyle,
style,
}: Props) {
const { colors } = useTheme();
const activeTintColor =
customActiveTintColor === undefined
? colors.primary
: customActiveTintColor;
const inactiveTintColor =
customInactiveTintColor === undefined
? Color(colors.text).mix(Color(colors.card), 0.5).hex()
: customInactiveTintColor;
const renderLabel = ({ focused }: { focused: boolean }) => {
if (showLabel === false) {
return null;
}
const color = focused ? activeTintColor : inactiveTintColor;
if (typeof label !== 'string') {
const { options } = descriptor;
const children = getLabel(
{
label:
typeof options.tabBarLabel === 'string'
? options.tabBarLabel
: undefined,
title: options.title,
},
route.name
);
return label({
focused,
color,
position: horizontal ? 'beside-icon' : 'below-icon',
children,
});
}
return (
<Label
style={[
horizontal ? styles.labelBeside : styles.labelBeneath,
labelStyle,
]}
allowFontScaling={allowFontScaling}
tintColor={color}
>
{label}
</Label>
);
};
const renderIcon = ({ focused }: { focused: boolean }) => {
if (icon === undefined) {
return null;
}
const activeOpacity = focused ? 1 : 0;
const inactiveOpacity = focused ? 0 : 1;
return (
<TabBarIcon
route={route}
horizontal={horizontal}
badge={badge}
badgeStyle={badgeStyle}
activeOpacity={activeOpacity}
inactiveOpacity={inactiveOpacity}
activeTintColor={activeTintColor}
inactiveTintColor={inactiveTintColor}
renderIcon={icon}
style={iconStyle}
/>
);
};
const scene = { route, focused };
const backgroundColor = focused
? activeBackgroundColor
: inactiveBackgroundColor;
return button({
href,
onPress,
onLongPress,
testID,
accessibilityLabel,
// FIXME: accessibilityRole: 'tab' doesn't seem to work as expected on iOS
accessibilityRole: Platform.select({ ios: 'button', default: 'tab' }),
accessibilityState: { selected: focused },
// @ts-expect-error: keep for compatibility with older React Native versions
accessibilityStates: focused ? ['selected'] : [],
style: [
styles.tab,
{ backgroundColor },
horizontal ? styles.tabLandscape : styles.tabPortrait,
style,
],
children: (
<React.Fragment>
{renderIcon(scene)}
{renderLabel(scene)}
</React.Fragment>
),
}) as React.ReactElement;
}
const styles = StyleSheet.create({
tab: {
alignItems: 'center',
},
tabPortrait: {
justifyContent: 'flex-end',
flexDirection: 'column',
},
tabLandscape: {
justifyContent: 'center',
flexDirection: 'row',
},
labelBeneath: {
fontSize: 10,
},
labelBeside: {
fontSize: 13,
marginLeft: 20,
},
});