@momo-kits/foundation 0.92.1-rc.10 → 0.92.1-rc.11

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