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,37 +1,74 @@
1
- import React from 'react';
2
- import Animated, { useSharedValue } from 'react-native-reanimated';
3
- import type { TInitializeCarouselProps } from './useInitProps';
1
+ import React from "react";
2
+ import type Animated from "react-native-reanimated";
3
+ import { useSharedValue, useAnimatedReaction } from "react-native-reanimated";
4
+
5
+ import { computeNewIndexWhenDataChanges } from "./computeNewIndexWhenDataChanges";
6
+ import type { TInitializeCarouselProps } from "./useInitProps";
7
+
8
+ import { handlerOffsetDirection } from "../utils/handlerOffsetDirection";
4
9
 
5
10
  interface ICommonVariables {
6
- size: number;
7
- validLength: number;
8
- handlerOffset: Animated.SharedValue<number>;
11
+ size: number
12
+ validLength: number
13
+ handlerOffset: Animated.SharedValue<number>
9
14
  }
10
15
 
11
16
  export function useCommonVariables(
12
- props: TInitializeCarouselProps<any>
17
+ props: TInitializeCarouselProps<any>,
13
18
  ): ICommonVariables {
14
- const {
15
- vertical,
16
- height,
17
- width,
18
- data,
19
- defaultIndex,
20
- defaultScrollOffsetValue,
21
- } = props;
22
- const size = vertical ? height : width;
23
- const validLength = data.length - 1;
24
- const defaultHandlerOffsetValue = -Math.abs(defaultIndex * size);
25
- const _handlerOffset = useSharedValue<number>(defaultHandlerOffsetValue);
26
- const handlerOffset = defaultScrollOffsetValue ?? _handlerOffset;
27
-
28
- React.useEffect(() => {
29
- handlerOffset.value = defaultHandlerOffsetValue;
30
- }, [vertical, handlerOffset, defaultHandlerOffsetValue]);
19
+ const {
20
+ vertical,
21
+ height,
22
+ width,
23
+ data,
24
+ defaultIndex,
25
+ defaultScrollOffsetValue,
26
+ loop,
27
+ } = props;
28
+ const size = vertical ? height : width;
29
+ const validLength = data.length - 1;
30
+ const defaultHandlerOffsetValue = -Math.abs(defaultIndex * size);
31
+ const _handlerOffset = useSharedValue<number>(defaultHandlerOffsetValue);
32
+ const handlerOffset = defaultScrollOffsetValue ?? _handlerOffset;
33
+ const prevData = useSharedValue(data);
34
+
35
+ React.useEffect(() => {
36
+ handlerOffset.value = defaultHandlerOffsetValue;
37
+ }, [vertical, handlerOffset, defaultHandlerOffsetValue]);
38
+
39
+ useAnimatedReaction(() => {
40
+ const _data = data.slice();
41
+ const previousLength = prevData.value.length;
42
+ const currentLength = _data.length;
43
+ const isLengthChanged = previousLength !== currentLength;
44
+ const shouldComputed = isLengthChanged && loop;
45
+
46
+ if (shouldComputed)
47
+ prevData.value = _data;
31
48
 
32
49
  return {
33
- size,
34
- validLength,
35
- handlerOffset,
50
+ shouldComputed,
51
+ previousLength,
52
+ currentLength,
36
53
  };
54
+ }, ({ shouldComputed, previousLength, currentLength }) => {
55
+ if (shouldComputed) {
56
+ // direction -> 1 | -1
57
+ const direction = handlerOffsetDirection(handlerOffset);
58
+
59
+ handlerOffset.value = computeNewIndexWhenDataChanges({
60
+ direction,
61
+ previousLength,
62
+ currentLength,
63
+ size,
64
+ handlerOffset: handlerOffset.value,
65
+ });
66
+ }
67
+ }, [data, loop]);
68
+
69
+ return {
70
+ size,
71
+ validLength,
72
+ handlerOffset,
73
+ };
37
74
  }
@@ -1,82 +1,83 @@
1
- import { computedFillDataWithAutoFillData } from '../utils/computedWithAutoFillData';
2
- import React from 'react';
3
- import type { TCarouselProps } from '../types';
1
+ import React from "react";
2
+
3
+ import type { TCarouselProps } from "../types";
4
+ import { computedFillDataWithAutoFillData } from "../utils/computedWithAutoFillData";
4
5
 
