@r0b0t3d/react-native-carousel 3.4.8 → 3.4.9
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/package.json +1 -1
- package/src/components/Carousel.tsx +26 -20
package/package.json
CHANGED
|
@@ -5,7 +5,7 @@ import React, {
|
|
|
5
5
|
useMemo,
|
|
6
6
|
useRef,
|
|
7
7
|
} from 'react';
|
|
8
|
-
import {
|
|
8
|
+
import { StyleSheet, Platform, AppState, useWindowDimensions } from 'react-native';
|
|
9
9
|
import { useInterval } from '@r0b0t3d/react-native-hooks';
|
|
10
10
|
import Animated, {
|
|
11
11
|
useSharedValue,
|
|
@@ -56,10 +56,6 @@ enum CarouselState {
|
|
|
56
56
|
// All consumer-facing callbacks receive logical indices.
|
|
57
57
|
// ---------------------------------------------------------------------------
|
|
58
58
|
|
|
59
|
-
const getScreenWidth = () => {
|
|
60
|
-
return Dimensions.get('screen').width;
|
|
61
|
-
};
|
|
62
|
-
|
|
63
59
|
function Carousel<TData>({
|
|
64
60
|
style,
|
|
65
61
|
data,
|
|
@@ -69,8 +65,8 @@ function Carousel<TData>({
|
|
|
69
65
|
autoPlay = false,
|
|
70
66
|
duration = 1000,
|
|
71
67
|
animation,
|
|
72
|
-
sliderWidth
|
|
73
|
-
itemWidth
|
|
68
|
+
sliderWidth,
|
|
69
|
+
itemWidth,
|
|
74
70
|
firstItemAlignment = 'center',
|
|
75
71
|
inactiveOpacity = 1,
|
|
76
72
|
inactiveScale = 1,
|
|
@@ -84,6 +80,10 @@ function Carousel<TData>({
|
|
|
84
80
|
disableItemPress = false,
|
|
85
81
|
scrollViewRef: externalScrollViewRef,
|
|
86
82
|
}: CarouselProps<TData>) {
|
|
83
|
+
const { width: screenWidth } = useWindowDimensions();
|
|
84
|
+
const effectiveSliderWidth = sliderWidth ?? screenWidth;
|
|
85
|
+
const effectiveItemWidth = itemWidth ?? screenWidth;
|
|
86
|
+
|
|
87
87
|
// ---- shared values (UI-thread state) ------------------------------------
|
|
88
88
|
|
|
89
89
|
const currentRenderPage = useSharedValue(0);
|
|
@@ -107,18 +107,18 @@ function Carousel<TData>({
|
|
|
107
107
|
// ---- derived values -----------------------------------------------------
|
|
108
108
|
|
|
109
109
|
const horizontalPadding = useMemo(() => {
|
|
110
|
-
const padding = (
|
|
110
|
+
const padding = (effectiveSliderWidth - effectiveItemWidth) / 2;
|
|
111
111
|
return firstItemAlignment === 'center' || loop ? padding : spaceHeadTail;
|
|
112
|
-
}, [
|
|
112
|
+
}, [effectiveSliderWidth, effectiveItemWidth, firstItemAlignment, loop, spaceHeadTail]);
|
|
113
113
|
|
|
114
114
|
const offsets = useMemo(() => {
|
|
115
115
|
return generateOffsets({
|
|
116
|
-
sliderWidth,
|
|
117
|
-
itemWidth,
|
|
116
|
+
sliderWidth: effectiveSliderWidth,
|
|
117
|
+
itemWidth: effectiveItemWidth,
|
|
118
118
|
itemCount: data.length + (loop ? additionalPagesPerSide * 2 : 0),
|
|
119
119
|
horizontalPadding,
|
|
120
120
|
});
|
|
121
|
-
}, [
|
|
121
|
+
}, [effectiveSliderWidth, effectiveItemWidth, data.length, loop, additionalPagesPerSide, horizontalPadding]);
|
|
122
122
|
|
|
123
123
|
const pageItems = useMemo(() => {
|
|
124
124
|
if (!data || data.length === 0) {
|
|
@@ -135,7 +135,7 @@ function Carousel<TData>({
|
|
|
135
135
|
return data;
|
|
136
136
|
}, [data, loop, additionalPagesPerSide]);
|
|
137
137
|
|
|
138
|
-
|
|
138
|
+
// ---- page-change callback (JS thread) -----------------------------------
|
|
139
139
|
|
|
140
140
|
const handlePageChange = useCallback(
|
|
141
141
|
(logicalPage: number) => {
|
|
@@ -187,7 +187,7 @@ function Carousel<TData>({
|
|
|
187
187
|
if (safeLogicalIdx !== prevLogical) {
|
|
188
188
|
handlePageChange(safeLogicalIdx);
|
|
189
189
|
}
|
|
190
|
-
}, [data.length,
|
|
190
|
+
}, [data.length, effectiveSliderWidth, effectiveItemWidth, loop, additionalPagesPerSide, offsets.length, handlePageChange]);
|
|
191
191
|
|
|
192
192
|
// ---- app-state listener (pause autoplay when backgrounded) --------------
|
|
193
193
|
|
|
@@ -318,6 +318,12 @@ function Carousel<TData>({
|
|
|
318
318
|
|
|
319
319
|
// ---- scroll handler (UI thread worklet) ---------------------------------
|
|
320
320
|
|
|
321
|
+
// Snapshot as a primitive so the worklets below close over a number,
|
|
322
|
+
// not the `data` array itself — `data` items may contain non-shareable
|
|
323
|
+
// values (e.g. JSX elements), which Reanimated can't clone for the UI
|
|
324
|
+
// thread ([Worklets] Cannot copy value of type `FiberNode`).
|
|
325
|
+
const dataLength = data.length;
|
|
326
|
+
|
|
321
327
|
const scrollHandler = useAnimatedScrollHandler(
|
|
322
328
|
{
|
|
323
329
|
onScroll: (event) => {
|
|
@@ -374,7 +380,7 @@ function Carousel<TData>({
|
|
|
374
380
|
if (loop) {
|
|
375
381
|
const boundaryTarget = getLoopBoundaryTarget(
|
|
376
382
|
renderPage,
|
|
377
|
-
|
|
383
|
+
dataLength,
|
|
378
384
|
additionalPagesPerSide
|
|
379
385
|
);
|
|
380
386
|
if (boundaryTarget !== -1) {
|
|
@@ -390,7 +396,7 @@ function Carousel<TData>({
|
|
|
390
396
|
currentRenderPage.value = boundaryTarget;
|
|
391
397
|
currentLogicalPage.value = getLogicalPage(
|
|
392
398
|
boundaryTarget,
|
|
393
|
-
|
|
399
|
+
dataLength,
|
|
394
400
|
additionalPagesPerSide,
|
|
395
401
|
loop
|
|
396
402
|
);
|
|
@@ -404,7 +410,7 @@ function Carousel<TData>({
|
|
|
404
410
|
currentRenderPage.value = renderPage;
|
|
405
411
|
currentLogicalPage.value = getLogicalPage(
|
|
406
412
|
renderPage,
|
|
407
|
-
|
|
413
|
+
dataLength,
|
|
408
414
|
additionalPagesPerSide,
|
|
409
415
|
loop
|
|
410
416
|
);
|
|
@@ -416,7 +422,7 @@ function Carousel<TData>({
|
|
|
416
422
|
[
|
|
417
423
|
offsets,
|
|
418
424
|
loop,
|
|
419
|
-
|
|
425
|
+
dataLength,
|
|
420
426
|
additionalPagesPerSide,
|
|
421
427
|
handlePageChange,
|
|
422
428
|
internalAnimatedRef,
|
|
@@ -492,7 +498,7 @@ function Carousel<TData>({
|
|
|
492
498
|
item={item}
|
|
493
499
|
index={logicalIndex}
|
|
494
500
|
offset={offsets[i]}
|
|
495
|
-
itemWidth={
|
|
501
|
+
itemWidth={effectiveItemWidth}
|
|
496
502
|
animatedValue={animatedScroll}
|
|
497
503
|
animation={animation}
|
|
498
504
|
renderItem={renderItem}
|
|
@@ -510,7 +516,7 @@ function Carousel<TData>({
|
|
|
510
516
|
getItemKey,
|
|
511
517
|
containerStyles,
|
|
512
518
|
offsets,
|
|
513
|
-
|
|
519
|
+
effectiveItemWidth,
|
|
514
520
|
animatedScroll,
|
|
515
521
|
animation,
|
|
516
522
|
renderItem,
|