react-native-reanimated-carousel 3.1.0 → 3.1.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 (166) hide show
  1. package/README.md +1 -0
  2. package/README.zh-CN.md +1 -0
  3. package/lib/commonjs/Carousel.js +1 -1
  4. package/lib/commonjs/Carousel.js.map +1 -1
  5. package/lib/commonjs/LazyView.js +1 -1
  6. package/lib/commonjs/LazyView.js.map +1 -1
  7. package/lib/commonjs/ScrollViewGesture.js +1 -1
  8. package/lib/commonjs/ScrollViewGesture.js.map +1 -1
  9. package/lib/commonjs/constants/index.js.map +1 -1
  10. package/lib/commonjs/hooks/computeNewIndexWhenDataChanges.js +2 -0
  11. package/lib/commonjs/hooks/computeNewIndexWhenDataChanges.js.map +1 -0
  12. package/lib/commonjs/hooks/index.test.js +2 -0
  13. package/lib/commonjs/hooks/index.test.js.map +1 -0
  14. package/lib/commonjs/hooks/useAutoPlay.js +1 -1
  15. package/lib/commonjs/hooks/useAutoPlay.js.map +1 -1
  16. package/lib/commonjs/hooks/useCarouselController.js +1 -1
  17. package/lib/commonjs/hooks/useCarouselController.js.map +1 -1
  18. package/lib/commonjs/hooks/useCheckMounted.js.map +1 -1
  19. package/lib/commonjs/hooks/useCommonVariables.js +1 -1
  20. package/lib/commonjs/hooks/useCommonVariables.js.map +1 -1
  21. package/lib/commonjs/hooks/useInitProps.js +1 -1
  22. package/lib/commonjs/hooks/useInitProps.js.map +1 -1
  23. package/lib/commonjs/hooks/useLayoutConfig.js +1 -1
  24. package/lib/commonjs/hooks/useLayoutConfig.js.map +1 -1
  25. package/lib/commonjs/hooks/useOffsetX.js +1 -1
  26. package/lib/commonjs/hooks/useOffsetX.js.map +1 -1
  27. package/lib/commonjs/hooks/useOnProgressChange.js +1 -1
  28. package/lib/commonjs/hooks/useOnProgressChange.js.map +1 -1
  29. package/lib/commonjs/hooks/usePropsErrorBoundary.js +1 -1
  30. package/lib/commonjs/hooks/usePropsErrorBoundary.js.map +1 -1
  31. package/lib/commonjs/hooks/useVisibleRanges.js +1 -1
  32. package/lib/commonjs/hooks/useVisibleRanges.js.map +1 -1
  33. package/lib/commonjs/index.js.map +1 -1
  34. package/lib/commonjs/layouts/BaseLayout.js +1 -1
  35. package/lib/commonjs/layouts/BaseLayout.js.map +1 -1
  36. package/lib/commonjs/layouts/ParallaxLayout.js +1 -1
  37. package/lib/commonjs/layouts/ParallaxLayout.js.map +1 -1
  38. package/lib/commonjs/layouts/index.js.map +1 -1
  39. package/lib/commonjs/layouts/normal.js +1 -1
  40. package/lib/commonjs/layouts/normal.js.map +1 -1
  41. package/lib/commonjs/layouts/parallax.js +1 -1
  42. package/lib/commonjs/layouts/parallax.js.map +1 -1
  43. package/lib/commonjs/layouts/stack.js +1 -1
  44. package/lib/commonjs/layouts/stack.js.map +1 -1
  45. package/lib/commonjs/store/index.js.map +1 -1
  46. package/lib/commonjs/utils/computedWithAutoFillData.js.map +1 -1
  47. package/lib/commonjs/utils/dealWithAnimation.js +1 -1
  48. package/lib/commonjs/utils/dealWithAnimation.js.map +1 -1
  49. package/lib/commonjs/utils/handlerOffsetDirection.js +2 -0
  50. package/lib/commonjs/utils/handlerOffsetDirection.js.map +1 -0
  51. package/lib/commonjs/utils/log.js +1 -1
  52. package/lib/commonjs/utils/log.js.map +1 -1
  53. package/lib/module/Carousel.js +24 -33
  54. package/lib/module/Carousel.js.map +1 -1
  55. package/lib/module/LazyView.js +2 -6
  56. package/lib/module/LazyView.js.map +1 -1
  57. package/lib/module/ScrollViewGesture.js +23 -39
  58. package/lib/module/ScrollViewGesture.js.map +1 -1
  59. package/lib/module/constants/index.js +1 -1
  60. package/lib/module/constants/index.js.map +1 -1
  61. package/lib/module/hooks/computeNewIndexWhenDataChanges.js +43 -0
  62. package/lib/module/hooks/computeNewIndexWhenDataChanges.js.map +1 -0
  63. package/lib/module/hooks/index.test.js +72 -0
  64. package/lib/module/hooks/index.test.js.map +1 -0
  65. package/lib/module/hooks/useAutoPlay.js +5 -19
  66. package/lib/module/hooks/useAutoPlay.js.map +1 -1
  67. package/lib/module/hooks/useCarouselController.js +24 -36
  68. package/lib/module/hooks/useCarouselController.js.map +1 -1
  69. package/lib/module/hooks/useCheckMounted.js +1 -1
  70. package/lib/module/hooks/useCheckMounted.js.map +1 -1
  71. package/lib/module/hooks/useCommonVariables.js +39 -3
  72. package/lib/module/hooks/useCommonVariables.js.map +1 -1
  73. package/lib/module/hooks/useInitProps.js +4 -7
  74. package/lib/module/hooks/useInitProps.js.map +1 -1
  75. package/lib/module/hooks/useLayoutConfig.js +5 -5
  76. package/lib/module/hooks/useLayoutConfig.js.map +1 -1
  77. package/lib/module/hooks/useOffsetX.js +5 -12
  78. package/lib/module/hooks/useOffsetX.js.map +1 -1
  79. package/lib/module/hooks/useOnProgressChange.js +4 -8
  80. package/lib/module/hooks/useOnProgressChange.js.map +1 -1
  81. package/lib/module/hooks/usePropsErrorBoundary.js +6 -6
  82. package/lib/module/hooks/usePropsErrorBoundary.js.map +1 -1
  83. package/lib/module/hooks/useVisibleRanges.js +1 -1
  84. package/lib/module/hooks/useVisibleRanges.js.map +1 -1
  85. package/lib/module/index.js +1 -1
  86. package/lib/module/index.js.map +1 -1
  87. package/lib/module/layouts/BaseLayout.js +15 -16
  88. package/lib/module/layouts/BaseLayout.js.map +1 -1
  89. package/lib/module/layouts/ParallaxLayout.js +9 -9
  90. package/lib/module/layouts/ParallaxLayout.js.map +1 -1
  91. package/lib/module/layouts/index.js +3 -3
  92. package/lib/module/layouts/index.js.map +1 -1
  93. package/lib/module/layouts/normal.js +2 -2
  94. package/lib/module/layouts/normal.js.map +1 -1
  95. package/lib/module/layouts/parallax.js +3 -3
  96. package/lib/module/layouts/parallax.js.map +1 -1
  97. package/lib/module/layouts/stack.js +20 -28
  98. package/lib/module/layouts/stack.js.map +1 -1
  99. package/lib/module/store/index.js +1 -1
  100. package/lib/module/store/index.js.map +1 -1
  101. package/lib/module/utils/computedWithAutoFillData.js +4 -4
  102. package/lib/module/utils/computedWithAutoFillData.js.map +1 -1
  103. package/lib/module/utils/dealWithAnimation.js +4 -4
  104. package/lib/module/utils/dealWithAnimation.js.map +1 -1
  105. package/lib/module/utils/handlerOffsetDirection.js +9 -0
  106. package/lib/module/utils/handlerOffsetDirection.js.map +1 -0
  107. package/lib/module/utils/log.js +2 -1
  108. package/lib/module/utils/log.js.map +1 -1
  109. package/lib/typescript/Carousel.d.ts +2 -2
  110. package/lib/typescript/LazyView.d.ts +1 -1
  111. package/lib/typescript/ScrollViewGesture.d.ts +3 -3
  112. package/lib/typescript/constants/index.d.ts +1 -1
  113. package/lib/typescript/hooks/computeNewIndexWhenDataChanges.d.ts +8 -0
  114. package/lib/typescript/hooks/index.test.d.ts +1 -0
  115. package/lib/typescript/hooks/useAutoPlay.d.ts +1 -1
  116. package/lib/typescript/hooks/useCarouselController.d.ts +5 -5
  117. package/lib/typescript/hooks/useCheckMounted.d.ts +1 -1
  118. package/lib/typescript/hooks/useCommonVariables.d.ts +2 -2
  119. package/lib/typescript/hooks/useInitProps.d.ts +2 -2
  120. package/lib/typescript/hooks/useLayoutConfig.d.ts +2 -2
  121. package/lib/typescript/hooks/useOffsetX.d.ts +3 -3
  122. package/lib/typescript/hooks/useOnProgressChange.d.ts +4 -4
  123. package/lib/typescript/hooks/usePropsErrorBoundary.d.ts +1 -1
  124. package/lib/typescript/hooks/useVisibleRanges.d.ts +1 -1
  125. package/lib/typescript/index.d.ts +2 -2
  126. package/lib/typescript/layouts/BaseLayout.d.ts +5 -4
  127. package/lib/typescript/layouts/ParallaxLayout.d.ts +5 -5
  128. package/lib/typescript/layouts/index.d.ts +4 -4
  129. package/lib/typescript/layouts/parallax.d.ts +15 -15
  130. package/lib/typescript/layouts/stack.d.ts +15 -15
  131. package/lib/typescript/store/index.d.ts +2 -2
  132. package/lib/typescript/types.d.ts +112 -112
  133. package/lib/typescript/utils/dealWithAnimation.d.ts +1 -1
  134. package/lib/typescript/utils/handlerOffsetDirection.d.ts +2 -0
  135. package/package.json +9 -5
  136. package/src/Carousel.tsx +228 -229
  137. package/src/LazyView.tsx +6 -7
  138. package/src/ScrollViewGesture.tsx +251 -257
  139. package/src/constants/index.ts +10 -9
  140. package/src/hooks/computeNewIndexWhenDataChanges.ts +51 -0
  141. package/src/hooks/index.test.ts +82 -0
  142. package/src/hooks/useAutoPlay.ts +59 -59
  143. package/src/hooks/useCarouselController.tsx +296 -299
  144. package/src/hooks/useCheckMounted.ts +9 -9
  145. package/src/hooks/useCommonVariables.ts +64 -27
  146. package/src/hooks/useInitProps.ts +71 -70
  147. package/src/hooks/useLayoutConfig.ts +21 -19
  148. package/src/hooks/useOffsetX.ts +71 -71
  149. package/src/hooks/useOnProgressChange.ts +48 -47
  150. package/src/hooks/usePropsErrorBoundary.ts +27 -26
  151. package/src/hooks/useVisibleRanges.tsx +39 -39
  152. package/src/index.tsx +5 -5
  153. package/src/layouts/BaseLayout.tsx +103 -101
  154. package/src/layouts/ParallaxLayout.tsx +126 -124
  155. package/src/layouts/index.tsx +8 -8
  156. package/src/layouts/normal.ts +16 -16
  157. package/src/layouts/parallax.ts +62 -61
  158. package/src/layouts/stack.ts +320 -315
  159. package/src/store/index.ts +8 -7
  160. package/src/types.ts +111 -110
  161. package/src/utils/computedWithAutoFillData.ts +60 -60
  162. package/src/utils/dealWithAnimation.ts +19 -18
  163. package/src/utils/handlerOffsetDirection.ts +15 -0
  164. package/src/utils/log.ts +4 -3
  165. package/CHANGELOG.md +0 -610
  166. package/src/.DS_Store +0 -0
