react-native-reanimated-carousel 4.0.0-alpha.1 → 4.0.0-alpha.11

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 (173) hide show
  1. package/README.md +2 -3
  2. package/lib/commonjs/{layouts → components}/BaseLayout.js +5 -21
  3. package/lib/commonjs/components/BaseLayout.js.map +1 -0
  4. package/lib/commonjs/components/Carousel.js +28 -48
  5. package/lib/commonjs/components/Carousel.js.map +1 -1
  6. package/lib/commonjs/components/ItemRenderer.js +80 -0
  7. package/lib/commonjs/components/ItemRenderer.js.map +1 -0
  8. package/lib/commonjs/components/Pagination/Basic/PaginationItem.js +81 -0
  9. package/lib/commonjs/components/Pagination/Basic/PaginationItem.js.map +1 -0
  10. package/lib/commonjs/components/Pagination/Basic/index.js +57 -0
  11. package/lib/commonjs/components/Pagination/Basic/index.js.map +1 -0
  12. package/lib/commonjs/components/Pagination/index.js +14 -0
  13. package/lib/commonjs/components/Pagination/index.js.map +1 -0
  14. package/lib/commonjs/components/ScrollViewGesture.js +51 -33
  15. package/lib/commonjs/components/ScrollViewGesture.js.map +1 -1
  16. package/lib/commonjs/components/rnr-demo.test.js +45 -0
  17. package/lib/commonjs/components/rnr-demo.test.js.map +1 -0
  18. package/lib/commonjs/hooks/useCarouselController.js +12 -11
  19. package/lib/commonjs/hooks/useCarouselController.js.map +1 -1
  20. package/lib/commonjs/hooks/useCommonVariables.js +38 -12
  21. package/lib/commonjs/hooks/useCommonVariables.js.map +1 -1
  22. package/lib/commonjs/hooks/useCommonVariables.test.js +38 -0
  23. package/lib/commonjs/hooks/useCommonVariables.test.js.map +1 -0
  24. package/lib/commonjs/hooks/useLayoutConfig.js.map +1 -1
  25. package/lib/commonjs/hooks/useOffsetX.js +9 -6
  26. package/lib/commonjs/hooks/useOffsetX.js.map +1 -1
  27. package/lib/commonjs/hooks/useOffsetX.test.js +53 -0
  28. package/lib/commonjs/hooks/useOffsetX.test.js.map +1 -0
  29. package/lib/commonjs/hooks/useOnProgressChange.js +4 -1
  30. package/lib/commonjs/hooks/useOnProgressChange.js.map +1 -1
  31. package/lib/commonjs/hooks/usePanGestureProxy.js +84 -0
  32. package/lib/commonjs/hooks/usePanGestureProxy.js.map +1 -0
  33. package/lib/commonjs/hooks/usePanGestureProxy.test.js +397 -0
  34. package/lib/commonjs/hooks/usePanGestureProxy.test.js.map +1 -0
  35. package/lib/commonjs/hooks/useUpdateGestureConfig.js.map +1 -1
  36. package/lib/commonjs/hooks/useVisibleRanges.js +48 -19
  37. package/lib/commonjs/hooks/useVisibleRanges.js.map +1 -1
  38. package/lib/commonjs/hooks/useVisibleRanges.test.js +162 -0
  39. package/lib/commonjs/hooks/useVisibleRanges.test.js.map +1 -0
  40. package/lib/commonjs/index.js +8 -0
  41. package/lib/commonjs/index.js.map +1 -1
  42. package/lib/commonjs/utils/{computeNewIndexWhenDataChanges.js → compute-offset-if-data-changed.js} +3 -3
  43. package/lib/commonjs/utils/compute-offset-if-data-changed.js.map +1 -0
  44. package/lib/commonjs/utils/compute-offset-if-data-changed.test.js +30 -0
  45. package/lib/commonjs/utils/compute-offset-if-data-changed.test.js.map +1 -0
  46. package/lib/commonjs/utils/compute-offset-if-size-changed.js +18 -0
  47. package/lib/commonjs/utils/compute-offset-if-size-changed.js.map +1 -0
  48. package/lib/commonjs/utils/compute-offset-if-size-changed.test.js +72 -0
  49. package/lib/commonjs/utils/compute-offset-if-size-changed.test.js.map +1 -0
  50. package/lib/commonjs/utils/handleroffset-direction.js +5 -5
  51. package/lib/commonjs/utils/handleroffset-direction.js.map +1 -1
  52. package/lib/commonjs/utils/handleroffset-direction.test.js +46 -0
  53. package/lib/commonjs/utils/handleroffset-direction.test.js.map +1 -0
  54. package/lib/commonjs/utils/index.test.js +6 -6
  55. package/lib/commonjs/utils/index.test.js.map +1 -1
  56. package/lib/module/{layouts → components}/BaseLayout.js +6 -16
  57. package/lib/module/components/BaseLayout.js.map +1 -0
  58. package/lib/module/components/Carousel.js +27 -44
  59. package/lib/module/components/Carousel.js.map +1 -1
  60. package/lib/module/components/ItemRenderer.js +62 -0
  61. package/lib/module/components/ItemRenderer.js.map +1 -0
  62. package/lib/module/components/Pagination/Basic/PaginationItem.js +63 -0
  63. package/lib/module/components/Pagination/Basic/PaginationItem.js.map +1 -0
  64. package/lib/module/components/Pagination/Basic/index.js +42 -0
  65. package/lib/module/components/Pagination/Basic/index.js.map +1 -0
  66. package/lib/module/components/Pagination/index.js +5 -0
  67. package/lib/module/components/Pagination/index.js.map +1 -0
  68. package/lib/module/components/ScrollViewGesture.js +53 -34
  69. package/lib/module/components/ScrollViewGesture.js.map +1 -1
  70. package/lib/module/components/rnr-demo.test.js +33 -0
  71. package/lib/module/components/rnr-demo.test.js.map +1 -0
  72. package/lib/module/hooks/useCarouselController.js +12 -11
  73. package/lib/module/hooks/useCarouselController.js.map +1 -1
  74. package/lib/module/hooks/useCommonVariables.js +38 -8
  75. package/lib/module/hooks/useCommonVariables.js.map +1 -1
  76. package/lib/module/hooks/useCommonVariables.test.js +34 -0
  77. package/lib/module/hooks/useCommonVariables.test.js.map +1 -0
  78. package/lib/module/hooks/useLayoutConfig.js.map +1 -1
  79. package/lib/module/hooks/useOffsetX.js +9 -6
  80. package/lib/module/hooks/useOffsetX.js.map +1 -1
  81. package/lib/module/hooks/useOffsetX.test.js +48 -0
  82. package/lib/module/hooks/useOffsetX.test.js.map +1 -0
  83. package/lib/module/hooks/useOnProgressChange.js +4 -1
  84. package/lib/module/hooks/useOnProgressChange.js.map +1 -1
  85. package/lib/module/hooks/usePanGestureProxy.js +71 -0
  86. package/lib/module/hooks/usePanGestureProxy.js.map +1 -0
  87. package/lib/module/hooks/usePanGestureProxy.test.js +383 -0
  88. package/lib/module/hooks/usePanGestureProxy.test.js.map +1 -0
  89. package/lib/module/hooks/useUpdateGestureConfig.js.map +1 -1
  90. package/lib/module/hooks/useVisibleRanges.js +47 -19
  91. package/lib/module/hooks/useVisibleRanges.js.map +1 -1
  92. package/lib/module/hooks/useVisibleRanges.test.js +157 -0
  93. package/lib/module/hooks/useVisibleRanges.test.js.map +1 -0
  94. package/lib/module/index.js +1 -0
  95. package/lib/module/index.js.map +1 -1
  96. package/lib/module/utils/{computeNewIndexWhenDataChanges.js → compute-offset-if-data-changed.js} +2 -2
  97. package/lib/module/utils/compute-offset-if-data-changed.js.map +1 -0
  98. package/lib/module/utils/compute-offset-if-data-changed.test.js +27 -0
  99. package/lib/module/utils/compute-offset-if-data-changed.test.js.map +1 -0
  100. package/lib/module/utils/compute-offset-if-size-changed.js +11 -0
  101. package/lib/module/utils/compute-offset-if-size-changed.js.map +1 -0
  102. package/lib/module/utils/compute-offset-if-size-changed.test.js +69 -0
  103. package/lib/module/utils/compute-offset-if-size-changed.test.js.map +1 -0
  104. package/lib/module/utils/handleroffset-direction.js +5 -5
  105. package/lib/module/utils/handleroffset-direction.js.map +1 -1
  106. package/lib/module/utils/handleroffset-direction.test.js +41 -0
  107. package/lib/module/utils/handleroffset-direction.test.js.map +1 -0
  108. package/lib/module/utils/index.test.js +6 -6
  109. package/lib/module/utils/index.test.js.map +1 -1
  110. package/lib/typescript/components/ItemRenderer.d.ts +22 -0
  111. package/lib/typescript/components/Pagination/Basic/PaginationItem.d.ts +17 -0
  112. package/lib/typescript/components/Pagination/Basic/index.d.ts +16 -0
  113. package/lib/typescript/components/Pagination/index.d.ts +3 -0
  114. package/lib/typescript/components/ScrollViewGesture.d.ts +1 -1
  115. package/lib/typescript/components/rnr-demo.test.d.ts +1 -0
  116. package/lib/typescript/hooks/useCarouselController.d.ts +3 -2
  117. package/lib/typescript/hooks/useCommonVariables.test.d.ts +1 -0
  118. package/lib/typescript/hooks/useLayoutConfig.d.ts +1 -1
  119. package/lib/typescript/hooks/useOffsetX.test.d.ts +1 -0
  120. package/lib/typescript/hooks/usePanGestureProxy.d.ts +9 -0
  121. package/lib/typescript/hooks/usePanGestureProxy.test.d.ts +1 -0
  122. package/lib/typescript/hooks/useUpdateGestureConfig.d.ts +3 -2
  123. package/lib/typescript/hooks/useVisibleRanges.d.ts +8 -4
  124. package/lib/typescript/hooks/useVisibleRanges.test.d.ts +1 -0
  125. package/lib/typescript/index.d.ts +2 -0
  126. package/lib/typescript/types.d.ts +27 -16
  127. package/lib/typescript/utils/{computeNewIndexWhenDataChanges.d.ts → compute-offset-if-data-changed.d.ts} +1 -1
  128. package/lib/typescript/utils/compute-offset-if-data-changed.test.d.ts +1 -0
  129. package/lib/typescript/utils/compute-offset-if-size-changed.d.ts +5 -0
  130. package/lib/typescript/utils/compute-offset-if-size-changed.test.d.ts +1 -0
  131. package/lib/typescript/utils/handleroffset-direction.d.ts +2 -1
  132. package/lib/typescript/utils/handleroffset-direction.test.d.ts +1 -0
  133. package/package.json +18 -60
  134. package/src/{layouts → components}/BaseLayout.tsx +7 -35
  135. package/src/components/Carousel.tsx +33 -71
  136. package/src/components/ItemRenderer.tsx +105 -0
  137. package/src/components/Pagination/Basic/PaginationItem.tsx +111 -0
  138. package/src/components/Pagination/Basic/index.tsx +81 -0
  139. package/src/components/Pagination/index.tsx +5 -0
  140. package/src/components/ScrollViewGesture.tsx +74 -49
  141. package/src/components/rnr-demo.test.tsx +43 -0
  142. package/src/hooks/useCarouselController.tsx +24 -21
  143. package/src/hooks/useCommonVariables.test.tsx +41 -0
  144. package/src/hooks/useCommonVariables.ts +35 -10
  145. package/src/hooks/useLayoutConfig.ts +1 -1
  146. package/src/hooks/useOffsetX.test.ts +54 -0
  147. package/src/hooks/useOffsetX.ts +33 -31
  148. package/src/hooks/useOnProgressChange.ts +7 -2
  149. package/src/hooks/usePanGestureProxy.test.tsx +376 -0
  150. package/src/hooks/usePanGestureProxy.ts +110 -0
  151. package/src/hooks/useUpdateGestureConfig.ts +4 -2
  152. package/src/hooks/useVisibleRanges.test.tsx +179 -0
  153. package/src/hooks/useVisibleRanges.tsx +72 -24
  154. package/src/index.tsx +3 -0
  155. package/src/types.ts +28 -17
  156. package/src/utils/compute-offset-if-data-changed.test.ts +30 -0
  157. package/src/utils/{computeNewIndexWhenDataChanges.ts → compute-offset-if-data-changed.ts} +1 -1
  158. package/src/utils/compute-offset-if-size-changed.test.ts +78 -0
  159. package/src/utils/compute-offset-if-size-changed.ts +11 -0
  160. package/src/utils/handleroffset-direction.test.ts +52 -0
  161. package/src/utils/handleroffset-direction.ts +12 -9
  162. package/src/utils/index.test.ts +6 -6
  163. package/lib/commonjs/layouts/BaseLayout.js.map +0 -1
  164. package/lib/commonjs/layouts/ParallaxLayout.js +0 -84
  165. package/lib/commonjs/layouts/ParallaxLayout.js.map +0 -1
  166. package/lib/commonjs/utils/computeNewIndexWhenDataChanges.js.map +0 -1
  167. package/lib/module/layouts/BaseLayout.js.map +0 -1
  168. package/lib/module/layouts/ParallaxLayout.js +0 -61
  169. package/lib/module/layouts/ParallaxLayout.js.map +0 -1
  170. package/lib/module/utils/computeNewIndexWhenDataChanges.js.map +0 -1
  171. package/lib/typescript/layouts/ParallaxLayout.d.ts +0 -13
  172. package/src/layouts/ParallaxLayout.tsx +0 -141
  173. /package/lib/typescript/{layouts → components}/BaseLayout.d.ts +0 -0