5
6
  type TGetRequiredProps<P extends keyof TCarouselProps> = Record<
6
- P,
7
- Required<TCarouselProps>[P]
7
+ P,
8
+ Required<TCarouselProps>[P]
8
9
  >;
9
10
 
10
11
  export type TInitializeCarouselProps<T> = TCarouselProps<T> &
11
- TGetRequiredProps<
12
- | 'defaultIndex'
13
- | 'loop'
14
- | 'width'
15
- | 'height'
16
- | 'scrollAnimationDuration'
17
- | 'autoPlayInterval'
18
- | 'autoFillData'
19
- > & {
20
- // Raw data that has not been processed
21
- rawData: T[];
22
- };
12
+ TGetRequiredProps<
13
+ | "defaultIndex"
14
+ | "loop"
15
+ | "width"
16
+ | "height"
17
+ | "scrollAnimationDuration"
18
+ | "autoPlayInterval"
19
+ | "autoFillData"
20
+ > & {
21
+ // Raw data that has not been processed
22
+ rawData: T[]
23
+ };
23
24
 
24
25
  export function useInitProps<T>(
25
- props: TCarouselProps<T>
26
+ props: TCarouselProps<T>,
26
27
  ): TInitializeCarouselProps<T> {
27
- const {
28
- defaultIndex = 0,
29
- data: rawData = [],
30
- loop = true,
31
- enabled = true,
32
- autoPlayInterval: _autoPlayInterval = 1000,
33
- scrollAnimationDuration = 500,
34
- style = {},
35
- panGestureHandlerProps = {},
36
- pagingEnabled = true,
37
- autoFillData = true,
38
- snapEnabled = props.enableSnap ?? true,
39
- width: _width,
40
- height: _height,
41
- } = props;
42
-
43
- const width = Math.round(_width || 0);
44
- const height = Math.round(_height || 0);
45
- const autoPlayInterval = Math.max(_autoPlayInterval, 0);
28
+ const {
29
+ defaultIndex = 0,
30
+ data: rawData = [],
31
+ loop = true,
32
+ enabled = true,
33
+ autoPlayInterval: _autoPlayInterval = 1000,
34
+ scrollAnimationDuration = 500,
35
+ style = {},
36
+ panGestureHandlerProps = {},
37
+ pagingEnabled = true,
38
+ autoFillData = true,
39
+ snapEnabled = props.enableSnap ?? true,
40
+ width: _width,
41
+ height: _height,
42
+ } = props;
46
43
 
47
- const data = React.useMemo<T[]>(
48
- () =>
49
- computedFillDataWithAutoFillData<T>({
50
- loop,
51
- autoFillData,
52
- data: rawData,
53
- dataLength: rawData.length,
54
- }),
55
- [rawData, loop, autoFillData]
56
- );
44
+ const width = Math.round(_width || 0);
45
+ const height = Math.round(_height || 0);
46
+ const autoPlayInterval = Math.max(_autoPlayInterval, 0);
57
47
 
58
- if (props.mode === 'vertical-stack' || props.mode === 'horizontal-stack') {
59
- if (!props.modeConfig) {
60
- props.modeConfig = {};
61
- }
62
- props.modeConfig.showLength =
63
- props.modeConfig?.showLength ?? data.length - 1;
64
- }
65
- return {
66
- ...props,
67
- defaultIndex,
68
- autoFillData,
69
- data,
70
- rawData,
48
+ const data = React.useMemo<T[]>(
49
+ () =>
50
+ computedFillDataWithAutoFillData<T>({
71
51
  loop,
72
- enabled,
73
- autoPlayInterval,
74
- scrollAnimationDuration,
75
- style,
76
- panGestureHandlerProps,
77
- pagingEnabled,
78
- snapEnabled,
79
- width,
80
- height,
81
- };
52
+ autoFillData,
53
+ data: rawData,
54
+ dataLength: rawData.length,
55
+ }),
56
+ [rawData, loop, autoFillData],
57
+ );
58
+
59
+ if (props.mode === "vertical-stack" || props.mode === "horizontal-stack") {
60
+ if (!props.modeConfig)
61
+ props.modeConfig = {};
62
+
63
+ props.modeConfig.showLength
64
+ = props.modeConfig?.showLength ?? data.length - 1;
65
+ }
66
+ return {
67
+ ...props,
68
+ defaultIndex,
69
+ autoFillData,
70
+ data,
71
+ rawData,
72
+ loop,
73
+ enabled,
74
+ autoPlayInterval,
75
+ scrollAnimationDuration,
76
+ style,
77
+ panGestureHandlerProps,
78
+ pagingEnabled,
79
+ snapEnabled,
80
+ width,
81
+ height,
82
+ };
82
83
  }
