react-native-reanimated-carousel 2.2.4 → 2.2.5-beta.3
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 -226
- package/lib/commonjs/Carousel.js.map +1 -1
- package/lib/commonjs/LazyView.js +1 -25
- package/lib/commonjs/LazyView.js.map +1 -1
- package/lib/commonjs/ScrollViewGesture.js +1 -225
- package/lib/commonjs/ScrollViewGesture.js.map +1 -1
- package/lib/commonjs/constants/index.js +1 -21
- package/lib/commonjs/constants/index.js.map +1 -1
- package/lib/commonjs/hooks/useAutoPlay.js +1 -65
- package/lib/commonjs/hooks/useAutoPlay.js.map +1 -1
- package/lib/commonjs/hooks/useCarouselController.js +1 -194
- package/lib/commonjs/hooks/useCarouselController.js.map +1 -1
- package/lib/commonjs/hooks/useCommonVariables.js +1 -36
- package/lib/commonjs/hooks/useCommonVariables.js.map +1 -1
- package/lib/commonjs/hooks/useInitProps.js +1 -70
- package/lib/commonjs/hooks/useInitProps.js.map +1 -1
- package/lib/commonjs/hooks/useLayoutConfig.js +1 -39
- package/lib/commonjs/hooks/useLayoutConfig.js.map +1 -1
- package/lib/commonjs/hooks/useOffsetX.js +1 -54
- package/lib/commonjs/hooks/useOffsetX.js.map +1 -1
- package/lib/commonjs/hooks/useOnProgressChange.js +1 -38
- package/lib/commonjs/hooks/useOnProgressChange.js.map +1 -1
- package/lib/commonjs/hooks/usePropsErrorBoundary.js +1 -37
- package/lib/commonjs/hooks/usePropsErrorBoundary.js.map +1 -1
- package/lib/commonjs/hooks/useVisibleRanges.js +1 -42
- package/lib/commonjs/hooks/useVisibleRanges.js.map +1 -1
- package/lib/commonjs/index.js +1 -13
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/layouts/BaseLayout.js +1 -100
- package/lib/commonjs/layouts/BaseLayout.js.map +1 -1
- package/lib/commonjs/layouts/ParallaxLayout.js +1 -82
- package/lib/commonjs/layouts/ParallaxLayout.js.map +1 -1
- package/lib/commonjs/layouts/index.js +1 -20
- package/lib/commonjs/layouts/index.js.map +1 -1
- package/lib/commonjs/layouts/normal.js +1 -27
- package/lib/commonjs/layouts/normal.js.map +1 -1
- package/lib/commonjs/layouts/parallax.js +1 -36
- package/lib/commonjs/layouts/parallax.js.map +1 -1
- package/lib/commonjs/layouts/stack.js +1 -219
- package/lib/commonjs/layouts/stack.js.map +1 -1
- package/lib/commonjs/store/index.js +1 -14
- package/lib/commonjs/store/index.js.map +1 -1
- package/lib/commonjs/types.js +1 -5
- package/lib/commonjs/utils/dealWithAnimation.js +2 -0
- package/lib/commonjs/utils/dealWithAnimation.js.map +1 -0
- package/lib/commonjs/utils/log.js +1 -14
- package/lib/commonjs/utils/log.js.map +1 -1
- package/lib/module/Carousel.js +2 -0
- package/lib/module/Carousel.js.map +1 -1
- package/lib/module/ScrollViewGesture.js +23 -12
- package/lib/module/ScrollViewGesture.js.map +1 -1
- package/lib/module/hooks/useCarouselController.js +21 -7
- package/lib/module/hooks/useCarouselController.js.map +1 -1
- package/lib/module/layouts/BaseLayout.js +2 -1
- package/lib/module/layouts/BaseLayout.js.map +1 -1
- package/lib/module/utils/dealWithAnimation.js +17 -0
- package/lib/module/utils/dealWithAnimation.js.map +1 -0
- package/lib/typescript/hooks/useCarouselController.d.ts +2 -1
- package/lib/typescript/layouts/BaseLayout.d.ts +2 -2
- package/lib/typescript/types.d.ts +14 -2
- package/lib/typescript/utils/dealWithAnimation.d.ts +2 -0
- package/package.json +12 -4
- package/src/Carousel.tsx +2 -0
- package/src/ScrollViewGesture.tsx +22 -12
- package/src/hooks/useCarouselController.tsx +28 -12
- package/src/layouts/BaseLayout.tsx +3 -3
- package/src/types.ts +22 -4
- package/src/utils/dealWithAnimation.ts +22 -0
package/package.json
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-reanimated-carousel",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.5-beta.3",
|
|
4
4
|
"description": "Simple carousel component.fully implemented using Reanimated 2.Infinitely scrolling, very smooth.",
|
|
5
5
|
"main": "lib/commonjs/index",
|
|
6
|
-
"module": "lib/module/index",
|
|
7
6
|
"types": "lib/typescript/index.d.ts",
|
|
8
|
-
"react-native": "src/index",
|
|
9
7
|
"source": "src/index",
|
|
10
8
|
"files": [
|
|
11
9
|
"src",
|
|
@@ -26,8 +24,10 @@
|
|
|
26
24
|
"test": "jest",
|
|
27
25
|
"typescript": "tsc --noEmit",
|
|
28
26
|
"lint": "eslint \"src/**/*.{js,ts,tsx}\"",
|
|
27
|
+
"dev": "yarn watch 'yarn prepare' ./src",
|
|
29
28
|
"prepare": "bob build",
|
|
30
29
|
"release": "release-it --no-git.requireUpstream",
|
|
30
|
+
"preRelease": "release-it --no-git.requireUpstream --preRelease=beta",
|
|
31
31
|
"ios": "yarn --cwd example ios",
|
|
32
32
|
"ios:pretty": "yarn --cwd example ios:pretty",
|
|
33
33
|
"web": "yarn --cwd example web",
|
|
@@ -54,12 +54,15 @@
|
|
|
54
54
|
"registry": "https://registry.npmjs.org/"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
|
+
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
|
|
57
58
|
"@commitlint/config-conventional": "^11.0.0",
|
|
58
59
|
"@react-native-community/eslint-config": "^2.0.0",
|
|
59
60
|
"@release-it/conventional-changelog": "^2.0.0",
|
|
60
61
|
"@types/jest": "^26.0.0",
|
|
61
62
|
"@types/react": "^16.9.19",
|
|
62
63
|
"@types/react-native": "0.62.13",
|
|
64
|
+
"babel-plugin-inline-dotenv": "^1.6.0",
|
|
65
|
+
"babel-plugin-module-resolver": "^4.1.0",
|
|
63
66
|
"commitlint": "^11.0.0",
|
|
64
67
|
"cz-conventional-changelog": "^3.3.0",
|
|
65
68
|
"eslint": "^7.2.0",
|
|
@@ -137,7 +140,12 @@
|
|
|
137
140
|
"source": "src",
|
|
138
141
|
"output": "lib",
|
|
139
142
|
"targets": [
|
|
140
|
-
|
|
143
|
+
[
|
|
144
|
+
"commonjs",
|
|
145
|
+
{
|
|
146
|
+
"configFile": "./babel.config.js"
|
|
147
|
+
}
|
|
148
|
+
],
|
|
141
149
|
"module",
|
|
142
150
|
[
|
|
143
151
|
"typescript",
|
package/src/Carousel.tsx
CHANGED
|
@@ -35,6 +35,7 @@ const Carousel = React.forwardRef<ICarouselInstance, TCarouselProps<any>>(
|
|
|
35
35
|
autoPlayReverse,
|
|
36
36
|
autoPlayInterval,
|
|
37
37
|
scrollAnimationDuration,
|
|
38
|
+
withAnimation,
|
|
38
39
|
renderItem,
|
|
39
40
|
onScrollEnd,
|
|
40
41
|
onSnapToItem,
|
|
@@ -65,6 +66,7 @@ const Carousel = React.forwardRef<ICarouselInstance, TCarouselProps<any>>(
|
|
|
65
66
|
handlerOffsetX,
|
|
66
67
|
length: data.length,
|
|
67
68
|
disable: !data.length,
|
|
69
|
+
withAnimation,
|
|
68
70
|
originalLength: data.length,
|
|
69
71
|
onScrollEnd: () => runOnJS(_onScrollEnd)(),
|
|
70
72
|
onScrollBegin: () => !!onScrollBegin && runOnJS(onScrollBegin)(),
|
|
@@ -12,10 +12,11 @@ import Animated, {
|
|
|
12
12
|
useDerivedValue,
|
|
13
13
|
useSharedValue,
|
|
14
14
|
withDecay,
|
|
15
|
-
withTiming,
|
|
16
15
|
} from 'react-native-reanimated';
|
|
17
16
|
import { Easing } from './constants';
|
|
18
17
|
import { CTX } from './store';
|
|
18
|
+
import type { WithAnimation } from './types';
|
|
19
|
+
import { dealWithAnimation } from './utils/dealWithAnimation';
|
|
19
20
|
|
|
20
21
|
type GestureContext = {
|
|
21
22
|
panOffset: number;
|
|
@@ -44,6 +45,7 @@ const IScrollViewGesture: React.FC<Props> = (props) => {
|
|
|
44
45
|
panGestureHandlerProps,
|
|
45
46
|
loop: infinite,
|
|
46
47
|
scrollAnimationDuration,
|
|
48
|
+
withAnimation,
|
|
47
49
|
},
|
|
48
50
|
} = React.useContext(CTX);
|
|
49
51
|
|
|
@@ -65,20 +67,27 @@ const IScrollViewGesture: React.FC<Props> = (props) => {
|
|
|
65
67
|
const _withSpring = React.useCallback(
|
|
66
68
|
(toValue: number, onFinished?: () => void) => {
|
|
67
69
|
'worklet';
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
{
|
|
70
|
+
const callback = (isFinished: boolean) => {
|
|
71
|
+
'worklet';
|
|
72
|
+
if (isFinished) {
|
|
73
|
+
onFinished && runOnJS(onFinished)();
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const defaultWithAnimation: WithAnimation = {
|
|
78
|
+
type: 'timing',
|
|
79
|
+
config: {
|
|
71
80
|
duration: scrollAnimationDuration,
|
|
72
81
|
easing: Easing.easeOutQuart,
|
|
73
82
|
},
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
return dealWithAnimation(withAnimation ?? defaultWithAnimation)(
|
|
86
|
+
toValue,
|
|
87
|
+
callback
|
|
79
88
|
);
|
|
80
89
|
},
|
|
81
|
-
[scrollAnimationDuration]
|
|
90
|
+
[scrollAnimationDuration, withAnimation]
|
|
82
91
|
);
|
|
83
92
|
|
|
84
93
|
const endWithSpring = React.useCallback(
|
|
@@ -141,7 +150,7 @@ const IScrollViewGesture: React.FC<Props> = (props) => {
|
|
|
141
150
|
touching.value = true;
|
|
142
151
|
translation.value = withDecay(
|
|
143
152
|
{ velocity: scrollEndVelocity.value },
|
|
144
|
-
onFinish
|
|
153
|
+
(isFinished) => onFinish(isFinished as boolean)
|
|
145
154
|
);
|
|
146
155
|
}, [onFinish, scrollEndVelocity.value, touching, translation]);
|
|
147
156
|
|
|
@@ -234,7 +243,8 @@ const IScrollViewGesture: React.FC<Props> = (props) => {
|
|
|
234
243
|
? translationX
|
|
235
244
|
: translationY;
|
|
236
245
|
|
|
237
|
-
endWithSpring(() => onScrollEnd && runOnJS(onScrollEnd)());
|
|
246
|
+
// endWithSpring(() => onScrollEnd && runOnJS(onScrollEnd)());
|
|
247
|
+
endWithSpring(onScrollEnd);
|
|
238
248
|
|
|
239
249
|
if (!infinite) {
|
|
240
250
|
touching.value = false;
|
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import type Animated from 'react-native-reanimated';
|
|
3
3
|
import { Easing } from '../constants';
|
|
4
|
-
import { runOnJS, useSharedValue
|
|
5
|
-
import type {
|
|
4
|
+
import { runOnJS, useSharedValue } from 'react-native-reanimated';
|
|
5
|
+
import type {
|
|
6
|
+
TCarouselActionOptions,
|
|
7
|
+
TCarouselProps,
|
|
8
|
+
WithAnimation,
|
|
9
|
+
} from '../types';
|
|
10
|
+
import { dealWithAnimation } from '@/utils/dealWithAnimation';
|
|
6
11
|
|
|
7
12
|
interface IOpts {
|
|
8
13
|
loop: boolean;
|
|
9
14
|
size: number;
|
|
10
15
|
handlerOffsetX: Animated.SharedValue<number>;
|
|
16
|
+
withAnimation?: TCarouselProps['withAnimation'];
|
|
11
17
|
disable?: boolean;
|
|
12
18
|
duration?: number;
|
|
13
19
|
originalLength: number;
|
|
@@ -36,6 +42,7 @@ export function useCarouselController(options: IOpts): ICarouselController {
|
|
|
36
42
|
size,
|
|
37
43
|
loop,
|
|
38
44
|
handlerOffsetX,
|
|
45
|
+
withAnimation,
|
|
39
46
|
disable = false,
|
|
40
47
|
originalLength,
|
|
41
48
|
length,
|
|
@@ -116,22 +123,31 @@ export function useCarouselController(options: IOpts): ICarouselController {
|
|
|
116
123
|
|
|
117
124
|
const scrollWithTiming = React.useCallback(
|
|
118
125
|
(toValue: number, onFinished?: () => void) => {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
(isFinished
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
onFinished && runOnJS(onFinished)();
|
|
126
|
-
}
|
|
126
|
+
'worklet';
|
|
127
|
+
const callback = (isFinished: boolean) => {
|
|
128
|
+
'worklet';
|
|
129
|
+
if (isFinished) {
|
|
130
|
+
runOnJS(onScrollEnd)();
|
|
131
|
+
onFinished && runOnJS(onFinished)();
|
|
127
132
|
}
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const defaultWithAnimation: WithAnimation = {
|
|
136
|
+
type: 'timing',
|
|
137
|
+
config: { duration, easing: Easing.easeOutQuart },
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
return dealWithAnimation(withAnimation ?? defaultWithAnimation)(
|
|
141
|
+
toValue,
|
|
142
|
+
callback
|
|
128
143
|
);
|
|
129
144
|
},
|
|
130
|
-
[
|
|
145
|
+
[duration, withAnimation, onScrollEnd]
|
|
131
146
|
);
|
|
132
147
|
|
|
133
148
|
const next = React.useCallback(
|
|
134
149
|
(opts: TCarouselActionOptions = {}) => {
|
|
150
|
+
'worklet';
|
|
135
151
|
const { count = 1, animated = true, onFinished } = opts;
|
|
136
152
|
if (!canSliding() || (!loop && index.value >= length - 1)) return;
|
|
137
153
|
|
|
@@ -144,7 +160,7 @@ export function useCarouselController(options: IOpts): ICarouselController {
|
|
|
144
160
|
handlerOffsetX.value = scrollWithTiming(
|
|
145
161
|
-nextPage * size,
|
|
146
162
|
onFinished
|
|
147
|
-
);
|
|
163
|
+
) as any;
|
|
148
164
|
} else {
|
|
149
165
|
handlerOffsetX.value = -nextPage * size;
|
|
150
166
|
onFinished?.();
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import type { ViewStyle } from 'react-native';
|
|
3
3
|
import Animated, {
|
|
4
|
+
AnimatedStyleProp,
|
|
4
5
|
runOnJS,
|
|
5
6
|
useAnimatedReaction,
|
|
6
7
|
useAnimatedStyle,
|
|
@@ -12,9 +13,7 @@ import { LazyView } from '../LazyView';
|
|
|
12
13
|
import { CTX } from '../store';
|
|
13
14
|
import type { ILayoutConfig } from './stack';
|
|
14
15
|
|
|
15
|
-
export type TAnimationStyle = (
|
|
16
|
-
value: number
|
|
17
|
-
) => Animated.AnimatedStyleProp<ViewStyle>;
|
|
16
|
+
export type TAnimationStyle = (value: number) => AnimatedStyleProp<ViewStyle>;
|
|
18
17
|
|
|
19
18
|
export const BaseLayout: React.FC<{
|
|
20
19
|
index: number;
|
|
@@ -69,6 +68,7 @@ export const BaseLayout: React.FC<{
|
|
|
69
68
|
const x = useOffsetX(offsetXConfig, visibleRanges);
|
|
70
69
|
const animationValue = useDerivedValue(() => x.value / size, [x, size]);
|
|
71
70
|
const animatedStyle = useAnimatedStyle(
|
|
71
|
+
// @ts-ignore
|
|
72
72
|
() => animationStyle(x.value / size),
|
|
73
73
|
[animationStyle]
|
|
74
74
|
);
|
package/src/types.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import type { ViewStyle } from 'react-native';
|
|
2
2
|
import type { PanGestureHandlerProps } from 'react-native-gesture-handler';
|
|
3
|
+
import type {
|
|
4
|
+
AnimatedStyleProp,
|
|
5
|
+
WithSpringConfig,
|
|
6
|
+
WithTimingConfig,
|
|
7
|
+
} from 'react-native-reanimated';
|
|
3
8
|
import type Animated from 'react-native-reanimated';
|
|
4
9
|
import type { TParallaxModeProps } from './layouts/parallax';
|
|
5
10
|
import type { TStackModeProps } from './layouts/stack';
|
|
@@ -41,6 +46,16 @@ export type CustomConfig = {
|
|
|
41
46
|
viewCount?: number;
|
|
42
47
|
};
|
|
43
48
|
|
|
49
|
+
export type WithAnimation =
|
|
50
|
+
| {
|
|
51
|
+
type: 'spring';
|
|
52
|
+
config: WithSpringConfig;
|
|
53
|
+
}
|
|
54
|
+
| {
|
|
55
|
+
type: 'timing';
|
|
56
|
+
config: WithTimingConfig;
|
|
57
|
+
};
|
|
58
|
+
|
|
44
59
|
export type TCarouselProps<T = any> = {
|
|
45
60
|
ref?: React.Ref<ICarouselInstance>;
|
|
46
61
|
/**
|
|
@@ -83,9 +98,8 @@ export type TCarouselProps<T = any> = {
|
|
|
83
98
|
/**
|
|
84
99
|
* PanGestureHandler props
|
|
85
100
|
*/
|
|
86
|
-
panGestureHandlerProps?:
|
|
87
|
-
|
|
88
|
-
'onHandlerStateChange'
|
|
101
|
+
panGestureHandlerProps?: Partial<
|
|
102
|
+
Omit<PanGestureHandlerProps, 'onHandlerStateChange'>
|
|
89
103
|
>;
|
|
90
104
|
/**
|
|
91
105
|
* Determines the maximum number of items will respond to pan gesture events,
|
|
@@ -105,6 +119,10 @@ export type TCarouselProps<T = any> = {
|
|
|
105
119
|
* @default true
|
|
106
120
|
*/
|
|
107
121
|
enableSnap?: boolean;
|
|
122
|
+
/**
|
|
123
|
+
* Specifies the scrolling animation effect.
|
|
124
|
+
*/
|
|
125
|
+
withAnimation?: WithAnimation;
|
|
108
126
|
/**
|
|
109
127
|
* Custom carousel config.
|
|
110
128
|
*/
|
|
@@ -113,7 +131,7 @@ export type TCarouselProps<T = any> = {
|
|
|
113
131
|
* Custom animations.
|
|
114
132
|
* Must use `worklet`, Details: https://docs.swmansion.com/react-native-reanimated/docs/2.2.0/worklets/
|
|
115
133
|
*/
|
|
116
|
-
customAnimation?: (value: number) =>
|
|
134
|
+
customAnimation?: (value: number) => AnimatedStyleProp<ViewStyle>;
|
|
117
135
|
/**
|
|
118
136
|
* Render carousel item.
|
|
119
137
|
*/
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { WithAnimation } from '../types';
|
|
2
|
+
import { withSpring, withTiming } from 'react-native-reanimated';
|
|
3
|
+
|
|
4
|
+
export function dealWithAnimation(
|
|
5
|
+
withAnimation: WithAnimation
|
|
6
|
+
): (value: number, cb: (isFinished: boolean) => void) => number {
|
|
7
|
+
'worklet';
|
|
8
|
+
switch (withAnimation.type) {
|
|
9
|
+
case 'spring':
|
|
10
|
+
return (value, cb) => {
|
|
11
|
+
return withSpring(value, withAnimation.config, (isFinished) =>
|
|
12
|
+
cb(isFinished as boolean)
|
|
13
|
+
);
|
|
14
|
+
};
|
|
15
|
+
case 'timing':
|
|
16
|
+
return (value, cb) => {
|
|
17
|
+
return withTiming(value, withAnimation.config, (isFinished) =>
|
|
18
|
+
cb(isFinished as boolean)
|
|
19
|
+
);
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
}
|