react-native-reanimated-carousel 2.3.10 → 2.5.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/lib/commonjs/Carousel.js +1 -1
- package/lib/commonjs/Carousel.js.map +1 -1
- package/lib/commonjs/ScrollViewGesture.js +1 -1
- package/lib/commonjs/ScrollViewGesture.js.map +1 -1
- package/lib/commonjs/hooks/useCarouselController.js +1 -1
- package/lib/commonjs/hooks/useCarouselController.js.map +1 -1
- package/lib/commonjs/hooks/useCommonVariables.js +1 -1
- package/lib/commonjs/hooks/useCommonVariables.js.map +1 -1
- package/lib/commonjs/hooks/useOffsetX.js +1 -1
- package/lib/commonjs/hooks/useOffsetX.js.map +1 -1
- package/lib/commonjs/hooks/useOnProgressChange.js +1 -1
- package/lib/commonjs/hooks/useOnProgressChange.js.map +1 -1
- package/lib/commonjs/hooks/useVisibleRanges.js +1 -1
- package/lib/commonjs/hooks/useVisibleRanges.js.map +1 -1
- package/lib/commonjs/layouts/BaseLayout.js +1 -1
- package/lib/commonjs/layouts/BaseLayout.js.map +1 -1
- package/lib/commonjs/layouts/ParallaxLayout.js +1 -1
- package/lib/commonjs/layouts/ParallaxLayout.js.map +1 -1
- package/lib/commonjs/layouts/normal.js +1 -1
- package/lib/commonjs/layouts/normal.js.map +1 -1
- package/lib/commonjs/layouts/parallax.js +1 -1
- package/lib/commonjs/layouts/parallax.js.map +1 -1
- package/lib/commonjs/layouts/stack.js +1 -1
- package/lib/commonjs/layouts/stack.js.map +1 -1
- package/lib/commonjs/utils/computedWithAutoFillData.js +1 -1
- package/lib/commonjs/utils/computedWithAutoFillData.js.map +1 -1
- package/lib/commonjs/utils/dealWithAnimation.js +1 -1
- package/lib/commonjs/utils/dealWithAnimation.js.map +1 -1
- package/lib/commonjs/utils/log.js +1 -1
- package/lib/commonjs/utils/log.js.map +1 -1
- package/lib/module/Carousel.js +14 -20
- package/lib/module/Carousel.js.map +1 -1
- package/lib/module/hooks/useCarouselController.js +50 -25
- package/lib/module/hooks/useCarouselController.js.map +1 -1
- package/lib/module/hooks/useCommonVariables.js +2 -3
- package/lib/module/hooks/useCommonVariables.js.map +1 -1
- package/lib/module/utils/log.js +5 -0
- package/lib/module/utils/log.js.map +1 -1
- package/lib/typescript/hooks/useCarouselController.d.ts +1 -3
- package/lib/typescript/types.d.ts +6 -10
- package/lib/typescript/utils/log.d.ts +1 -0
- package/package.json +1 -1
- package/src/Carousel.tsx +23 -33
- package/src/hooks/useCarouselController.tsx +53 -31
- package/src/hooks/useCommonVariables.ts +2 -2
- package/src/types.ts +6 -10
- package/src/utils/log.ts +5 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React from 'react';
|
|
1
|
+
import React, { useRef } from 'react';
|
|
2
2
|
import type Animated from 'react-native-reanimated';
|
|
3
3
|
import { Easing } from '../constants';
|
|
4
4
|
import {
|
|
@@ -13,6 +13,7 @@ import type {
|
|
|
13
13
|
} from '../types';
|
|
14
14
|
import { dealWithAnimation } from '@/utils/dealWithAnimation';
|
|
15
15
|
import { convertToSharedIndex } from '@/utils/computedWithAutoFillData';
|
|
16
|
+
import { round } from '@/utils/log';
|
|
16
17
|
|
|
17
18
|
interface IOpts {
|
|
18
19
|
loop: boolean;
|
|
@@ -28,12 +29,10 @@ interface IOpts {
|
|
|
28
29
|
}
|
|
29
30
|
|
|
30
31
|
export interface ICarouselController {
|
|
31
|
-
|
|
32
|
-
sharedPreIndex: Animated.SharedValue<number>;
|
|
32
|
+
getSharedIndex: () => number;
|
|
33
33
|
prev: (opts?: TCarouselActionOptions) => void;
|
|
34
34
|
next: (opts?: TCarouselActionOptions) => void;
|
|
35
35
|
getCurrentIndex: () => number;
|
|
36
|
-
to: (index: number, animated?: boolean) => void;
|
|
37
36
|
scrollTo: (opts?: TCarouselActionOptions) => void;
|
|
38
37
|
}
|
|
39
38
|
|
|
@@ -60,8 +59,8 @@ export function useCarouselController(options: IOpts): ICarouselController {
|
|
|
60
59
|
|
|
61
60
|
const index = useSharedValue<number>(defaultIndex);
|
|
62
61
|
// The Index displayed to the user
|
|
63
|
-
const sharedIndex =
|
|
64
|
-
const sharedPreIndex =
|
|
62
|
+
const sharedIndex = useRef<number>(defaultIndex);
|
|
63
|
+
const sharedPreIndex = useRef<number>(defaultIndex);
|
|
65
64
|
|
|
66
65
|
const currentFixedPage = React.useCallback(() => {
|
|
67
66
|
if (loop) {
|
|
@@ -76,30 +75,47 @@ export function useCarouselController(options: IOpts): ICarouselController {
|
|
|
76
75
|
);
|
|
77
76
|
}, [handlerOffsetX, dataInfo, size, loop]);
|
|
78
77
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
78
|
+
function setSharedIndex(newSharedIndex: number) {
|
|
79
|
+
sharedIndex.current = newSharedIndex;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
useAnimatedReaction(
|
|
83
|
+
() => {
|
|
84
|
+
const handlerOffsetXValue = handlerOffsetX.value;
|
|
85
|
+
const toInt = round(handlerOffsetXValue / size) % dataInfo.length;
|
|
84
86
|
const isPositive = handlerOffsetXValue <= 0;
|
|
85
87
|
const i = isPositive
|
|
86
88
|
? Math.abs(toInt)
|
|
87
89
|
: Math.abs(toInt > 0 ? dataInfo.length - toInt : 0);
|
|
88
|
-
|
|
89
|
-
|
|
90
|
+
|
|
91
|
+
const newSharedIndexValue = convertToSharedIndex({
|
|
90
92
|
loop,
|
|
91
93
|
rawDataLength: dataInfo.originalLength,
|
|
92
94
|
autoFillData: autoFillData!,
|
|
93
95
|
index: i,
|
|
94
96
|
});
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
i,
|
|
100
|
+
newSharedIndexValue,
|
|
101
|
+
};
|
|
95
102
|
},
|
|
96
|
-
|
|
103
|
+
({ i, newSharedIndexValue }) => {
|
|
104
|
+
index.value = i;
|
|
105
|
+
runOnJS(setSharedIndex)(newSharedIndexValue);
|
|
106
|
+
},
|
|
107
|
+
[
|
|
108
|
+
sharedPreIndex,
|
|
109
|
+
sharedIndex,
|
|
110
|
+
size,
|
|
111
|
+
dataInfo,
|
|
112
|
+
index,
|
|
113
|
+
loop,
|
|
114
|
+
autoFillData,
|
|
115
|
+
handlerOffsetX,
|
|
116
|
+
]
|
|
97
117
|
);
|
|
98
118
|
|
|
99
|
-
useAnimatedReaction(() => handlerOffsetX.value, computedIndex, [
|
|
100
|
-
handlerOffsetX,
|
|
101
|
-
]);
|
|
102
|
-
|
|
103
119
|
const getCurrentIndex = React.useCallback(() => {
|
|
104
120
|
return index.value;
|
|
105
121
|
}, [index]);
|
|
@@ -208,21 +224,22 @@ export function useCarouselController(options: IOpts): ICarouselController {
|
|
|
208
224
|
);
|
|
209
225
|
|
|
210
226
|
const to = React.useCallback(
|
|
211
|
-
(
|
|
212
|
-
|
|
227
|
+
(opts: { i: number; animated: boolean; onFinished?: () => void }) => {
|
|
228
|
+
const { i, animated = false, onFinished } = opts;
|
|
229
|
+
if (i === index.value) return;
|
|
213
230
|
if (!canSliding()) return;
|
|
214
231
|
|
|
215
232
|
onScrollBegin?.();
|
|
216
233
|
|
|
217
|
-
const offset = handlerOffsetX.value + (index.value -
|
|
234
|
+
const offset = handlerOffsetX.value + (index.value - i) * size;
|
|
218
235
|
|
|
219
236
|
if (animated) {
|
|
220
|
-
index.value =
|
|
221
|
-
handlerOffsetX.value = scrollWithTiming(offset);
|
|
237
|
+
index.value = i;
|
|
238
|
+
handlerOffsetX.value = scrollWithTiming(offset, onFinished);
|
|
222
239
|
} else {
|
|
223
240
|
handlerOffsetX.value = offset;
|
|
224
|
-
index.value =
|
|
225
|
-
|
|
241
|
+
index.value = i;
|
|
242
|
+
onFinished?.();
|
|
226
243
|
}
|
|
227
244
|
},
|
|
228
245
|
[
|
|
@@ -232,33 +249,38 @@ export function useCarouselController(options: IOpts): ICarouselController {
|
|
|
232
249
|
handlerOffsetX,
|
|
233
250
|
size,
|
|
234
251
|
scrollWithTiming,
|
|
235
|
-
onScrollEnd,
|
|
236
252
|
]
|
|
237
253
|
);
|
|
238
254
|
|
|
239
255
|
const scrollTo = React.useCallback(
|
|
240
256
|
(opts: TCarouselActionOptions = {}) => {
|
|
241
|
-
const { count, animated = false, onFinished } = opts;
|
|
257
|
+
const { index: i, count, animated = false, onFinished } = opts;
|
|
258
|
+
|
|
259
|
+
if (i) {
|
|
260
|
+
to({ i, animated, onFinished });
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
|
|
242
264
|
if (!count) {
|
|
243
265
|
return;
|
|
244
266
|
}
|
|
267
|
+
|
|
245
268
|
const n = Math.round(count);
|
|
269
|
+
|
|
246
270
|
if (n < 0) {
|
|
247
271
|
prev({ count: Math.abs(n), animated, onFinished });
|
|
248
272
|
} else {
|
|
249
273
|
next({ count: n, animated, onFinished });
|
|
250
274
|
}
|
|
251
275
|
},
|
|
252
|
-
[prev, next]
|
|
276
|
+
[prev, next, to]
|
|
253
277
|
);
|
|
254
278
|
|
|
255
279
|
return {
|
|
256
|
-
sharedIndex,
|
|
257
|
-
sharedPreIndex,
|
|
258
|
-
to,
|
|
259
280
|
next,
|
|
260
281
|
prev,
|
|
261
282
|
scrollTo,
|
|
262
283
|
getCurrentIndex,
|
|
284
|
+
getSharedIndex: () => sharedIndex.current,
|
|
263
285
|
};
|
|
264
286
|
}
|
|
@@ -11,7 +11,7 @@ interface ICommonVariables {
|
|
|
11
11
|
export function useCommonVariables(
|
|
12
12
|
props: TInitializeCarouselProps<any>
|
|
13
13
|
): ICommonVariables {
|
|
14
|
-
const { vertical, height, width, data, defaultIndex
|
|
14
|
+
const { vertical, height, width, data, defaultIndex } = props;
|
|
15
15
|
const size = vertical ? height : width;
|
|
16
16
|
const validLength = data.length - 1;
|
|
17
17
|
const defaultHandlerOffsetX = -Math.abs(defaultIndex * size);
|
|
@@ -19,7 +19,7 @@ export function useCommonVariables(
|
|
|
19
19
|
|
|
20
20
|
React.useEffect(() => {
|
|
21
21
|
handlerOffsetX.value = defaultHandlerOffsetX;
|
|
22
|
-
}, [vertical, handlerOffsetX, defaultHandlerOffsetX
|
|
22
|
+
}, [vertical, handlerOffsetX, defaultHandlerOffsetX]);
|
|
23
23
|
|
|
24
24
|
return {
|
|
25
25
|
size,
|
package/src/types.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ViewStyle } from 'react-native';
|
|
1
|
+
import type { StyleProp, ViewStyle } from 'react-native';
|
|
2
2
|
import type { PanGestureHandlerProps } from 'react-native-gesture-handler';
|
|
3
3
|
import type {
|
|
4
4
|
AnimatedStyleProp,
|
|
@@ -104,7 +104,7 @@ export type TCarouselProps<T = any> = {
|
|
|
104
104
|
/**
|
|
105
105
|
* Carousel container style
|
|
106
106
|
*/
|
|
107
|
-
style?: ViewStyle
|
|
107
|
+
style?: StyleProp<ViewStyle>;
|
|
108
108
|
/**
|
|
109
109
|
* PanGestureHandler props
|
|
110
110
|
*/
|
|
@@ -172,7 +172,7 @@ export type TCarouselProps<T = any> = {
|
|
|
172
172
|
/**
|
|
173
173
|
* On scroll end
|
|
174
174
|
*/
|
|
175
|
-
onScrollEnd?: (
|
|
175
|
+
onScrollEnd?: (index: number) => void;
|
|
176
176
|
/**
|
|
177
177
|
* On progress change
|
|
178
178
|
* @param offsetProgress Total of offset distance (0 390 780 ...)
|
|
@@ -189,21 +189,16 @@ export interface ICarouselInstance {
|
|
|
189
189
|
* Scroll to previous item, it takes one optional argument (count),
|
|
190
190
|
* which allows you to specify how many items to cross
|
|
191
191
|
*/
|
|
192
|
-
prev: (opts?: TCarouselActionOptions) => void;
|
|
192
|
+
prev: (opts?: Omit<TCarouselActionOptions, 'index'>) => void;
|
|
193
193
|
/**
|
|
194
194
|
* Scroll to next item, it takes one optional argument (count),
|
|
195
195
|
* which allows you to specify how many items to cross
|
|
196
196
|
*/
|
|
197
|
-
next: (opts?: TCarouselActionOptions) => void;
|
|
197
|
+
next: (opts?: Omit<TCarouselActionOptions, 'index'>) => void;
|
|
198
198
|
/**
|
|
199
199
|
* Get current item index
|
|
200
200
|
*/
|
|
201
201
|
getCurrentIndex: () => number;
|
|
202
|
-
/**
|
|
203
|
-
* Go to index
|
|
204
|
-
* @deprecated use scrollTo instead
|
|
205
|
-
*/
|
|
206
|
-
goToIndex: (index: number, animated?: boolean) => void;
|
|
207
202
|
/**
|
|
208
203
|
* Use value to scroll to a position where relative to the current position,
|
|
209
204
|
* scrollTo(-2) is equivalent to prev(2), scrollTo(2) is equivalent to next(2)
|
|
@@ -222,6 +217,7 @@ export type CarouselRenderItem<ItemT> = (
|
|
|
222
217
|
) => React.ReactElement;
|
|
223
218
|
|
|
224
219
|
export interface TCarouselActionOptions {
|
|
220
|
+
index?: number;
|
|
225
221
|
count?: number;
|
|
226
222
|
animated?: boolean;
|
|
227
223
|
onFinished?: () => void;
|