@momo-kits/foundation 0.169.12 → 0.169.13
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.
- package/Application/BottomTab/Legacy/BottomTabBar.styles.ts +60 -0
- package/Application/BottomTab/Legacy/BottomTabBar.tsx +270 -0
- package/Application/BottomTab/Legacy/CustomBottomTabItem.tsx +153 -0
- package/Application/BottomTab/Legacy/DuoBarCompression.tsx +135 -0
- package/Application/BottomTab/Legacy/DuoRailTabBar.tsx +149 -0
- package/Application/BottomTab/Legacy/DuoTabBar.parts.tsx +202 -0
- package/Application/BottomTab/Legacy/DuoTabBar.styles.ts +123 -0
- package/Application/BottomTab/Legacy/DuoTabBar.tsx +151 -0
- package/Application/BottomTab/Legacy/TabBarIcon.tsx +113 -0
- package/Application/BottomTab/Legacy/TabScreen.tsx +177 -0
- package/Application/BottomTab/Legacy/badge.ts +41 -0
- package/Application/BottomTab/Legacy/hooks.ts +358 -0
- package/Application/BottomTab/Legacy/index.styles.ts +30 -0
- package/Application/BottomTab/Legacy/index.tsx +230 -0
- package/Application/BottomTab/index.tsx +15 -1
- package/Application/types.tabs.ts +8 -0
- package/package.json +1 -1
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import type { Route } from '@react-navigation/native';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import {
|
|
4
|
+
type StyleProp,
|
|
5
|
+
StyleSheet,
|
|
6
|
+
type TextStyle,
|
|
7
|
+
View,
|
|
8
|
+
type ViewStyle,
|
|
9
|
+
} from 'react-native';
|
|
10
|
+
|
|
11
|
+
import { Colors } from '../../../Consts';
|
|
12
|
+
import { Badge, BadgeDot } from '../../../Badge';
|
|
13
|
+
|
|
14
|
+
type Props = {
|
|
15
|
+
route: Route<string>;
|
|
16
|
+
badge?: string | number;
|
|
17
|
+
badgeStyle?: StyleProp<TextStyle> | any;
|
|
18
|
+
activeOpacity: number;
|
|
19
|
+
inactiveOpacity: number;
|
|
20
|
+
activeTintColor?: string;
|
|
21
|
+
inactiveTintColor?: string;
|
|
22
|
+
renderIcon: (props: {
|
|
23
|
+
focused: boolean;
|
|
24
|
+
color: string;
|
|
25
|
+
size: number;
|
|
26
|
+
}) => React.ReactNode;
|
|
27
|
+
allowFontScaling?: boolean;
|
|
28
|
+
style: StyleProp<ViewStyle>;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Icon sizes taken from Apple HIG
|
|
33
|
+
* https://developer.apple.com/design/human-interface-guidelines/tab-bars
|
|
34
|
+
*/
|
|
35
|
+
const ICON_SIZE_WIDE = 35;
|
|
36
|
+
|
|
37
|
+
export function TabBarIcon({
|
|
38
|
+
route: _,
|
|
39
|
+
badge,
|
|
40
|
+
badgeStyle,
|
|
41
|
+
activeOpacity,
|
|
42
|
+
inactiveOpacity,
|
|
43
|
+
activeTintColor,
|
|
44
|
+
inactiveTintColor,
|
|
45
|
+
renderIcon,
|
|
46
|
+
style,
|
|
47
|
+
}: Props) {
|
|
48
|
+
const iconSize = 25;
|
|
49
|
+
|
|
50
|
+
const renderBadge = () => {
|
|
51
|
+
if (badge == null) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
if (badge === '') {
|
|
55
|
+
return <BadgeDot size={'small'} style={[styles.badge, badgeStyle]} />;
|
|
56
|
+
}
|
|
57
|
+
return <Badge style={[styles.badge, badgeStyle]} label={badge} />;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// We render the icon twice at the same position on top of each other:
|
|
61
|
+
// active and inactive one, so we can fade between them.
|
|
62
|
+
return (
|
|
63
|
+
<View style={[styles.wrapperUikit, style]}>
|
|
64
|
+
<View
|
|
65
|
+
style={[
|
|
66
|
+
styles.icon,
|
|
67
|
+
{
|
|
68
|
+
opacity: activeOpacity,
|
|
69
|
+
// Workaround for react-native >= 0.54 layout bug
|
|
70
|
+
minWidth: iconSize,
|
|
71
|
+
},
|
|
72
|
+
]}
|
|
73
|
+
>
|
|
74
|
+
{renderIcon({
|
|
75
|
+
focused: true,
|
|
76
|
+
size: iconSize,
|
|
77
|
+
color: activeTintColor ?? Colors.black_20,
|
|
78
|
+
})}
|
|
79
|
+
</View>
|
|
80
|
+
<View style={[styles.icon, { opacity: inactiveOpacity }]}>
|
|
81
|
+
{renderIcon({
|
|
82
|
+
focused: false,
|
|
83
|
+
size: iconSize,
|
|
84
|
+
color: inactiveTintColor ?? Colors.black_04,
|
|
85
|
+
})}
|
|
86
|
+
</View>
|
|
87
|
+
{renderBadge()}
|
|
88
|
+
</View>
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const styles = StyleSheet.create({
|
|
93
|
+
icon: {
|
|
94
|
+
// We render the icon twice at the same position on top of each other:
|
|
95
|
+
// active and inactive one, so we can fade between them:
|
|
96
|
+
// Cover the whole iconContainer:
|
|
97
|
+
position: 'absolute',
|
|
98
|
+
alignSelf: 'center',
|
|
99
|
+
alignItems: 'center',
|
|
100
|
+
justifyContent: 'center',
|
|
101
|
+
height: '100%',
|
|
102
|
+
width: '100%',
|
|
103
|
+
},
|
|
104
|
+
wrapperUikit: {
|
|
105
|
+
width: ICON_SIZE_WIDE,
|
|
106
|
+
height: ICON_SIZE_WIDE,
|
|
107
|
+
},
|
|
108
|
+
badge: {
|
|
109
|
+
position: 'absolute',
|
|
110
|
+
end: -3,
|
|
111
|
+
top: -3,
|
|
112
|
+
},
|
|
113
|
+
});
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import {
|
|
2
|
+
NavigationContainer,
|
|
3
|
+
NavigationIndependentTree,
|
|
4
|
+
useFocusEffect,
|
|
5
|
+
} from '@react-navigation/native';
|
|
6
|
+
import { createStackNavigator } from '@react-navigation/stack';
|
|
7
|
+
import React, { useContext, useEffect } from 'react';
|
|
8
|
+
import Animated, {
|
|
9
|
+
useAnimatedStyle,
|
|
10
|
+
useSharedValue,
|
|
11
|
+
withTiming,
|
|
12
|
+
} from 'react-native-reanimated';
|
|
13
|
+
import StackScreen from '../../StackScreen';
|
|
14
|
+
import { getOptions, getStackOptions } from '../../navigationOptions';
|
|
15
|
+
import { useAppState } from '../../hooks';
|
|
16
|
+
import { ApplicationContext, MiniAppContext } from '../../../Context';
|
|
17
|
+
import { useScreenLayout } from '../../../Layout';
|
|
18
|
+
import { getHeaderRowOptions } from '../../../Layout/utils';
|
|
19
|
+
import { styles } from './index.styles';
|
|
20
|
+
|
|
21
|
+
const Stack = createStackNavigator();
|
|
22
|
+
|
|
23
|
+
const TabScreen: React.FC<any> = ({ route, navigation }) => {
|
|
24
|
+
const { navigator } = useContext(ApplicationContext);
|
|
25
|
+
const context = useContext<any>(MiniAppContext);
|
|
26
|
+
const { nested, options, screen, initialParams } = route?.params;
|
|
27
|
+
const headerStyleOptions = getHeaderRowOptions(useScreenLayout());
|
|
28
|
+
|
|
29
|
+
const opacityValue = 0.3;
|
|
30
|
+
const scaleValue = 0.97;
|
|
31
|
+
const opacity = useSharedValue(opacityValue);
|
|
32
|
+
const scale = useSharedValue(scaleValue);
|
|
33
|
+
const screenName = screen?.name || screen?.type?.name || 'Invalid';
|
|
34
|
+
const { isBackgroundToForeground } = useAppState();
|
|
35
|
+
|
|
36
|
+
const tabAnimatedStyle = useAnimatedStyle(() => ({
|
|
37
|
+
opacity: opacity.value,
|
|
38
|
+
transform: [{ scale: scale.value }],
|
|
39
|
+
}));
|
|
40
|
+
|
|
41
|
+
const onScreenNavigated = (pre: string, current: string) => {
|
|
42
|
+
if (!isBackgroundToForeground) {
|
|
43
|
+
const data: any = {
|
|
44
|
+
preScreenName: pre,
|
|
45
|
+
screenName: current,
|
|
46
|
+
componentName: 'Screen',
|
|
47
|
+
state: 'navigated',
|
|
48
|
+
action: 'push',
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
context?.autoTracking?.({
|
|
52
|
+
...context,
|
|
53
|
+
...data,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* debug toast
|
|
58
|
+
*/
|
|
59
|
+
navigator?.maxApi?.showToastDebug?.({
|
|
60
|
+
appId: context.appId,
|
|
61
|
+
message: `${screenName} screen_navigated`,
|
|
62
|
+
type: 'ERROR',
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
useFocusEffect(
|
|
68
|
+
React.useCallback(
|
|
69
|
+
() => {
|
|
70
|
+
opacity.value = withTiming(1, { duration: 200 });
|
|
71
|
+
scale.value = withTiming(1, { duration: 200 });
|
|
72
|
+
|
|
73
|
+
setTimeout(() => {
|
|
74
|
+
navigator?.maxApi?.getDataObserver?.(
|
|
75
|
+
'current_screen',
|
|
76
|
+
(data: any) => {
|
|
77
|
+
onScreenNavigated(data?.screenName, screenName);
|
|
78
|
+
navigator?.maxApi?.setObserver?.('current_screen', {
|
|
79
|
+
screenName,
|
|
80
|
+
});
|
|
81
|
+
},
|
|
82
|
+
);
|
|
83
|
+
}, 100);
|
|
84
|
+
|
|
85
|
+
return () => {
|
|
86
|
+
if (navigation.getState().index !== route?.params.index) {
|
|
87
|
+
opacity.value = withTiming(opacityValue, { duration: 200 });
|
|
88
|
+
scale.value = withTiming(scaleValue, { duration: 200 });
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
},
|
|
92
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
93
|
+
[],
|
|
94
|
+
),
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
useEffect(
|
|
98
|
+
() => {
|
|
99
|
+
const onFocusApp = navigator?.maxApi?.listen?.('onFocusApp', () => {
|
|
100
|
+
if (navigation.isFocused()) {
|
|
101
|
+
navigator?.maxApi?.getDataObserver?.(
|
|
102
|
+
'current_screen',
|
|
103
|
+
(data: any) => {
|
|
104
|
+
onScreenNavigated(data?.screenName, screenName);
|
|
105
|
+
navigator?.maxApi?.setObserver?.('current_screen', {
|
|
106
|
+
screenName,
|
|
107
|
+
});
|
|
108
|
+
},
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
return () => {
|
|
114
|
+
onFocusApp?.remove?.();
|
|
115
|
+
};
|
|
116
|
+
},
|
|
117
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
118
|
+
[],
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
let stackOptions = {};
|
|
122
|
+
if (options) {
|
|
123
|
+
stackOptions = getOptions(options);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (nested) {
|
|
127
|
+
return (
|
|
128
|
+
<Animated.View style={[styles.container, tabAnimatedStyle]}>
|
|
129
|
+
<Stack.Navigator screenOptions={headerStyleOptions}>
|
|
130
|
+
<Stack.Screen
|
|
131
|
+
name="Stack"
|
|
132
|
+
component={StackScreen}
|
|
133
|
+
initialParams={{
|
|
134
|
+
screen,
|
|
135
|
+
options: stackOptions,
|
|
136
|
+
initialParams,
|
|
137
|
+
bottomTab: 'nested',
|
|
138
|
+
}}
|
|
139
|
+
options={{ ...getStackOptions() }}
|
|
140
|
+
/>
|
|
141
|
+
</Stack.Navigator>
|
|
142
|
+
</Animated.View>
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return (
|
|
147
|
+
<Animated.View
|
|
148
|
+
style={[
|
|
149
|
+
styles.container,
|
|
150
|
+
{
|
|
151
|
+
opacity,
|
|
152
|
+
transform: [{ scale }],
|
|
153
|
+
},
|
|
154
|
+
]}
|
|
155
|
+
>
|
|
156
|
+
<NavigationIndependentTree>
|
|
157
|
+
<NavigationContainer>
|
|
158
|
+
<Stack.Navigator screenOptions={headerStyleOptions}>
|
|
159
|
+
<Stack.Screen
|
|
160
|
+
name="Stack"
|
|
161
|
+
component={StackScreen}
|
|
162
|
+
initialParams={{
|
|
163
|
+
screen,
|
|
164
|
+
options: stackOptions,
|
|
165
|
+
initialParams,
|
|
166
|
+
bottomTab: 'default',
|
|
167
|
+
}}
|
|
168
|
+
options={{ ...getStackOptions(), ...options }}
|
|
169
|
+
/>
|
|
170
|
+
</Stack.Navigator>
|
|
171
|
+
</NavigationContainer>
|
|
172
|
+
</NavigationIndependentTree>
|
|
173
|
+
</Animated.View>
|
|
174
|
+
);
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
export default TabScreen;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Colors } from '../../../Consts';
|
|
2
|
+
|
|
3
|
+
export const getBadgeProps = (itemBadgeLabel: string | number | undefined) => {
|
|
4
|
+
const isNum = !isNaN(parseInt(`${itemBadgeLabel}`, 10));
|
|
5
|
+
const isDot = typeof itemBadgeLabel === 'string' && !itemBadgeLabel;
|
|
6
|
+
const isNumZero = isNum && Number(itemBadgeLabel) === 0;
|
|
7
|
+
|
|
8
|
+
let badgeLabel = itemBadgeLabel;
|
|
9
|
+
if (isNumZero) {
|
|
10
|
+
badgeLabel = undefined;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
let badgeStyle: {
|
|
14
|
+
marginRight: number;
|
|
15
|
+
top: number;
|
|
16
|
+
backgroundColor: string;
|
|
17
|
+
width?: number;
|
|
18
|
+
} = {
|
|
19
|
+
marginRight: -8,
|
|
20
|
+
top: -4,
|
|
21
|
+
backgroundColor: Colors.orange_03,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
if (isNum) {
|
|
25
|
+
badgeStyle = {
|
|
26
|
+
marginRight: -4,
|
|
27
|
+
top: -4,
|
|
28
|
+
backgroundColor: Colors.red_03,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
if (isDot) {
|
|
32
|
+
badgeStyle = {
|
|
33
|
+
marginRight: -2,
|
|
34
|
+
top: -2,
|
|
35
|
+
backgroundColor: Colors.orange_03,
|
|
36
|
+
width: 12,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return { badgeLabel, badgeStyle };
|
|
41
|
+
};
|
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
import { useCallback, useContext, useEffect, useMemo, useRef } from 'react';
|
|
2
|
+
import { Gesture } from 'react-native-gesture-handler';
|
|
3
|
+
import {
|
|
4
|
+
clamp,
|
|
5
|
+
useAnimatedReaction,
|
|
6
|
+
useAnimatedStyle,
|
|
7
|
+
useReducedMotion,
|
|
8
|
+
useSharedValue,
|
|
9
|
+
withSequence,
|
|
10
|
+
withSpring,
|
|
11
|
+
withTiming,
|
|
12
|
+
} from 'react-native-reanimated';
|
|
13
|
+
import { scheduleOnRN } from 'react-native-worklets';
|
|
14
|
+
import { ApplicationContext } from '../../../Context';
|
|
15
|
+
|
|
16
|
+
type Axis = 'x' | 'y';
|
|
17
|
+
|
|
18
|
+
type Options = {
|
|
19
|
+
count: number;
|
|
20
|
+
// distance between two neighbouring tab centers
|
|
21
|
+
pitch: number;
|
|
22
|
+
// position of the first tab's center inside the gesture view
|
|
23
|
+
firstCenter: number;
|
|
24
|
+
axis: Axis;
|
|
25
|
+
index: number;
|
|
26
|
+
onSelect: (index: number) => void;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const SPRING = { damping: 18, stiffness: 220, mass: 0.8 };
|
|
30
|
+
const DRAG_SPRING = { damping: 24, stiffness: 600, mass: 0.6 };
|
|
31
|
+
const REDUCED_TIMING = { duration: 200 };
|
|
32
|
+
const LENS_SCALE_X = 1.2;
|
|
33
|
+
const LENS_SCALE_Y = 1.3;
|
|
34
|
+
const BUTTON_LIFT_RATIO = 0.5;
|
|
35
|
+
const STRETCH_PER_POINT = 0.012;
|
|
36
|
+
const MAX_STRETCH = 0.25;
|
|
37
|
+
const STRETCH_EASING = 0.25;
|
|
38
|
+
const REST_STRETCH = 0.002;
|
|
39
|
+
const CROSS_STRETCH_RATIO = 0.5;
|
|
40
|
+
const TAP_LIFT_DURATION = 120;
|
|
41
|
+
const BAR_SWELL = 0.04;
|
|
42
|
+
const ICON_POP = 1.15;
|
|
43
|
+
const ICON_POP_DURATION = 120;
|
|
44
|
+
const DRAG_SLOP = 10;
|
|
45
|
+
// UIScrollView rubber band constant
|
|
46
|
+
const RUBBER_BAND = 0.55;
|
|
47
|
+
const RESYNC_DELAY = 100;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Rubber band past the first and last tab: the further the finger goes, the
|
|
51
|
+
* less the lens follows.
|
|
52
|
+
*/
|
|
53
|
+
const rubberBand = (value: number, max: number, dimension: number) => {
|
|
54
|
+
'worklet';
|
|
55
|
+
const band = (overshoot: number) => {
|
|
56
|
+
return (1 - 1 / ((overshoot * RUBBER_BAND) / dimension + 1)) * dimension;
|
|
57
|
+
};
|
|
58
|
+
if (value < 0) {
|
|
59
|
+
return -band(-value);
|
|
60
|
+
}
|
|
61
|
+
if (value > max) {
|
|
62
|
+
return max + band(value - max);
|
|
63
|
+
}
|
|
64
|
+
return value;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* iOS 26 tab bar selection motion without the glass: press lifts a lens over
|
|
69
|
+
* the tab, a drag slides it under the finger, release selects the tab under it.
|
|
70
|
+
*/
|
|
71
|
+
const useLiquidSelection = ({
|
|
72
|
+
count,
|
|
73
|
+
pitch,
|
|
74
|
+
firstCenter,
|
|
75
|
+
axis,
|
|
76
|
+
index,
|
|
77
|
+
onSelect,
|
|
78
|
+
}: Options) => {
|
|
79
|
+
const { navigator } = useContext<any>(ApplicationContext);
|
|
80
|
+
const reduceMotion = useReducedMotion();
|
|
81
|
+
|
|
82
|
+
const offset = useSharedValue(index * pitch);
|
|
83
|
+
// lets a re-render skip a spring already heading to the same target
|
|
84
|
+
const destination = useSharedValue(index * pitch);
|
|
85
|
+
const selected = useSharedValue(index);
|
|
86
|
+
const hovered = useSharedValue(index);
|
|
87
|
+
const lift = useSharedValue(0);
|
|
88
|
+
const isDragging = useSharedValue(false);
|
|
89
|
+
const stretch = useSharedValue(0);
|
|
90
|
+
const metrics = useSharedValue({ count, pitch, firstCenter });
|
|
91
|
+
const hasMeasured = useRef(pitch > 0);
|
|
92
|
+
|
|
93
|
+
const indexRef = useRef(index);
|
|
94
|
+
const pitchRef = useRef(pitch);
|
|
95
|
+
// read through refs so the gesture is never rebuilt mid drag
|
|
96
|
+
const callbacks = useRef({ onSelect, navigator });
|
|
97
|
+
indexRef.current = index;
|
|
98
|
+
pitchRef.current = pitch;
|
|
99
|
+
callbacks.current = { onSelect, navigator };
|
|
100
|
+
|
|
101
|
+
useEffect(() => {
|
|
102
|
+
metrics.value = { count, pitch, firstCenter };
|
|
103
|
+
}, [count, pitch, firstCenter, metrics]);
|
|
104
|
+
|
|
105
|
+
const move = useCallback(
|
|
106
|
+
(target: number, velocity: number, config: typeof SPRING) => {
|
|
107
|
+
'worklet';
|
|
108
|
+
if (destination.value === target && velocity === 0) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
destination.value = target;
|
|
112
|
+
if (reduceMotion) {
|
|
113
|
+
offset.value = withTiming(target, REDUCED_TIMING);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
offset.value = withSpring(target, { ...config, velocity });
|
|
117
|
+
},
|
|
118
|
+
[offset, destination, reduceMotion],
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
useEffect(() => {
|
|
122
|
+
const target = index * pitch;
|
|
123
|
+
selected.value = index;
|
|
124
|
+
// snap on first layout so the lens doesn't slide in from the leading edge
|
|
125
|
+
if (hasMeasured.current) {
|
|
126
|
+
move(target, 0, SPRING);
|
|
127
|
+
} else {
|
|
128
|
+
destination.value = target;
|
|
129
|
+
offset.value = target;
|
|
130
|
+
}
|
|
131
|
+
hasMeasured.current = pitch > 0;
|
|
132
|
+
}, [index, pitch, offset, destination, selected, move]);
|
|
133
|
+
|
|
134
|
+
useAnimatedReaction(
|
|
135
|
+
() => offset.value,
|
|
136
|
+
(current, previous) => {
|
|
137
|
+
if (previous === null || reduceMotion) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const speed = Math.abs(current - previous);
|
|
141
|
+
const target = Math.min(speed * STRETCH_PER_POINT, MAX_STRETCH);
|
|
142
|
+
// eased both ways so a slow frame never snaps the shape
|
|
143
|
+
const next = stretch.value + (target - stretch.value) * STRETCH_EASING;
|
|
144
|
+
stretch.value = next < REST_STRETCH ? 0 : next;
|
|
145
|
+
},
|
|
146
|
+
[reduceMotion],
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Returns the lens to the selected tab, unless the tab it is heading to got
|
|
151
|
+
* selected in the meantime.
|
|
152
|
+
*/
|
|
153
|
+
const resync = useCallback(() => {
|
|
154
|
+
setTimeout(() => {
|
|
155
|
+
const target = indexRef.current * pitchRef.current;
|
|
156
|
+
if (!isDragging.value && destination.value !== target) {
|
|
157
|
+
move(target, 0, SPRING);
|
|
158
|
+
}
|
|
159
|
+
}, RESYNC_DELAY);
|
|
160
|
+
}, [destination, isDragging, move]);
|
|
161
|
+
|
|
162
|
+
const handlePressIn = (itemIndex: number) => {
|
|
163
|
+
move(itemIndex * pitch, 0, SPRING);
|
|
164
|
+
if (!reduceMotion) {
|
|
165
|
+
lift.value = withSpring(1, SPRING);
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Settles the lift after a tap. Returns false when a drag took the press over.
|
|
171
|
+
*/
|
|
172
|
+
const handlePressOut = () => {
|
|
173
|
+
if (isDragging.value) {
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
if (!reduceMotion) {
|
|
177
|
+
lift.value = withSequence(
|
|
178
|
+
withTiming(1, { duration: TAP_LIFT_DURATION }),
|
|
179
|
+
withSpring(0, SPRING),
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
// onPress switches to the pressed tab, resync covers a cancelled press
|
|
183
|
+
resync();
|
|
184
|
+
return true;
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
const tick = useCallback(() => {
|
|
188
|
+
callbacks.current.navigator?.maxApi?.triggerEventVibration?.('light');
|
|
189
|
+
}, []);
|
|
190
|
+
|
|
191
|
+
const select = useCallback(
|
|
192
|
+
(target: number) => {
|
|
193
|
+
callbacks.current.onSelect(target);
|
|
194
|
+
resync();
|
|
195
|
+
},
|
|
196
|
+
[resync],
|
|
197
|
+
);
|
|
198
|
+
|
|
199
|
+
const gesture = useMemo(() => {
|
|
200
|
+
const position = (e: { x: number; y: number }) => {
|
|
201
|
+
'worklet';
|
|
202
|
+
return (axis === 'x' ? e.x : e.y) - metrics.value.firstCenter;
|
|
203
|
+
};
|
|
204
|
+
const nearest = (value: number) => {
|
|
205
|
+
'worklet';
|
|
206
|
+
const { count: tabs, pitch: step } = metrics.value;
|
|
207
|
+
return step > 0 ? clamp(Math.round(value / step), 0, tabs - 1) : 0;
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
return Gesture.Pan()
|
|
211
|
+
.minDistance(DRAG_SLOP)
|
|
212
|
+
.shouldCancelWhenOutside(false)
|
|
213
|
+
.onStart(e => {
|
|
214
|
+
isDragging.value = true;
|
|
215
|
+
hovered.value = nearest(position(e));
|
|
216
|
+
if (!reduceMotion) {
|
|
217
|
+
lift.value = withSpring(1, SPRING);
|
|
218
|
+
}
|
|
219
|
+
})
|
|
220
|
+
.onUpdate(e => {
|
|
221
|
+
const { count: tabs, pitch: step } = metrics.value;
|
|
222
|
+
const value = position(e);
|
|
223
|
+
const target = nearest(value);
|
|
224
|
+
if (target !== hovered.value) {
|
|
225
|
+
hovered.value = target;
|
|
226
|
+
scheduleOnRN(tick);
|
|
227
|
+
}
|
|
228
|
+
const follow = rubberBand(value, (tabs - 1) * step, step);
|
|
229
|
+
if (reduceMotion) {
|
|
230
|
+
destination.value = follow;
|
|
231
|
+
offset.value = follow;
|
|
232
|
+
} else {
|
|
233
|
+
move(follow, 0, DRAG_SPRING);
|
|
234
|
+
}
|
|
235
|
+
})
|
|
236
|
+
.onEnd((e, success) => {
|
|
237
|
+
isDragging.value = false;
|
|
238
|
+
lift.value = reduceMotion ? 0 : withSpring(0, SPRING);
|
|
239
|
+
const raw = axis === 'x' ? e.velocityX : e.velocityY;
|
|
240
|
+
const velocity = Number.isFinite(raw) ? raw : 0;
|
|
241
|
+
if (!success) {
|
|
242
|
+
move(selected.value * metrics.value.pitch, velocity, SPRING);
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
const target = nearest(position(e));
|
|
246
|
+
move(target * metrics.value.pitch, velocity, SPRING);
|
|
247
|
+
scheduleOnRN(select, target);
|
|
248
|
+
});
|
|
249
|
+
}, [
|
|
250
|
+
axis,
|
|
251
|
+
reduceMotion,
|
|
252
|
+
offset,
|
|
253
|
+
destination,
|
|
254
|
+
selected,
|
|
255
|
+
hovered,
|
|
256
|
+
lift,
|
|
257
|
+
isDragging,
|
|
258
|
+
metrics,
|
|
259
|
+
move,
|
|
260
|
+
tick,
|
|
261
|
+
select,
|
|
262
|
+
]);
|
|
263
|
+
|
|
264
|
+
const bubbleStyle = useAnimatedStyle(() => {
|
|
265
|
+
const isHorizontal = axis === 'x';
|
|
266
|
+
const along = 1 + stretch.value;
|
|
267
|
+
const across = 1 - stretch.value * CROSS_STRETCH_RATIO;
|
|
268
|
+
const lensX = 1 + lift.value * (LENS_SCALE_X - 1);
|
|
269
|
+
const lensY = 1 + lift.value * (LENS_SCALE_Y - 1);
|
|
270
|
+
const scaleX = lensX * (isHorizontal ? along : across);
|
|
271
|
+
const scaleY = lensY * (isHorizontal ? across : along);
|
|
272
|
+
const translateX = isHorizontal ? offset.value : 0;
|
|
273
|
+
const translateY = isHorizontal ? 0 : offset.value;
|
|
274
|
+
return {
|
|
275
|
+
transform: [{ translateX }, { translateY }, { scaleX }, { scaleY }],
|
|
276
|
+
};
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
const barStyle = useAnimatedStyle(() => ({
|
|
280
|
+
transform: [{ scale: 1 + lift.value * BAR_SWELL }],
|
|
281
|
+
}));
|
|
282
|
+
|
|
283
|
+
return {
|
|
284
|
+
gesture,
|
|
285
|
+
bubbleStyle,
|
|
286
|
+
barStyle,
|
|
287
|
+
handlePressIn,
|
|
288
|
+
handlePressOut,
|
|
289
|
+
offset,
|
|
290
|
+
selected,
|
|
291
|
+
pitch,
|
|
292
|
+
reduceMotion,
|
|
293
|
+
};
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
type LiquidSelection = ReturnType<typeof useLiquidSelection>;
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Per tab styles: the active tint follows the lens over the tab, and the icon
|
|
300
|
+
* pops when the lens lands on it.
|
|
301
|
+
*/
|
|
302
|
+
const useLiquidItem = (
|
|
303
|
+
{ offset, selected, pitch, reduceMotion }: LiquidSelection,
|
|
304
|
+
itemIndex: number,
|
|
305
|
+
) => {
|
|
306
|
+
const pop = useSharedValue(1);
|
|
307
|
+
|
|
308
|
+
useAnimatedReaction(
|
|
309
|
+
() => selected.value === itemIndex,
|
|
310
|
+
(isSelected, wasSelected) => {
|
|
311
|
+
if (isSelected && wasSelected === false && !reduceMotion) {
|
|
312
|
+
pop.value = withSequence(
|
|
313
|
+
withTiming(ICON_POP, { duration: ICON_POP_DURATION }),
|
|
314
|
+
withSpring(1, SPRING),
|
|
315
|
+
);
|
|
316
|
+
}
|
|
317
|
+
},
|
|
318
|
+
[itemIndex, reduceMotion],
|
|
319
|
+
);
|
|
320
|
+
|
|
321
|
+
const activeStyle = useAnimatedStyle(() => {
|
|
322
|
+
if (pitch <= 0) {
|
|
323
|
+
return { opacity: selected.value === itemIndex ? 1 : 0 };
|
|
324
|
+
}
|
|
325
|
+
const distance = Math.abs(offset.value - itemIndex * pitch) / pitch;
|
|
326
|
+
return { opacity: clamp(1 - distance, 0, 1) };
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
const popStyle = useAnimatedStyle(() => ({
|
|
330
|
+
transform: [{ scale: pop.value }],
|
|
331
|
+
}));
|
|
332
|
+
|
|
333
|
+
return { activeStyle, popStyle };
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Press lift for a single button, e.g. the compressed rail.
|
|
338
|
+
*/
|
|
339
|
+
const usePressLift = () => {
|
|
340
|
+
const reduceMotion = useReducedMotion();
|
|
341
|
+
const lift = useSharedValue(0);
|
|
342
|
+
const style = useAnimatedStyle(() => {
|
|
343
|
+
const scale = 1 + lift.value * (LENS_SCALE_X - 1) * BUTTON_LIFT_RATIO;
|
|
344
|
+
return { transform: [{ scale }] };
|
|
345
|
+
});
|
|
346
|
+
const onPressIn = () => {
|
|
347
|
+
if (!reduceMotion) {
|
|
348
|
+
lift.value = withSpring(1, SPRING);
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
const onPressOut = () => {
|
|
352
|
+
lift.value = withSpring(0, SPRING);
|
|
353
|
+
};
|
|
354
|
+
return { style, onPressIn, onPressOut };
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
export type { LiquidSelection };
|
|
358
|
+
export { useLiquidSelection, useLiquidItem, usePressLift };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Platform, StyleSheet } from 'react-native';
|
|
2
|
+
import { Colors } from '../../../Consts';
|
|
3
|
+
|
|
4
|
+
export const styles = StyleSheet.create({
|
|
5
|
+
container: { flex: 1 },
|
|
6
|
+
indicatorContainer: { position: 'absolute', top: 0 },
|
|
7
|
+
indicator: {
|
|
8
|
+
height: 2,
|
|
9
|
+
width: 48,
|
|
10
|
+
alignSelf: 'center',
|
|
11
|
+
borderBottomLeftRadius: 2,
|
|
12
|
+
borderBottomRightRadius: 2,
|
|
13
|
+
},
|
|
14
|
+
tabBarStyle: {
|
|
15
|
+
...Platform.select({
|
|
16
|
+
ios: {
|
|
17
|
+
shadowColor: Colors.black_10,
|
|
18
|
+
shadowOffset: { width: 0, height: 4 },
|
|
19
|
+
shadowOpacity: 1,
|
|
20
|
+
shadowRadius: 4,
|
|
21
|
+
},
|
|
22
|
+
android: {
|
|
23
|
+
elevation: 4,
|
|
24
|
+
},
|
|
25
|
+
}),
|
|
26
|
+
},
|
|
27
|
+
tabStyle: {
|
|
28
|
+
paddingVertical: 4,
|
|
29
|
+
},
|
|
30
|
+
});
|