react-native-reanimated-carousel 2.2.3 → 2.2.5-beta.2

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.
Files changed (74) hide show
  1. package/README.md +1 -1
  2. package/README.zh-CN.md +1 -1
  3. package/lib/commonjs/Carousel.js +1 -227
  4. package/lib/commonjs/Carousel.js.map +1 -1
  5. package/lib/commonjs/LazyView.js +1 -25
  6. package/lib/commonjs/LazyView.js.map +1 -1
  7. package/lib/commonjs/ScrollViewGesture.js +1 -225
  8. package/lib/commonjs/ScrollViewGesture.js.map +1 -1
  9. package/lib/commonjs/constants/index.js +1 -21
  10. package/lib/commonjs/constants/index.js.map +1 -1
  11. package/lib/commonjs/hooks/useAutoPlay.js +1 -65
  12. package/lib/commonjs/hooks/useAutoPlay.js.map +1 -1
  13. package/lib/commonjs/hooks/useCarouselController.js +1 -194
  14. package/lib/commonjs/hooks/useCarouselController.js.map +1 -1
  15. package/lib/commonjs/hooks/useCommonVariables.js +1 -36
  16. package/lib/commonjs/hooks/useCommonVariables.js.map +1 -1
  17. package/lib/commonjs/hooks/useInitProps.js +1 -70
  18. package/lib/commonjs/hooks/useInitProps.js.map +1 -1
  19. package/lib/commonjs/hooks/useLayoutConfig.js +1 -39
  20. package/lib/commonjs/hooks/useLayoutConfig.js.map +1 -1
  21. package/lib/commonjs/hooks/useOffsetX.js +1 -54
  22. package/lib/commonjs/hooks/useOffsetX.js.map +1 -1
  23. package/lib/commonjs/hooks/useOnProgressChange.js +1 -38
  24. package/lib/commonjs/hooks/useOnProgressChange.js.map +1 -1
  25. package/lib/commonjs/hooks/usePropsErrorBoundary.js +1 -37
  26. package/lib/commonjs/hooks/usePropsErrorBoundary.js.map +1 -1
  27. package/lib/commonjs/hooks/useVisibleRanges.js +1 -42
  28. package/lib/commonjs/hooks/useVisibleRanges.js.map +1 -1
  29. package/lib/commonjs/index.js +1 -13
  30. package/lib/commonjs/index.js.map +1 -1
  31. package/lib/commonjs/layouts/BaseLayout.js +1 -100
  32. package/lib/commonjs/layouts/BaseLayout.js.map +1 -1
  33. package/lib/commonjs/layouts/ParallaxLayout.js +1 -82
  34. package/lib/commonjs/layouts/ParallaxLayout.js.map +1 -1
  35. package/lib/commonjs/layouts/index.js +1 -20
  36. package/lib/commonjs/layouts/index.js.map +1 -1
  37. package/lib/commonjs/layouts/normal.js +1 -27
  38. package/lib/commonjs/layouts/normal.js.map +1 -1
  39. package/lib/commonjs/layouts/parallax.js +1 -36
  40. package/lib/commonjs/layouts/parallax.js.map +1 -1
  41. package/lib/commonjs/layouts/stack.js +1 -219
  42. package/lib/commonjs/layouts/stack.js.map +1 -1
  43. package/lib/commonjs/store/index.js +1 -14
  44. package/lib/commonjs/store/index.js.map +1 -1
  45. package/lib/commonjs/types.js +1 -5
  46. package/lib/commonjs/utils/dealWithAnimation.js +2 -0
  47. package/lib/commonjs/utils/dealWithAnimation.js.map +1 -0
  48. package/lib/commonjs/utils/log.js +1 -14
  49. package/lib/commonjs/utils/log.js.map +1 -1
  50. package/lib/module/Carousel.js +5 -5
  51. package/lib/module/Carousel.js.map +1 -1
  52. package/lib/module/ScrollViewGesture.js +23 -12
  53. package/lib/module/ScrollViewGesture.js.map +1 -1
  54. package/lib/module/hooks/useCarouselController.js +21 -7
  55. package/lib/module/hooks/useCarouselController.js.map +1 -1
  56. package/lib/module/layouts/BaseLayout.js +2 -1
  57. package/lib/module/layouts/BaseLayout.js.map +1 -1
  58. package/lib/module/layouts/stack.js.map +1 -1
  59. package/lib/module/utils/dealWithAnimation.js +17 -0
  60. package/lib/module/utils/dealWithAnimation.js.map +1 -0
  61. package/lib/typescript/Carousel.d.ts +3 -4
  62. package/lib/typescript/hooks/useCarouselController.d.ts +2 -1
  63. package/lib/typescript/layouts/BaseLayout.d.ts +2 -2
  64. package/lib/typescript/layouts/stack.d.ts +1 -1
  65. package/lib/typescript/types.d.ts +14 -2
  66. package/lib/typescript/utils/dealWithAnimation.d.ts +2 -0
  67. package/package.json +12 -3
  68. package/src/Carousel.tsx +203 -194
  69. package/src/ScrollViewGesture.tsx +22 -12
  70. package/src/hooks/useCarouselController.tsx +28 -12
  71. package/src/layouts/BaseLayout.tsx +3 -3
  72. package/src/layouts/stack.ts +1 -1
  73. package/src/types.ts +22 -4
  74. package/src/utils/dealWithAnimation.ts +22 -0
@@ -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, withTiming } from 'react-native-reanimated';
5
- import type { TCarouselActionOptions } from '../types';
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
- return withTiming(
120
- toValue,
121
- { duration, easing: Easing.easeOutQuart },
122
- (isFinished: boolean) => {
123
- if (isFinished) {
124
- runOnJS(onScrollEnd)();
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
- [onScrollEnd, duration]
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
  );
@@ -1,7 +1,7 @@
1
1
  import { useMemo } from 'react';
2
2
  import { Dimensions, TransformsStyle, ViewStyle } from 'react-native';
3
3
  import { Extrapolate, interpolate } from 'react-native-reanimated';
4
- import type { ComputedDirectionTypes, CustomConfig } from 'src/types';
4
+ import type { ComputedDirectionTypes, CustomConfig } from '../types';
5
5
 
6
6
  const screen = Dimensions.get('window');
7
7
 
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?: Omit<
87
- Partial<PanGestureHandlerProps>,
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) => Animated.AnimatedStyleProp<ViewStyle>;
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
+ }