swipefeed 1.0.0
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/LICENSE +22 -0
- package/README.md +203 -0
- package/dist/__tests__/SwipeDeck.browser.test.d.ts +1 -0
- package/dist/__tests__/SwipeDeck.gestures.test.d.ts +1 -0
- package/dist/__tests__/SwipeDeck.preload.test.d.ts +1 -0
- package/dist/__tests__/SwipeDeck.props.test.d.ts +1 -0
- package/dist/__tests__/gestures.test.d.ts +1 -0
- package/dist/__tests__/useKeyboard.test.d.ts +1 -0
- package/dist/__tests__/useScrollTo.test.d.ts +1 -0
- package/dist/__tests__/useSwipeDeck.browser.test.d.ts +1 -0
- package/dist/__tests__/useSwipeDeck.callbacks.test.d.ts +1 -0
- package/dist/__tests__/useSwipeDeck.loop.test.d.ts +1 -0
- package/dist/__tests__/useSwipeDeck.test.d.ts +1 -0
- package/dist/__tests__/useSwipeItem.test.d.ts +1 -0
- package/dist/__tests__/useVirtualizer.test.d.ts +1 -0
- package/dist/__tests__/useWheel.continuous.test.d.ts +1 -0
- package/dist/__tests__/useWheel.dampening.test.d.ts +1 -0
- package/dist/__tests__/useWheel.test.d.ts +1 -0
- package/dist/__tests__/useWheel.tracking.test.d.ts +1 -0
- package/dist/__tests__/useWindowSize.test.d.ts +1 -0
- package/dist/__tests__/utils.test.d.ts +1 -0
- package/dist/__tests__/wheel.test.d.ts +1 -0
- package/dist/components/SwipeDeck.d.ts +5 -0
- package/dist/hooks/useGestures.d.ts +32 -0
- package/dist/hooks/useKeyboard.d.ts +18 -0
- package/dist/hooks/useScrollTo.d.ts +9 -0
- package/dist/hooks/useSwipeDeck.d.ts +2 -0
- package/dist/hooks/useSwipeItem.d.ts +3 -0
- package/dist/hooks/useVirtualizer.d.ts +24 -0
- package/dist/hooks/useWheel.d.ts +23 -0
- package/dist/hooks/useWindowSize.d.ts +4 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +907 -0
- package/dist/index.js.map +19 -0
- package/dist/stories/SwipeDeck.stories.d.ts +19 -0
- package/dist/types.d.ts +122 -0
- package/dist/utils/clamp.d.ts +1 -0
- package/dist/utils/debounce.d.ts +4 -0
- package/dist/utils/prefersReducedMotion.d.ts +1 -0
- package/dist/utils/raf.d.ts +5 -0
- package/package.json +95 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,907 @@
|
|
|
1
|
+
// src/hooks/useSwipeDeck.ts
|
|
2
|
+
import { useCallback as useCallback5, useEffect as useEffect4, useMemo as useMemo3, useRef as useRef3, useState as useState3 } from "react";
|
|
3
|
+
|
|
4
|
+
// src/utils/clamp.ts
|
|
5
|
+
function clamp(value, min, max) {
|
|
6
|
+
if (Number.isNaN(value))
|
|
7
|
+
return min;
|
|
8
|
+
return Math.min(Math.max(value, min), max);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// src/utils/prefersReducedMotion.ts
|
|
12
|
+
var cached = null;
|
|
13
|
+
function prefersReducedMotion() {
|
|
14
|
+
if (cached !== null)
|
|
15
|
+
return cached;
|
|
16
|
+
if (typeof window === "undefined" || typeof window.matchMedia !== "function") {
|
|
17
|
+
cached = false;
|
|
18
|
+
return cached;
|
|
19
|
+
}
|
|
20
|
+
cached = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
21
|
+
return cached;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// src/hooks/useGestures.ts
|
|
25
|
+
import { useCallback, useRef, useState, useMemo } from "react";
|
|
26
|
+
function primaryCoord(evt, orientation) {
|
|
27
|
+
return orientation === "vertical" ? evt.clientY : evt.clientX;
|
|
28
|
+
}
|
|
29
|
+
function useGestures(params) {
|
|
30
|
+
const {
|
|
31
|
+
orientation,
|
|
32
|
+
direction,
|
|
33
|
+
threshold,
|
|
34
|
+
flickVelocity,
|
|
35
|
+
lockAxis,
|
|
36
|
+
ignoreWhileAnimating,
|
|
37
|
+
loop,
|
|
38
|
+
getIndex,
|
|
39
|
+
maxIndex,
|
|
40
|
+
onRequestIndexChange,
|
|
41
|
+
setAnimating,
|
|
42
|
+
isAnimating,
|
|
43
|
+
onDragStart,
|
|
44
|
+
onDrag,
|
|
45
|
+
onDragEnd
|
|
46
|
+
} = params;
|
|
47
|
+
const [isDragging, setIsDragging] = useState(false);
|
|
48
|
+
const state = useRef({
|
|
49
|
+
isDragging: false,
|
|
50
|
+
start: 0,
|
|
51
|
+
last: 0,
|
|
52
|
+
delta: 0,
|
|
53
|
+
startTime: 0
|
|
54
|
+
});
|
|
55
|
+
const startDrag = useCallback((evt) => {
|
|
56
|
+
evt.currentTarget.setPointerCapture?.(evt.pointerId);
|
|
57
|
+
const coord = primaryCoord(evt, orientation);
|
|
58
|
+
state.current = {
|
|
59
|
+
isDragging: true,
|
|
60
|
+
start: coord,
|
|
61
|
+
last: coord,
|
|
62
|
+
delta: 0,
|
|
63
|
+
startTime: performance.now()
|
|
64
|
+
};
|
|
65
|
+
setIsDragging(true);
|
|
66
|
+
onDragStart?.(evt);
|
|
67
|
+
}, [orientation, onDragStart]);
|
|
68
|
+
const updateDrag = useCallback((evt) => {
|
|
69
|
+
if (!state.current.isDragging)
|
|
70
|
+
return;
|
|
71
|
+
const coord = primaryCoord(evt, orientation);
|
|
72
|
+
const delta = coord - state.current.start;
|
|
73
|
+
state.current.delta = delta;
|
|
74
|
+
state.current.last = coord;
|
|
75
|
+
onDrag?.(delta, evt);
|
|
76
|
+
}, [orientation, onDrag]);
|
|
77
|
+
const endDrag = useCallback(() => {
|
|
78
|
+
if (!state.current.isDragging)
|
|
79
|
+
return;
|
|
80
|
+
const now = performance.now();
|
|
81
|
+
const duration = Math.max(now - state.current.startTime, 1);
|
|
82
|
+
const distance = Math.abs(state.current.delta);
|
|
83
|
+
const velocity = distance / duration;
|
|
84
|
+
const isForward = (() => {
|
|
85
|
+
const raw = state.current.delta;
|
|
86
|
+
if (orientation === "vertical")
|
|
87
|
+
return raw < 0;
|
|
88
|
+
const normalized = direction === "rtl" ? raw * -1 : raw;
|
|
89
|
+
return normalized < 0;
|
|
90
|
+
})();
|
|
91
|
+
const current = getIndex();
|
|
92
|
+
const deltaSign = isForward ? 1 : -1;
|
|
93
|
+
const meetsFlick = velocity > flickVelocity;
|
|
94
|
+
const meetsDistance = distance > threshold;
|
|
95
|
+
let next = current;
|
|
96
|
+
if (meetsFlick || meetsDistance) {
|
|
97
|
+
next = current + deltaSign;
|
|
98
|
+
}
|
|
99
|
+
if (loop) {
|
|
100
|
+
next = (next + (maxIndex + 1)) % (maxIndex + 1);
|
|
101
|
+
} else {
|
|
102
|
+
next = clamp(next, 0, maxIndex);
|
|
103
|
+
}
|
|
104
|
+
setAnimating(true);
|
|
105
|
+
onRequestIndexChange(next);
|
|
106
|
+
state.current.isDragging = false;
|
|
107
|
+
state.current.delta = 0;
|
|
108
|
+
setIsDragging(false);
|
|
109
|
+
onDragEnd?.();
|
|
110
|
+
}, [direction, flickVelocity, getIndex, maxIndex, onRequestIndexChange, orientation, loop, setAnimating, threshold, onDragEnd]);
|
|
111
|
+
const cancelDrag = useCallback(() => {
|
|
112
|
+
state.current.isDragging = false;
|
|
113
|
+
state.current.delta = 0;
|
|
114
|
+
setIsDragging(false);
|
|
115
|
+
}, []);
|
|
116
|
+
const onPointerDown = useCallback((evt) => {
|
|
117
|
+
if (ignoreWhileAnimating && isAnimating)
|
|
118
|
+
return;
|
|
119
|
+
if (evt.button !== 0)
|
|
120
|
+
return;
|
|
121
|
+
startDrag(evt);
|
|
122
|
+
}, [ignoreWhileAnimating, isAnimating, startDrag]);
|
|
123
|
+
const onPointerMove = useCallback((evt) => {
|
|
124
|
+
if (!lockAxis && !state.current.isDragging)
|
|
125
|
+
return;
|
|
126
|
+
if (state.current.isDragging) {
|
|
127
|
+
updateDrag(evt);
|
|
128
|
+
}
|
|
129
|
+
}, [lockAxis, updateDrag]);
|
|
130
|
+
const onPointerUp = useCallback(() => {
|
|
131
|
+
endDrag();
|
|
132
|
+
}, [endDrag]);
|
|
133
|
+
const onPointerCancel = useCallback(() => {
|
|
134
|
+
cancelDrag();
|
|
135
|
+
}, [cancelDrag]);
|
|
136
|
+
return useMemo(() => ({
|
|
137
|
+
isDragging,
|
|
138
|
+
handlers: { onPointerDown, onPointerMove, onPointerUp, onPointerCancel }
|
|
139
|
+
}), [isDragging, onPointerDown, onPointerMove, onPointerUp, onPointerCancel]);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// src/hooks/useWheel.ts
|
|
143
|
+
import { useCallback as useCallback2, useRef as useRef2, useEffect } from "react";
|
|
144
|
+
function useWheel(params) {
|
|
145
|
+
const {
|
|
146
|
+
orientation,
|
|
147
|
+
direction,
|
|
148
|
+
discretePaging,
|
|
149
|
+
threshold,
|
|
150
|
+
debounce,
|
|
151
|
+
cooldown = 500,
|
|
152
|
+
onRequestIndexChange,
|
|
153
|
+
viewport,
|
|
154
|
+
onDragStart,
|
|
155
|
+
onDrag,
|
|
156
|
+
onDragEnd
|
|
157
|
+
} = params;
|
|
158
|
+
const accumulated = useRef2(0);
|
|
159
|
+
const timeoutRef = useRef2(null);
|
|
160
|
+
const lastNavigationTime = useRef2(0);
|
|
161
|
+
const isInCooldown = useRef2(false);
|
|
162
|
+
const lastEventTime = useRef2(0);
|
|
163
|
+
const gestureDirection = useRef2(null);
|
|
164
|
+
const eventCount = useRef2(0);
|
|
165
|
+
const isTracking = useRef2(false);
|
|
166
|
+
const velocityRef = useRef2(0);
|
|
167
|
+
const MAX_DELTA_ACCUMULATION = 120;
|
|
168
|
+
const MAX_VISUAL_DELTA = 40;
|
|
169
|
+
const endGesture = useCallback2(() => {
|
|
170
|
+
isTracking.current = false;
|
|
171
|
+
accumulated.current = 0;
|
|
172
|
+
gestureDirection.current = null;
|
|
173
|
+
eventCount.current = 0;
|
|
174
|
+
velocityRef.current = 0;
|
|
175
|
+
onDragEnd?.();
|
|
176
|
+
}, [onDragEnd]);
|
|
177
|
+
const resetAccumulated = useCallback2(() => {
|
|
178
|
+
if (timeoutRef.current)
|
|
179
|
+
clearTimeout(timeoutRef.current);
|
|
180
|
+
timeoutRef.current = setTimeout(() => {
|
|
181
|
+
endGesture();
|
|
182
|
+
}, debounce);
|
|
183
|
+
}, [debounce, endGesture]);
|
|
184
|
+
useEffect(() => {
|
|
185
|
+
const element = viewport;
|
|
186
|
+
if (!element)
|
|
187
|
+
return;
|
|
188
|
+
const onWheel = (evt) => {
|
|
189
|
+
if (!discretePaging)
|
|
190
|
+
return;
|
|
191
|
+
evt.preventDefault();
|
|
192
|
+
evt.stopPropagation();
|
|
193
|
+
const now = performance.now();
|
|
194
|
+
if (isInCooldown.current) {
|
|
195
|
+
if (now - lastNavigationTime.current < cooldown) {
|
|
196
|
+
if (isTracking.current)
|
|
197
|
+
endGesture();
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
isInCooldown.current = false;
|
|
201
|
+
}
|
|
202
|
+
const rawDelta = orientation === "vertical" ? evt.deltaY : evt.deltaX;
|
|
203
|
+
const adjusted = orientation === "horizontal" && direction === "rtl" ? -rawDelta : rawDelta;
|
|
204
|
+
const currentDirection = adjusted > 0 ? 1 : -1;
|
|
205
|
+
if (gestureDirection.current !== null && gestureDirection.current !== currentDirection) {
|
|
206
|
+
accumulated.current = 0;
|
|
207
|
+
eventCount.current = 0;
|
|
208
|
+
}
|
|
209
|
+
gestureDirection.current = currentDirection;
|
|
210
|
+
if (!isTracking.current) {
|
|
211
|
+
isTracking.current = true;
|
|
212
|
+
onDragStart?.();
|
|
213
|
+
}
|
|
214
|
+
const timeDelta = now - lastEventTime.current;
|
|
215
|
+
lastEventTime.current = now;
|
|
216
|
+
eventCount.current++;
|
|
217
|
+
const accumDelta = Math.sign(adjusted) * Math.min(Math.abs(adjusted), MAX_DELTA_ACCUMULATION);
|
|
218
|
+
const visualDelta = Math.sign(adjusted) * Math.min(Math.abs(adjusted), MAX_VISUAL_DELTA);
|
|
219
|
+
const instantVelocity = timeDelta > 0 && timeDelta < 100 ? Math.abs(accumDelta / timeDelta) : 0;
|
|
220
|
+
velocityRef.current = velocityRef.current * 0.8 + instantVelocity * 0.2;
|
|
221
|
+
accumulated.current += accumDelta;
|
|
222
|
+
onDrag?.(-visualDelta);
|
|
223
|
+
resetAccumulated();
|
|
224
|
+
const matchesThreshold = Math.abs(accumulated.current) >= threshold;
|
|
225
|
+
const isFlick = velocityRef.current > 1.2 && Math.abs(accumulated.current) > 20;
|
|
226
|
+
if (matchesThreshold || isFlick) {
|
|
227
|
+
const navDelta = accumulated.current > 0 ? 1 : -1;
|
|
228
|
+
endGesture();
|
|
229
|
+
lastNavigationTime.current = now;
|
|
230
|
+
isInCooldown.current = true;
|
|
231
|
+
if (timeoutRef.current)
|
|
232
|
+
clearTimeout(timeoutRef.current);
|
|
233
|
+
onRequestIndexChange(navDelta);
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
element.addEventListener("wheel", onWheel, { passive: false });
|
|
237
|
+
return () => {
|
|
238
|
+
element.removeEventListener("wheel", onWheel);
|
|
239
|
+
if (timeoutRef.current)
|
|
240
|
+
clearTimeout(timeoutRef.current);
|
|
241
|
+
};
|
|
242
|
+
}, [
|
|
243
|
+
orientation,
|
|
244
|
+
direction,
|
|
245
|
+
discretePaging,
|
|
246
|
+
threshold,
|
|
247
|
+
cooldown,
|
|
248
|
+
onRequestIndexChange,
|
|
249
|
+
resetAccumulated,
|
|
250
|
+
viewport,
|
|
251
|
+
endGesture,
|
|
252
|
+
onDragStart,
|
|
253
|
+
onDrag
|
|
254
|
+
]);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// src/hooks/useKeyboard.ts
|
|
258
|
+
import { useCallback as useCallback3, useEffect as useEffect2 } from "react";
|
|
259
|
+
var DEFAULT_BINDINGS_VERTICAL = {
|
|
260
|
+
prev: ["ArrowUp"],
|
|
261
|
+
next: ["ArrowDown"],
|
|
262
|
+
first: ["Home"],
|
|
263
|
+
last: ["End"],
|
|
264
|
+
altPrev: ["PageUp"],
|
|
265
|
+
altNext: ["PageDown"]
|
|
266
|
+
};
|
|
267
|
+
var DEFAULT_BINDINGS_HORIZONTAL = {
|
|
268
|
+
prev: ["ArrowLeft"],
|
|
269
|
+
next: ["ArrowRight"],
|
|
270
|
+
first: ["Home"],
|
|
271
|
+
last: ["End"],
|
|
272
|
+
altPrev: ["PageUp"],
|
|
273
|
+
altNext: ["PageDown"]
|
|
274
|
+
};
|
|
275
|
+
function useKeyboard(params) {
|
|
276
|
+
const { orientation, direction, config, onPrev, onNext, onFirst, onLast } = params;
|
|
277
|
+
const enabled = config?.enabled ?? true;
|
|
278
|
+
const isGlobal = config?.global ?? false;
|
|
279
|
+
const bindings = config?.bindings ?? (orientation === "vertical" ? DEFAULT_BINDINGS_VERTICAL : DEFAULT_BINDINGS_HORIZONTAL);
|
|
280
|
+
const handleKeyDown = useCallback3((evt) => {
|
|
281
|
+
if (!enabled)
|
|
282
|
+
return;
|
|
283
|
+
const key = evt.key;
|
|
284
|
+
const prevKeys = [...bindings.prev ?? [], ...bindings.altPrev ?? []];
|
|
285
|
+
const nextKeys = [...bindings.next ?? [], ...bindings.altNext ?? []];
|
|
286
|
+
const isHorizontal = orientation === "horizontal";
|
|
287
|
+
const rtl = direction === "rtl";
|
|
288
|
+
if (prevKeys.includes(key)) {
|
|
289
|
+
evt.preventDefault();
|
|
290
|
+
if (isHorizontal && rtl)
|
|
291
|
+
onNext();
|
|
292
|
+
else
|
|
293
|
+
onPrev();
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
if (nextKeys.includes(key)) {
|
|
297
|
+
evt.preventDefault();
|
|
298
|
+
if (isHorizontal && rtl)
|
|
299
|
+
onPrev();
|
|
300
|
+
else
|
|
301
|
+
onNext();
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
if (bindings.first?.includes(key)) {
|
|
305
|
+
evt.preventDefault();
|
|
306
|
+
onFirst?.();
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
if (bindings.last?.includes(key)) {
|
|
310
|
+
evt.preventDefault();
|
|
311
|
+
onLast?.();
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
}, [bindings, direction, enabled, onFirst, onLast, onNext, onPrev, orientation]);
|
|
315
|
+
useEffect2(() => {
|
|
316
|
+
if (!enabled || !isGlobal)
|
|
317
|
+
return;
|
|
318
|
+
window.addEventListener("keydown", handleKeyDown);
|
|
319
|
+
return () => {
|
|
320
|
+
window.removeEventListener("keydown", handleKeyDown);
|
|
321
|
+
};
|
|
322
|
+
}, [enabled, isGlobal, handleKeyDown]);
|
|
323
|
+
return { onKeyDown: handleKeyDown };
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// src/hooks/useVirtualizer.ts
|
|
327
|
+
import { useEffect as useEffect3, useLayoutEffect, useState as useState2, useMemo as useMemo2, useCallback as useCallback4 } from "react";
|
|
328
|
+
import { useVirtualizer as useTanStackVirtualizer } from "@tanstack/react-virtual";
|
|
329
|
+
var useIsomorphicLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect3;
|
|
330
|
+
function useVirtualizer(params) {
|
|
331
|
+
const { items, virtual, getScrollElement, orientation = "vertical" } = params;
|
|
332
|
+
const count = items.length;
|
|
333
|
+
const [sizeState, setSizeState] = useState2(() => {
|
|
334
|
+
const fallback = typeof window !== "undefined" ? orientation === "vertical" ? window.innerHeight : window.innerWidth : 800;
|
|
335
|
+
return { size: fallback, measured: false };
|
|
336
|
+
});
|
|
337
|
+
useIsomorphicLayoutEffect(() => {
|
|
338
|
+
const scrollElement = getScrollElement();
|
|
339
|
+
if (!scrollElement)
|
|
340
|
+
return;
|
|
341
|
+
const updateSize = () => {
|
|
342
|
+
const size = orientation === "vertical" ? scrollElement.clientHeight : scrollElement.clientWidth;
|
|
343
|
+
if (size > 0) {
|
|
344
|
+
setSizeState((prev) => {
|
|
345
|
+
if (prev.size !== size || !prev.measured) {
|
|
346
|
+
return { size, measured: true };
|
|
347
|
+
}
|
|
348
|
+
return prev;
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
};
|
|
352
|
+
updateSize();
|
|
353
|
+
const resizeObserver = new ResizeObserver(updateSize);
|
|
354
|
+
resizeObserver.observe(scrollElement);
|
|
355
|
+
return () => {
|
|
356
|
+
resizeObserver.disconnect();
|
|
357
|
+
};
|
|
358
|
+
}, [getScrollElement, orientation]);
|
|
359
|
+
const measuredSize = sizeState.size;
|
|
360
|
+
const getEstimatedSize = useCallback4((i) => {
|
|
361
|
+
const explicitSize = virtual?.estimatedSize;
|
|
362
|
+
if (typeof explicitSize === "function") {
|
|
363
|
+
return explicitSize(items[i], i);
|
|
364
|
+
}
|
|
365
|
+
if (measuredSize > 0) {
|
|
366
|
+
return measuredSize;
|
|
367
|
+
}
|
|
368
|
+
return typeof explicitSize === "number" ? explicitSize : 800;
|
|
369
|
+
}, [virtual?.estimatedSize, items, measuredSize]);
|
|
370
|
+
const initialRect = useMemo2(() => {
|
|
371
|
+
if (typeof window === "undefined") {
|
|
372
|
+
return { width: 800, height: 800 };
|
|
373
|
+
}
|
|
374
|
+
return {
|
|
375
|
+
width: window.innerWidth,
|
|
376
|
+
height: window.innerHeight
|
|
377
|
+
};
|
|
378
|
+
}, []);
|
|
379
|
+
const getItemKey = useMemo2(() => {
|
|
380
|
+
if (!virtual?.getItemKey)
|
|
381
|
+
return;
|
|
382
|
+
return (index) => virtual.getItemKey?.(items[index], index) ?? index;
|
|
383
|
+
}, [virtual?.getItemKey, items]);
|
|
384
|
+
const rowVirtualizer = useTanStackVirtualizer({
|
|
385
|
+
count,
|
|
386
|
+
getScrollElement,
|
|
387
|
+
estimateSize: getEstimatedSize,
|
|
388
|
+
overscan: virtual?.overscan ?? 5,
|
|
389
|
+
horizontal: orientation === "horizontal",
|
|
390
|
+
getItemKey,
|
|
391
|
+
initialRect
|
|
392
|
+
});
|
|
393
|
+
return {
|
|
394
|
+
virtualItems: rowVirtualizer.getVirtualItems().map((item) => ({
|
|
395
|
+
index: item.index,
|
|
396
|
+
key: item.key,
|
|
397
|
+
offset: item.start,
|
|
398
|
+
size: item.size,
|
|
399
|
+
measureElement: rowVirtualizer.measureElement
|
|
400
|
+
})),
|
|
401
|
+
totalSize: rowVirtualizer.getTotalSize(),
|
|
402
|
+
getItemOffset: (index) => {
|
|
403
|
+
const found = rowVirtualizer.getVirtualItems().find((v) => v.index === index);
|
|
404
|
+
return found ? found.start : 0;
|
|
405
|
+
},
|
|
406
|
+
getItemSize: (index) => {
|
|
407
|
+
const found = rowVirtualizer.getVirtualItems().find((v) => v.index === index);
|
|
408
|
+
return found ? found.size : 0;
|
|
409
|
+
},
|
|
410
|
+
scrollToIndex: (index, options) => {
|
|
411
|
+
rowVirtualizer.scrollToIndex(index, {
|
|
412
|
+
align: options?.align,
|
|
413
|
+
behavior: options?.behavior
|
|
414
|
+
});
|
|
415
|
+
},
|
|
416
|
+
containerSize: measuredSize
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// src/hooks/useSwipeDeck.ts
|
|
421
|
+
var DEFAULTS = {
|
|
422
|
+
orientation: "vertical",
|
|
423
|
+
direction: "ltr",
|
|
424
|
+
loop: false,
|
|
425
|
+
defaultIndex: 0,
|
|
426
|
+
gesture: {
|
|
427
|
+
threshold: 10,
|
|
428
|
+
flickVelocity: 0.1,
|
|
429
|
+
lockAxis: true,
|
|
430
|
+
ignoreWhileAnimating: true
|
|
431
|
+
},
|
|
432
|
+
wheel: {
|
|
433
|
+
discretePaging: true,
|
|
434
|
+
threshold: 100,
|
|
435
|
+
debounce: 120,
|
|
436
|
+
cooldown: 800
|
|
437
|
+
},
|
|
438
|
+
keyboard: {
|
|
439
|
+
enabled: true
|
|
440
|
+
},
|
|
441
|
+
visibility: {
|
|
442
|
+
strategy: "position",
|
|
443
|
+
intersectionRatio: 0.6,
|
|
444
|
+
debounce: 100
|
|
445
|
+
},
|
|
446
|
+
virtual: {
|
|
447
|
+
overscan: 5
|
|
448
|
+
},
|
|
449
|
+
endReachedThreshold: 3,
|
|
450
|
+
ariaLabel: "Swipe feed",
|
|
451
|
+
keyboardNavigation: true
|
|
452
|
+
};
|
|
453
|
+
function useSwipeDeck(options) {
|
|
454
|
+
const items = options.items ?? [];
|
|
455
|
+
const orientation = options.orientation ?? DEFAULTS.orientation;
|
|
456
|
+
const direction = options.direction ?? DEFAULTS.direction;
|
|
457
|
+
const loop = options.loop ?? DEFAULTS.loop;
|
|
458
|
+
const defaultIndex = options.defaultIndex ?? DEFAULTS.defaultIndex;
|
|
459
|
+
const preload = options.preload ?? 0;
|
|
460
|
+
const gestureCfg = useMemo3(() => ({ ...DEFAULTS.gesture, ...options.gesture ?? {} }), [options.gesture]);
|
|
461
|
+
const wheelCfg = useMemo3(() => ({ ...DEFAULTS.wheel, ...options.wheel ?? {} }), [options.wheel]);
|
|
462
|
+
const keyboardCfg = useMemo3(() => ({ ...DEFAULTS.keyboard, ...options.keyboard ?? {} }), [options.keyboard]);
|
|
463
|
+
const virtualCfg = useMemo3(() => {
|
|
464
|
+
const base = { ...DEFAULTS.virtual, ...options.virtual ?? {} };
|
|
465
|
+
return {
|
|
466
|
+
...base,
|
|
467
|
+
overscan: Math.max(base.overscan ?? 0, preload)
|
|
468
|
+
};
|
|
469
|
+
}, [options.virtual, preload]);
|
|
470
|
+
const endReachedThreshold = options.endReachedThreshold ?? DEFAULTS.endReachedThreshold;
|
|
471
|
+
const ariaLabel = options.ariaLabel ?? DEFAULTS.ariaLabel;
|
|
472
|
+
const keyboardNavigation = options.keyboardNavigation ?? true;
|
|
473
|
+
const isControlled = typeof options.index === "number";
|
|
474
|
+
const [internalIndex, setInternalIndex] = useState3(() => clamp(defaultIndex, 0, Math.max(items.length - 1, 0)));
|
|
475
|
+
const index = isControlled ? clamp(options.index ?? 0, 0, Math.max(items.length - 1, 0)) : internalIndex;
|
|
476
|
+
const [isAnimating, setIsAnimating] = useState3(false);
|
|
477
|
+
const [viewport, setViewport] = useState3(null);
|
|
478
|
+
const sourceRef = useRef3("snap");
|
|
479
|
+
const dragStartScrollRef = useRef3(0);
|
|
480
|
+
const isNavigatingRef = useRef3(false);
|
|
481
|
+
useEffect4(() => {
|
|
482
|
+
const clamped = clamp(index, 0, Math.max(items.length - 1, 0));
|
|
483
|
+
if (!isControlled)
|
|
484
|
+
setInternalIndex(clamped);
|
|
485
|
+
}, [index, isControlled, items.length]);
|
|
486
|
+
const virtualizer = useVirtualizer({
|
|
487
|
+
items,
|
|
488
|
+
virtual: virtualCfg,
|
|
489
|
+
getScrollElement: useCallback5(() => viewport, [viewport]),
|
|
490
|
+
orientation
|
|
491
|
+
});
|
|
492
|
+
const latest = useRef3({
|
|
493
|
+
index,
|
|
494
|
+
items,
|
|
495
|
+
isControlled,
|
|
496
|
+
options,
|
|
497
|
+
virtualizer,
|
|
498
|
+
loop,
|
|
499
|
+
orientation,
|
|
500
|
+
setInternalIndex,
|
|
501
|
+
setIsAnimating,
|
|
502
|
+
viewport
|
|
503
|
+
});
|
|
504
|
+
latest.current = {
|
|
505
|
+
index,
|
|
506
|
+
items,
|
|
507
|
+
isControlled,
|
|
508
|
+
options,
|
|
509
|
+
virtualizer,
|
|
510
|
+
loop,
|
|
511
|
+
orientation,
|
|
512
|
+
setInternalIndex,
|
|
513
|
+
setIsAnimating,
|
|
514
|
+
viewport
|
|
515
|
+
};
|
|
516
|
+
const applyIndexChange = useCallback5((nextIndex) => {
|
|
517
|
+
const { index: index2, isControlled: isControlled2, options: options2, setInternalIndex: setInternalIndex2, setIsAnimating: setIsAnimating2 } = latest.current;
|
|
518
|
+
if (nextIndex === index2)
|
|
519
|
+
return;
|
|
520
|
+
if (!isControlled2)
|
|
521
|
+
setInternalIndex2(nextIndex);
|
|
522
|
+
options2.onIndexChange?.(nextIndex, sourceRef.current ?? "snap");
|
|
523
|
+
setIsAnimating2(false);
|
|
524
|
+
sourceRef.current = "snap";
|
|
525
|
+
}, []);
|
|
526
|
+
const navigateTo = useCallback5((targetIndex, source, behavior) => {
|
|
527
|
+
const { items: items2, loop: loop2, index: index2, isControlled: isControlled2, options: options2, orientation: orientation2, virtualizer: virtualizer2, setInternalIndex: setInternalIndex2, viewport: viewport2 } = latest.current;
|
|
528
|
+
if (items2.length === 0)
|
|
529
|
+
return;
|
|
530
|
+
const maxIndex = items2.length - 1;
|
|
531
|
+
let next2 = targetIndex;
|
|
532
|
+
if (loop2) {
|
|
533
|
+
next2 = (next2 + items2.length) % items2.length;
|
|
534
|
+
} else {
|
|
535
|
+
next2 = clamp(next2, 0, maxIndex);
|
|
536
|
+
}
|
|
537
|
+
if (next2 === index2 && source !== "snap")
|
|
538
|
+
return;
|
|
539
|
+
sourceRef.current = source;
|
|
540
|
+
isNavigatingRef.current = true;
|
|
541
|
+
if (!isControlled2)
|
|
542
|
+
setInternalIndex2(next2);
|
|
543
|
+
options2.onIndexChange?.(next2, source);
|
|
544
|
+
if (!viewport2)
|
|
545
|
+
return;
|
|
546
|
+
const originalSnapType = viewport2.style.scrollSnapType;
|
|
547
|
+
viewport2.style.scrollSnapType = "none";
|
|
548
|
+
viewport2.style.scrollBehavior = "auto";
|
|
549
|
+
const targetBehavior = behavior ?? (prefersReducedMotion() ? "instant" : "smooth");
|
|
550
|
+
const targetScroll = next2 * virtualizer2.containerSize;
|
|
551
|
+
if (orientation2 === "vertical") {
|
|
552
|
+
viewport2.scrollTo({ top: targetScroll, behavior: targetBehavior });
|
|
553
|
+
} else {
|
|
554
|
+
viewport2.scrollTo({ left: targetScroll, behavior: targetBehavior });
|
|
555
|
+
}
|
|
556
|
+
setTimeout(() => {
|
|
557
|
+
isNavigatingRef.current = false;
|
|
558
|
+
viewport2.style.scrollSnapType = originalSnapType || (orientation2 === "vertical" ? "y mandatory" : "x mandatory");
|
|
559
|
+
}, targetBehavior === "smooth" ? 600 : 50);
|
|
560
|
+
}, []);
|
|
561
|
+
const onDragStart = useCallback5(() => {
|
|
562
|
+
const { viewport: viewport2, orientation: orientation2 } = latest.current;
|
|
563
|
+
if (viewport2) {
|
|
564
|
+
viewport2.style.scrollBehavior = "auto";
|
|
565
|
+
dragStartScrollRef.current = orientation2 === "vertical" ? viewport2.scrollTop : viewport2.scrollLeft;
|
|
566
|
+
}
|
|
567
|
+
}, []);
|
|
568
|
+
const onDrag = useCallback5((delta) => {
|
|
569
|
+
const { viewport: viewport2, orientation: orientation2 } = latest.current;
|
|
570
|
+
if (viewport2) {
|
|
571
|
+
const newScroll = dragStartScrollRef.current - delta;
|
|
572
|
+
if (orientation2 === "vertical") {
|
|
573
|
+
viewport2.scrollTop = newScroll;
|
|
574
|
+
} else {
|
|
575
|
+
viewport2.scrollLeft = newScroll;
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
}, []);
|
|
579
|
+
const wheel = useWheel({
|
|
580
|
+
orientation,
|
|
581
|
+
direction,
|
|
582
|
+
discretePaging: wheelCfg.discretePaging ?? true,
|
|
583
|
+
threshold: wheelCfg.threshold ?? 80,
|
|
584
|
+
debounce: wheelCfg.debounce ?? 120,
|
|
585
|
+
cooldown: wheelCfg.cooldown ?? 400,
|
|
586
|
+
onRequestIndexChange: (delta) => navigateTo(latest.current.index + delta, "user:wheel"),
|
|
587
|
+
viewport,
|
|
588
|
+
onDragStart,
|
|
589
|
+
onDrag,
|
|
590
|
+
onDragEnd: () => navigateTo(latest.current.index, "snap")
|
|
591
|
+
});
|
|
592
|
+
const gestures = useGestures({
|
|
593
|
+
orientation,
|
|
594
|
+
direction,
|
|
595
|
+
threshold: gestureCfg.threshold ?? 50,
|
|
596
|
+
flickVelocity: gestureCfg.flickVelocity ?? 0.5,
|
|
597
|
+
lockAxis: gestureCfg.lockAxis ?? true,
|
|
598
|
+
ignoreWhileAnimating: gestureCfg.ignoreWhileAnimating ?? true,
|
|
599
|
+
loop,
|
|
600
|
+
getIndex: () => latest.current.index,
|
|
601
|
+
maxIndex: Math.max(items.length - 1, 0),
|
|
602
|
+
onRequestIndexChange: (next2) => navigateTo(next2, "user:gesture"),
|
|
603
|
+
setAnimating: setIsAnimating,
|
|
604
|
+
isAnimating,
|
|
605
|
+
onDragStart,
|
|
606
|
+
onDrag
|
|
607
|
+
});
|
|
608
|
+
const keyboard = useKeyboard({
|
|
609
|
+
orientation,
|
|
610
|
+
direction,
|
|
611
|
+
config: keyboardNavigation ? keyboardCfg : { enabled: false },
|
|
612
|
+
onPrev: () => navigateTo(latest.current.index - 1, "user:keyboard"),
|
|
613
|
+
onNext: () => navigateTo(latest.current.index + 1, "user:keyboard"),
|
|
614
|
+
onFirst: () => navigateTo(0, "user:keyboard"),
|
|
615
|
+
onLast: () => navigateTo(items.length - 1, "user:keyboard")
|
|
616
|
+
});
|
|
617
|
+
const rafId = useRef3(null);
|
|
618
|
+
const scrollEndTimeoutRef = useRef3(null);
|
|
619
|
+
const handleScroll = useCallback5(() => {
|
|
620
|
+
if (isNavigatingRef.current)
|
|
621
|
+
return;
|
|
622
|
+
if (scrollEndTimeoutRef.current)
|
|
623
|
+
clearTimeout(scrollEndTimeoutRef.current);
|
|
624
|
+
scrollEndTimeoutRef.current = setTimeout(() => {
|
|
625
|
+
if (isNavigatingRef.current)
|
|
626
|
+
return;
|
|
627
|
+
const { viewport: viewport2, orientation: orientation2, index: index2 } = latest.current;
|
|
628
|
+
if (!viewport2)
|
|
629
|
+
return;
|
|
630
|
+
const scrollOffset = orientation2 === "vertical" ? viewport2.scrollTop : viewport2.scrollLeft;
|
|
631
|
+
const computedSize = orientation2 === "vertical" ? viewport2.clientHeight : viewport2.clientWidth;
|
|
632
|
+
const idealOffset = index2 * computedSize;
|
|
633
|
+
if (Math.abs(scrollOffset - idealOffset) > 2) {
|
|
634
|
+
viewport2.scrollTo({
|
|
635
|
+
top: orientation2 === "vertical" ? idealOffset : 0,
|
|
636
|
+
left: orientation2 === "horizontal" ? idealOffset : 0,
|
|
637
|
+
behavior: "smooth"
|
|
638
|
+
});
|
|
639
|
+
}
|
|
640
|
+
}, 150);
|
|
641
|
+
if (rafId.current !== null)
|
|
642
|
+
return;
|
|
643
|
+
rafId.current = requestAnimationFrame(() => {
|
|
644
|
+
rafId.current = null;
|
|
645
|
+
if (isNavigatingRef.current)
|
|
646
|
+
return;
|
|
647
|
+
const { viewport: viewport2, orientation: orientation2, items: items2 } = latest.current;
|
|
648
|
+
if (!viewport2)
|
|
649
|
+
return;
|
|
650
|
+
const scrollOffset = orientation2 === "vertical" ? viewport2.scrollTop : viewport2.scrollLeft;
|
|
651
|
+
const viewportSize = orientation2 === "vertical" ? viewport2.clientHeight : viewport2.clientWidth;
|
|
652
|
+
if (viewportSize > 0 && items2.length > 0) {
|
|
653
|
+
const estimatedIndex = Math.round(scrollOffset / viewportSize);
|
|
654
|
+
const newIndex = clamp(estimatedIndex, 0, items2.length - 1);
|
|
655
|
+
applyIndexChange(newIndex);
|
|
656
|
+
}
|
|
657
|
+
});
|
|
658
|
+
}, [applyIndexChange]);
|
|
659
|
+
useEffect4(() => {
|
|
660
|
+
if (items.length === 0)
|
|
661
|
+
return;
|
|
662
|
+
const maxIndex = items.length - 1;
|
|
663
|
+
const distanceFromEnd = maxIndex - index;
|
|
664
|
+
if (distanceFromEnd <= endReachedThreshold) {
|
|
665
|
+
options.onEndReached?.({ distanceFromEnd, direction: "forward" });
|
|
666
|
+
}
|
|
667
|
+
if (index <= endReachedThreshold) {
|
|
668
|
+
options.onEndReached?.({ distanceFromEnd: index, direction: "backward" });
|
|
669
|
+
}
|
|
670
|
+
}, [endReachedThreshold, index, items.length, options]);
|
|
671
|
+
const scrollBehavior = useMemo3(() => prefersReducedMotion() ? "instant" : "smooth", []);
|
|
672
|
+
const scrollTo = useCallback5((target, opts) => {
|
|
673
|
+
navigateTo(target, "programmatic", opts?.behavior ?? scrollBehavior);
|
|
674
|
+
}, [navigateTo, scrollBehavior]);
|
|
675
|
+
const apiState = useMemo3(() => ({
|
|
676
|
+
index,
|
|
677
|
+
isAnimating,
|
|
678
|
+
canPrev: loop || index > 0,
|
|
679
|
+
canNext: loop || index < items.length - 1
|
|
680
|
+
}), [index, isAnimating, items.length, loop]);
|
|
681
|
+
const resizeTimeoutRef = useRef3(null);
|
|
682
|
+
useEffect4(() => {
|
|
683
|
+
if (!viewport)
|
|
684
|
+
return;
|
|
685
|
+
const handleResize = () => {
|
|
686
|
+
if (resizeTimeoutRef.current) {
|
|
687
|
+
clearTimeout(resizeTimeoutRef.current);
|
|
688
|
+
}
|
|
689
|
+
resizeTimeoutRef.current = setTimeout(() => {
|
|
690
|
+
const { orientation: orientation2, index: currentIndex } = latest.current;
|
|
691
|
+
if (!viewport)
|
|
692
|
+
return;
|
|
693
|
+
const size = orientation2 === "vertical" ? viewport.clientHeight : viewport.clientWidth;
|
|
694
|
+
const targetScroll = currentIndex * size;
|
|
695
|
+
const currentScroll = orientation2 === "vertical" ? viewport.scrollTop : viewport.scrollLeft;
|
|
696
|
+
if (Math.abs(currentScroll - targetScroll) > size * 0.1) {
|
|
697
|
+
if (orientation2 === "vertical") {
|
|
698
|
+
viewport.scrollTop = targetScroll;
|
|
699
|
+
} else {
|
|
700
|
+
viewport.scrollLeft = targetScroll;
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
}, 50);
|
|
704
|
+
};
|
|
705
|
+
const resizeObserver = new ResizeObserver(handleResize);
|
|
706
|
+
resizeObserver.observe(viewport);
|
|
707
|
+
return () => {
|
|
708
|
+
resizeObserver.disconnect();
|
|
709
|
+
if (resizeTimeoutRef.current) {
|
|
710
|
+
clearTimeout(resizeTimeoutRef.current);
|
|
711
|
+
}
|
|
712
|
+
};
|
|
713
|
+
}, [viewport]);
|
|
714
|
+
const handleTouchStart = useCallback5(() => {
|
|
715
|
+
const { viewport: viewport2, orientation: orientation2 } = latest.current;
|
|
716
|
+
if (viewport2) {
|
|
717
|
+
viewport2.style.scrollSnapType = orientation2 === "vertical" ? "y mandatory" : "x mandatory";
|
|
718
|
+
}
|
|
719
|
+
}, []);
|
|
720
|
+
const getViewportProps = useCallback5(() => {
|
|
721
|
+
const virtualStyles = {
|
|
722
|
+
position: "relative",
|
|
723
|
+
overflow: "auto",
|
|
724
|
+
scrollSnapType: orientation === "vertical" ? "y mandatory" : "x mandatory",
|
|
725
|
+
touchAction: orientation === "vertical" ? "pan-y" : "pan-x",
|
|
726
|
+
willChange: "scroll-position"
|
|
727
|
+
};
|
|
728
|
+
return {
|
|
729
|
+
ref: setViewport,
|
|
730
|
+
role: "feed",
|
|
731
|
+
"aria-label": ariaLabel,
|
|
732
|
+
"aria-busy": isAnimating,
|
|
733
|
+
tabIndex: 0,
|
|
734
|
+
onScroll: handleScroll,
|
|
735
|
+
onKeyDown: keyboard.onKeyDown,
|
|
736
|
+
onTouchStart: handleTouchStart,
|
|
737
|
+
...gestures.handlers,
|
|
738
|
+
style: virtualStyles
|
|
739
|
+
};
|
|
740
|
+
}, [ariaLabel, handleScroll, handleTouchStart, isAnimating, keyboard.onKeyDown, orientation, gestures.handlers]);
|
|
741
|
+
const getItemProps = useCallback5((itemIndex) => {
|
|
742
|
+
const isActive = index === itemIndex;
|
|
743
|
+
const sizeUnit = orientation === "vertical" ? "100dvh" : "100dvw";
|
|
744
|
+
return {
|
|
745
|
+
ref: undefined,
|
|
746
|
+
"data-index": itemIndex,
|
|
747
|
+
"data-active": isActive,
|
|
748
|
+
style: {
|
|
749
|
+
position: "absolute",
|
|
750
|
+
top: 0,
|
|
751
|
+
left: 0,
|
|
752
|
+
transform: orientation === "vertical" ? `translateY(calc(${itemIndex} * ${sizeUnit}))` : `translateX(calc(${itemIndex} * ${sizeUnit}))`,
|
|
753
|
+
height: orientation === "vertical" ? sizeUnit : "100%",
|
|
754
|
+
width: orientation === "horizontal" ? sizeUnit : "100%",
|
|
755
|
+
willChange: "transform",
|
|
756
|
+
scrollSnapAlign: "start",
|
|
757
|
+
scrollSnapStop: "always"
|
|
758
|
+
}
|
|
759
|
+
};
|
|
760
|
+
}, [index, orientation]);
|
|
761
|
+
const prev = useCallback5(() => navigateTo(latest.current.index - 1, "user:keyboard"), [navigateTo]);
|
|
762
|
+
const next = useCallback5(() => navigateTo(latest.current.index + 1, "user:keyboard"), [navigateTo]);
|
|
763
|
+
const api = useMemo3(() => ({
|
|
764
|
+
...apiState,
|
|
765
|
+
prev,
|
|
766
|
+
next,
|
|
767
|
+
scrollTo: (target, opts) => navigateTo(target, "programmatic", opts?.behavior),
|
|
768
|
+
getViewportProps,
|
|
769
|
+
getItemProps,
|
|
770
|
+
virtualItems: virtualizer.virtualItems,
|
|
771
|
+
totalSize: virtualizer.totalSize,
|
|
772
|
+
items,
|
|
773
|
+
orientation,
|
|
774
|
+
viewport
|
|
775
|
+
}), [apiState, prev, next, navigateTo, getViewportProps, getItemProps, virtualizer.virtualItems, virtualizer.totalSize, items, orientation, viewport]);
|
|
776
|
+
return api;
|
|
777
|
+
}
|
|
778
|
+
// src/hooks/useWindowSize.ts
|
|
779
|
+
import { useState as useState4, useEffect as useEffect5 } from "react";
|
|
780
|
+
function useWindowSize() {
|
|
781
|
+
const [size, setSize] = useState4({
|
|
782
|
+
width: typeof window !== "undefined" ? window.innerWidth : 0,
|
|
783
|
+
height: typeof window !== "undefined" ? window.innerHeight : 0
|
|
784
|
+
});
|
|
785
|
+
useEffect5(() => {
|
|
786
|
+
if (typeof window === "undefined")
|
|
787
|
+
return;
|
|
788
|
+
let timeoutId;
|
|
789
|
+
const handleResize = () => {
|
|
790
|
+
clearTimeout(timeoutId);
|
|
791
|
+
timeoutId = setTimeout(() => {
|
|
792
|
+
setSize({ width: window.innerWidth, height: window.innerHeight });
|
|
793
|
+
}, 100);
|
|
794
|
+
};
|
|
795
|
+
window.addEventListener("resize", handleResize);
|
|
796
|
+
return () => {
|
|
797
|
+
window.removeEventListener("resize", handleResize);
|
|
798
|
+
clearTimeout(timeoutId);
|
|
799
|
+
};
|
|
800
|
+
}, []);
|
|
801
|
+
return size;
|
|
802
|
+
}
|
|
803
|
+
// src/hooks/useSwipeItem.ts
|
|
804
|
+
import { createContext, useContext } from "react";
|
|
805
|
+
var SwipeItemContext = createContext(undefined);
|
|
806
|
+
function useSwipeItem() {
|
|
807
|
+
const context = useContext(SwipeItemContext);
|
|
808
|
+
if (!context) {
|
|
809
|
+
throw new Error("useSwipeItem must be used within a SwipeDeck item");
|
|
810
|
+
}
|
|
811
|
+
return context;
|
|
812
|
+
}
|
|
813
|
+
// src/components/SwipeDeck.tsx
|
|
814
|
+
import { useImperativeHandle, useMemo as useMemo4 } from "react";
|
|
815
|
+
import { jsxDEV } from "react/jsx-dev-runtime";
|
|
816
|
+
function SwipeDeck(props) {
|
|
817
|
+
const {
|
|
818
|
+
as: As = "div",
|
|
819
|
+
children,
|
|
820
|
+
className,
|
|
821
|
+
style,
|
|
822
|
+
ref,
|
|
823
|
+
...options
|
|
824
|
+
} = props;
|
|
825
|
+
const deck = useSwipeDeck(options);
|
|
826
|
+
useImperativeHandle(ref, () => ({
|
|
827
|
+
prev: deck.prev,
|
|
828
|
+
next: deck.next,
|
|
829
|
+
scrollTo: deck.scrollTo,
|
|
830
|
+
getState: () => ({
|
|
831
|
+
index: deck.index,
|
|
832
|
+
isAnimating: deck.isAnimating,
|
|
833
|
+
canPrev: deck.canPrev,
|
|
834
|
+
canNext: deck.canNext
|
|
835
|
+
}),
|
|
836
|
+
viewport: deck.viewport
|
|
837
|
+
}), [deck]);
|
|
838
|
+
const { style: virtualStyle, ...restViewportProps } = deck.getViewportProps();
|
|
839
|
+
const fullscreenStyle = props.fullscreen ? {
|
|
840
|
+
height: "100dvh",
|
|
841
|
+
width: "100%",
|
|
842
|
+
position: "absolute",
|
|
843
|
+
top: 0,
|
|
844
|
+
left: 0
|
|
845
|
+
} : {};
|
|
846
|
+
const viewportStyle = useMemo4(() => ({ ...virtualStyle, ...fullscreenStyle, ...style }), [virtualStyle, fullscreenStyle, style]);
|
|
847
|
+
const contentStyle = useMemo4(() => ({
|
|
848
|
+
height: deck.orientation === "vertical" ? `calc(${deck.items.length} * 100dvh)` : "100%",
|
|
849
|
+
width: deck.orientation === "horizontal" ? `calc(${deck.items.length} * 100dvw)` : "100%",
|
|
850
|
+
position: "relative"
|
|
851
|
+
}), [deck.orientation, deck.items.length]);
|
|
852
|
+
const Comp = As;
|
|
853
|
+
return /* @__PURE__ */ jsxDEV(Comp, {
|
|
854
|
+
...restViewportProps,
|
|
855
|
+
className,
|
|
856
|
+
style: viewportStyle,
|
|
857
|
+
children: /* @__PURE__ */ jsxDEV("div", {
|
|
858
|
+
style: contentStyle,
|
|
859
|
+
children: deck.items.map((item, itemIndex) => {
|
|
860
|
+
const itemProps = deck.getItemProps(itemIndex);
|
|
861
|
+
const preload = options.preload ?? 0;
|
|
862
|
+
const preloadPrevious = options.preloadPrevious ?? 0;
|
|
863
|
+
let shouldPreload = false;
|
|
864
|
+
if (preload > 0 || preloadPrevious > 0) {
|
|
865
|
+
const currentIndex = deck.index;
|
|
866
|
+
if (!options.loop) {
|
|
867
|
+
const isNext = itemIndex > currentIndex && itemIndex <= currentIndex + preload;
|
|
868
|
+
const isPrev = itemIndex < currentIndex && itemIndex >= currentIndex - preloadPrevious;
|
|
869
|
+
shouldPreload = isNext || isPrev;
|
|
870
|
+
} else {
|
|
871
|
+
const len = deck.items.length;
|
|
872
|
+
const forwardDist = (itemIndex - currentIndex + len) % len;
|
|
873
|
+
const backwardDist = (currentIndex - itemIndex + len) % len;
|
|
874
|
+
const isNext = forwardDist > 0 && forwardDist <= preload;
|
|
875
|
+
const isPrev = backwardDist > 0 && backwardDist <= preloadPrevious;
|
|
876
|
+
shouldPreload = isNext || isPrev;
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
const contextValue = {
|
|
880
|
+
item,
|
|
881
|
+
index: itemIndex,
|
|
882
|
+
isActive: deck.index === itemIndex,
|
|
883
|
+
shouldPreload,
|
|
884
|
+
props: itemProps
|
|
885
|
+
};
|
|
886
|
+
const itemKey = options.virtual?.getItemKey?.(item, itemIndex) ?? itemIndex;
|
|
887
|
+
return /* @__PURE__ */ jsxDEV("article", {
|
|
888
|
+
...itemProps,
|
|
889
|
+
"aria-label": `Item ${itemIndex + 1} of ${deck.items.length}`,
|
|
890
|
+
children: /* @__PURE__ */ jsxDEV(SwipeItemContext.Provider, {
|
|
891
|
+
value: contextValue,
|
|
892
|
+
children: children(contextValue)
|
|
893
|
+
}, undefined, false, undefined, this)
|
|
894
|
+
}, itemKey, false, undefined, this);
|
|
895
|
+
})
|
|
896
|
+
}, undefined, false, undefined, this)
|
|
897
|
+
}, undefined, false, undefined, this);
|
|
898
|
+
}
|
|
899
|
+
export {
|
|
900
|
+
useWindowSize,
|
|
901
|
+
useSwipeItem,
|
|
902
|
+
useSwipeDeck,
|
|
903
|
+
SwipeDeck
|
|
904
|
+
};
|
|
905
|
+
|
|
906
|
+
//# debugId=081A8E066DB65B9364756E2164756E21
|
|
907
|
+
//# sourceMappingURL=index.js.map
|