@@ -21,12 +21,13 @@ interface IOpts {
21
21
  loop: boolean
22
22
  size: number
23
23
  dataLength: number
24
- autoFillData: TCarouselProps["autoFillData"]
25
24
  handlerOffset: Animated.SharedValue<number>
25
+ autoFillData: TCarouselProps["autoFillData"]
26
26
  withAnimation?: TCarouselProps["withAnimation"]
27
+ fixedDirection?: TCarouselProps["fixedDirection"]
27
28
  duration?: number
28
29
  defaultIndex?: number
29
- onScrollBegin?: () => void
30
+ onScrollStart?: () => void
30
31
  onScrollEnd?: () => void
31
32
  }
32
33
 
@@ -48,6 +49,7 @@ export function useCarouselController(options: IOpts): ICarouselController {
48
49
  defaultIndex = 0,
49
50
  duration,
50
51
  autoFillData,
52
+ fixedDirection,
51
53
  } = options;
52
54
 
53
55
  const dataInfo = React.useMemo(
@@ -136,8 +138,8 @@ export function useCarouselController(options: IOpts): ICarouselController {
136
138
  options.onScrollEnd?.();
137
139
  }, [options]);
138
140
 
139
- const onScrollBegin = React.useCallback(() => {
140
- options.onScrollBegin?.();
141
+ const onScrollStart = React.useCallback(() => {
142
+ options.onScrollStart?.();
141
143
  }, [options]);
142
144
 
143
145
  const scrollWithTiming = React.useCallback(
@@ -171,7 +173,7 @@ export function useCarouselController(options: IOpts): ICarouselController {
171
173
  if (!canSliding() || (!loop && index.value >= dataInfo.length - 1))
172
174
  return;
173
175
 
174
- onScrollBegin?.();
176
+ onScrollStart?.();
175
177
 
176
178
  const nextPage = currentFixedPage() + count;
177
179
  index.value = nextPage;
@@ -192,7 +194,7 @@ export function useCarouselController(options: IOpts): ICarouselController {
192
194
  loop,
193
195
  index,
194
196
  dataInfo,
195
- onScrollBegin,
197
+ onScrollStart,
196
198
  handlerOffset,
197
199
  size,
198
200
  scrollWithTiming,
@@ -205,7 +207,7 @@ export function useCarouselController(options: IOpts): ICarouselController {
205
207
  const { count = 1, animated = true, onFinished } = opts;
206
208
  if (!canSliding() || (!loop && index.value <= 0)) return;
207
209
 
208
- onScrollBegin?.();
210
+ onScrollStart?.();
209
211
 
210
212
  const prevPage = currentFixedPage() - count;
211
213
  index.value = prevPage;
@@ -225,7 +227,7 @@ export function useCarouselController(options: IOpts): ICarouselController {
225
227
  canSliding,
226
228
  loop,
227
229
  index,
228
- onScrollBegin,
230
+ onScrollStart,
229
231
  handlerOffset,
230
232
  size,
231
233
  scrollWithTiming,
@@ -239,9 +241,9 @@ export function useCarouselController(options: IOpts): ICarouselController {
239
241
  if (i === index.value) return;
240
242
  if (!canSliding()) return;
241
243
 
242
- onScrollBegin?.();
244
+ onScrollStart?.();
243
245
  // direction -> 1 | -1
244
- const direction = handlerOffsetDirection(handlerOffset);
246
+ const direction = handlerOffsetDirection(handlerOffset, fixedDirection);
245
247
 
246
248
  // target offset
247
249
  const offset = i * size * direction;
@@ -252,16 +254,16 @@ export function useCarouselController(options: IOpts): ICarouselController {
252
254
 
253
255
  if (loop) {
254
256
  isCloseToNextLoop
255
- = Math.abs(handlerOffset.value % totalSize) / totalSize
256
- >= 0.5;
257
+ = Math.abs(handlerOffset.value % totalSize) / totalSize
258
+ >= 0.5;
257
259
  }
258
260
 
259
261
  const finalOffset
260
- = (Math.floor(Math.abs(handlerOffset.value / totalSize))
261
- + (isCloseToNextLoop ? 1 : 0))
262
- * totalSize
263
- * direction
264
- + offset;
262
+ = (Math.floor(Math.abs(handlerOffset.value / totalSize))
263
+ + (isCloseToNextLoop ? 1 : 0))
264
+ * totalSize
265
+ * direction
266
+ + offset;
265
267
 
266
268
  if (animated) {
267
269
  index.value = i;
@@ -274,13 +276,14 @@ export function useCarouselController(options: IOpts): ICarouselController {
274
276
  }
275
277
  },
276
278
  [
279
+ size,
280
+ loop,
277
281
  index,
278
- canSliding,
279
- onScrollBegin,
282
+ fixedDirection,
280
283
  handlerOffset,
281
- size,
282
284
  dataInfo.length,
283
- loop,
285
+ canSliding,
286
+ onScrollStart,
284
287
  scrollWithTiming,
285
288
  ],
286
289
  );
@@ -0,0 +1,41 @@
1
+ import { renderHook } from "@testing-library/react-hooks";
2
+
3
+ import { useCommonVariables } from "./useCommonVariables";
4
+
5
+ type UseCommonVariablesInput = Parameters<typeof useCommonVariables>[0];
6
+
7
+ const input = {
8
+ vertical: false,
9
+ width: 700,
10
+ height: 350,
11
+ loop: true,
12
+ enabled: true,
13
+ testID: "xxx",
14
+ style: {
15
+ width: "100%",
16
+ },
17
+ autoPlay: false,
18
+ autoPlayInterval: 2000,
19
+ data: [0, 1, 2, 3],
20
+ pagingEnabled: true,
21
+ defaultIndex: 0,
22
+ autoFillData: true,
23
+ dataLength: 4,
24
+ rawData: [0, 1, 2, 3],
25
+ rawDataLength: 4,
26
+ scrollAnimationDuration: 500,
27
+ snapEnabled: true,
28
+ overscrollEnabled: true,
29
+ } as unknown as UseCommonVariablesInput;
30
+
31
+ describe("useCommonVariables", () => {
32
+ it("should return the correct values", async () => {
33
+ const hook = renderHook(() => useCommonVariables(input));
34
+
35
+ expect(hook.result.current.size).toMatchInlineSnapshot("700");
36
+ expect(hook.result.current.validLength).toMatchInlineSnapshot("3");
37
+ expect(hook.result.current.handlerOffset.value).toMatchInlineSnapshot(
38
+ "-0",
39
+ );
40
+ });
41
+ });
@@ -1,10 +1,10 @@
1
- import React from "react";
2
1
  import type Animated from "react-native-reanimated";
3
2
  import { useSharedValue, useAnimatedReaction } from "react-native-reanimated";
4
3
 
5
4
  import type { TInitializeCarouselProps } from "./useInitProps";
6
5
 
7
- import { computeNewIndexWhenDataChanges } from "../utils/computeNewIndexWhenDataChanges";
6
+ import { computeOffsetIfDataChanged } from "../utils/compute-offset-if-data-changed";
7
+ import { computeOffsetIfSizeChanged } from "../utils/compute-offset-if-size-changed";
8
8
  import { handlerOffsetDirection } from "../utils/handleroffset-direction";
9
9
 
10
10
  interface ICommonVariables {
@@ -26,21 +26,20 @@ export function useCommonVariables(
26
26
  loop,
27
27
  } = props;
28
28
  const size = vertical ? height : width;
29
- const validLength = dataLength - 1;
30
29
  const defaultHandlerOffsetValue = -Math.abs(defaultIndex * size);
31
30
  const _handlerOffset = useSharedValue<number>(defaultHandlerOffsetValue);
32
31
  const handlerOffset = defaultScrollOffsetValue ?? _handlerOffset;
33
32
  const prevDataLength = useSharedValue(dataLength);
33
+ const prevSize = useSharedValue(size);
34
34
 
35
- React.useEffect(() => {
36
- handlerOffset.value = defaultHandlerOffsetValue;
37
- }, [vertical, handlerOffset, defaultHandlerOffsetValue]);
38
-
35
+ /**
36
+ * When data changes, we need to compute new index for handlerOffset
37
+ */
39
38
  useAnimatedReaction(() => {
40
39
  const previousLength = prevDataLength.value;
41
40
  const currentLength = dataLength;
42
41
  const isLengthChanged = previousLength !== currentLength;
43
- const shouldComputed = isLengthChanged && loop;
42
+ const shouldComputed = (isLengthChanged && loop);
44
43
 
45
44
  if (shouldComputed)
46
45
  prevDataLength.value = dataLength;
@@ -55,7 +54,7 @@ export function useCommonVariables(
55
54
  // direction -> 1 | -1
56
55
  const direction = handlerOffsetDirection(handlerOffset);
57
56
 
58
- handlerOffset.value = computeNewIndexWhenDataChanges({
57
+ handlerOffset.value = computeOffsetIfDataChanged({
59
58
  direction,
60
59
  previousLength,
61
60
  currentLength,
@@ -65,9 +64,35 @@ export function useCommonVariables(
65
64
  }
66
65
  }, [dataLength, loop]);
67
66
 
67
+ /**
68
+ * When size changes, we need to compute new index for handlerOffset
69
+ */
70
+ useAnimatedReaction(() => {
71
+ const previousSize = prevSize.value;
72
+ const isSizeChanged = previousSize !== size;
73
+ const shouldComputed = isSizeChanged;
74
+
75
+ if (shouldComputed)
76
+ prevSize.value = size;
77
+
78
+ return {
79
+ shouldComputed,
80
+ previousSize,
81
+ size,
82
+ };
83
+ }, ({ shouldComputed, previousSize, size }) => {
84
+ if (shouldComputed) {
85
+ handlerOffset.value = computeOffsetIfSizeChanged({
86
+ handlerOffset: handlerOffset.value,
87
+ prevSize: previousSize,
88
+ size,
89
+ });
90
+ }
91
+ }, [size]);
92
+
68
93
  return {
69
94
  size,
70
- validLength,
95
+ validLength: dataLength - 1,
71
96
  handlerOffset,
72
97
  };
73
98
  }
@@ -2,8 +2,8 @@ import React from "react";
2
2
 
3
3
  import type { TInitializeCarouselProps } from "./useInitProps";
4
4
 
5
+ import type { TAnimationStyle } from "../components/BaseLayout";
5
6
  import { Layouts } from "../layouts";
6
- import type { TAnimationStyle } from "../layouts/BaseLayout";
7
7
 
8
8
  type TLayoutConfigOpts<T> = TInitializeCarouselProps<T> & { size: number };
9
9
 
@@ -0,0 +1,54 @@
1
+ import { useSharedValue } from "react-native-reanimated";
2
+
3
+ import { renderHook } from "@testing-library/react-hooks";
4
+
5
+ import type { IOpts } from "./useOffsetX";
6
+ import { useOffsetX } from "./useOffsetX";
7
+ import type { IVisibleRanges } from "./useVisibleRanges";
8
+
9
+ describe("useSharedValue", () => {
10
+ it("should return the correct values", async () => {
11
+ const hook = renderHook(() => {
12
+ const range = useSharedValue({
13
+ negativeRange: [7, 9],
14
+ positiveRange: [0, 3],
15
+ }) as IVisibleRanges;
16
+ const inputs: Array<{
17
+ config: IOpts
18
+ range: IVisibleRanges
19
+ }> = Array.from({ length: 10 }).map((_, index) => ({
20
+ config: {
21
+ dataLength: 10,
22
+ handlerOffset: useSharedValue(-0),
23
+ index,
24
+ loop: false,
25
+ size: 393,
26
+ },
27
+ range,
28
+ }));
29
+
30
+ return inputs.map((input) => {
31
+ const { config, range } = input;
32
+
33
+ return useOffsetX(config, range);
34
+ });
35
+ });
36
+
37
+ const expected = hook.result.current.map(v => v.value).slice();
38
+
39
+ expect(expected).toMatchInlineSnapshot(`
40
+ [
41
+ 0,
42
+ 393,
43
+ 786,
44
+ 1179,
45
+ 9007199254740991,
46
+ 9007199254740991,
47
+ 9007199254740991,
48
+ 2751,
49
+ 3144,
50
+ 3537,
51
+ ]
52
+ `);
53
+ });
54
+ });
@@ -35,7 +35,7 @@ export const useOffsetX = (opts: IOpts, visibleRanges: IVisibleRanges) => {
35
35
 
36
36
  const viewCount = _viewCount ?? Math.round((ITEM_LENGTH - 1) / 2);
37
37
  const positiveCount
38
- = type === "positive" ? viewCount : VALID_LENGTH - viewCount;
38
+ = type === "positive" ? viewCount : VALID_LENGTH - viewCount;
39
39
 
40
40
  let startPos = size * index;
41
41
  if (index > positiveCount)
@@ -46,42 +46,44 @@ export const useOffsetX = (opts: IOpts, visibleRanges: IVisibleRanges) => {
46
46
 
47
47
  const x = useDerivedValue(() => {
48
48
  const { negativeRange, positiveRange } = visibleRanges.value;
49
+
49
50
  if (
50
- (index < negativeRange[0] || index > negativeRange[1])
51
- && (index < positiveRange[0] || index > positiveRange[1])
52
- )
53
- return Number.MAX_SAFE_INTEGER;
51
+ (index >= negativeRange[0] && index <= negativeRange[1])
52
+ || (index >= positiveRange[0] && index <= positiveRange[1])
53
+ ) {
54
+ if (loop) {
55
+ const inputRange = [
56
+ -TOTAL_WIDTH,
57
+ MIN - HALF_WIDTH - startPos - Number.MIN_VALUE,
58
+ MIN - HALF_WIDTH - startPos,
59
+ 0,
60
+ MAX + HALF_WIDTH - startPos,
61
+ MAX + HALF_WIDTH - startPos + Number.MIN_VALUE,
62
+ TOTAL_WIDTH,
63
+ ];
54
64
 
55
- if (loop) {
56
- const inputRange = [
57
- -TOTAL_WIDTH,
58
- MIN - HALF_WIDTH - startPos - Number.MIN_VALUE,
59
- MIN - HALF_WIDTH - startPos,
60
- 0,
61
- MAX + HALF_WIDTH - startPos,
62
- MAX + HALF_WIDTH - startPos + Number.MIN_VALUE,
63
- TOTAL_WIDTH,
64
- ];
65
+ const outputRange = [
66
+ startPos,
67
+ MAX + HALF_WIDTH - Number.MIN_VALUE,
68
+ MIN - HALF_WIDTH,
69
+ startPos,
70
+ MAX + HALF_WIDTH,
71
+ MIN - HALF_WIDTH + Number.MIN_VALUE,
72
+ startPos,
73
+ ];
65
74
 
66
- const outputRange = [
67
- startPos,
68
- MAX + HALF_WIDTH - Number.MIN_VALUE,
69
- MIN - HALF_WIDTH,
70
- startPos,
71
- MAX + HALF_WIDTH,
72
- MIN - HALF_WIDTH + Number.MIN_VALUE,
73
- startPos,
74
- ];
75
+ return interpolate(
76
+ handlerOffset.value,
77
+ inputRange,
78
+ outputRange,
79
+ Extrapolate.CLAMP,
80
+ );
81
+ }
75
82
 
76
- return interpolate(
77
- handlerOffset.value,
78
- inputRange,
79
- outputRange,
80
- Extrapolate.CLAMP,
81
- );
83
+ return handlerOffset.value + size * index;
82
84
  }
83
85
 
84
- return handlerOffset.value + size * index;
86
+ return Number.MAX_SAFE_INTEGER;
85
87
  }, [loop, dataLength, viewCount, type, size, visibleRanges]);
86
88
 
87
89
  return x;
@@ -42,8 +42,13 @@ export function useOnProgressChange(
42
42
  if (value > 0)
43
43
  absoluteProgress = rawDataLength - absoluteProgress;
44
44
 
45
- if (onProgressChange)
46
- runOnJS(onProgressChange)(value, absoluteProgress);
45
+ if (onProgressChange) {
46
+ if (typeof onProgressChange === "function")
47
+ runOnJS(onProgressChange)(value, absoluteProgress);
48
+
49
+ else
50
+ onProgressChange.value = absoluteProgress;
51
+ }
47
52
  },
48
53
  [loop, autoFillData, rawDataLength, onProgressChange],
49
54
  );