@@ -1,49 +1,49 @@
1
- import type Animated from 'react-native-reanimated';
2
- import { useDerivedValue } from 'react-native-reanimated';
1
+ import type Animated from "react-native-reanimated";
2
+ import { useDerivedValue } from "react-native-reanimated";
3
3
 
4
4
  export type IVisibleRanges = Animated.SharedValue<{
5
- negativeRange: number[];
6
- positiveRange: number[];
5
+ negativeRange: number[]
6
+ positiveRange: number[]
7
7
  }>;
8
8
 
9
9
  export function useVisibleRanges(options: {
10
- total: number;
11
- viewSize: number;
12
- windowSize?: number;
13
- translation: Animated.SharedValue<number>;
10
+ total: number
11
+ viewSize: number
12
+ windowSize?: number
13
+ translation: Animated.SharedValue<number>
14
14
  }): IVisibleRanges {
15
- const {
16
- total = 0,
17
- viewSize,
18
- translation,
19
- windowSize: _windowSize = 0,
20
- } = options;
15
+ const {
16
+ total = 0,
17
+ viewSize,
18
+ translation,
19
+ windowSize: _windowSize = 0,
20
+ } = options;
21
21
 
22
- const windowSize = total <= _windowSize ? total : _windowSize;
22
+ const windowSize = total <= _windowSize ? total : _windowSize;
23
23
 
24
- const ranges = useDerivedValue(() => {
25
- const positiveCount = Math.round(windowSize / 2);
26
- const negativeCount = windowSize - positiveCount;
27
- let curIndex = Math.round(-translation.value / viewSize);
28
- curIndex = curIndex < 0 ? (curIndex % total) + total : curIndex;
29
- const negativeRange = [
30
- (curIndex - negativeCount + total) % total,
31
- (curIndex - 1 + total) % total,
32
- ];
33
- const positiveRange = [
34
- (curIndex + total) % total,
35
- (curIndex + positiveCount + total) % total,
36
- ];
37
- if (negativeRange[0] < total && negativeRange[0] > negativeRange[1]) {
38
- negativeRange[1] = total - 1;
39
- positiveRange[0] = 0;
40
- }
41
- if (positiveRange[0] > positiveRange[1]) {
42
- negativeRange[1] = total - 1;
43
- positiveRange[0] = 0;
44
- }
45
- return { negativeRange, positiveRange };
46
- }, [total, windowSize, translation]);
24
+ const ranges = useDerivedValue(() => {
25
+ const positiveCount = Math.round(windowSize / 2);
26
+ const negativeCount = windowSize - positiveCount;
27
+ let curIndex = Math.round(-translation.value / viewSize);
28
+ curIndex = curIndex < 0 ? (curIndex % total) + total : curIndex;
29
+ const negativeRange = [
30
+ (curIndex - negativeCount + total) % total,
31
+ (curIndex - 1 + total) % total,
32
+ ];
33
+ const positiveRange = [
34
+ (curIndex + total) % total,
35
+ (curIndex + positiveCount + total) % total,
36
+ ];
37
+ if (negativeRange[0] < total && negativeRange[0] > negativeRange[1]) {
38
+ negativeRange[1] = total - 1;
39
+ positiveRange[0] = 0;
40
+ }
41
+ if (positiveRange[0] > positiveRange[1]) {
42
+ negativeRange[1] = total - 1;
43
+ positiveRange[0] = 0;
44
+ }
45
+ return { negativeRange, positiveRange };
46
+ }, [total, windowSize, translation]);
47
47
 
48
- return ranges;
48
+ return ranges;
49
49
  }
