-
Notifications
You must be signed in to change notification settings - Fork 0
/
StickyTab.tsx
234 lines (226 loc) · 7.6 KB
/
StickyTab.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { StyleProp, StyleSheet, View, ViewStyle } from "react-native";
import Animated, {
Easing,
Extrapolate,
cancelAnimation,
interpolate,
runOnJS,
useDerivedValue,
useSharedValue,
withSpring,
withTiming,
} from "react-native-reanimated";
import { snapPoint } from "react-native-redash";
import { LinearGradient } from "expo-linear-gradient";
import { Gesture, GestureHandlerRootView } from "react-native-gesture-handler";
import React, { FC, useState } from "react";
import { Placeholder } from "./Placeholder";
import { TABS_GAP, INNER_PADDING, CONTAINER_BORDER_WIDTH } from "./constants";
import { TabItem, TabItemBackgroundProps, TabItemProps } from "./TabItem";
type Props = Pick<
TabItemProps,
| "gradientEnabled"
| "tabHeight"
| "horizontalResistance"
| "verticalResistance"
> &
TabItemBackgroundProps & {
/**
* the default value is container / number of tabs + padding in between. If you want to set a fixed width, you can use this prop
*/
tabWidth?: TabItemProps["tabWidth"];
values: string[];
renderText: (value: string, index: number) => React.ReactNode;
containerWidth: number;
containerStyle?: StyleProp<ViewStyle>;
placeholderBackgroundColor: string;
innerPadding?: number;
tabHeadBorderRadius?: number;
tabTailBorderRadius?: number;
containerBorderRadius?: number;
containerBorderWidth?: number;
backgroundColor: string;
onStretchStart?: () => void;
onStretchEnd?: () => void;
onPressTab?: (index: number) => void;
};
export const StickyTab: FC<Props> = (props) => {
const innerPadding = props.innerPadding ?? INNER_PADDING;
const tabNum = props.values.length;
const tabWidth =
props.tabWidth ??
(props.containerWidth - props.innerPadding * 2) / tabNum - TABS_GAP;
const containerBorderWidth =
props.containerBorderWidth ?? CONTAINER_BORDER_WIDTH;
const MAX_DRAG =
props.containerWidth -
2 * innerPadding -
tabWidth -
containerBorderWidth * 2;
const CONTAINER_HEIGHT =
props.tabHeight + 2 * innerPadding + 2 * containerBorderWidth;
const MAX_WIDTH = tabWidth * props.horizontalResistance;
const tabHeadBorderRadius = props.tabHeadBorderRadius ?? props.tabHeight / 2;
const tabTailBorderRadius = props.tabTailBorderRadius ?? props.tabHeight / 2;
const containerBorderRadius =
props.containerBorderRadius ??
(tabHeadBorderRadius + tabTailBorderRadius) / 2 + innerPadding;
const STEP = MAX_DRAG / (tabNum - 1);
const snapPoints = new Array(tabNum).fill(0).map((_, i) => STEP * i);
const sticked = useSharedValue(true);
const [selectedIndex, setSelectedIndex] = useState(0);
const position = useSharedValue(0);
const sticking = useDerivedValue(() => withSpring(sticked.value ? 1 : 0));
const translateX = useSharedValue(0);
const translateY = useSharedValue(0);
const offsetX = useSharedValue(0);
const isRunningId = useSharedValue<null | number>(null);
const tabTranslation = useDerivedValue(
() =>
sticked.value
? position.value
: position.value + (1 - sticking.value) * translateX.value,
[sticked.value, position.value, translateX.value]
);
const progress = useDerivedValue(
() =>
sticking.value *
interpolate(
translateX.value,
[-MAX_WIDTH, 0, MAX_WIDTH],
[-1, 0, 1],
Extrapolate.CLAMP
)
);
const handleEndStretch = () => {
if (props.onStretchEnd) props.onStretchEnd();
};
const handleStartStretch = () => {
if (props.onStretchStart) props.onStretchStart();
};
const panGesture = Gesture.Pan()
.onBegin(() => {
// Cancel animation but keep the current value
const prevTranslation = translateX.value;
cancelAnimation(translateX);
translateX.value = prevTranslation;
isRunningId.value = Date.now();
runOnJS(handleStartStretch)();
})
.onChange(({ translationX, translationY }) => {
translateX.value = translationX + offsetX.value;
translateY.value = translationY;
if (Math.abs(translationX) > MAX_WIDTH && sticked.value) {
sticked.value = false;
runOnJS(handleEndStretch)();
}
})
.onEnd(({ velocityX: velocity }) => {
const dest = snapPoint(
translateX.value + position.value,
velocity,
snapPoints
);
translateY.value = withSpring(0);
const runningId = isRunningId.value;
const finalDest = sticked.value ? 0 : dest - position.value;
translateX.value = withTiming(
finalDest,
{ easing: Easing.inOut(Easing.ease) },
() => {
const cancelled = runningId !== isRunningId.value;
if (!cancelled) {
position.value = sticked.value ? position.value : dest;
translateX.value = 0;
sticked.value = true;
isRunningId.value = null;
offsetX.value = 0;
runOnJS(setSelectedIndex)(Math.round(dest / STEP));
} else {
offsetX.value = translateX.value;
}
}
);
});
return (
<View
style={[
{
width: props.containerWidth + containerBorderWidth,
height: CONTAINER_HEIGHT + containerBorderWidth,
},
props.containerStyle,
]}
>
<LinearGradient
start={{ x: 0, y: 0 }}
end={{ x: 1, y: 1 }}
colors={
props.gradientEnabled === false
? [props.backgroundColor]
: props.colors
}
style={{
width: props.containerWidth + containerBorderWidth,
height: CONTAINER_HEIGHT + containerBorderWidth,
borderRadius: containerBorderRadius + containerBorderWidth,
padding: containerBorderWidth,
overflow: "hidden",
}}
>
<View
style={{
width: "100%",
height: "100%",
backgroundColor: props.backgroundColor,
borderRadius: containerBorderRadius,
overflow: "hidden",
}}
>
<TabItem
verticalResistance={props.verticalResistance}
horizontalResistance={props.horizontalResistance}
headBorderRadius={tabHeadBorderRadius}
tailBorderRadius={tabTailBorderRadius}
progress={progress}
padding={innerPadding}
translateX={tabTranslation}
translateY={translateY}
panGesture={panGesture}
tabWidth={tabWidth}
tabHeight={props.tabHeight}
gradientEnabled={props.gradientEnabled}
backgroundColor={
props.gradientEnabled === false && props.backgroundColor
}
colors={props.gradientEnabled && props.colors}
/>
{props.values.map((value, index) => (
<Placeholder
key={value}
borderRadius={containerBorderRadius - innerPadding}
containerPadding={innerPadding}
backgroundColor={props.placeholderBackgroundColor}
translateX={STEP}
renderText={() => props.renderText(value, index)}
tabHeight={props.tabHeight}
tabWidth={tabWidth}
tabTranslation={tabTranslation}
index={index}
onPress={(translation) => {
position.value = withTiming(translation, {
easing: Easing.inOut(Easing.ease),
});
handleStartStretch();
translateX.value = 0;
offsetX.value = 0;
sticked.value = true;
setSelectedIndex(index);
}}
/>
))}
</View>
</LinearGradient>
</View>
);
};