@momo-kits/foundation 0.92.1-tracking.9 → 0.92.2-tracking.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,479 @@
1
+ import {BottomTabBarProps} from '@react-navigation/bottom-tabs';
2
+ import {LabelPosition} from '@react-navigation/bottom-tabs/src/types';
3
+ import BottomTabBarHeightCallbackContext from '@react-navigation/bottom-tabs/src/utils/BottomTabBarHeightCallbackContext';
4
+ import BottomTabBarItem from '@react-navigation/bottom-tabs/src/views/BottomTabItem';
5
+ import {
6
+ CommonActions,
7
+ NavigationContext,
8
+ NavigationRouteContext,
9
+ ParamListBase,
10
+ TabNavigationState,
11
+ useLinkBuilder,
12
+ useTheme,
13
+ } from '@react-navigation/native';
14
+ import React, {useCallback} from 'react';
15
+ import {
16
+ Animated,
17
+ LayoutChangeEvent,
18
+ Platform,
19
+ StyleProp,
20
+ StyleSheet,
21
+ TouchableOpacity,
22
+ useWindowDimensions,
23
+ View,
24
+ ViewStyle,
25
+ } from 'react-native';
26
+ import {EdgeInsets, useSafeAreaInsets} from 'react-native-safe-area-context';
27
+ import {Radius} from '../../Consts';
28
+ import {Icon} from '../../Icon';
29
+ import {Text} from '../../Text';
30
+ import {FloatingButtonProps} from '../types';
31
+ import {getAccessibilityID} from '../utils';
32
+
33
+ type Props = BottomTabBarProps & {
34
+ activeTintColor?: string;
35
+ inactiveTintColor?: string;
36
+ floatingButton?: FloatingButtonProps;
37
+ };
38
+
39
+ type Options = {
40
+ state: TabNavigationState<ParamListBase>;
41
+ layout: {height: number; width: number};
42
+ dimensions: {height: number; width: number};
43
+ tabStyle: StyleProp<ViewStyle>;
44
+ labelPosition: LabelPosition | undefined;
45
+ adaptive: boolean | undefined;
46
+ };
47
+
48
+ const DEFAULT_MAX_TAB_ITEM_WIDTH = 125;
49
+ const DEFAULT_TABBAR_HEIGHT = 49;
50
+ const COMPACT_TABBAR_HEIGHT = 32;
51
+
52
+ const shouldUseHorizontalLabels = ({
53
+ state,
54
+ layout,
55
+ dimensions,
56
+ adaptive = true,
57
+ labelPosition,
58
+ tabStyle,
59
+ }: Options) => {
60
+ if (labelPosition) {
61
+ return labelPosition === 'beside-icon';
62
+ }
63
+
64
+ if (!adaptive) {
65
+ return false;
66
+ }
67
+
68
+ if (layout.width >= 768) {
69
+ // Screen size matches a tablet
70
+ let maxTabItemWidth = DEFAULT_MAX_TAB_ITEM_WIDTH;
71
+
72
+ const flattenedStyle = StyleSheet.flatten(tabStyle);
73
+
74
+ if (flattenedStyle) {
75
+ if (typeof flattenedStyle.width === 'number') {
76
+ maxTabItemWidth = flattenedStyle.width;
77
+ } else if (typeof flattenedStyle.maxWidth === 'number') {
78
+ maxTabItemWidth = flattenedStyle.maxWidth;
79
+ }
80
+ }
81
+
82
+ return state.routes.length * maxTabItemWidth <= layout.width;
83
+ } else {
84
+ return dimensions.width > dimensions.height;
85
+ }
86
+ };
87
+ const getPaddingBottom = (insets: EdgeInsets) =>
88
+ Math.max(insets.bottom - Platform.select({ios: 4, default: 0}), 0);
89
+
90
+ export const getTabBarHeight = ({
91
+ dimensions,
92
+ insets,
93
+ style,
94
+ ...rest
95
+ }: Options & {
96
+ insets: EdgeInsets;
97
+ style: Animated.WithAnimatedValue<StyleProp<ViewStyle>>;
98
+ }) => {
99
+ // @ts-ignore
100
+ const customHeight = StyleSheet.flatten(style)?.height;
101
+
102
+ if (typeof customHeight === 'number') {
103
+ return customHeight;
104
+ }
105
+
106
+ const isLandscape = dimensions.width > dimensions.height;
107
+ const horizontalLabels = shouldUseHorizontalLabels({dimensions, ...rest});
108
+ const paddingBottom = getPaddingBottom(insets);
109
+
110
+ if (
111
+ Platform.OS === 'ios' &&
112
+ !Platform.isPad &&
113
+ isLandscape &&
114
+ horizontalLabels
115
+ ) {
116
+ return COMPACT_TABBAR_HEIGHT + paddingBottom;
117
+ }
118
+
119
+ return DEFAULT_TABBAR_HEIGHT + paddingBottom;
120
+ };
121
+
122
+ export default function BottomTabBar({
123
+ state,
124
+ navigation,
125
+ descriptors,
126
+ activeBackgroundColor,
127
+ activeTintColor,
128
+ adaptive,
129
+ allowFontScaling,
130
+ inactiveBackgroundColor,
131
+ inactiveTintColor,
132
+ keyboardHidesTabBar = false,
133
+ labelPosition,
134
+ labelStyle,
135
+ iconStyle,
136
+ safeAreaInsets,
137
+ showLabel,
138
+ style,
139
+ tabStyle,
140
+ floatingButton,
141
+ }: Props) {
142
+ const {routes} = state;
143
+ const dimensions = useWindowDimensions();
144
+ const isFloating = floatingButton?.icon && state.routes.length == 4;
145
+ const itemWidth = isFloating
146
+ ? dimensions.width / 5
147
+ : dimensions.width / state.routes.length;
148
+
149
+ const [layout, setLayout] = React.useState({
150
+ height: 0,
151
+ width: dimensions.width,
152
+ });
153
+ const buildLink = useLinkBuilder();
154
+ const {colors} = useTheme();
155
+
156
+ const hasHorizontalLabels = shouldUseHorizontalLabels({
157
+ state,
158
+ dimensions,
159
+ layout,
160
+ adaptive,
161
+ labelPosition,
162
+ tabStyle,
163
+ });
164
+ const focusedRoute = state.routes[state.index] as any;
165
+ const focusedDescriptor = descriptors[focusedRoute?.key] as any;
166
+ const focusedOptions = focusedDescriptor?.options;
167
+ const shouldShowTabBar =
168
+ focusedOptions?.tabBarVisible !== false && !keyboardHidesTabBar;
169
+ const [visible] = React.useState(
170
+ () => new Animated.Value(shouldShowTabBar ? 1 : 0),
171
+ );
172
+
173
+ const indicatorAnimated = React.useRef(new Animated.Value(0)).current;
174
+
175
+ const visibilityAnimationConfigRef = React.useRef(
176
+ focusedOptions?.tabBarVisibilityAnimationConfig,
177
+ );
178
+ const onHeightChange = React.useContext(BottomTabBarHeightCallbackContext);
179
+ const [isTabBarHidden, setIsTabBarHidden] = React.useState(!shouldShowTabBar);
180
+ React.useEffect(() => {
181
+ visibilityAnimationConfigRef.current =
182
+ focusedOptions?.tabBarVisibilityAnimationConfig;
183
+ });
184
+
185
+ const handleLayout = (e: LayoutChangeEvent) => {
186
+ const {height, width} = e.nativeEvent.layout;
187
+
188
+ onHeightChange?.(height);
189
+
190
+ setLayout(layout => {
191
+ if (height === layout.height && width === layout.width) {
192
+ return layout;
193
+ } else {
194
+ return {
195
+ height,
196
+ width,
197
+ };
198
+ }
199
+ });
200
+ };
201
+
202
+ React.useEffect(() => {
203
+ const visibilityAnimationConfig = visibilityAnimationConfigRef.current;
204
+
205
+ if (shouldShowTabBar) {
206
+ const animation =
207
+ visibilityAnimationConfig?.show?.animation === 'spring'
208
+ ? Animated.spring
209
+ : Animated.timing;
210
+
211
+ animation(visible, {
212
+ toValue: 1,
213
+ useNativeDriver: true,
214
+ duration: 250,
215
+ ...visibilityAnimationConfig?.show?.config,
216
+ }).start(({finished}) => {
217
+ if (finished) {
218
+ setIsTabBarHidden(false);
219
+ }
220
+ });
221
+ } else {
222
+ setIsTabBarHidden(true);
223
+
224
+ const animation =
225
+ visibilityAnimationConfig?.hide?.animation === 'spring'
226
+ ? Animated.spring
227
+ : Animated.timing;
228
+
229
+ animation(visible, {
230
+ toValue: 0,
231
+ useNativeDriver: true,
232
+ duration: 200,
233
+ ...visibilityAnimationConfig?.hide?.config,
234
+ }).start();
235
+ }
236
+ }, [visible, shouldShowTabBar]);
237
+ const defaultInsets = useSafeAreaInsets();
238
+
239
+ const insets = {
240
+ top: safeAreaInsets?.top ?? defaultInsets.top,
241
+ right: safeAreaInsets?.right ?? defaultInsets.right,
242
+ bottom: safeAreaInsets?.bottom ?? defaultInsets.bottom,
243
+ left: safeAreaInsets?.left ?? defaultInsets.left,
244
+ };
245
+ const paddingBottom = getPaddingBottom(insets);
246
+
247
+ const leftArr = routes.slice(0, 2);
248
+ const rightArr = routes.slice(2, routes.length);
249
+ const focusedTab = state?.routes[state.index]?.name || routes[0]?.params;
250
+
251
+ const renderTabItem = useCallback(
252
+ (data: any[]) => {
253
+ return data?.map((route, index) => {
254
+ const focused = route.params.name === focusedTab;
255
+ const {options} = descriptors[route?.key] as any;
256
+ const onPress = () => {
257
+ const event = navigation.emit({
258
+ type: 'tabPress',
259
+ target: route.key,
260
+ canPreventDefault: true,
261
+ });
262
+
263
+ if (!focused && !event.defaultPrevented) {
264
+ navigation.dispatch({
265
+ ...CommonActions.navigate(route.name),
266
+ target: state.key,
267
+ });
268
+ }
269
+ };
270
+
271
+ const onLongPress = () => {
272
+ navigation.emit({
273
+ type: 'tabLongPress',
274
+ target: route.key,
275
+ });
276
+ };
277
+
278
+ const label =
279
+ options.tabBarLabel !== undefined
280
+ ? options.tabBarLabel
281
+ : options.title !== undefined
282
+ ? options.title
283
+ : route.name;
284
+
285
+ const accessibilityLabel =
286
+ options.tabBarAccessibilityLabel !== undefined
287
+ ? options.tabBarAccessibilityLabel
288
+ : typeof label === 'string'
289
+ ? `${label}, tab, ${index + 1} of ${routes.length}`
290
+ : undefined;
291
+
292
+ return (
293
+ <NavigationContext.Provider
294
+ key={route.key}
295
+ value={(descriptors as any)[route.key].navigation}>
296
+ <NavigationRouteContext.Provider value={route}>
297
+ <BottomTabBarItem
298
+ route={route}
299
+ focused={focused}
300
+ horizontal={hasHorizontalLabels}
301
+ onPress={onPress}
302
+ onLongPress={onLongPress}
303
+ accessibilityLabel={accessibilityLabel}
304
+ to={buildLink(route.name, route.params)}
305
+ testID={options.tabBarTestID}
306
+ allowFontScaling={allowFontScaling}
307
+ activeTintColor={activeTintColor}
308
+ inactiveTintColor={inactiveTintColor}
309
+ activeBackgroundColor={activeBackgroundColor}
310
+ inactiveBackgroundColor={inactiveBackgroundColor}
311
+ button={options.tabBarButton}
312
+ icon={options.tabBarIcon}
313
+ badge={options.tabBarBadge}
314
+ badgeStyle={options.tabBarBadgeStyle}
315
+ label={label}
316
+ showLabel={showLabel}
317
+ labelStyle={labelStyle}
318
+ iconStyle={iconStyle}
319
+ style={tabStyle}
320
+ {...getAccessibilityID(`btn_tabbar_${route.name}`)}
321
+ />
322
+ </NavigationRouteContext.Provider>
323
+ </NavigationContext.Provider>
324
+ );
325
+ });
326
+ },
327
+ [JSON.stringify(routes), state, descriptors, focusedTab],
328
+ );
329
+
330
+ const renderCenterComponent = useCallback(() => {
331
+ const renderFloatingButton = () => {
332
+ if (typeof floatingButton?.icon == 'string') {
333
+ return <Icon source={floatingButton?.icon} size={32} color={'white'} />;
334
+ } else if (floatingButton?.icon) {
335
+ return floatingButton?.icon;
336
+ } else {
337
+ return null;
338
+ }
339
+ };
340
+ if (floatingButton?.icon && state.routes.length !== 4) {
341
+ console.warn(
342
+ "Can't render floating button with icon when routes length is not 4",
343
+ );
344
+ return null;
345
+ }
346
+ return (
347
+ <View style={{flex: 1}}>
348
+ <TouchableOpacity
349
+ onPress={floatingButton?.onPress}
350
+ style={styles.floatingContainer}>
351
+ <View
352
+ style={{
353
+ ...styles.floatingContent,
354
+ backgroundColor: activeTintColor ?? colors.primary,
355
+ }}>
356
+ {renderFloatingButton()}
357
+ </View>
358
+ <View
359
+ style={{
360
+ top: -6,
361
+ backgroundColor: activeTintColor ?? colors.primary,
362
+ borderRadius: Radius.XL,
363
+ paddingHorizontal: 6,
364
+ }}>
365
+ <Text
366
+ numberOfLines={1}
367
+ typography={'description_xs_regular'}
368
+ color={'white'}>
369
+ {floatingButton?.label}
370
+ </Text>
371
+ </View>
372
+ </TouchableOpacity>
373
+ </View>
374
+ );
375
+ }, [floatingButton]);
376
+
377
+ React.useEffect(() => {
378
+ Animated.timing(indicatorAnimated, {
379
+ toValue:
380
+ isFloating && state.index > 1
381
+ ? itemWidth * (state.index + 1)
382
+ : itemWidth * state.index,
383
+ useNativeDriver: false,
384
+ duration: 200,
385
+ }).start();
386
+ }, [state.index, itemWidth, indicatorAnimated, isFloating]);
387
+
388
+ return (
389
+ <Animated.View
390
+ style={[
391
+ styles.tabBar,
392
+ {
393
+ backgroundColor: colors.card,
394
+ borderTopColor: colors.border,
395
+ },
396
+ {
397
+ transform: [
398
+ {
399
+ translateY: visible.interpolate({
400
+ inputRange: [0, 1],
401
+ outputRange: [
402
+ layout.height + paddingBottom + StyleSheet.hairlineWidth,
403
+ 0,
404
+ ],
405
+ }),
406
+ },
407
+ ],
408
+ // Absolutely position the tab bar so that the content is below it
409
+ // This is needed to avoid gap at bottom when the tab bar is hidden
410
+ position: isTabBarHidden ? 'absolute' : (null as any),
411
+ },
412
+ {
413
+ paddingBottom: paddingBottom - 0,
414
+ paddingHorizontal: Math.max(insets.left, insets.right),
415
+ },
416
+ style,
417
+ ]}
418
+ pointerEvents={isTabBarHidden ? 'none' : 'auto'}
419
+ onLayout={handleLayout}>
420
+ <View style={styles.content}>
421
+ {isFloating && floatingButton?.container}
422
+ <Animated.View
423
+ style={{
424
+ width: itemWidth,
425
+ position: 'absolute',
426
+ top: 0,
427
+ transform: [{translateX: indicatorAnimated}],
428
+ }}>
429
+ <View
430
+ style={[
431
+ styles.indicator,
432
+ {backgroundColor: activeTintColor ?? colors.primary},
433
+ ]}
434
+ />
435
+ </Animated.View>
436
+ {isFloating ? renderTabItem(leftArr) : renderTabItem(routes)}
437
+ {isFloating && renderCenterComponent()}
438
+ {isFloating && renderTabItem(rightArr)}
439
+ </View>
440
+ </Animated.View>
441
+ );
442
+ }
443
+
444
+ const styles = StyleSheet.create({
445
+ content: {
446
+ flex: 1,
447
+ flexDirection: 'row',
448
+ },
449
+ tabBar: {
450
+ left: 0,
451
+ right: 0,
452
+ bottom: 0,
453
+ borderTopWidth: StyleSheet.hairlineWidth,
454
+ elevation: 8,
455
+ },
456
+ floatingContainer: {
457
+ position: 'absolute',
458
+ alignItems: 'center',
459
+ left: 0,
460
+ right: 0,
461
+ bottom: Platform.OS === 'ios' ? -6 : 0,
462
+ },
463
+ floatingContent: {
464
+ alignItems: 'center',
465
+ justifyContent: 'center',
466
+ height: 48,
467
+ width: 48,
468
+
469
+ borderRadius: 100,
470
+ top: -12,
471
+ },
472
+ indicator: {
473
+ height: 2,
474
+ width: 44,
475
+ alignSelf: 'center',
476
+ borderBottomLeftRadius: 1,
477
+ borderBottomRightRadius: 1,
478
+ },
479
+ });
@@ -1,15 +1,16 @@
1
- import React, {useContext, useEffect} from 'react';
2
- import {Platform} from 'react-native';
3
- import {BottomTabProps, NavigationScreenProps} from './types';
4
1
  import {createBottomTabNavigator} from '@react-navigation/bottom-tabs';