package/src/index.tsx CHANGED
@@ -1,8 +1,8 @@
1
+ import Carousel from "./Carousel";
1
2
  export type {
2
- TCarouselProps,
3
- ICarouselInstance,
4
- IComputedDirectionTypes,
5
- } from './types';
6
- import Carousel from './Carousel';
3
+ TCarouselProps,
4
+ ICarouselInstance,
5
+ IComputedDirectionTypes,
6
+ } from "./types";
7
7
 
8
8
  export default Carousel;
@@ -1,116 +1,118 @@
1
- import React from 'react';
2
- import type { ViewStyle } from 'react-native';
1
+ import React from "react";
2
+ import type { ViewStyle } from "react-native";
3
+ import type { AnimatedStyleProp } from "react-native-reanimated";
3
4
  import Animated, {
4
- AnimatedStyleProp,
5
- runOnJS,
6
- useAnimatedReaction,
7
- useAnimatedStyle,
8
- useDerivedValue,
9
- } from 'react-native-reanimated';
10
- import { useCheckMounted } from '../hooks/useCheckMounted';
11
- import { IOpts, useOffsetX } from '../hooks/useOffsetX';
12
- import type { IVisibleRanges } from '../hooks/useVisibleRanges';
13
- import { LazyView } from '../LazyView';
14
- import { CTX } from '../store';
15
- import type { ILayoutConfig } from './stack';
5
+ runOnJS,
6
+ useAnimatedReaction,
7
+ useAnimatedStyle,
8
+ useDerivedValue,
9
+ } from "react-native-reanimated";
10
+
11
+ import type { ILayoutConfig } from "./stack";
12
+
13
+ import { useCheckMounted } from "../hooks/useCheckMounted";
14
+ import type { IOpts } from "../hooks/useOffsetX";
15
+ import { useOffsetX } from "../hooks/useOffsetX";
16
+ import type { IVisibleRanges } from "../hooks/useVisibleRanges";
17
+ import { LazyView } from "../LazyView";
18
+ import { CTX } from "../store";
16
19
 
