react-native-reanimated-carousel 3.0.6 → 3.1.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/CHANGELOG.md +7 -0
- package/lib/commonjs/Carousel.js +1 -1
- package/lib/commonjs/Carousel.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/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/module/Carousel.js +7 -7
- package/lib/module/Carousel.js.map +1 -1
- package/lib/module/hooks/useCarouselController.js +23 -23
- package/lib/module/hooks/useCarouselController.js.map +1 -1
- package/lib/module/hooks/useCommonVariables.js +10 -6
- package/lib/module/hooks/useCommonVariables.js.map +1 -1
- package/lib/module/hooks/useOffsetX.js +3 -3
- package/lib/module/hooks/useOffsetX.js.map +1 -1
- package/lib/module/layouts/BaseLayout.js +3 -3
- package/lib/module/layouts/BaseLayout.js.map +1 -1
- package/lib/module/layouts/ParallaxLayout.js +2 -2
- package/lib/module/layouts/ParallaxLayout.js.map +1 -1
- package/lib/typescript/hooks/useCarouselController.d.ts +1 -1
- package/lib/typescript/hooks/useCommonVariables.d.ts +1 -1
- package/lib/typescript/hooks/useOffsetX.d.ts +1 -1
- package/lib/typescript/layouts/BaseLayout.d.ts +1 -1
- package/lib/typescript/layouts/ParallaxLayout.d.ts +1 -1
- package/lib/typescript/types.d.ts +6 -2
- package/package.json +1 -1
- package/src/Carousel.tsx +7 -7
- package/src/hooks/useCarouselController.tsx +24 -27
- package/src/hooks/useCommonVariables.ts +15 -7
- package/src/hooks/useOffsetX.ts +4 -4
- package/src/layouts/BaseLayout.tsx +4 -4
- package/src/layouts/ParallaxLayout.tsx +3 -3
- package/src/types.ts +6 -1
package/src/Carousel.tsx
CHANGED
|
@@ -48,15 +48,15 @@ const Carousel = React.forwardRef<ICarouselInstance, TCarouselProps<any>>(
|
|
|
48
48
|
} = props;
|
|
49
49
|
|
|
50
50
|
const commonVariables = useCommonVariables(props);
|
|
51
|
-
const { size,
|
|
51
|
+
const { size, handlerOffset } = commonVariables;
|
|
52
52
|
const dataLength = data.length;
|
|
53
53
|
|
|
54
54
|
const offsetX = useDerivedValue(() => {
|
|
55
55
|
const totalSize = size * dataLength;
|
|
56
|
-
const x =
|
|
56
|
+
const x = handlerOffset.value % totalSize;
|
|
57
57
|
|
|
58
58
|
if (!loop) {
|
|
59
|
-
return
|
|
59
|
+
return handlerOffset.value;
|
|
60
60
|
}
|
|
61
61
|
return isNaN(x) ? 0 : x;
|
|
62
62
|
}, [loop, size, dataLength]);
|
|
@@ -76,7 +76,7 @@ const Carousel = React.forwardRef<ICarouselInstance, TCarouselProps<any>>(
|
|
|
76
76
|
size,
|
|
77
77
|
data,
|
|
78
78
|
autoFillData,
|
|
79
|
-
|
|
79
|
+
handlerOffset,
|
|
80
80
|
withAnimation,
|
|
81
81
|
defaultIndex,
|
|
82
82
|
onScrollEnd: () => runOnJS(_onScrollEnd)(),
|
|
@@ -151,7 +151,7 @@ const Carousel = React.forwardRef<ICarouselInstance, TCarouselProps<any>>(
|
|
|
151
151
|
const visibleRanges = useVisibleRanges({
|
|
152
152
|
total: data.length,
|
|
153
153
|
viewSize: size,
|
|
154
|
-
translation:
|
|
154
|
+
translation: handlerOffset,
|
|
155
155
|
windowSize,
|
|
156
156
|
});
|
|
157
157
|
|
|
@@ -170,7 +170,7 @@ const Carousel = React.forwardRef<ICarouselInstance, TCarouselProps<any>>(
|
|
|
170
170
|
<BaseLayout
|
|
171
171
|
key={i}
|
|
172
172
|
index={i}
|
|
173
|
-
|
|
173
|
+
handlerOffset={offsetX}
|
|
174
174
|
visibleRanges={visibleRanges}
|
|
175
175
|
animationStyle={customAnimation || layoutConfig}
|
|
176
176
|
>
|
|
@@ -201,7 +201,7 @@ const Carousel = React.forwardRef<ICarouselInstance, TCarouselProps<any>>(
|
|
|
201
201
|
<ScrollViewGesture
|
|
202
202
|
key={mode}
|
|
203
203
|
size={size}
|
|
204
|
-
translation={
|
|
204
|
+
translation={handlerOffset}
|
|
205
205
|
style={[
|
|
206
206
|
styles.container,
|
|
207
207
|
{
|
|
@@ -20,7 +20,7 @@ interface IOpts {
|
|
|
20
20
|
size: number;
|
|
21
21
|
data: TCarouselProps['data'];
|
|
22
22
|
autoFillData: TCarouselProps['autoFillData'];
|
|
23
|
-
|
|
23
|
+
handlerOffset: Animated.SharedValue<number>;
|
|
24
24
|
withAnimation?: TCarouselProps['withAnimation'];
|
|
25
25
|
duration?: number;
|
|
26
26
|
defaultIndex?: number;
|
|
@@ -41,7 +41,7 @@ export function useCarouselController(options: IOpts): ICarouselController {
|
|
|
41
41
|
size,
|
|
42
42
|
data,
|
|
43
43
|
loop,
|
|
44
|
-
|
|
44
|
+
handlerOffset,
|
|
45
45
|
withAnimation,
|
|
46
46
|
defaultIndex = 0,
|
|
47
47
|
duration,
|
|
@@ -64,16 +64,16 @@ export function useCarouselController(options: IOpts): ICarouselController {
|
|
|
64
64
|
|
|
65
65
|
const currentFixedPage = React.useCallback(() => {
|
|
66
66
|
if (loop) {
|
|
67
|
-
return -Math.round(
|
|
67
|
+
return -Math.round(handlerOffset.value / size);
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
const fixed = (
|
|
70
|
+
const fixed = (handlerOffset.value / size) % dataInfo.length;
|
|
71
71
|
return Math.round(
|
|
72
|
-
|
|
72
|
+
handlerOffset.value <= 0
|
|
73
73
|
? Math.abs(fixed)
|
|
74
74
|
: Math.abs(fixed > 0 ? dataInfo.length - fixed : 0)
|
|
75
75
|
);
|
|
76
|
-
}, [
|
|
76
|
+
}, [handlerOffset, dataInfo, size, loop]);
|
|
77
77
|
|
|
78
78
|
function setSharedIndex(newSharedIndex: number) {
|
|
79
79
|
sharedIndex.current = newSharedIndex;
|
|
@@ -81,9 +81,9 @@ export function useCarouselController(options: IOpts): ICarouselController {
|
|
|
81
81
|
|
|
82
82
|
useAnimatedReaction(
|
|
83
83
|
() => {
|
|
84
|
-
const
|
|
85
|
-
const toInt = round(
|
|
86
|
-
const isPositive =
|
|
84
|
+
const handlerOffsetValue = handlerOffset.value;
|
|
85
|
+
const toInt = round(handlerOffsetValue / size) % dataInfo.length;
|
|
86
|
+
const isPositive = handlerOffsetValue <= 0;
|
|
87
87
|
const i = isPositive
|
|
88
88
|
? Math.abs(toInt)
|
|
89
89
|
: Math.abs(toInt > 0 ? dataInfo.length - toInt : 0);
|
|
@@ -112,7 +112,7 @@ export function useCarouselController(options: IOpts): ICarouselController {
|
|
|
112
112
|
index,
|
|
113
113
|
loop,
|
|
114
114
|
autoFillData,
|
|
115
|
-
|
|
115
|
+
handlerOffset,
|
|
116
116
|
]
|
|
117
117
|
);
|
|
118
118
|
|
|
@@ -169,12 +169,12 @@ export function useCarouselController(options: IOpts): ICarouselController {
|
|
|
169
169
|
index.value = nextPage;
|
|
170
170
|
|
|
171
171
|
if (animated) {
|
|
172
|
-
|
|
172
|
+
handlerOffset.value = scrollWithTiming(
|
|
173
173
|
-nextPage * size,
|
|
174
174
|
onFinished
|
|
175
175
|
) as any;
|
|
176
176
|
} else {
|
|
177
|
-
|
|
177
|
+
handlerOffset.value = -nextPage * size;
|
|
178
178
|
onFinished?.();
|
|
179
179
|
}
|
|
180
180
|
},
|
|
@@ -184,7 +184,7 @@ export function useCarouselController(options: IOpts): ICarouselController {
|
|
|
184
184
|
index,
|
|
185
185
|
dataInfo,
|
|
186
186
|
onScrollBegin,
|
|
187
|
-
|
|
187
|
+
handlerOffset,
|
|
188
188
|
size,
|
|
189
189
|
scrollWithTiming,
|
|
190
190
|
currentFixedPage,
|
|
@@ -202,12 +202,12 @@ export function useCarouselController(options: IOpts): ICarouselController {
|
|
|
202
202
|
index.value = prevPage;
|
|
203
203
|
|
|
204
204
|
if (animated) {
|
|
205
|
-
|
|
205
|
+
handlerOffset.value = scrollWithTiming(
|
|
206
206
|
-prevPage * size,
|
|
207
207
|
onFinished
|
|
208
208
|
);
|
|
209
209
|
} else {
|
|
210
|
-
|
|
210
|
+
handlerOffset.value = -prevPage * size;
|
|
211
211
|
onFinished?.();
|
|
212
212
|
}
|
|
213
213
|
},
|
|
@@ -216,7 +216,7 @@ export function useCarouselController(options: IOpts): ICarouselController {
|
|
|
216
216
|
loop,
|
|
217
217
|
index,
|
|
218
218
|
onScrollBegin,
|
|
219
|
-
|
|
219
|
+
handlerOffset,
|
|
220
220
|
size,
|
|
221
221
|
scrollWithTiming,
|
|
222
222
|
currentFixedPage,
|
|
@@ -231,13 +231,13 @@ export function useCarouselController(options: IOpts): ICarouselController {
|
|
|
231
231
|
|
|
232
232
|
onScrollBegin?.();
|
|
233
233
|
// direction -> 1 | -1
|
|
234
|
-
const isPositiveZero = Object.is(
|
|
235
|
-
const isNegativeZero = Object.is(
|
|
234
|
+
const isPositiveZero = Object.is(handlerOffset.value, +0);
|
|
235
|
+
const isNegativeZero = Object.is(handlerOffset.value, -0);
|
|
236
236
|
const direction = isPositiveZero
|
|
237
237
|
? 1
|
|
238
238
|
: isNegativeZero
|
|
239
239
|
? -1
|
|
240
|
-
: Math.sign(
|
|
240
|
+
: Math.sign(handlerOffset.value);
|
|
241
241
|
|
|
242
242
|
// target offset
|
|
243
243
|
const offset = i * size * direction;
|
|
@@ -248,12 +248,12 @@ export function useCarouselController(options: IOpts): ICarouselController {
|
|
|
248
248
|
|
|
249
249
|
if (loop) {
|
|
250
250
|
isCloseToNextLoop =
|
|
251
|
-
Math.abs(
|
|
251
|
+
Math.abs(handlerOffset.value % totalSize) / totalSize >=
|
|
252
252
|
0.5;
|
|
253
253
|
}
|
|
254
254
|
|
|
255
255
|
const finalOffset =
|
|
256
|
-
(Math.floor(Math.abs(
|
|
256
|
+
(Math.floor(Math.abs(handlerOffset.value / totalSize)) +
|
|
257
257
|
(isCloseToNextLoop ? 1 : 0)) *
|
|
258
258
|
totalSize *
|
|
259
259
|
direction +
|
|
@@ -261,12 +261,9 @@ export function useCarouselController(options: IOpts): ICarouselController {
|
|
|
261
261
|
|
|
262
262
|
if (animated) {
|
|
263
263
|
index.value = i;
|
|
264
|
-
|
|
265
|
-
finalOffset,
|
|
266
|
-
onFinished
|
|
267
|
-
);
|
|
264
|
+
handlerOffset.value = scrollWithTiming(finalOffset, onFinished);
|
|
268
265
|
} else {
|
|
269
|
-
|
|
266
|
+
handlerOffset.value = finalOffset;
|
|
270
267
|
index.value = i;
|
|
271
268
|
onFinished?.();
|
|
272
269
|
}
|
|
@@ -275,7 +272,7 @@ export function useCarouselController(options: IOpts): ICarouselController {
|
|
|
275
272
|
index,
|
|
276
273
|
canSliding,
|
|
277
274
|
onScrollBegin,
|
|
278
|
-
|
|
275
|
+
handlerOffset,
|
|
279
276
|
size,
|
|
280
277
|
dataInfo.length,
|
|
281
278
|
loop,
|
|
@@ -5,25 +5,33 @@ import type { TInitializeCarouselProps } from './useInitProps';
|
|
|
5
5
|
interface ICommonVariables {
|
|
6
6
|
size: number;
|
|
7
7
|
validLength: number;
|
|
8
|
-
|
|
8
|
+
handlerOffset: Animated.SharedValue<number>;
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
export function useCommonVariables(
|
|
12
12
|
props: TInitializeCarouselProps<any>
|
|
13
13
|
): ICommonVariables {
|
|
14
|
-
const {
|
|
14
|
+
const {
|
|
15
|
+
vertical,
|
|
16
|
+
height,
|
|
17
|
+
width,
|
|
18
|
+
data,
|
|
19
|
+
defaultIndex,
|
|
20
|
+
defaultScrollOffsetValue,
|
|
21
|
+
} = props;
|
|
15
22
|
const size = vertical ? height : width;
|
|
16
23
|
const validLength = data.length - 1;
|
|
17
|
-
const
|
|
18
|
-
const
|
|
24
|
+
const defaultHandlerOffsetValue = -Math.abs(defaultIndex * size);
|
|
25
|
+
const _handlerOffset = useSharedValue<number>(defaultHandlerOffsetValue);
|
|
26
|
+
const handlerOffset = defaultScrollOffsetValue ?? _handlerOffset;
|
|
19
27
|
|
|
20
28
|
React.useEffect(() => {
|
|
21
|
-
|
|
22
|
-
}, [vertical,
|
|
29
|
+
handlerOffset.value = defaultHandlerOffsetValue;
|
|
30
|
+
}, [vertical, handlerOffset, defaultHandlerOffsetValue]);
|
|
23
31
|
|
|
24
32
|
return {
|
|
25
33
|
size,
|
|
26
34
|
validLength,
|
|
27
|
-
|
|
35
|
+
handlerOffset,
|
|
28
36
|
};
|
|
29
37
|
}
|
package/src/hooks/useOffsetX.ts
CHANGED
|
@@ -8,7 +8,7 @@ import type { IVisibleRanges } from './useVisibleRanges';
|
|
|
8
8
|
export interface IOpts {
|
|
9
9
|
index: number;
|
|
10
10
|
size: number;
|
|
11
|
-
|
|
11
|
+
handlerOffset: Animated.SharedValue<number>;
|
|
12
12
|
data: unknown[];
|
|
13
13
|
type?: 'positive' | 'negative';
|
|
14
14
|
viewCount?: number;
|
|
@@ -17,7 +17,7 @@ export interface IOpts {
|
|
|
17
17
|
|
|
18
18
|
export const useOffsetX = (opts: IOpts, visibleRanges: IVisibleRanges) => {
|
|
19
19
|
const {
|
|
20
|
-
|
|
20
|
+
handlerOffset,
|
|
21
21
|
index,
|
|
22
22
|
size,
|
|
23
23
|
loop,
|
|
@@ -73,14 +73,14 @@ export const useOffsetX = (opts: IOpts, visibleRanges: IVisibleRanges) => {
|
|
|
73
73
|
];
|
|
74
74
|
|
|
75
75
|
return interpolate(
|
|
76
|
-
|
|
76
|
+
handlerOffset.value,
|
|
77
77
|
inputRange,
|
|
78
78
|
outputRange,
|
|
79
79
|
Extrapolate.CLAMP
|
|
80
80
|
);
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
-
return
|
|
83
|
+
return handlerOffset.value + size * index;
|
|
84
84
|
}, [loop, data, viewCount, type, size, visibleRanges]);
|
|
85
85
|
|
|
86
86
|
return x;
|
|
@@ -18,7 +18,7 @@ export type TAnimationStyle = (value: number) => AnimatedStyleProp<ViewStyle>;
|
|
|
18
18
|
|
|
19
19
|
export const BaseLayout: React.FC<{
|
|
20
20
|
index: number;
|
|
21
|
-
|
|
21
|
+
handlerOffset: Animated.SharedValue<number>;
|
|
22
22
|
visibleRanges: IVisibleRanges;
|
|
23
23
|
animationStyle: TAnimationStyle;
|
|
24
24
|
children: (ctx: {
|
|
@@ -26,7 +26,7 @@ export const BaseLayout: React.FC<{
|
|
|
26
26
|
}) => React.ReactElement;
|
|
27
27
|
}> = (props) => {
|
|
28
28
|
const mounted = useCheckMounted();
|
|
29
|
-
const {
|
|
29
|
+
const { handlerOffset, index, children, visibleRanges, animationStyle } =
|
|
30
30
|
props;
|
|
31
31
|
|
|
32
32
|
const context = React.useContext(CTX);
|
|
@@ -45,7 +45,7 @@ export const BaseLayout: React.FC<{
|
|
|
45
45
|
const size = vertical ? height : width;
|
|
46
46
|
const [shouldUpdate, setShouldUpdate] = React.useState(false);
|
|
47
47
|
let offsetXConfig: IOpts = {
|
|
48
|
-
|
|
48
|
+
handlerOffset: handlerOffset,
|
|
49
49
|
index,
|
|
50
50
|
size,
|
|
51
51
|
data,
|
|
@@ -57,7 +57,7 @@ export const BaseLayout: React.FC<{
|
|
|
57
57
|
const { snapDirection, showLength } = modeConfig as ILayoutConfig;
|
|
58
58
|
|
|
59
59
|
offsetXConfig = {
|
|
60
|
-
|
|
60
|
+
handlerOffset: handlerOffset,
|
|
61
61
|
index,
|
|
62
62
|
size,
|
|
63
63
|
data,
|
|
@@ -16,7 +16,7 @@ export const ParallaxLayout: React.FC<
|
|
|
16
16
|
IComputedDirectionTypes<
|
|
17
17
|
{
|
|
18
18
|
loop?: boolean;
|
|
19
|
-
|
|
19
|
+
handlerOffset: Animated.SharedValue<number>;
|
|
20
20
|
index: number;
|
|
21
21
|
data: unknown[];
|
|
22
22
|
visibleRanges: IVisibleRanges;
|
|
@@ -24,7 +24,7 @@ export const ParallaxLayout: React.FC<
|
|
|
24
24
|
>
|
|
25
25
|
> = (props) => {
|
|
26
26
|
const {
|
|
27
|
-
|
|
27
|
+
handlerOffset,
|
|
28
28
|
parallaxScrollingOffset = 100,
|
|
29
29
|
parallaxScrollingScale = 0.8,
|
|
30
30
|
parallaxAdjacentItemScale = Math.pow(parallaxScrollingScale, 2),
|
|
@@ -44,7 +44,7 @@ export const ParallaxLayout: React.FC<
|
|
|
44
44
|
|
|
45
45
|
const x = useOffsetX(
|
|
46
46
|
{
|
|
47
|
-
|
|
47
|
+
handlerOffset: handlerOffset,
|
|
48
48
|
index,
|
|
49
49
|
size,
|
|
50
50
|
data,
|
package/src/types.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { StyleProp, ViewStyle } from 'react-native';
|
|
|
2
2
|
import type { PanGestureHandlerProps } from 'react-native-gesture-handler';
|
|
3
3
|
import type {
|
|
4
4
|
AnimatedStyleProp,
|
|
5
|
+
SharedValue,
|
|
5
6
|
WithSpringConfig,
|
|
6
7
|
WithTimingConfig,
|
|
7
8
|
} from 'react-native-reanimated';
|
|
@@ -60,6 +61,10 @@ export type WithAnimation = WithSpringAnimation | WithTimingAnimation;
|
|
|
60
61
|
|
|
61
62
|
export type TCarouselProps<T = any> = {
|
|
62
63
|
ref?: React.Ref<ICarouselInstance>;
|
|
64
|
+
/**
|
|
65
|
+
* The default animated value of the carousel.
|
|
66
|
+
*/
|
|
67
|
+
defaultScrollOffsetValue?: SharedValue<number>;
|
|
63
68
|
/**
|
|
64
69
|
* Carousel loop playback.
|
|
65
70
|
* @default true
|
|
@@ -162,7 +167,7 @@ export type TCarouselProps<T = any> = {
|
|
|
162
167
|
*/
|
|
163
168
|
renderItem: CarouselRenderItem<T>;
|
|
164
169
|
/**
|
|
165
|
-
* Callback fired when navigating to an item
|
|
170
|
+
* Callback fired when navigating to an item.
|
|
166
171
|
*/
|
|
167
172
|
onSnapToItem?: (index: number) => void;
|
|
168
173
|
/**
|