2
+ import {NavigationContainer} from '@react-navigation/native';
5
3
  import {createStackNavigator} from '@react-navigation/stack';
4
+ import React, {useContext, useEffect} from 'react';
5
+ import {Platform} from 'react-native';
6
6
  import {useSafeAreaInsets} from 'react-native-safe-area-context';
7
- import {NavigationContainer} from '@react-navigation/native';
8
- import {Icon} from '../Icon';
9
- import {Text} from '../Text';
10
- import {ApplicationContext} from './index';
11
- import {getAccessibilityID, getStackOptions} from './utils';
12
- import StackScreen from './StackScreen';
7
+ import {Icon} from '../../Icon';
8
+ import {Text} from '../../Text';
9
+ import {ApplicationContext} from '../index';
10
+ import StackScreen from '../StackScreen';
11
+ import {BottomTabProps, NavigationScreenProps} from '../types';
12
+ import {getAccessibilityID, getStackOptions} from '../utils';
13
+ import BottomTabBar from './BottomTabBar';
13
14
 
14
15
  const Tab = createBottomTabNavigator();
15
16
  const Stack = createStackNavigator();
@@ -40,12 +41,14 @@ const TabScreen: React.FC<NavigationScreenProps> = ({route}) => {
40
41
  </NavigationContainer>
41
42
  );