17
20
  export type TAnimationStyle = (value: number) => AnimatedStyleProp<ViewStyle>;
18
21
 
19
22
  export const BaseLayout: React.FC<{
20
- index: number;
21
- handlerOffset: Animated.SharedValue<number>;
22
- visibleRanges: IVisibleRanges;
23
- animationStyle: TAnimationStyle;
24
- children: (ctx: {
25
- animationValue: Animated.SharedValue<number>;
26
- }) => React.ReactElement;
23
+ index: number
24
+ handlerOffset: Animated.SharedValue<number>
25
+ visibleRanges: IVisibleRanges
26
+ animationStyle: TAnimationStyle
27
+ children: (ctx: {
28
+ animationValue: Animated.SharedValue<number>
29
+ }) => React.ReactElement
27
30
  }> = (props) => {
28
- const mounted = useCheckMounted();
29
- const { handlerOffset, index, children, visibleRanges, animationStyle } =
30
- props;
31
+ const mounted = useCheckMounted();
32
+ const { handlerOffset, index, children, visibleRanges, animationStyle }
33
+ = props;
31
34
 
32
- const context = React.useContext(CTX);
33
- const {
34
- props: {
35
- loop,
36
- data,
37
- width,
38
- height,
39
- vertical,
40
- customConfig,
41
- mode,
42
- modeConfig,
43
- },
44
- } = context;
45
- const size = vertical ? height : width;
46
- const [shouldUpdate, setShouldUpdate] = React.useState(false);
47
- let offsetXConfig: IOpts = {
48
- handlerOffset: handlerOffset,
49
- index,
50
- size,
51
- data,
52
- loop,
53
- ...(typeof customConfig === 'function' ? customConfig() : {}),
54
- };
35
+ const context = React.useContext(CTX);
36
+ const {
37
+ props: {
38
+ loop,
39
+ data,
40
+ width,
41
+ height,
42
+ vertical,
43
+ customConfig,
44
+ mode,
45
+ modeConfig,
46
+ },
47
+ } = context;
48
+ const size = vertical ? height : width;
49
+ const [shouldUpdate, setShouldUpdate] = React.useState(false);
50
+ let offsetXConfig: IOpts = {
51
+ handlerOffset,
52
+ index,
53
+ size,
54
+ data,
55
+ loop,
56
+ ...(typeof customConfig === "function" ? customConfig() : {}),
57
+ };
55
58
 
56
- if (mode === 'horizontal-stack') {
57
- const { snapDirection, showLength } = modeConfig as ILayoutConfig;
59
+ if (mode === "horizontal-stack") {
60
+ const { snapDirection, showLength } = modeConfig as ILayoutConfig;
58
61
 
59
- offsetXConfig = {
60
- handlerOffset: handlerOffset,
61
- index,
62
- size,
63
- data,
64
- loop,
65
- type: snapDirection === 'right' ? 'negative' : 'positive',
66
- viewCount: showLength,
67
- };
68
- }
62
+ offsetXConfig = {
63
+ handlerOffset,
64
+ index,
65
+ size,
66
+ data,
67
+ loop,
68
+ type: snapDirection === "right" ? "negative" : "positive",
69
+ viewCount: showLength,
70
+ };
71
+ }
69
72
 
70
- const x = useOffsetX(offsetXConfig, visibleRanges);
71
- const animationValue = useDerivedValue(() => x.value / size, [x, size]);
72
- const animatedStyle = useAnimatedStyle(
73
- // @ts-ignore
74
- () => animationStyle(x.value / size),
75
- [animationStyle]
76
- );
73
+ const x = useOffsetX(offsetXConfig, visibleRanges);
74
+ const animationValue = useDerivedValue(() => x.value / size, [x, size]);
75
+ const animatedStyle = useAnimatedStyle(
76
+ () => animationStyle(x.value / size),
77
+ [animationStyle],
78
+ );
77
79
 
78
- const updateView = React.useCallback(
79
- (negativeRange: number[], positiveRange: number[]) => {
80
- mounted.current &&
81
- setShouldUpdate(
82
- (index >= negativeRange[0] && index <= negativeRange[1]) ||
83
- (index >= positiveRange[0] && index <= positiveRange[1])
80
+ const updateView = React.useCallback(
81
+ (negativeRange: number[], positiveRange: number[]) => {
82
+ mounted.current
83
+ && setShouldUpdate(
84
+ (index >= negativeRange[0] && index <= negativeRange[1])
85
+ || (index >= positiveRange[0] && index <= positiveRange[1]),
84
86
  );
85
- },
86
- [index, mounted]
87
- );
87
+ },
88
+ [index, mounted],
89
+ );
88
90
 
89
- useAnimatedReaction(
90
- () => visibleRanges.value,
91
- () => {
92
- runOnJS(updateView)(
93
- visibleRanges.value.negativeRange,
94
- visibleRanges.value.positiveRange
95
- );
96
- },
97
- [visibleRanges.value]
98
- );
91
+ useAnimatedReaction(
92
+ () => visibleRanges.value,
93
+ () => {
94
+ runOnJS(updateView)(
95
+ visibleRanges.value.negativeRange,
96
+ visibleRanges.value.positiveRange,
97
+ );
98
+ },
99
+ [visibleRanges.value],
100
+ );
99
101
 
100
- return (
101
- <Animated.View
102
- style={[
103
- {
104
- width: width || '100%',
105
- height: height || '100%',
106
- position: 'absolute',
107
- },
108
- animatedStyle,
109
- ]}
110
- >
111
- <LazyView shouldUpdate={shouldUpdate}>
112
- {children({ animationValue })}
113
- </LazyView>
114
- </Animated.View>
115
- );
102
+ return (
103
+ <Animated.View
104
+ style={[
105
+ {
106
+ width: width || "100%",
107
+ height: height || "100%",
108
+ position: "absolute",
109
+ },
110
+ animatedStyle,
111
+ ]}
112
+ >
113
+ <LazyView shouldUpdate={shouldUpdate}>
114
+ {children({ animationValue })}
115
+ </LazyView>
116
+ </Animated.View>
117
+ );
116
118
  };
@@ -1,140 +1,142 @@
1
- import React from 'react';
1
+ import React from "react";
2
2
  import Animated, {
3
- Extrapolate,
4
- interpolate,
5
- runOnJS,
6
- useAnimatedReaction,
7
- useAnimatedStyle,
8
- } from 'react-native-reanimated';
9
- import type { IComputedDirectionTypes } from '../types';
10
- import { useOffsetX } from '../hooks/useOffsetX';
11
- import type { IVisibleRanges } from '../hooks/useVisibleRanges';
12
- import { LazyView } from '../LazyView';
13
- import type { ILayoutConfig } from './parallax';
3
+ Extrapolate,
4
+ interpolate,
5
+ runOnJS,
6
+ useAnimatedReaction,
7
+ useAnimatedStyle,
8
+ } from "react-native-reanimated";
14
9
 
