@momo-kits/foundation 0.169.13-beta.1 → 0.169.14

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,60 @@
1
+ import { Platform, StyleSheet } from 'react-native';
2
+ import { Colors, Radius } from '../../../Consts';
3
+
4
+ export const styles = StyleSheet.create({
5
+ content: {
6
+ flex: 1,
7
+ flexDirection: 'row',
8
+ },
9
+ tabBar: {
10
+ left: 0,
11
+ right: 0,
12
+ bottom: 0,
13
+ borderTopWidth: StyleSheet.hairlineWidth,
14
+ elevation: 8,
15
+ },
16
+ tabBarFloatingButton: {
17
+ left: 0,
18
+ right: 0,
19
+ bottom: 0,
20
+ zIndex: 1,
21
+ shadowColor: Colors.black_20,
22
+ shadowOffset: { width: 0, height: -2 },
23
+ shadowOpacity: 0.05,
24
+ shadowRadius: 3,
25
+ elevation: 8,
26
+ },
27
+ floatingContainer: {
28
+ position: 'absolute',
29
+ alignItems: 'center',
30
+ left: 0,
31
+ right: 0,
32
+ top: -2,
33
+ bottom: Platform.OS === 'ios' ? -6 : 4,
34
+ },
35
+ floatingContent: {
36
+ alignItems: 'center',
37
+ justifyContent: 'center',
38
+ height: 48,
39
+ width: 48,
40
+ borderRadius: 24,
41
+ top: -10,
42
+ },
43
+ indicator: {
44
+ height: 2,
45
+ width: 44,
46
+ alignSelf: 'center',
47
+ borderBottomLeftRadius: 1,
48
+ borderBottomRightRadius: 1,
49
+ },
50
+ debugBaseLine: { borderWidth: 1, borderColor: Colors.green_06 },
51
+ floatingLabel: {
52
+ top: -4,
53
+ borderRadius: Radius.XL,
54
+ paddingHorizontal: 6,
55
+ },
56
+ indicatorContainer: {
57
+ position: 'absolute',
58
+ top: 0,
59
+ },
60
+ });
@@ -0,0 +1,270 @@
1
+ import { BottomTabBarProps } from '@react-navigation/bottom-tabs';
2
+ import { CommonActions, Route } from '@react-navigation/native';
3
+ import React, { useCallback, useContext } from 'react';
4
+ import {
5
+ Platform,
6
+ TouchableOpacity,
7
+ useWindowDimensions,
8
+ View,
9
+ } from 'react-native';
10
+ import Animated, {
11
+ useAnimatedStyle,
12
+ useSharedValue,
13
+ withTiming,
14
+ } from 'react-native-reanimated';
15
+ import { EdgeInsets, useSafeAreaInsets } from 'react-native-safe-area-context';
16
+ import { Styles } from '../../../Consts';
17
+ import { Icon } from '../../../Icon';
18
+ import { Text } from '../../../Text';
19
+ import { FloatingButtonProps } from '../../types';
20
+ import CustomBottomTabItem from './CustomBottomTabItem';
21
+ import { ApplicationContext, MiniAppContext } from '../../../Context';
22
+ import { styles } from './BottomTabBar.styles';
23
+
24
+ type Props = BottomTabBarProps & {
25
+ activeTintColor?: string;
26
+ inactiveTintColor?: string;
27
+ floatingButton?: FloatingButtonProps;
28
+ activeBackgroundColor?: string;
29
+ allowFontScaling?: boolean;
30
+ inactiveBackgroundColor?: string;
31
+ labelStyle?: any;
32
+ iconStyle?: any;
33
+ showLabel?: boolean;
34
+ style?: any;
35
+ tabStyle?: any;
36
+ badgeStyle?: any;
37
+ };
38
+
39
+ const getPaddingBottom = (insets: EdgeInsets) => {
40
+ return Math.max(insets.bottom - Platform.select({ ios: 4, default: 0 }), 0);
41
+ };
42
+
43
+ export default function BottomTabBar({
44
+ state,
45
+ navigation,
46
+ descriptors,
47
+ activeBackgroundColor,
48
+ activeTintColor,
49
+ allowFontScaling,
50
+ inactiveBackgroundColor,
51
+ inactiveTintColor,
52
+ labelStyle,
53
+ iconStyle,
54
+ showLabel,
55
+ style,
56
+ floatingButton,
57
+ }: Props) {
58
+ const { theme } = useContext(ApplicationContext);
59
+ const { routes } = state;
60
+ const widthDevice = useWindowDimensions()?.width;
61
+ const insets = useSafeAreaInsets();
62
+ const useFloating = floatingButton && state.routes.length % 2 === 0;
63
+ const itemWidth =
64
+ (widthDevice - insets.left - insets.right) /
65
+ (state.routes.length + (useFloating ? 1 : 0));
66
+ // const buildLink = useLinkBuilder();
67
+ const indicatorAnimated = useSharedValue(0);
68
+
69
+ const indicatorStyle = useAnimatedStyle(() => ({
70
+ width: itemWidth,
71
+ transform: [{ translateX: indicatorAnimated.value }],
72
+ }));
73
+ const paddingBottom = getPaddingBottom(insets);
74
+ const focusedTab = state?.routes[state.index]?.name || routes[0]?.params;
75
+
76
+ const context = useContext<any>(MiniAppContext);
77
+ const showBaseLineDebug = context?.features?.showBaseLineDebug ?? false;
78
+
79
+ /**
80
+ * render tab item
81
+ */
82
+ const renderTabItem = useCallback(
83
+ (data: Route<string, any>[]) => {
84
+ return data?.map(route => {
85
+ const focused = route?.params?.name === focusedTab;
86
+ const { options } = descriptors[route.key];
87
+
88
+ const onPress = () => {
89
+ const event = navigation.emit({
90
+ type: 'tabPress',
91
+ target: route.key,
92
+ canPreventDefault: true,
93
+ });
94
+
95
+ if (!focused && !event.defaultPrevented) {
96
+ navigation.dispatch({
97
+ ...CommonActions.navigate(route.name),
98
+ target: state.key,
99
+ });
100
+ }
101
+ };
102
+
103
+ const onLongPress = () => {
104
+ navigation.emit({
105
+ type: 'tabLongPress',
106
+ target: route.key,
107
+ });
108
+ };
109
+
110
+ return (
111
+ <CustomBottomTabItem
112
+ key={route.key}
113
+ route={route}
114
+ label={options.tabBarLabel || options.title || route.name}
115
+ focused={focused}
116
+ onPress={onPress}
117
+ onLongPress={onLongPress}
118
+ activeTintColor={activeTintColor ?? theme.colors.primary}
119
+ inactiveTintColor={inactiveTintColor ?? theme.colors.text.default}
120
+ activeBackgroundColor={activeBackgroundColor}
121
+ inactiveBackgroundColor={inactiveBackgroundColor}
122
+ icon={options.tabBarIcon}
123
+ badge={options.tabBarBadge}
124
+ showLabel={showLabel}
125
+ labelStyle={labelStyle}
126
+ iconStyle={iconStyle}
127
+ style={options.tabBarStyle}
128
+ badgeStyle={options.tabBarBadgeStyle}
129
+ allowFontScaling={allowFontScaling}
130
+ />
131
+ );
132
+ });
133
+ },
134
+ [
135
+ focusedTab,
136
+ descriptors,
137
+ activeTintColor,
138
+ theme.colors.primary,
139
+ theme.colors.text.default,
140
+ inactiveTintColor,
141
+ activeBackgroundColor,
142
+ inactiveBackgroundColor,
143
+ showLabel,
144
+ labelStyle,
145
+ iconStyle,
146
+ allowFontScaling,
147
+ navigation,
148
+ state.key,
149
+ ],
150
+ );
151
+
152
+ /**
153
+ * render floating button
154
+ */
155
+ const renderFloatingButton = useCallback(() => {
156
+ const renderFloatingIcon = () => {
157
+ if (typeof floatingButton?.icon === 'string') {
158
+ return <Icon source={floatingButton?.icon} size={32} color={'white'} />;
159
+ }
160
+ return floatingButton?.icon;
161
+ };
162
+
163
+ return (
164
+ <View style={[Styles.flex, showBaseLineDebug && styles.debugBaseLine]}>
165
+ <TouchableOpacity
166
+ onPress={floatingButton?.onPress}
167
+ style={styles.floatingContainer}
168
+ >
169
+ <View
170
+ style={{
171
+ ...styles.floatingContent,
172
+ backgroundColor: activeTintColor ?? theme.colors.primary,
173
+ }}
174
+ >
175
+ {renderFloatingIcon()}
176
+ </View>
177
+ <View
178
+ style={[
179
+ styles.floatingLabel,
180
+ { backgroundColor: activeTintColor ?? theme.colors.primary },
181
+ ]}
182
+ >
183
+ <Text
184
+ numberOfLines={1}
185
+ typography={'description_xs_regular'}
186
+ color={'white'}
187
+ >
188
+ {floatingButton?.label}
189
+ </Text>
190
+ </View>
191
+ </TouchableOpacity>
192
+ </View>
193
+ );
194
+ }, [
195
+ activeTintColor,
196
+ floatingButton?.icon,
197
+ floatingButton?.label,
198
+ floatingButton?.onPress,
199
+ showBaseLineDebug,
200
+ theme.colors.primary,
201
+ ]);
202
+
203
+ /**
204
+ * animated indicator
205
+ */
206
+ React.useEffect(() => {
207
+ let index = state.index;
208
+ if (useFloating && index > (state.routes.length - 1) / 2) {
209
+ index += 1;
210
+ }
211
+ indicatorAnimated.value = withTiming(itemWidth * index, { duration: 200 });
212
+ }, [
213
+ state.index,
214
+ itemWidth,
215
+ indicatorAnimated,
216
+ useFloating,
217
+ state.routes.length,
218
+ ]);
219
+
220
+ /**
221
+ * render tab bar
222
+ */
223
+ const renderTabBar = () => {
224
+ if (useFloating) {
225
+ const middleIndex = routes.length / 2;
226
+ const leftArr = routes.slice(0, middleIndex);
227
+ const rightArr = routes.slice(middleIndex);
228
+ return (
229
+ <>
230
+ {renderTabItem(leftArr)}
231
+ {renderFloatingButton()}
232
+ {renderTabItem(rightArr)}
233
+ </>
234
+ );
235
+ }
236
+ return renderTabItem(routes);
237
+ };
238
+
239
+ return (
240
+ <View
241
+ style={[
242
+ floatingButton ? styles.tabBarFloatingButton : styles.tabBar,
243
+ {
244
+ backgroundColor: theme.colors.background.surface,
245
+ borderTopColor: theme.colors.border.default,
246
+ },
247
+ {
248
+ paddingBottom: paddingBottom,
249
+ paddingLeft: insets.left,
250
+ paddingRight: insets.right,
251
+ },
252
+ style,
253
+ ]}
254
+ pointerEvents={'auto'}
255
+ >
256
+ <View style={styles.content} accessibilityLabel={'bottom_tab_bar'}>
257
+ {useFloating && floatingButton?.container}
258
+ <Animated.View style={[styles.indicatorContainer, indicatorStyle]}>
259
+ <View
260
+ style={[
261
+ styles.indicator,
262
+ { backgroundColor: activeTintColor ?? theme.colors.primary },
263
+ ]}
264
+ />
265
+ </Animated.View>
266
+ {renderTabBar()}
267
+ </View>
268
+ </View>
269
+ );
270
+ }
@@ -0,0 +1,153 @@
1
+ import React, { useContext } from 'react';
2
+ import { TouchableOpacity, StyleSheet } from 'react-native';
3
+ import {
4
+ BottomTabBarButtonProps,
5
+ BottomTabNavigationOptions,
6
+ } from '@react-navigation/bottom-tabs';
7
+ import { Route } from '@react-navigation/native';
8
+ import { TabBarIcon } from './TabBarIcon';
9
+ import { Text } from '../../../Text';
10
+ import { MiniAppContext } from '../../../Context';
11
+ import { Colors } from '../../../Consts';
12
+
13
+ type LabelType = NonNullable<BottomTabNavigationOptions['tabBarLabel']>;
14
+
15
+ interface CustomBottomTabItemProps {
16
+ route: Route<string>;
17
+ focused: boolean;
18
+ label: LabelType;
19
+ onPress: () => void;
20
+ onLongPress: () => void;
21
+ activeTintColor: string;
22
+ inactiveTintColor: string;
23
+ activeBackgroundColor?: string;
24
+ inactiveBackgroundColor?: string;
25
+ icon: any;
26
+ badge?: string | number;
27
+ showLabel?: boolean;
28
+ labelStyle?: any;
29
+ iconStyle?: any;
30
+ style?: any;
31
+ badgeStyle?: any;
32
+ allowFontScaling?: boolean;
33
+ button?: (props: BottomTabBarButtonProps) => React.ReactNode;
34
+ /**
35
+ * The accessibility label for the tab.
36
+ */
37
+ accessibilityLabel?: string;
38
+ /**
39
+ * An unique ID for testing for the tab.
40
+ */
41
+ testID?: string;
42
+ }
43
+
44
+ const CustomBottomTabItem: React.FC<CustomBottomTabItemProps> = ({
45
+ route,
46
+ focused,
47
+ label,
48
+ onPress,
49
+ onLongPress,
50
+ activeTintColor,
51
+ inactiveTintColor,
52
+ activeBackgroundColor,
53
+ inactiveBackgroundColor,
54
+ icon,
55
+ badge,
56
+ showLabel,
57
+ labelStyle,
58
+ iconStyle,
59
+ style,
60
+ badgeStyle,
61
+ allowFontScaling,
62
+ }) => {
63
+ const context = useContext<any>(MiniAppContext);
64
+ const showBaseLineDebug = context?.features?.showBaseLineDebug ?? false;
65
+
66
+ const renderIcon = () => {
67
+ if (icon === undefined) {
68
+ return null;
69
+ }
70
+
71
+ const activeOpacity = focused ? 1 : 0;
72
+ const inactiveOpacity = focused ? 0 : 1;
73
+
74
+ return (
75
+ <TabBarIcon
76
+ route={route}
77
+ badge={badge}
78
+ badgeStyle={badgeStyle}
79
+ activeOpacity={activeOpacity}
80
+ allowFontScaling={allowFontScaling}
81
+ inactiveOpacity={inactiveOpacity}
82
+ activeTintColor={activeTintColor}
83
+ inactiveTintColor={inactiveTintColor}
84
+ renderIcon={icon}
85
+ style={iconStyle}
86
+ />
87
+ );
88
+ };
89
+
90
+ const renderLabel = ({ focused: isFocused }: { focused: boolean }) => {
91
+ if (showLabel === false) {
92
+ return null;
93
+ }
94
+
95
+ const color = isFocused ? activeTintColor : inactiveTintColor;
96
+
97
+ if (typeof label === 'string') {
98
+ return (
99
+ <Text
100
+ numberOfLines={1}
101
+ style={[styles.label, labelStyle]}
102
+ color={isFocused ? activeTintColor : inactiveTintColor}
103
+ typography={isFocused ? 'label_xs_medium' : 'description_xs_regular'}
104
+ allowFontScaling={allowFontScaling}
105
+ >
106
+ {label}
107
+ </Text>
108
+ );
109
+ }
110
+
111
+ return label({
112
+ focused: isFocused,
113
+ color,
114
+ children: route.name,
115
+ position: 'below-icon',
116
+ });
117
+ };
118
+
119
+ const background = focused ? activeBackgroundColor : inactiveBackgroundColor;
120
+
121
+ return (
122
+ <TouchableOpacity
123
+ onPress={onPress}
124
+ onLongPress={onLongPress}
125
+ style={[
126
+ styles.container,
127
+ { backgroundColor: background },
128
+ style,
129
+ showBaseLineDebug && styles.debugBaseLine,
130
+ ]}
131
+ >
132
+ {renderIcon()}
133
+ {renderLabel({ focused })}
134
+ </TouchableOpacity>
135
+ );
136
+ };
137
+
138
+ const styles = StyleSheet.create({
139
+ container: {
140
+ flex: 1,
141
+ alignItems: 'center',
142
+ justifyContent: 'center',
143
+ paddingVertical: 8,
144
+ bottom: -2,
145
+ },
146
+ label: {
147
+ fontSize: 12,
148
+ fontWeight: '600',
149
+ },
150
+ debugBaseLine: { borderWidth: 1, borderColor: Colors.green_06 },
151
+ });
152
+
153
+ export default CustomBottomTabItem;
@@ -0,0 +1,135 @@
1
+ import React, {
2
+ createContext,
3
+ ReactNode,
4
+ useContext,
5
+ useMemo,
6
+ useState,
7
+ } from 'react';
8
+ import { Spacing } from '../../../Consts';
9
+ import { useScreenLayout } from '../../../Layout';
10
+ import { DuoMetrics } from '../../../Layout/utils';
11
+
12
+ export type DuoBarCompressionBehavior = 'toolbar' | 'tabBar';
13
+
14
+ type DuoBarCompression = {
15
+ toolbarCompressed: boolean;
16
+ tabBarCompressed: boolean;
17
+ // the open rail needs the toolbar button slot too: only Back stays
18
+ toolbarHidden?: boolean;
19
+ // tapping the compressed bar expands it and compresses the other one
20
+ expandToolbar?: () => void;
21
+ expandTabBar?: () => void;
22
+ // a rail opened from the compressed state closes once a tab is picked
23
+ onTabSelected?: () => void;
24
+ // header buttons report their full height so the tab bar knows what is left
25
+ setToolbarHeight?: (height: number) => void;
26
+ };
27
+
28
+ const RAIL_PADDING = 6;
29
+ const RAIL_GAP = Spacing.XXS;
30
+ // space kept between the top and the bottom groups (HIG Vertical controls)
31
+ const GROUP_GAP = Spacing.M;
32
+ // Back, then the right toolkit: help + home capsule
33
+ const DEFAULT_TOOLBAR_HEIGHT =
34
+ DuoMetrics.controlSize + Spacing.M + 2 * DuoMetrics.itemPitch;
35
+ // Back, then the single button the toolbar folds into
36
+ const COMPRESSED_TOOLBAR_HEIGHT = 2 * DuoMetrics.controlSize + Spacing.M;
37
+
38
+ const NO_COMPRESSION: DuoBarCompression = {
39
+ toolbarCompressed: false,
40
+ tabBarCompressed: false,
41
+ };
42
+
43
+ const DuoBarCompressionContext =
44
+ createContext<DuoBarCompression>(NO_COMPRESSION);
45
+
46
+ /**
47
+ * height of the vertical tab rail, plus the QR button under it
48
+ */
49
+ const getRailHeight = (tabCount: number, hasQr: boolean) =>
50
+ 2 * RAIL_PADDING +
51
+ tabCount * DuoMetrics.controlSize +
52
+ Math.max(tabCount - 1, 0) * RAIL_GAP +
53
+ (hasQr ? Spacing.M + DuoMetrics.controlSize : 0);
54
+
55
+ /**
56
+ * keeps the side bar's toolbar and tab bar clear of each other (HIG, Designing for
57
+ * iPhone Duo: Vertical controls):
58
+ * - both fit: both stay whole
59
+ * - one fits whole beside the other compressed: one is compressed, tapping it swaps them
60
+ * - otherwise both rest compressed; tapping one opens it for a moment (the rail closes
61
+ * again once a tab is picked)
62
+ */
63
+ const DuoBarCompressionProvider: React.FC<{
64
+ behavior?: DuoBarCompressionBehavior;
65
+ tabCount: number;
66
+ hasQr: boolean;
67
+ children: ReactNode;
68
+ }> = ({ behavior = 'toolbar', tabCount, hasQr, children }) => {
69
+ const { height, sideBar } = useScreenLayout();
70
+ const [toolbarHeight, setToolbarHeight] = useState(DEFAULT_TOOLBAR_HEIGHT);
71
+ // 'toolbar' compression keeps the tab bar whole until the toolbar is tapped
72
+ const [expanded, setExpanded] = useState<'toolbar' | 'tabBar'>(
73
+ behavior === 'toolbar' ? 'tabBar' : 'toolbar',
74
+ );
75
+ // bar opened for a moment while both rest compressed
76
+ const [opened, setOpened] = useState<'toolbar' | 'tabBar'>();
77
+
78
+ const value = useMemo(() => {
79
+ if (!sideBar) {
80
+ return NO_COMPRESSION;
81
+ }
82
+ const room = height - sideBar.bottom - sideBar.top - GROUP_GAP;
83
+ const tabBar = getRailHeight(tabCount, hasQr);
84
+ const compressedTabBar = getRailHeight(1, hasQr) - 2 * RAIL_PADDING;
85
+ const fits = (toolbar: number, tabs: number) => toolbar + tabs <= room;
86
+
87
+ if (fits(toolbarHeight, tabBar)) {
88
+ return { ...NO_COMPRESSION, setToolbarHeight };
89
+ }
90
+ const canSwap =
91
+ fits(COMPRESSED_TOOLBAR_HEIGHT, tabBar) &&
92
+ fits(toolbarHeight, compressedTabBar);
93
+ if (canSwap) {
94
+ return {
95
+ toolbarCompressed: expanded === 'tabBar',
96
+ tabBarCompressed: expanded === 'toolbar',
97
+ expandToolbar: () => setExpanded('toolbar'),
98
+ expandTabBar: () => setExpanded('tabBar'),
99
+ setToolbarHeight,
100
+ };
101
+ }
102
+
103
+ const open = {
104
+ expandToolbar: () => setOpened('toolbar'),
105
+ expandTabBar: () => setOpened('tabBar'),
106
+ setToolbarHeight,
107
+ };
108
+ // an opened rail always shows, even when only the gap between the groups is short:
109
+ // it closes again once a tab is picked
110
+ if (opened === 'tabBar') {
111
+ return {
112
+ ...open,
113
+ toolbarCompressed: true,
114
+ toolbarHidden: true,
115
+ tabBarCompressed: false,
116
+ onTabSelected: () => setOpened(undefined),
117
+ };
118
+ }
119
+ return {
120
+ ...open,
121
+ toolbarCompressed: opened !== 'toolbar',
122
+ tabBarCompressed: true,
123
+ };
124
+ }, [expanded, opened, hasQr, height, sideBar, tabCount, toolbarHeight]);
125
+
126
+ return (
127
+ <DuoBarCompressionContext.Provider value={value}>
128
+ {children}
129
+ </DuoBarCompressionContext.Provider>
130
+ );
131
+ };
132
+
133
+ const useDuoBarCompression = () => useContext(DuoBarCompressionContext);
134
+
135
+ export { DuoBarCompressionProvider, useDuoBarCompression };