@momo-kits/foundation 0.169.12-beta.6 → 0.169.12-beta.7

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,230 @@
1
+ import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
2
+ import { useFocusEffect } from '@react-navigation/native';
3
+ import React, { useContext, useEffect, useLayoutEffect } from 'react';
4
+ import { useSafeAreaInsets } from 'react-native-safe-area-context';
5
+ import {
6
+ NativeModules,
7
+ Platform,
8
+ StatusBar,
9
+ useWindowDimensions,
10
+ View,
11
+ } from 'react-native';
12
+ import Animated, {
13
+ useAnimatedStyle,
14
+ useSharedValue,
15
+ withTiming,
16
+ } from 'react-native-reanimated';
17
+ import { Icon } from '../../../Icon';
18
+ import { exportFontFamily, Text } from '../../../Text';
19
+ import { BottomTabProps } from '../../types';
20
+ import BottomTabBar from './BottomTabBar';
21
+ import { DuoTabBar } from './DuoTabBar';
22
+ import { DuoBarCompressionProvider } from './DuoBarCompression';
23
+ import { ApplicationContext } from '../../../Context';
24
+ import { useScreenLayout } from '../../../Layout';
25
+ import TabScreen from './TabScreen';
26
+ import { getBadgeProps } from './badge';
27
+ import { styles } from './index.styles';
28
+
29
+ const Tab = createBottomTabNavigator();
30
+
31
+ const BottomTab: React.FC<BottomTabProps> = ({
32
+ nested,
33
+ tabs,
34
+ listeners,
35
+ navigation,
36
+ initialRouteName,
37
+ floatingButton,
38
+ compressionBehavior,
39
+ }) => {
40
+ const { theme, navigator } = useContext(ApplicationContext);
41
+ const { isIPhoneDuo } = useScreenLayout();
42
+ const dimensions = useWindowDimensions();
43
+ const insets = useSafeAreaInsets();
44
+ const initialIndex = tabs.findIndex(i => i.name === initialRouteName);
45
+ const indicatorAnimated = useSharedValue(0);
46
+ const activeIndex = React.useRef(initialIndex > 0 ? initialIndex : 0);
47
+ const itemWidth = dimensions.width / tabs.length;
48
+
49
+ const indicatorStyle = useAnimatedStyle(() => ({
50
+ width: itemWidth,
51
+ transform: [{ translateX: indicatorAnimated.value }],
52
+ }));
53
+
54
+ useLayoutEffect(() => {
55
+ navigation?.setOptions({
56
+ headerShown: false,
57
+ headerTransparent: true,
58
+ headerBackground: undefined,
59
+ });
60
+ }, [navigation]);
61
+
62
+ useEffect(() => {
63
+ indicatorAnimated.value = withTiming(itemWidth * activeIndex.current, {
64
+ duration: 200,
65
+ });
66
+ }, [itemWidth, indicatorAnimated]);
67
+
68
+ // Focus, not mount: pushing a screen from a tab leaves BottomTab mounted but blurred, and
69
+ // onFocus only fires on tab changes.
70
+ useFocusEffect(
71
+ React.useCallback(() => {
72
+ NativeModules?.MiniAppModule?.setShouldUseFloatingArrowBack?.(
73
+ activeIndex.current !== 0,
74
+ );
75
+ return () =>
76
+ NativeModules?.MiniAppModule?.setShouldUseFloatingArrowBack?.(false);
77
+ }, []),
78
+ );
79
+
80
+ const onFocus = (e: any) => {
81
+ activeIndex.current = tabs.findIndex(i => e.target.includes(i.name));
82
+ indicatorAnimated.value = withTiming(itemWidth * activeIndex.current, {
83
+ duration: 200,
84
+ });
85
+
86
+ const isFirstTab = activeIndex.current === 0;
87
+ const gestureEnabled = Platform.OS === 'ios' && isFirstTab;
88
+ navigation?.setOptions({
89
+ gestureEnabled: gestureEnabled,
90
+ });
91
+ NativeModules?.MiniAppModule?.setShouldUseFloatingArrowBack?.(
92
+ !gestureEnabled,
93
+ );
94
+ };
95
+
96
+ const handler: {
97
+ tabPress?: (e: any) => void;
98
+ focus?: (e: any) => void;
99
+ blur?: (e: any) => void;
100
+ state?: (e: any) => void;
101
+ } = {
102
+ tabPress: e => {
103
+ navigator?.maxApi?.triggerEventVibration?.('light');
104
+ listeners?.tabPress?.(e);
105
+ },
106
+ focus: e => {
107
+ onFocus(e);
108
+ StatusBar.setBarStyle('dark-content', true);
109
+ listeners?.focus?.(e);
110
+ },
111
+ blur: e => {
112
+ listeners?.blur?.(e);
113
+ },
114
+ };
115
+
116
+ const isIOS = Platform.OS === 'ios';
117
+ const bottomInset = isIOS ? Math.min(insets.bottom, 21) : insets.bottom;
118
+
119
+ return (
120
+ <DuoBarCompressionProvider
121
+ behavior={compressionBehavior}
122
+ tabCount={tabs.length}
123
+ hasQr={!!floatingButton}
124
+ >
125
+ <Tab.Navigator
126
+ screenOptions={{
127
+ headerShown: false,
128
+ headerTransparent: true,
129
+ headerBackground: undefined,
130
+ tabBarLabelPosition: 'below-icon',
131
+ tabBarBackground: () => (
132
+ <View
133
+ style={[
134
+ styles.container,
135
+ {
136
+ backgroundColor: theme.colors.background.surface,
137
+ },
138
+ ]}
139
+ >
140
+ <Animated.View
141
+ style={[styles.indicatorContainer, indicatorStyle]}
142
+ >
143
+ <View
144
+ style={[
145
+ styles.indicator,
146
+ { backgroundColor: theme.colors.primary },
147
+ ]}
148
+ />
149
+ </Animated.View>
150
+ </View>
151
+ ),
152
+ }}
153
+ tabBar={props =>
154
+ isIPhoneDuo ? (
155
+ <DuoTabBar
156
+ {...props}
157
+ floatingButton={floatingButton}
158
+ inactiveTintColor={theme.colors.text.hint}
159
+ activeTintColor={theme.colors.primary}
160
+ />
161
+ ) : (
162
+ <BottomTabBar
163
+ {...props}
164
+ floatingButton={floatingButton}
165
+ inactiveTintColor={theme.colors.text.hint}
166
+ activeTintColor={theme.colors.primary}
167
+ style={[{ height: 64 + bottomInset }]}
168
+ />
169
+ )
170
+ }
171
+ initialRouteName={initialRouteName}
172
+ >
173
+ {tabs.map((item, index) => {
174
+ const { badgeLabel, badgeStyle } = getBadgeProps(item?.badgeLabel);
175
+
176
+ return (
177
+ <Tab.Screen
178
+ key={`BottomTab-${item.name}`}
179
+ name={item.name}
180
+ component={TabScreen}
181
+ initialParams={{ ...item, nested, index }}
182
+ listeners={handler}
183
+ options={{
184
+ tabBarLabel: ({ focused, color }) => {
185
+ return (
186
+ <Text
187
+ numberOfLines={1}
188
+ typography={
189
+ focused ? 'label_xs_medium' : 'description_xs_regular'
190
+ }
191
+ color={color}
192
+ >
193
+ {item.label}
194
+ </Text>
195
+ );
196
+ },
197
+ title: item.label,
198
+ tabBarStyle: [
199
+ {
200
+ height: 64,
201
+ },
202
+ styles.tabStyle,
203
+ ],
204
+ tabBarBadge: badgeLabel,
205
+ tabBarBadgeStyle: [
206
+ {
207
+ borderColor: 'white',
208
+ borderWidth: 2,
209
+ fontSize: 10,
210
+ lineHeight: 14,
211
+ fontWeight: 'bold',
212
+ alignSelf: 'center',
213
+ fontFamily: exportFontFamily('bold'),
214
+ },
215
+ badgeStyle,
216
+ ],
217
+ tabBarIcon: ({ color }) => (
218
+ <Icon source={item.icon} color={color} size={28} />
219
+ ),
220
+ ...item,
221
+ }}
222
+ />
223
+ );
224
+ })}
225
+ </Tab.Navigator>
226
+ </DuoBarCompressionProvider>
227
+ );
228
+ };
229
+
230
+ export default BottomTab;
@@ -21,7 +21,7 @@ import { getTabBadge } from './TabBarIcon';
21
21
  import { DuoTabBar, useDuoTabBarInset } from './DuoTabBar';