42
43
  };
44
+
43
45
  const BottomTab: React.FC<BottomTabProps> = ({
44
46
  nested,
45
47
  tabs,
46
48
  listeners,
47
49
  navigation,
48
50
  initialRouteName,
51
+ floatingButton,
49
52
  }) => {
50
53
  const {theme} = useContext(ApplicationContext);
51
54
  const insets = useSafeAreaInsets();
@@ -58,13 +61,17 @@ const BottomTab: React.FC<BottomTabProps> = ({
58
61
  });
59
62
  }, []);
60
63
 
61
- const accessibilityID = getAccessibilityID?.("tbr_main_bottom")
62
-
63
64
  return (
64
65
  <Tab.Navigator
65
- {...accessibilityID}
66
66
  initialRouteName={initialRouteName}
67
67
  backBehavior={'firstRoute'}
68
+ tabBar={props => (
69
+ <BottomTabBar
70
+ {...getAccessibilityID('tbr_main_bottom')}
71
+ {...props}
72
+ floatingButton={floatingButton}
73
+ />
74
+ )}
68
75
  tabBarOptions={{
69
76
  style: {
70
77
  height: 68 + Math.min(insets.bottom, 21),
@@ -75,10 +82,8 @@ const BottomTab: React.FC<BottomTabProps> = ({
75
82
  },
76
83
  }}>
77
84
  {tabs.map(item => {
78
- const itemAccessibilityID = getAccessibilityID?.(`btn_tabbar_${item.name}`)
79
85
  return (
80
86
  <Tab.Screen
81
- {...itemAccessibilityID}
82
87
  key={item.name}
83
88
  name={item.name}
84
89
  component={TabScreen}