@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,149 @@
1
+ import React, { useContext, useMemo } from 'react';
2
+ import { TouchableOpacity, View } from 'react-native';
3
+ import { GestureDetector } from 'react-native-gesture-handler';
4
+ import Animated from 'react-native-reanimated';
5
+ import { Radius } from '../../../Consts';
6
+ import { ApplicationContext } from '../../../Context';
7
+ import { useScreenLayout } from '../../../Layout/utils';
8
+ import { useDuoBarCompression } from './DuoBarCompression';
9
+ import { useLiquidSelection, usePressLift } from './hooks';
10
+ import {
11
+ BarMaterial,
12
+ getTabItems,
13
+ LiquidTabItem,
14
+ Props,
15
+ QrIcon,
16
+ SelectionMaterial,
17
+ TabItem,
18
+ } from './DuoTabBar.parts';
19
+ import {
20
+ ITEM_SIZE,
21
+ QR_ICON_SIZE,
22
+ RAIL_BUBBLE_WIDTH,
23
+ RAIL_PADDING,
24
+ RAIL_PITCH,
25
+ styles,
26
+ } from './DuoTabBar.styles';
27
+
28
+ /**
29
+ * column of icon-only tab items with the sliding selection bubble
30
+ */
31
+ const Rail: React.FC<{
32
+ items: TabItem[];
33
+ index: number;
34
+ onItemPress?: () => void;
35
+ }> = ({ items, index, onItemPress }) => {
36
+ const onSelect = (itemIndex: number) => {
37
+ items[itemIndex]?.onPress();
38
+ onItemPress?.();
39
+ };
40
+ const selection = useLiquidSelection({
41
+ count: items.length,
42
+ pitch: RAIL_PITCH,
43
+ firstCenter: RAIL_PADDING + ITEM_SIZE / 2,
44
+ axis: 'y',
45
+ index,
46
+ onSelect,
47
+ });
48
+ const { barStyle, bubbleStyle } = selection;
49
+ // stable style arrays: the tab switch re-renders the rail while the lens is still
50
+ // springing back, a new array would re-attach the animated style mid animation
51
+ const railStyle = useMemo(() => [styles.rail, barStyle], [barStyle]);
52
+ const railBubbleStyle = useMemo(
53
+ () => [styles.railBubble, bubbleStyle],
54
+ [bubbleStyle],
55
+ );
56
+
57
+ return (
58
+ <GestureDetector gesture={selection.gesture}>
59
+ <Animated.View style={railStyle} accessibilityLabel={'bottom_tab_bar'}>
60
+ <BarMaterial radius={ITEM_SIZE / 2} />
61
+ <Animated.View pointerEvents={'none'} style={railBubbleStyle}>
62
+ <SelectionMaterial radius={RAIL_BUBBLE_WIDTH / 2} />
63
+ </Animated.View>
64
+ {items.map((item, itemIndex) => (
65
+ <LiquidTabItem
66
+ key={item.route.key}
67
+ item={item}
68
+ itemIndex={itemIndex}
69
+ selection={selection}
70
+ onSelect={() => onSelect(itemIndex)}
71
+ renderContent={item.renderIcon}
72
+ style={styles.railItem}
73
+ />
74
+ ))}
75
+ </Animated.View>
76
+ </GestureDetector>
77
+ );
78
+ };
79
+
80
+ /**
81
+ * vertical bar anchored to the bottom of the Duo side bar, QR button below it (Figma MT1-C/E).
82
+ * When the side bar is short, it can shrink to one button showing the current tab
83
+ * (HIG "Tab bar compressed"); tapping it expands the rail and compresses the toolbar
84
+ */
85
+ export const DuoRailTabBar: React.FC<Props> = props => {
86
+ const { theme } = useContext(ApplicationContext);
87
+ const { sideBar } = useScreenLayout();
88
+ const { tabBarCompressed, expandTabBar, onTabSelected } =
89
+ useDuoBarCompression();
90
+ const compactLift = usePressLift();
91
+ const { state, floatingButton } = props;
92
+ const items = getTabItems(props);
93
+ const current = items[state.index];
94
+
95
+ const columnStyle = sideBar && [
96
+ styles.railColumn,
97
+ {
98
+ [sideBar.edge]: 0,
99
+ width: sideBar.width,
100
+ bottom: sideBar.bottom,
101
+ },
102
+ sideBar.edge === 'right' ? styles.railColumnRight : styles.railColumnLeft,
103
+ ];
104
+
105
+ const qr = floatingButton && (
106
+ <TouchableOpacity
107
+ accessibilityRole={'button'}
108
+ accessibilityLabel={floatingButton.label}
109
+ onPress={floatingButton.onPress}
110
+ style={[styles.railQr, { backgroundColor: theme.colors.primary }]}
111
+ >
112
+ <QrIcon icon={floatingButton.icon} size={QR_ICON_SIZE} />
113
+ </TouchableOpacity>
114
+ );
115
+
116
+ if (!sideBar) {
117
+ return null;
118
+ }
119
+
120
+ if (!tabBarCompressed) {
121
+ return (
122
+ <View pointerEvents={'box-none'} style={columnStyle}>
123
+ <Rail items={items} index={state.index} onItemPress={onTabSelected} />
124
+ {qr}
125
+ </View>
126
+ );
127
+ }
128
+
129
+ return (
130
+ <View pointerEvents={'box-none'} style={columnStyle}>
131
+ <Animated.View style={compactLift.style}>
132
+ <TouchableOpacity
133
+ accessibilityRole={'button'}
134
+ accessibilityLabel={current?.label}
135
+ accessibilityHint={'bottom_tab_bar'}
136
+ activeOpacity={1}
137
+ onPressIn={compactLift.onPressIn}
138
+ onPressOut={compactLift.onPressOut}
139
+ onPress={expandTabBar}
140
+ style={styles.railCompact}
141
+ >
142
+ <BarMaterial radius={Radius.XL} />
143
+ {current?.renderIcon(true)}
144
+ </TouchableOpacity>
145
+ </Animated.View>
146
+ {qr}
147
+ </View>
148
+ );
149
+ };
@@ -0,0 +1,202 @@
1
+ import { CommonActions, Route } from '@react-navigation/native';
2
+ import { BottomTabBarProps } from '@react-navigation/bottom-tabs';
3
+ import React, { ReactNode, useContext, useMemo, useRef } from 'react';
4
+ import {
5
+ StyleProp,
6
+ StyleSheet,
7
+ TouchableOpacity,
8
+ View,
9
+ ViewStyle,
10
+ } from 'react-native';
11
+ import Animated from 'react-native-reanimated';
12
+ import { Colors } from '../../../Consts';
13
+ import { ApplicationContext, MiniAppContext } from '../../../Context';
14
+ import { Icon } from '../../../Icon';
15
+ import { Text } from '../../../Text';
16
+ import { FloatingButtonProps } from '../../types';
17
+ import { TabBarIcon } from './TabBarIcon';
18
+ import { LiquidSelection, useLiquidItem } from './hooks';
19
+ import { styles } from './DuoTabBar.styles';
20
+
21
+ export type Props = BottomTabBarProps & {
22
+ activeTintColor: string;
23
+ inactiveTintColor: string;
24
+ floatingButton?: FloatingButtonProps;
25
+ };
26
+
27
+ export const BarMaterial: React.FC<{ radius: number }> = ({ radius }) => {
28
+ const { theme } = useContext(ApplicationContext);
29
+ return (
30
+ <View
31
+ pointerEvents={'none'}
32
+ style={[
33
+ StyleSheet.absoluteFill,
34
+ styles.barMaterial,
35
+ {
36
+ borderRadius: radius,
37
+ backgroundColor: theme.colors.background.surface,
38
+ borderColor: theme.colors.border.default,
39
+ },
40
+ ]}
41
+ />
42
+ );
43
+ };
44
+
45
+ // Figma Home-2026 tab bar "Selection": brand pink at 8%
46
+ const SELECTION_TINT_ALPHA = '14';
47
+
48
+ /**
49
+ * pink tinted capsule under the selected tab
50
+ */
51
+ export const SelectionMaterial: React.FC<{ radius: number }> = ({ radius }) => {
52
+ const { theme } = useContext(ApplicationContext);
53
+ return (
54
+ <View
55
+ pointerEvents={'none'}
56
+ style={[
57
+ StyleSheet.absoluteFill,
58
+ {
59
+ borderRadius: radius,
60
+ backgroundColor: theme.colors.primary + SELECTION_TINT_ALPHA,
61
+ },
62
+ ]}
63
+ />
64
+ );
65
+ };
66
+
67
+ /**
68
+ * tab label at its design size: like the system tab bar it doesn't grow with the wide
69
+ * Duo displays, which Text otherwise scales up to 5pt
70
+ */
71
+ export const TabLabel: React.FC<{
72
+ label: string;
73
+ color: string;
74
+ typography: 'label_xs_medium';
75
+ }> = ({ label, color, typography }) => {
76
+ const context = useContext<any>(MiniAppContext);
77
+ return (
78
+ <MiniAppContext.Provider
79
+ value={{
80
+ ...context,
81
+ fontScale: { useOSScaleRate: false, userScaleRate: 1 },
82
+ }}
83
+ >
84
+ <Text numberOfLines={1} typography={typography} color={color}>
85
+ {label}
86
+ </Text>
87
+ </MiniAppContext.Provider>
88
+ );
89
+ };
90
+
91
+ export const getTabItems = ({
92
+ state,
93
+ descriptors,
94
+ navigation,
95
+ activeTintColor,
96
+ inactiveTintColor,
97
+ }: Props) => {
98
+ return state.routes.map((route: Route<string>, index: number) => {
99
+ const { options } = descriptors[route.key];
100
+ const focused = index === state.index;
101
+ const label = options.title ?? route.name;
102
+
103
+ const onPress = () => {
104
+ const event = navigation.emit({
105
+ type: 'tabPress',
106
+ target: route.key,
107
+ canPreventDefault: true,
108
+ });
109
+ if (!focused && !event.defaultPrevented) {
110
+ navigation.dispatch({
111
+ ...CommonActions.navigate(route.name),
112
+ target: state.key,
113
+ });
114
+ }
115
+ };
116
+
117
+ const onLongPress = () => {
118
+ navigation.emit({ type: 'tabLongPress', target: route.key });
119
+ };
120
+
121
+ const renderIcon = (active: boolean) =>
122
+ options.tabBarIcon && (
123
+ <TabBarIcon
124
+ route={route}
125
+ badge={options.tabBarBadge}
126
+ badgeStyle={options.tabBarBadgeStyle}
127
+ activeOpacity={active ? 1 : 0}
128
+ inactiveOpacity={active ? 0 : 1}
129
+ activeTintColor={activeTintColor}
130
+ inactiveTintColor={inactiveTintColor}
131
+ renderIcon={options.tabBarIcon}
132
+ style={styles.icon}
133
+ />
134
+ );
135
+
136
+ return { route, focused, label, onPress, onLongPress, renderIcon };
137
+ });
138
+ };
139
+
140
+ export const QrIcon: React.FC<{
141
+ icon: FloatingButtonProps['icon'];
142
+ size: number;
143
+ }> = ({ icon, size }) => {
144
+ if (typeof icon === 'string') {
145
+ return <Icon source={icon} size={size} color={Colors.black_01} />;
146
+ }
147
+ return <>{icon}</>;
148
+ };
149
+
150
+ export type TabItem = ReturnType<typeof getTabItems>[number];
151
+
152
+ /**
153
+ * tab item: a tap is handled here as before, a drag over the bar is handled by the bar's
154
+ * gesture. The active layer fades in as the lens passes over it
155
+ */
156
+ export const LiquidTabItem: React.FC<{
157
+ item: TabItem;
158
+ itemIndex: number;
159
+ selection: LiquidSelection;
160
+ onSelect: () => void;
161
+ renderContent: (active: boolean) => ReactNode;
162
+ style: StyleProp<ViewStyle>;
163
+ }> = ({ item, itemIndex, selection, onSelect, renderContent, style }) => {
164
+ const { activeStyle, popStyle } = useLiquidItem(selection, itemIndex);
165
+ const activeLayerStyle = useMemo(
166
+ () => [StyleSheet.absoluteFill, styles.itemLayer, activeStyle],
167
+ [activeStyle],
168
+ );
169
+ // like the system tab bar, a held tab is still picked when released
170
+ const longPressed = useRef(false);
171
+
172
+ return (
173
+ <TouchableOpacity
174
+ accessibilityRole={'tab'}
175
+ accessibilityLabel={item.label}
176
+ accessibilityState={{ selected: item.focused }}
177
+ activeOpacity={1}
178
+ onPressIn={() => {
179
+ longPressed.current = false;
180
+ selection.handlePressIn(itemIndex);
181
+ }}
182
+ onPressOut={() => {
183
+ if (selection.handlePressOut() && longPressed.current) {
184
+ onSelect();
185
+ }
186
+ }}
187
+ onPress={onSelect}
188
+ onLongPress={() => {
189
+ longPressed.current = true;
190
+ item.onLongPress();
191
+ }}
192
+ style={style}
193
+ >
194
+ <Animated.View style={popStyle}>
195
+ <View style={styles.itemLayer}>{renderContent(false)}</View>
196
+ <Animated.View style={activeLayerStyle}>
197
+ {renderContent(true)}
198
+ </Animated.View>
199
+ </Animated.View>
200
+ </TouchableOpacity>
201
+ );
202
+ };
@@ -0,0 +1,123 @@
1
+ import { StyleSheet } from 'react-native';
2
+ import { Colors, Spacing } from '../../../Consts';
3
+ import { DuoMetrics } from '../../../Layout/utils';
4
+
5
+ export const ITEM_SIZE = DuoMetrics.controlSize;
6
+ export const ICON_SIZE = 28;
7
+ export const QR_ICON_SIZE = 24;
8
+ export const RAIL_PADDING = 6;
9
+ export const RAIL_GAP = Spacing.XXS;
10
+ export const RAIL_PITCH = ITEM_SIZE + RAIL_GAP;
11
+ // selection bubble overflows the 48 rail cell by 5 above and below (Figma MT2)
12
+ export const RAIL_BUBBLE_WIDTH = 44;
13
+ export const RAIL_BUBBLE_HEIGHT = 58;
14
+ export const FLOATING_HEIGHT = DuoMetrics.floatingTabBarHeight;
15
+ export const FLOATING_PADDING = Spacing.XS;
16
+ // the system bar sizes its pill to the tabs instead of stretching it across the display
17
+ export const FLOATING_ITEM_WIDTH = 80;
18
+ export const FLOATING_BUBBLE_RADIUS =
19
+ (FLOATING_HEIGHT - 2 * FLOATING_PADDING) / 2;
20
+
21
+ /**
22
+ * distance from the screen bottom to the floating tab bar, overlapping the home
23
+ * indicator inset like the system tab bar
24
+ */
25
+ export const getFloatingTabBarBottom = (bottomInset: number) =>
26
+ Math.max(bottomInset - DuoMetrics.floatingTabBarInsetOverlap, Spacing.S);
27
+
28
+ export const styles = StyleSheet.create({
29
+ barMaterial: {
30
+ borderWidth: 1,
31
+ },
32
+ icon: {
33
+ width: ICON_SIZE,
34
+ height: ICON_SIZE,
35
+ },
36
+ railColumn: {
37
+ position: 'absolute',
38
+ gap: Spacing.M,
39
+ },
40
+ railColumnRight: {
41
+ alignItems: 'flex-start',
42
+ paddingLeft: Spacing.M,
43
+ },
44
+ railColumnLeft: {
45
+ alignItems: 'flex-end',
46
+ paddingRight: Spacing.M,
47
+ },
48
+ railCompact: {
49
+ width: ITEM_SIZE,
50
+ height: ITEM_SIZE,
51
+ borderRadius: ITEM_SIZE / 2,
52
+ alignItems: 'center',
53
+ justifyContent: 'center',
54
+ },
55
+ rail: {
56
+ width: ITEM_SIZE,
57
+ paddingVertical: RAIL_PADDING,
58
+ gap: RAIL_GAP,
59
+ },
60
+ railBubble: {
61
+ position: 'absolute',
62
+ top: RAIL_PADDING - (RAIL_BUBBLE_HEIGHT - ITEM_SIZE) / 2,
63
+ left: (ITEM_SIZE - RAIL_BUBBLE_WIDTH) / 2,
64
+ width: RAIL_BUBBLE_WIDTH,
65
+ height: RAIL_BUBBLE_HEIGHT,
66
+ },
67
+ railItem: {
68
+ width: ITEM_SIZE,
69
+ height: ITEM_SIZE,
70
+ alignItems: 'center',
71
+ justifyContent: 'center',
72
+ },
73
+ railQr: {
74
+ width: ITEM_SIZE,
75
+ height: ITEM_SIZE,
76
+ borderRadius: ITEM_SIZE / 2,
77
+ alignItems: 'center',
78
+ justifyContent: 'center',
79
+ },
80
+ floating: {
81
+ position: 'absolute',
82
+ flexDirection: 'row',
83
+ justifyContent: 'center',
84
+ gap: Spacing.S,
85
+ },
86
+ floatingBar: {
87
+ flexShrink: 1,
88
+ height: FLOATING_HEIGHT,
89
+ padding: FLOATING_PADDING,
90
+ },
91
+ floatingRow: {
92
+ flex: 1,
93
+ flexDirection: 'row',
94
+ },
95
+ floatingBubble: {
96
+ position: 'absolute',
97
+ top: 0,
98
+ bottom: 0,
99
+ left: 0,
100
+ },
101
+ floatingItem: {
102
+ flex: 1,
103
+ alignItems: 'center',
104
+ justifyContent: 'center',
105
+ },
106
+ itemLayer: {
107
+ alignItems: 'center',
108
+ justifyContent: 'center',
109
+ gap: Spacing.XXS,
110
+ },
111
+ // pink fills the whole pinned platter
112
+ floatingQr: {
113
+ width: FLOATING_HEIGHT,
114
+ height: FLOATING_HEIGHT,
115
+ borderRadius: FLOATING_HEIGHT / 2,
116
+ alignItems: 'center',
117
+ justifyContent: 'center',
118
+ shadowColor: Colors.black_20,
119
+ shadowOffset: { width: 0, height: 6 },
120
+ shadowOpacity: 0.16,
121
+ shadowRadius: 16,
122
+ },
123
+ });
@@ -0,0 +1,151 @@
1
+ import React, { useContext, useMemo, useState } from 'react';
2
+ import { LayoutChangeEvent, TouchableOpacity, View } from 'react-native';
3
+ import { GestureDetector } from 'react-native-gesture-handler';
4
+ import Animated from 'react-native-reanimated';
5
+ import { ApplicationContext } from '../../../Context';
6
+ import { DuoMetrics, useScreenLayout } from '../../../Layout/utils';
7
+ import { useLiquidSelection } from './hooks';
8
+ import { DuoRailTabBar } from './DuoRailTabBar';
9
+ import {
10
+ BarMaterial,
11
+ getTabItems,
12
+ LiquidTabItem,
13
+ Props,
14
+ QrIcon,
15
+ SelectionMaterial,
16
+ TabLabel,
17
+ } from './DuoTabBar.parts';
18
+ import {
19
+ FLOATING_BUBBLE_RADIUS,
20
+ FLOATING_HEIGHT,
21
+ FLOATING_ITEM_WIDTH,
22
+ FLOATING_PADDING,
23
+ getFloatingTabBarBottom,
24
+ QR_ICON_SIZE,
25
+ styles,
26
+ } from './DuoTabBar.styles';
27
+
28
+ /**
29
+ * space tab content leaves under the Duo floating tab bar, 0 elsewhere
30
+ */
31
+ const useDuoTabBarInset = () => {
32
+ const { isIPhoneDuo, sideBar, insets } = useScreenLayout();
33
+ if (!isIPhoneDuo || sideBar) {
34
+ return 0;
35
+ }
36
+ return FLOATING_HEIGHT + getFloatingTabBarBottom(insets.bottom);
37
+ };
38
+
39
+ /**
40
+ * floating labelled bar for the unfolded portrait display, QR pinned in its own
41
+ * platter on the trailing side
42
+ */
43
+ const DuoFloatingTabBar: React.FC<Props> = props => {
44
+ const { theme } = useContext(ApplicationContext);
45
+ const { insets } = useScreenLayout();
46
+ const { state, floatingButton, activeTintColor, inactiveTintColor } = props;
47
+ const items = getTabItems(props);
48
+ const [rowWidth, setRowWidth] = useState(0);
49
+ const cellWidth = rowWidth / state.routes.length;
50
+
51
+ const selection = useLiquidSelection({
52
+ count: items.length,
53
+ pitch: cellWidth,
54
+ firstCenter: cellWidth / 2,
55
+ axis: 'x',
56
+ index: state.index,
57
+ onSelect: itemIndex => items[itemIndex]?.onPress(),
58
+ });
59
+ const { barStyle, bubbleStyle } = selection;
60
+ const tabCount = state.routes.length;
61
+ // stable style arrays, see Rail
62
+ const floatingBarStyle = useMemo(
63
+ () => [
64
+ styles.floatingBar,
65
+ { width: tabCount * FLOATING_ITEM_WIDTH + 2 * FLOATING_PADDING },
66
+ barStyle,
67
+ ],
68
+ [tabCount, barStyle],
69
+ );
70
+ const floatingBubbleStyle = useMemo(
71
+ () => [styles.floatingBubble, { width: cellWidth }, bubbleStyle],
72
+ [cellWidth, bubbleStyle],
73
+ );
74
+
75
+ const onRowLayout = (e: LayoutChangeEvent) =>
76
+ setRowWidth(e.nativeEvent.layout.width);
77
+
78
+ return (
79
+ <View
80
+ pointerEvents={'box-none'}
81
+ style={[
82
+ styles.floating,
83
+ {
84
+ left: insets.left + DuoMetrics.floatingTabBarMargin,
85
+ right: insets.right + DuoMetrics.floatingTabBarMargin,
86
+ bottom: getFloatingTabBarBottom(insets.bottom),
87
+ },
88
+ ]}
89
+ >
90
+ <Animated.View
91
+ style={floatingBarStyle}
92
+ accessibilityLabel={'bottom_tab_bar'}
93
+ >
94
+ <BarMaterial radius={FLOATING_HEIGHT / 2} />
95
+ <GestureDetector gesture={selection.gesture}>
96
+ <View style={styles.floatingRow} onLayout={onRowLayout}>
97
+ {rowWidth > 0 && (
98
+ <Animated.View pointerEvents={'none'} style={floatingBubbleStyle}>
99
+ <SelectionMaterial radius={FLOATING_BUBBLE_RADIUS} />
100
+ </Animated.View>
101
+ )}
102
+ {items.map((item, index) => (
103
+ <LiquidTabItem
104
+ key={item.route.key}
105
+ item={item}
106
+ itemIndex={index}
107
+ selection={selection}
108
+ onSelect={item.onPress}
109
+ renderContent={active => (
110
+ <>
111
+ {item.renderIcon(active)}
112
+ <TabLabel
113
+ label={item.label}
114
+ typography={'label_xs_medium'}
115
+ color={active ? activeTintColor : inactiveTintColor}
116
+ />
117
+ </>
118
+ )}
119
+ style={styles.floatingItem}
120
+ />
121
+ ))}
122
+ </View>
123
+ </GestureDetector>
124
+ </Animated.View>
125
+ {floatingButton && (
126
+ <TouchableOpacity
127
+ accessibilityRole={'button'}
128
+ accessibilityLabel={floatingButton.label}
129
+ onPress={floatingButton.onPress}
130
+ style={[styles.floatingQr, { backgroundColor: theme.colors.primary }]}
131
+ >
132
+ <QrIcon icon={floatingButton.icon} size={QR_ICON_SIZE} />
133
+ </TouchableOpacity>
134
+ )}
135
+ </View>
136
+ );
137
+ };
138
+
139
+ /**
140
+ * iPhone Duo tab bar: rail in the side bar, or a floating bar when unfolded portrait
141
+ */
142
+ const DuoTabBar: React.FC<Props> = props => {
143
+ const { sideBar } = useScreenLayout();
144
+ return sideBar ? (
145
+ <DuoRailTabBar {...props} />
146
+ ) : (
147
+ <DuoFloatingTabBar {...props} />
148
+ );
149
+ };
150
+
151
+ export { DuoTabBar, useDuoTabBarInset };