@@ -1,26 +1,28 @@
1
- import React from 'react';
2
- import type { TAnimationStyle } from '../layouts/BaseLayout';
3
- import { Layouts } from '../layouts';
4
- import type { TInitializeCarouselProps } from './useInitProps';
1
+ import React from "react";
2
+
3
+ import type { TInitializeCarouselProps } from "./useInitProps";
4
+
5
+ import { Layouts } from "../layouts";
6
+ import type { TAnimationStyle } from "../layouts/BaseLayout";
5
7
 
6
8
  type TLayoutConfigOpts<T> = TInitializeCarouselProps<T> & { size: number };
7
9
 
8
10
  export function useLayoutConfig<T>(
9
- opts: TLayoutConfigOpts<T>
11
+ opts: TLayoutConfigOpts<T>,
10
12
  ): TAnimationStyle {
11
- const { size, vertical } = opts as Required<TLayoutConfigOpts<T>>;
13
+ const { size, vertical } = opts as Required<TLayoutConfigOpts<T>>;
12
14
 
13
- return React.useMemo(() => {
14
- const baseConfig = { size, vertical };
15
- switch (opts.mode) {
16
- case 'parallax':
17
- return Layouts.parallax(baseConfig, opts.modeConfig);
18
- case 'horizontal-stack':
19
- return Layouts.horizontalStack(opts.modeConfig);
20
- case 'vertical-stack':
21
- return Layouts.verticalStack(opts.modeConfig);
22
- default:
23
- return Layouts.normal(baseConfig);
24
- }
25
- }, [opts.mode, opts.modeConfig, size, vertical]);
15
+ return React.useMemo(() => {
16
+ const baseConfig = { size, vertical };
17
+ switch (opts.mode) {
18
+ case "parallax":
19
+ return Layouts.parallax(baseConfig, opts.modeConfig);
20
+ case "horizontal-stack":
21
+ return Layouts.horizontalStack(opts.modeConfig);
22
+ case "vertical-stack":
23
+ return Layouts.verticalStack(opts.modeConfig);
24
+ default:
25
+ return Layouts.normal(baseConfig);
26
+ }
27
+ }, [opts.mode, opts.modeConfig, size, vertical]);
26
28
  }
@@ -1,87 +1,87 @@
1
- import Animated, {
2
- Extrapolate,
3
- interpolate,
4
- useDerivedValue,
5
- } from 'react-native-reanimated';
6
- import type { IVisibleRanges } from './useVisibleRanges';
1
+ import type Animated from "react-native-reanimated";
2
+ import {
3
+ Extrapolate,
4
+ interpolate,
5
+ useDerivedValue,
6
+ } from "react-native-reanimated";
7
+
8
+ import type { IVisibleRanges } from "./useVisibleRanges";
7
9
 
8
10
  export interface IOpts {
9
- index: number;
10
- size: number;
11
- handlerOffset: Animated.SharedValue<number>;
12
- data: unknown[];
13
- type?: 'positive' | 'negative';
14
- viewCount?: number;
15
- loop?: boolean;
11
+ index: number
12
+ size: number
13
+ handlerOffset: Animated.SharedValue<number>
14
+ data: unknown[]
15
+ type?: "positive" | "negative"
16
+ viewCount?: number
17
+ loop?: boolean
16
18
  }
17
19
 
