@momo-kits/foundation 0.89.6 → 0.92.1-rc.1

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