-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathStackCarousel.tsx
More file actions
214 lines (190 loc) · 5.65 KB
/
StackCarousel.tsx
File metadata and controls
214 lines (190 loc) · 5.65 KB
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
import { ReactElement, useState, useImperativeHandle, forwardRef } from 'react';
import { View } from 'react-native';
import { Gesture, GestureDetector } from 'react-native-gesture-handler';
import Animated, {
useAnimatedStyle,
useSharedValue,
interpolate,
withTiming,
Extrapolation,
SharedValue,
} from 'react-native-reanimated';
import { runOnJS } from 'react-native-worklets';
export interface StackCarouselRef {
goToNext: () => void;
goToPrevious: () => void;
}
interface Props {
content: ReactElement[];
itemCount?: number;
baseSpacing?: number;
itemHeight: number;
snapsDuration?: number;
scrollEnabled?: boolean;
}
// From React 19 we could pass the ref as a prop directly, but for now we use forwardRef
export const StackCarousel = forwardRef<StackCarouselRef, Props>(
(
{
content,
itemHeight,
itemCount = 4,
baseSpacing = 60,
snapsDuration = 200,
scrollEnabled = true,
},
ref
) => {
const [baseIndex, setBaseIndex] = useState(0);
const scrollOffset = useSharedValue(0);
const totalOffset = useSharedValue(0);
function rebaseIndex(offsetChange: number) {
const newBaseIndex =
(((baseIndex + offsetChange) % content.length) + content.length) % content.length;
setBaseIndex(newBaseIndex);
totalOffset.value -= offsetChange;
}
function animateToOffset(targetOffset: number) {
totalOffset.value = withTiming(targetOffset, { duration: snapsDuration }, (finished) => {
if (!finished) return;
const currentTotalOffset = totalOffset.value;
if (Math.abs(currentTotalOffset) > content.length) {
const rebaseAmount = Math.round(currentTotalOffset / content.length) * content.length;
runOnJS(rebaseIndex)(rebaseAmount);
}
});
}
useImperativeHandle(
ref,
() => ({
goToNext: () => {
const targetOffset = totalOffset.value + 1;
animateToOffset(targetOffset);
},
goToPrevious: () => {
const targetOffset = totalOffset.value - 1;
animateToOffset(targetOffset);
},
}),
[totalOffset]
);
const gesture = Gesture.Pan()
.enabled(scrollEnabled)
.onChange((e) => {
scrollOffset.value = e.translationY;
})
.onEnd(() => {
const itemsMoved = Math.round(scrollOffset.value / itemHeight);
if (itemsMoved !== 0) {
totalOffset.value = withTiming(
totalOffset.value + itemsMoved,
{
duration: snapsDuration,
},
(finished) => {
if (!finished) return;
const currentTotalOffset = totalOffset.value + itemsMoved;
if (Math.abs(currentTotalOffset) > content.length) {
const rebaseAmount =
Math.round(currentTotalOffset / content.length) * content.length;
runOnJS(rebaseIndex)(rebaseAmount);
}
}
);
} else {
totalOffset.value = withTiming(totalOffset.value, {
duration: snapsDuration,
});
}
scrollOffset.value = withTiming(0, {
duration: snapsDuration,
});
});
function getCurrentItems() {
const items = [];
const bufferSize = content.length * 2;
for (let i = -bufferSize; i < bufferSize; i++) {
const itemIndex = (((baseIndex + i) % content.length) + content.length) % content.length;
items.push({
element: content[itemIndex],
position: i,
key: `${baseIndex}-${i}`,
});
}
return items;
}
const items = getCurrentItems();
return (
<GestureDetector gesture={gesture}>
<View>
{items.map(({ element, position, key }) => (
<StackCarouselSlot
position={position}
key={key}
itemCount={itemCount}
itemHeight={itemHeight}
scrollOffset={scrollOffset}
totalOffset={totalOffset}
baseSpacing={baseSpacing}>
{element}
</StackCarouselSlot>
))}
</View>
</GestureDetector>
);
}
);
function StackCarouselSlot({
scrollOffset,
position,
itemCount,
itemHeight,
totalOffset,
baseSpacing,
children,
}: {
children: ReactElement;
scrollOffset: SharedValue<number>;
position: number;
itemHeight: number;
itemCount: number;
baseSpacing: number;
totalOffset: SharedValue<number>;
}) {
const rStyle = useAnimatedStyle(() => {
const currentScrollOffset = scrollOffset.value / itemHeight;
const currentPosition = position - currentScrollOffset - totalOffset.value;
const isVisible = currentPosition >= -3 && currentPosition <= itemCount + 2;
if (!isVisible) {
return { opacity: 0, pointerEvents: 'none', display: 'none' };
}
const visualPosition = itemCount - 1 - currentPosition;
const scale = interpolate(visualPosition, [0, itemCount - 1], [1.0, 0.3], Extrapolation.CLAMP);
const translateY = -visualPosition * baseSpacing;
const opacity = interpolate(
visualPosition,
[-1, 0, itemCount - 1, itemCount],
[0, 1.0, 1, 0],
Extrapolation.CLAMP
);
return {
transform: [{ scale: Math.max(0.1, scale) }, { translateY }],
opacity: Math.max(0, opacity),
pointerEvents: opacity <= 0.01 ? 'none' : 'auto',
display: opacity <= 0.01 ? 'none' : 'flex',
};
});
return (
<Animated.View
style={[
{
position: 'absolute',
width: '100%',
height: '100%',
},
rStyle,
]}>
{children}
</Animated.View>
);
}