18
20
  export const useOffsetX = (opts: IOpts, visibleRanges: IVisibleRanges) => {
19
- const {
20
- handlerOffset,
21
- index,
22
- size,
23
- loop,
24
- data,
25
- type = 'positive',
26
- viewCount = Math.round((data.length - 1) / 2),
27
- } = opts;
21
+ const {
22
+ handlerOffset,
23
+ index,
24
+ size,
25
+ loop,
26
+ data,
27
+ type = "positive",
28
+ viewCount = Math.round((data.length - 1) / 2),
29
+ } = opts;
28
30
 
29
- const ITEM_LENGTH = data.length;
30
- const VALID_LENGTH = ITEM_LENGTH - 1;
31
- const TOTAL_WIDTH = size * ITEM_LENGTH;
32
- const HALF_WIDTH = 0.5 * size;
31
+ const ITEM_LENGTH = data.length;
32
+ const VALID_LENGTH = ITEM_LENGTH - 1;
33
+ const TOTAL_WIDTH = size * ITEM_LENGTH;
34
+ const HALF_WIDTH = 0.5 * size;
33
35
 
34
- const positiveCount =
35
- type === 'positive' ? viewCount : VALID_LENGTH - viewCount;
36
+ const positiveCount
37
+ = type === "positive" ? viewCount : VALID_LENGTH - viewCount;
36
38
 
37
- let startPos = size * index;
38
- if (index > positiveCount) {
39
- startPos = (index - ITEM_LENGTH) * size;
40
- }
39
+ let startPos = size * index;
40
+ if (index > positiveCount)
41
+ startPos = (index - ITEM_LENGTH) * size;
41
42
 
42
- const MAX = positiveCount * size;
43
- const MIN = -((VALID_LENGTH - positiveCount) * size);
43
+ const MAX = positiveCount * size;
44
+ const MIN = -((VALID_LENGTH - positiveCount) * size);
44
45
 
45
- const x = useDerivedValue(() => {
46
- const { negativeRange, positiveRange } = visibleRanges.value;
47
- if (
48
- (index < negativeRange[0] || index > negativeRange[1]) &&
49
- (index < positiveRange[0] || index > positiveRange[1])
50
- ) {
51
- return Number.MAX_SAFE_INTEGER;
52
- }
46
+ const x = useDerivedValue(() => {
47
+ const { negativeRange, positiveRange } = visibleRanges.value;
48
+ if (
49
+ (index < negativeRange[0] || index > negativeRange[1])
50
+ && (index < positiveRange[0] || index > positiveRange[1])
51
+ )
52
+ return Number.MAX_SAFE_INTEGER;
53
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
+ 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
+ ];
64
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
+ 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
+ ];
74
74
 
75
- return interpolate(
76
- handlerOffset.value,
77
- inputRange,
78
- outputRange,
79
- Extrapolate.CLAMP
80
- );
81
- }
75
+ return interpolate(
76
+ handlerOffset.value,
77
+ inputRange,
78
+ outputRange,
79
+ Extrapolate.CLAMP,
80
+ );
81
+ }
82
82
 
83
- return handlerOffset.value + size * index;
84
- }, [loop, data, viewCount, type, size, visibleRanges]);
83
+ return handlerOffset.value + size * index;
84
+ }, [loop, data, viewCount, type, size, visibleRanges]);
85
85
 
86
- return x;
86
+ return x;
87
87
  };
@@ -1,51 +1,52 @@
1
- import Animated, {
2
- runOnJS,
3
- useAnimatedReaction,
4
- } from 'react-native-reanimated';
5
- import { computedOffsetXValueWithAutoFillData } from '../utils/computedWithAutoFillData';
6
- import type { TCarouselProps } from '../types';
1
+ import type Animated from "react-native-reanimated";
2
+ import {
3
+ runOnJS,
4
+ useAnimatedReaction,
5
+ } from "react-native-reanimated";
6
+
7
+ import type { TCarouselProps } from "../types";
8
+ import { computedOffsetXValueWithAutoFillData } from "../utils/computedWithAutoFillData";
7
9
 