15
- export const ParallaxLayout: React.FC<
16
- IComputedDirectionTypes<
17
- {
18
- loop?: boolean;
19
- handlerOffset: Animated.SharedValue<number>;
20
- index: number;
21
- data: unknown[];
22
- visibleRanges: IVisibleRanges;
23
- } & ILayoutConfig
24
- >
25
- > = (props) => {
26
- const {
27
- handlerOffset,
28
- parallaxScrollingOffset = 100,
29
- parallaxScrollingScale = 0.8,
30
- parallaxAdjacentItemScale = Math.pow(parallaxScrollingScale, 2),
31
- index,
32
- width,
33
- height,
34
- loop,
35
- data,
36
- children,
37
- visibleRanges,
38
- vertical,
39
- } = props;
10
+ import type { ILayoutConfig } from "./parallax";
40
11
 
41
- const [shouldUpdate, setShouldUpdate] = React.useState(false);
12
+ import { useOffsetX } from "../hooks/useOffsetX";
13
+ import type { IVisibleRanges } from "../hooks/useVisibleRanges";
14
+ import { LazyView } from "../LazyView";
15
+ import type { IComputedDirectionTypes } from "../types";
42
16
 
43
- const size = props.vertical ? props.height : props.width;
17
+ export const ParallaxLayout: React.FC<
18
+ IComputedDirectionTypes<
19
+ {
20
+ loop?: boolean
21
+ handlerOffset: Animated.SharedValue<number>
22
+ index: number
23
+ data: unknown[]
24
+ visibleRanges: IVisibleRanges
25
+ } & ILayoutConfig
26
+ >
27
+ > = (props) => {
28
+ const {
29
+ handlerOffset,
30
+ parallaxScrollingOffset = 100,
31
+ parallaxScrollingScale = 0.8,
32
+ parallaxAdjacentItemScale = parallaxScrollingScale ** 2,
33
+ index,
34
+ width,
35
+ height,
36
+ loop,
37
+ data,
38
+ children,
39
+ visibleRanges,
40
+ vertical,
41
+ } = props;
44
42
 
45
- const x = useOffsetX(
46
- {
47
- handlerOffset: handlerOffset,
48
- index,
49
- size,
50
- data,
51
- loop,
52
- },
53
- visibleRanges
54
- );
43
+ const [shouldUpdate, setShouldUpdate] = React.useState(false);
55
44
 
56
- const offsetXStyle = useAnimatedStyle(() => {
57
- const value = x.value / size;
45
+ const size = props.vertical ? props.height : props.width;
58
46
 
59
- const translate = interpolate(
60
- value,
61
- [-1, 0, 1],
62
- [
63
- -size + parallaxScrollingOffset,
64
- 0,
65
- size - parallaxScrollingOffset,
66
- ],
67
- Extrapolate.EXTEND
68
- );
47
+ const x = useOffsetX(
48
+ {
49
+ handlerOffset,
50
+ index,
51
+ size,
52
+ data,
53
+ loop,
54
+ },
55
+ visibleRanges,
56
+ );
69
57
 
70
- const zIndex = interpolate(
71
- value,
72
- [-1, 0, 1],
73
- [0, size, 0],
74
- Extrapolate.CLAMP
75
- );
58
+ const offsetXStyle = useAnimatedStyle(() => {
59
+ const value = x.value / size;
76
60
 
77
- const scale = interpolate(
78
- value,
79
- [-1, 0, 1],
80
- [
81
- parallaxAdjacentItemScale,
82
- parallaxScrollingScale,
83
- parallaxAdjacentItemScale,
84
- ],
85
- Extrapolate.CLAMP
86
- );
61
+ const translate = interpolate(
62
+ value,
63
+ [-1, 0, 1],
64
+ [
65
+ -size + parallaxScrollingOffset,
66
+ 0,
67
+ size - parallaxScrollingOffset,
68
+ ],
69
+ Extrapolate.EXTEND,
70
+ );
87
71
 
88
- return {
89
- transform: [
90
- vertical
91
- ? {
92
- translateY: translate,
93
- }
94
- : {
95
- translateX: translate,
96
- },
97
- {
98
- scale,
99
- },
100
- ],
101
- zIndex,
102
- };
103
- }, [loop, vertical, parallaxScrollingOffset]);
72
+ const zIndex = interpolate(
73
+ value,
74
+ [-1, 0, 1],
75
+ [0, size, 0],
76
+ Extrapolate.CLAMP,
77
+ );
104
78
 
105
- const updateView = React.useCallback(
106
- (negativeRange: number[], positiveRange: number[]) => {
107
- setShouldUpdate(
108
- (index >= negativeRange[0] && index <= negativeRange[1]) ||
109
- (index >= positiveRange[0] && index <= positiveRange[1])
110
- );
111
- },
112
- [index]
79
+ const scale = interpolate(
80
+ value,
81
+ [-1, 0, 1],
82
+ [
83
+ parallaxAdjacentItemScale,
84
+ parallaxScrollingScale,
85
+ parallaxAdjacentItemScale,
86
+ ],
87
+ Extrapolate.CLAMP,
113
88
  );
114
89
 
115
- useAnimatedReaction(
116
- () => visibleRanges.value,
117
- () => {
118
- runOnJS(updateView)(
119
- visibleRanges.value.negativeRange,
120
- visibleRanges.value.positiveRange
121
- );
90
+ return {
91
+ transform: [
92
+ vertical
93
+ ? {
94
+ translateY: translate,
95
+ }
96
+ : {
97
+ translateX: translate,
98
+ },
99
+ {
100
+ scale,
122
101
  },
123
- [visibleRanges.value]
124
- );
102
+ ],
103
+ zIndex,
104
+ };
105
+ }, [loop, vertical, parallaxScrollingOffset]);
125
106
 
126
- return (
127
- <Animated.View
128
- style={[
129
- {
130
- width: width || '100%',
131
- height: height || '100%',
132
- position: 'absolute',
133
- },
134
- offsetXStyle,
135
- ]}
136
- >
137
- <LazyView shouldUpdate={shouldUpdate}>{children}</LazyView>
138
- </Animated.View>
139
- );
107
+ const updateView = React.useCallback(
108
+ (negativeRange: number[], positiveRange: number[]) => {
109
+ setShouldUpdate(
110
+ (index >= negativeRange[0] && index <= negativeRange[1])
111
+ || (index >= positiveRange[0] && index <= positiveRange[1]),
112
+ );
113
+ },
114
+ [index],
115
+ );
116
+
117
+ useAnimatedReaction(
118
+ () => visibleRanges.value,
119
+ () => {
120
+ runOnJS(updateView)(
121
+ visibleRanges.value.negativeRange,
122
+ visibleRanges.value.positiveRange,
123
+ );
124
+ },
125
+ [visibleRanges.value],
126
+ );
127
+
128
+ return (
129
+ <Animated.View
130
+ style={[
131
+ {
132
+ width: width || "100%",
133
+ height: height || "100%",
134
+ position: "absolute",
135
+ },
136
+ offsetXStyle,
137
+ ]}
138
+ >
139
+ <LazyView shouldUpdate={shouldUpdate}>{children}</LazyView>
140
+ </Animated.View>
141
+ );
140
142
  };
@@ -1,12 +1,12 @@
1
- import { normalLayout } from './normal';
2
- import { parallaxLayout } from './parallax';
3
- import { horizontalStackLayout, verticalStackLayout } from './stack';
1
+ import { normalLayout } from "./normal";
2
+ import { parallaxLayout } from "./parallax";
3
+ import { horizontalStackLayout, verticalStackLayout } from "./stack";
4
4
 
5
- export type TMode = 'parallax' | 'horizontal-stack' | 'vertical-stack';
5
+ export type TMode = "parallax" | "horizontal-stack" | "vertical-stack";
6
6
 
7
7
  export const Layouts = {
8
- normal: normalLayout,
9
- parallax: parallaxLayout,
10
- horizontalStack: horizontalStackLayout,
11
- verticalStack: verticalStackLayout,
8
+ normal: normalLayout,
9
+ parallax: parallaxLayout,
10
+ horizontalStack: horizontalStackLayout,
11
+ verticalStack: verticalStackLayout,
12
12
  };
@@ -1,22 +1,22 @@
1
- import { interpolate } from 'react-native-reanimated';
1
+ import { interpolate } from "react-native-reanimated";
2
2
 
3
3
  export function normalLayout(opts: { size: number; vertical: boolean }) {
4
- const { size, vertical } = opts;
4
+ const { size, vertical } = opts;
5
5
 
6
- return (value: number) => {
7
- 'worklet';
8
- const translate = interpolate(value, [-1, 0, 1], [-size, 0, size]);
6
+ return (value: number) => {
7
+ "worklet";
8
+ const translate = interpolate(value, [-1, 0, 1], [-size, 0, size]);
9
9
 
10
- return {
11
- transform: [
12
- vertical
13
- ? {
14
- translateY: translate,
15
- }
16
- : {
17
- translateX: translate,
18
- },
19
- ],
20
- };
10
+ return {
11
+ transform: [
12
+ vertical
13
+ ? {
14
+ translateY: translate,
15
+ }
16
+ : {
17
+ translateX: translate,
18
+ },
19
+ ],
21
20
  };
21
+ };
22
22
  }