22
22
  import { BottomTabInsetContext } from './BottomTabInset';
23
23
  import { DuoBarCompressionProvider } from './DuoBarCompression';
24
- import { LegacyBottomTab } from './LegacyBottomTab';
24
+ import LegacyBottomTab from './Legacy';
25
25
  import TabScreen from './TabScreen';
26
26
  import { ApplicationContext } from '../../Context';
27
27
  import { useScreenLayout } from '../../Layout';
@@ -42,6 +42,8 @@ export type BottomTabProps = {
42
42
  };
43
43
 
44
44
  export type FloatingButtonProps = {
45
+ /** Legacy bar only (`useLegacy`): custom background drawn behind the bar, e.g. a curved cutout */
46
+ container?: React.ReactNode;
45
47
  icon: string | React.ReactNode;
46
48
  label: string;
47
49
  onPress: () => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momo-kits/foundation",
3
- "version": "0.169.12-beta.6",
3
+ "version": "0.169.12-beta.7",
4
4
  "description": "React Native Component Kits",
5
5
  "main": "index.ts",
6
6
  "scripts": {},
@@ -1,197 +0,0 @@
1
- import React, { useContext, useLayoutEffect } from 'react';
2
- import { NativeModules, Platform, StatusBar, StyleSheet } from 'react-native';
3
- import {
4
- type BottomTabBarProps,
5
- createBottomTabNavigator,
6
- } from '@react-navigation/bottom-tabs';
7
- import { useFocusEffect } from '@react-navigation/native';
8
- import { useSafeAreaInsets } from 'react-native-safe-area-context';
9
- import { Colors, Spacing } from '../../Consts';
10
- import { Icon } from '../../Icon';
11
- import { exportFontFamily, Text } from '../../Text';
12
- import { ApplicationContext } from '../../Context';
13
- import { type BottomTabProps } from '../types';
14
- import { LegacyBottomTabBar } from './LegacyBottomTabBar';
15
- import TabScreen from './TabScreen';
16
-
17
- const BAR_HEIGHT = 64;
18
- const IOS_MAX_BOTTOM_INSET = 21;
19
- const TAB_ICON_SIZE = 28;
20
- const BADGE_FONT_SIZE = 10;
21
- const BADGE_LINE_HEIGHT = 14;
22
- const BADGE_BORDER_WIDTH = 2;
23
- const DOT_SIZE = 12;
24
-
25
- const Tab = createBottomTabNavigator();
26
-
27
- /**
28
- * Old badge rules: 0 hides it, '' is an orange dot, numbers are red, any other
29
- * text is orange and shown in full.
30
- */
31
- const getLegacyBadgeProps = (label?: string | number) => {
32
- const isNum = !isNaN(parseInt(`${label}`, 10));
33
- const isDot = typeof label === 'string' && !label;
34
- const badgeLabel = isNum && Number(label) === 0 ? undefined : label;
35
-
36
- if (isDot) {
37
- const badgeStyle = {
38
- marginRight: -2,
39
- top: -2,
40
- backgroundColor: Colors.orange_03,
41
- width: DOT_SIZE,
42
- };
43
- return { badgeLabel, badgeStyle };
44
- }
45
- const badgeStyle = {
46
- marginRight: isNum ? -4 : -8,
47
- top: -4,
48
- backgroundColor: isNum ? Colors.red_03 : Colors.orange_03,
49
- };
50
- return { badgeLabel, badgeStyle };
51
- };
52
-
53
- /**
54
- * Pre-redesign tab navigator (`useLegacy`): docked full-width bar that the tab
55
- * content sits above, no tab slide. `showDot` and `compressionBehavior` are
56
- * ignored; phone only, iPhone Duo keeps the current bars.
57
- */
58
- const LegacyBottomTab = ({
59
- nested,
60
- tabs,
61
- listeners,
62
- navigation,
63
- initialRouteName,
64
- floatingButton,
65
- }: BottomTabProps) => {
66
- const { theme, navigator } = useContext(ApplicationContext);
67
- const insets = useSafeAreaInsets();
68
- const initialIndex = tabs.findIndex(i => i.name === initialRouteName);
69
- const activeIndex = React.useRef(initialIndex > 0 ? initialIndex : 0);
70
- const isIOS = Platform.OS === 'ios';
71
- const bottomInset = isIOS
72
- ? Math.min(insets.bottom, IOS_MAX_BOTTOM_INSET)
73
- : insets.bottom;
74
-
75
- useLayoutEffect(() => {
76
- navigation?.setOptions({
77
- headerShown: false,
78
- headerTransparent: true,
79
- headerBackground: undefined,
80
- });
81
- }, [navigation]);
82
-
83
- useFocusEffect(
84
- React.useCallback(() => {
85
- NativeModules?.MiniAppModule?.setShouldUseFloatingArrowBack?.(
86
- activeIndex.current !== 0,
87
- );
88
- return () => {
89
- NativeModules?.MiniAppModule?.setShouldUseFloatingArrowBack?.(false);
90
- };
91
- }, []),
92
- );
93
-
94
- const handleFocus = (e: any) => {
95
- activeIndex.current = tabs.findIndex(i => e.target.includes(i.name));
96
- const isGestureEnabled = isIOS && activeIndex.current === 0;
97
- navigation?.setOptions({ gestureEnabled: isGestureEnabled });
98
- NativeModules?.MiniAppModule?.setShouldUseFloatingArrowBack?.(
99
- !isGestureEnabled,
100
- );
101
- };
102
-
103
- const handler = {
104
- tabPress: (e: any) => {
105
- navigator?.maxApi?.triggerEventVibration?.('light');
106
- listeners?.tabPress?.(e);
107
- },
108
- focus: (e: any) => {
109
- handleFocus(e);
110
- StatusBar.setBarStyle('dark-content', true);
111
- listeners?.focus?.(e);
112
- },
113
- blur: (e: any) => {
114
- listeners?.blur?.(e);
115
- },
116
- };
117
-
118
- const renderTabBar = (props: BottomTabBarProps) => {
119
- return (
120
- <LegacyBottomTabBar
121
- {...props}
122
- floatingButton={floatingButton}
123
- inactiveTintColor={theme.colors.text.hint}
124
- activeTintColor={theme.colors.primary}
125
- style={{ height: BAR_HEIGHT + bottomInset }}
126
- />
127
- );
128
- };
129
-
130
- return (
131
- <Tab.Navigator
132
- screenOptions={{
133
- headerShown: false,
134
- headerTransparent: true,
135
- headerBackground: undefined,
136
- tabBarLabelPosition: 'below-icon',
137
- }}
138
- tabBar={renderTabBar}
139
- initialRouteName={initialRouteName}
140
- >
141
- {tabs.map((item, index) => {
142
- const { badgeLabel, badgeStyle } = getLegacyBadgeProps(item.badgeLabel);
143
- return (
144
- <Tab.Screen
145
- key={`BottomTab-${item.name}`}
146
- name={item.name}
147
- component={TabScreen}
148
- initialParams={{ ...item, nested, index }}
149
- listeners={handler}
150
- options={{
151
- tabBarLabel: ({ focused, color }) => (
152
- <Text
153
- numberOfLines={1}
154
- typography={
155
- focused ? 'label_xs_medium' : 'description_xs_regular'
156
- }
157
- color={color}
158
- >
159
- {item.label}
160
- </Text>
161
- ),
162
- title: item.label,
163
- tabBarStyle: styles.tab,
164
- tabBarBadge: badgeLabel,
165
- tabBarBadgeStyle: [
166
- styles.badge,
167
- { fontFamily: exportFontFamily('bold') },
168
- badgeStyle,
169
- ],
170
- tabBarIcon: ({ color }) => (
171
- <Icon source={item.icon} color={color} size={TAB_ICON_SIZE} />
172
- ),
173
- ...item,
174
- }}
175
- />
176
- );
177
- })}
178
- </Tab.Navigator>
179
- );
180
- };
181
-
182
- const styles = StyleSheet.create({
183
- tab: {
184
- height: BAR_HEIGHT,
185
- paddingVertical: Spacing.XS,
186
- },
187
- badge: {
188
- borderColor: Colors.black_01,
189
- borderWidth: BADGE_BORDER_WIDTH,
190
- fontSize: BADGE_FONT_SIZE,
191
- lineHeight: BADGE_LINE_HEIGHT,
192
- fontWeight: 'bold',
193
- alignSelf: 'center',
194
- },
195
- });
196
-
197
- export { LegacyBottomTab };
@@ -1,208 +0,0 @@
1
- import React, { useContext, useEffect } from 'react';
2
- import {
3
- Platform,
4
- type StyleProp,
5
- StyleSheet,
6
- TouchableOpacity,
7
- useWindowDimensions,
8
- View,
9
- type ViewStyle,
10
- } from 'react-native';
11
- import { type BottomTabBarProps } from '@react-navigation/bottom-tabs';
12
- import { CommonActions, type Route } from '@react-navigation/native';
13
- import Animated, {
14
- useAnimatedStyle,
15
- useSharedValue,
16
- withTiming,
17
- } from 'react-native-reanimated';
18
- import {
19
- type EdgeInsets,
20
- useSafeAreaInsets,
21
- } from 'react-native-safe-area-context';
22
- import { Colors, Styles } from '../../Consts';
23
- import { Icon } from '../../Icon';
24
- import { Text } from '../../Text';
25
- import { ApplicationContext, MiniAppContext } from '../../Context';
26
- import { type FloatingButtonProps } from '../types';
27
- import { LegacyBottomTabItem } from './LegacyBottomTabItem';
28
- import { styles } from './LegacyBottomTabBar.styles';
29
-
30
- const INDICATOR_DURATION = 200;
31
- const FLOATING_ICON_SIZE = 32;
32
- const IOS_BOTTOM_INSET_TRIM = 4;
33
-
34
- type Props = BottomTabBarProps & {
35
- activeTintColor?: string;
36
- inactiveTintColor?: string;
37
- floatingButton?: FloatingButtonProps;
38
- style?: StyleProp<ViewStyle>;
39
- };
40
-
41
- /**
42
- * Bottom padding of the docked bar: the home indicator inset, trimmed on iOS.
43
- */
44
- const getPaddingBottom = (insets: EdgeInsets) => {
45
- const trim = Platform.select({ ios: IOS_BOTTOM_INSET_TRIM, default: 0 });
46
- return Math.max(insets.bottom - trim, 0);
47
- };
48
-
49
- /**
50
- * Pre-redesign phone tab bar: full-width docked bar with a 44x2 sliding top
51
- * indicator and the round floating button in the middle (even tab counts only).
52
- * `floatingButton.labelContent` has no legacy meaning and is ignored.
53
- */
54
- const LegacyBottomTabBar = ({
55
- state,
56
- navigation,
57
- descriptors,
58
- activeTintColor,
59
- inactiveTintColor,
60
- style,
61
- floatingButton,
62
- }: Props) => {
63
- const { theme } = useContext(ApplicationContext);
64
- const context = useContext<any>(MiniAppContext);
65
- const showBaseLineDebug = context?.features?.showBaseLineDebug ?? false;
66
- const { routes } = state;
67
- const widthDevice = useWindowDimensions().width;
68
- const insets = useSafeAreaInsets();
69
- const hasFloating = !!floatingButton && routes.length % 2 === 0;
70
- const slotCount = routes.length + (hasFloating ? 1 : 0);
71
- const itemWidth = (widthDevice - insets.left - insets.right) / slotCount;
72
- const activeTint = activeTintColor ?? theme.colors.primary;
73
- const indicatorAnimated = useSharedValue(0);
74
-
75
- const indicatorStyle = useAnimatedStyle(() => ({
76
- width: itemWidth,
77
- transform: [{ translateX: indicatorAnimated.value }],
78
- }));
79
-
80
- useEffect(() => {
81
- let slot = state.index;
82
- if (hasFloating && slot > (routes.length - 1) / 2) {
83
- slot += 1;
84
- }
85
- indicatorAnimated.value = withTiming(itemWidth * slot, {
86
- duration: INDICATOR_DURATION,
87
- });
88
- }, [state.index, itemWidth, indicatorAnimated, hasFloating, routes.length]);
89
-
90
- const renderTabItems = (data: Route<string>[]) => {
91
- return data.map(route => {
92
- const focused = state.routes[state.index]?.key === route.key;
93
- const { options } = descriptors[route.key];
94
-
95
- const handlePress = () => {
96
- const event = navigation.emit({
97
- type: 'tabPress',
98
- target: route.key,
99
- canPreventDefault: true,
100
- });
101
- if (!focused && !event.defaultPrevented) {
102
- navigation.dispatch({
103
- ...CommonActions.navigate(route.name),
104
- target: state.key,
105
- });
106
- }
107
- };
108
-
109
- const handleLongPress = () => {
110
- navigation.emit({ type: 'tabLongPress', target: route.key });
111
- };
112
-
113
- return (
114
- <LegacyBottomTabItem
115
- key={route.key}
116
- route={route}
117
- label={options.tabBarLabel || options.title || route.name}
118
- focused={focused}
119
- onPress={handlePress}
120
- onLongPress={handleLongPress}
121
- activeTintColor={activeTint}
122
- inactiveTintColor={inactiveTintColor ?? theme.colors.text.default}
123
- icon={options.tabBarIcon}
124
- badge={options.tabBarBadge}
125
- style={options.tabBarStyle as StyleProp<ViewStyle>}
126
- badgeStyle={StyleSheet.flatten(options.tabBarBadgeStyle) as ViewStyle}
127
- />
128
- );
129
- });
130
- };
131
-
132
- const renderFloatingButton = () => {
133
- const icon =
134
- typeof floatingButton?.icon === 'string' ? (
135
- <Icon
136
- source={floatingButton.icon}
137
- size={FLOATING_ICON_SIZE}
138
- color={Colors.black_01}
139
- />
140
- ) : (
141
- floatingButton?.icon
142
- );
143
-
144
- return (
145
- <View style={[Styles.flex, showBaseLineDebug && styles.debugBaseLine]}>
146
- <TouchableOpacity
147
- onPress={floatingButton?.onPress}
148
- style={styles.floatingContainer}
149
- >
150
- <View
151
- style={[styles.floatingContent, { backgroundColor: activeTint }]}
152
- >
153
- {icon}
154
- </View>
155
- <View style={[styles.floatingLabel, { backgroundColor: activeTint }]}>
156
- <Text
157
- numberOfLines={1}
158
- typography={'description_xs_regular'}
159
- color={Colors.black_01}
160
- >
161
- {floatingButton?.label}
162
- </Text>
163
- </View>
164
- </TouchableOpacity>
165
- </View>
166
- );
167
- };
168
-
169
- const renderTabs = () => {
170
- if (!hasFloating) {
171
- return renderTabItems(routes);
172
- }
173
- const middle = routes.length / 2;
174
- return (
175
- <>
176
- {renderTabItems(routes.slice(0, middle))}
177
- {renderFloatingButton()}
178
- {renderTabItems(routes.slice(middle))}
179
- </>
180
- );
181
- };
182
-
183
- return (
184
- <View
185
- style={[
186
- floatingButton ? styles.tabBarFloatingButton : styles.tabBar,
187
- {
188
- backgroundColor: theme.colors.background.surface,
189
- borderTopColor: theme.colors.border.default,
190
- paddingBottom: getPaddingBottom(insets),
191
- paddingLeft: insets.left,
192
- paddingRight: insets.right,
193
- },
194
- style,
195
- ]}
196
- pointerEvents={'auto'}
197
- >
198
- <View style={styles.content} accessibilityLabel={'bottom_tab_bar'}>
199
- <Animated.View style={[styles.indicatorContainer, indicatorStyle]}>
200
- <View style={[styles.indicator, { backgroundColor: activeTint }]} />
201
- </Animated.View>
202
- {renderTabs()}
203
- </View>
204
- </View>
205
- );
206
- };
207
-
208
- export { LegacyBottomTabBar };