8
10
  export function useOnProgressChange(
9
- opts: {
10
- size: number;
11
- autoFillData: boolean;
12
- loop: boolean;
13
- offsetX: Animated.SharedValue<number>;
14
- rawData: TCarouselProps['data'];
15
- } & Pick<TCarouselProps, 'onProgressChange'>
11
+ opts: {
12
+ size: number
13
+ autoFillData: boolean
14
+ loop: boolean
15
+ offsetX: Animated.SharedValue<number>
16
+ rawData: TCarouselProps["data"]
17
+ } & Pick<TCarouselProps, "onProgressChange">,
16
18
  ) {
17
- const { autoFillData, loop, offsetX, rawData, size, onProgressChange } =
18
- opts;
19
-
20
- const rawDataLength = rawData.length;
21
-
22
- useAnimatedReaction(
23
- () => offsetX.value,
24
- (_value) => {
25
- let value = computedOffsetXValueWithAutoFillData({
26
- value: _value,
27
- rawDataLength,
28
- size,
29
- autoFillData,
30
- loop,
31
- });
32
-
33
- if (!loop) {
34
- value = Math.max(
35
- -((rawDataLength - 1) * size),
36
- Math.min(value, 0)
37
- );
38
- }
39
-
40
- let absoluteProgress = Math.abs(value / size);
41
-
42
- if (value > 0) {
43
- absoluteProgress = rawDataLength - absoluteProgress;
44
- }
45
-
46
- !!onProgressChange &&
47
- runOnJS(onProgressChange)(value, absoluteProgress);
48
- },
49
- [loop, autoFillData, rawDataLength, onProgressChange]
50
- );
19
+ const { autoFillData, loop, offsetX, rawData, size, onProgressChange }
20
+ = opts;
21
+
22
+ const rawDataLength = rawData.length;
23
+
24
+ useAnimatedReaction(
25
+ () => offsetX.value,
26
+ (_value) => {
27
+ let value = computedOffsetXValueWithAutoFillData({
28
+ value: _value,
29
+ rawDataLength,
30
+ size,
31
+ autoFillData,
32
+ loop,
33
+ });
34
+
35
+ if (!loop) {
36
+ value = Math.max(
37
+ -((rawDataLength - 1) * size),
38
+ Math.min(value, 0),
39
+ );
40
+ }
41
+
42
+ let absoluteProgress = Math.abs(value / size);
43
+
44
+ if (value > 0)
45
+ absoluteProgress = rawDataLength - absoluteProgress;
46
+
47
+ if (onProgressChange)
48
+ runOnJS(onProgressChange)(value, absoluteProgress);
49
+ },
50
+ [loop, autoFillData, rawDataLength, onProgressChange],
51
+ );
51
52
  }
@@ -1,31 +1,32 @@
1
- import React from 'react';
2
- import type { TCarouselProps } from '../types';
1
+ import React from "react";
2
+
3
+ import type { TCarouselProps } from "../types";
3
4
 
4
5
  export function usePropsErrorBoundary(props: TCarouselProps) {
5
- React.useEffect(() => {
6
- const { defaultIndex, data } = props;
6
+ React.useEffect(() => {
7
+ const { defaultIndex, data } = props;
7
8
 
8
- const viewCount = data.length;
9
- if (typeof defaultIndex === 'number' && viewCount > 0) {
10
- if (defaultIndex < 0 || defaultIndex >= viewCount) {
11
- throw Error(
12
- 'DefaultIndex must be in the range of data length.'
13
- );
14
- }
15
- }
9
+ const viewCount = data.length;
10
+ if (typeof defaultIndex === "number" && viewCount > 0) {
11
+ if (defaultIndex < 0 || defaultIndex >= viewCount) {
12
+ throw new Error(
13
+ "DefaultIndex must be in the range of data length.",
14
+ );
15
+ }
16
+ }
16
17
 
17
- // TODO
18
- if (!props.mode || props.mode === 'parallax') {
19
- if (!props.vertical && !props.width) {
20
- throw Error(
21
- '`width` must be specified for horizontal carousels.'
22
- );
23
- }
24
- if (props.vertical && !props.height) {
25
- throw Error(
26
- '`height` must be specified for vertical carousels.'
27
- );
28
- }
29
- }
30
- }, [props]);
18
+ // TODO
19
+ if (!props.mode || props.mode === "parallax") {
20
+ if (!props.vertical && !props.width) {
21
+ throw new Error(
22
+ "`width` must be specified for horizontal carousels.",
23
+ );
24
+ }
25
+ if (props.vertical && !props.height) {
26
+ throw new Error(
27
+ "`height` must be specified for vertical carousels.",
28
+ );
29
+ }
30
+ }
31
+ }, [props]);
31
32
  }