@leonsilicon/react-native-reanimated-carousel 0.0.0
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/LICENSE +21 -0
- package/README.md +652 -0
- package/lib/commonjs/components/Carousel.js +2 -0
- package/lib/commonjs/components/Carousel.js.map +1 -0
- package/lib/commonjs/components/CarouselLayout.js +2 -0
- package/lib/commonjs/components/CarouselLayout.js.map +1 -0
- package/lib/commonjs/components/ItemLayout.js +2 -0
- package/lib/commonjs/components/ItemLayout.js.map +1 -0
- package/lib/commonjs/components/ItemRenderer.js +2 -0
- package/lib/commonjs/components/ItemRenderer.js.map +1 -0
- package/lib/commonjs/components/LazyView.js +2 -0
- package/lib/commonjs/components/LazyView.js.map +1 -0
- package/lib/commonjs/components/Pagination/Basic/PaginationItem.js +2 -0
- package/lib/commonjs/components/Pagination/Basic/PaginationItem.js.map +1 -0
- package/lib/commonjs/components/Pagination/Basic/index.js +2 -0
- package/lib/commonjs/components/Pagination/Basic/index.js.map +1 -0
- package/lib/commonjs/components/Pagination/Custom/PaginationItem.js +2 -0
- package/lib/commonjs/components/Pagination/Custom/PaginationItem.js.map +1 -0
- package/lib/commonjs/components/Pagination/Custom/index.js +2 -0
- package/lib/commonjs/components/Pagination/Custom/index.js.map +1 -0
- package/lib/commonjs/components/Pagination/index.js +2 -0
- package/lib/commonjs/components/Pagination/index.js.map +1 -0
- package/lib/commonjs/components/ScrollViewGesture.js +2 -0
- package/lib/commonjs/components/ScrollViewGesture.js.map +1 -0
- package/lib/commonjs/constants/index.js +2 -0
- package/lib/commonjs/constants/index.js.map +1 -0
- package/lib/commonjs/hooks/useAutoPlay.js +2 -0
- package/lib/commonjs/hooks/useAutoPlay.js.map +1 -0
- package/lib/commonjs/hooks/useCarouselController.js +2 -0
- package/lib/commonjs/hooks/useCarouselController.js.map +1 -0
- package/lib/commonjs/hooks/useCheckMounted.js +2 -0
- package/lib/commonjs/hooks/useCheckMounted.js.map +1 -0
- package/lib/commonjs/hooks/useCommonVariables.js +2 -0
- package/lib/commonjs/hooks/useCommonVariables.js.map +1 -0
- package/lib/commonjs/hooks/useInitProps.js +2 -0
- package/lib/commonjs/hooks/useInitProps.js.map +1 -0
- package/lib/commonjs/hooks/useLayoutConfig.js +2 -0
- package/lib/commonjs/hooks/useLayoutConfig.js.map +1 -0
- package/lib/commonjs/hooks/useOffsetX.js +2 -0
- package/lib/commonjs/hooks/useOffsetX.js.map +1 -0
- package/lib/commonjs/hooks/useOnProgressChange.js +2 -0
- package/lib/commonjs/hooks/useOnProgressChange.js.map +1 -0
- package/lib/commonjs/hooks/usePanGestureProxy.js +2 -0
- package/lib/commonjs/hooks/usePanGestureProxy.js.map +1 -0
- package/lib/commonjs/hooks/usePropsErrorBoundary.js +2 -0
- package/lib/commonjs/hooks/usePropsErrorBoundary.js.map +1 -0
- package/lib/commonjs/hooks/useSizeResolver.js +2 -0
- package/lib/commonjs/hooks/useSizeResolver.js.map +1 -0
- package/lib/commonjs/hooks/useUpdateGestureConfig.js +2 -0
- package/lib/commonjs/hooks/useUpdateGestureConfig.js.map +1 -0
- package/lib/commonjs/hooks/useVisibleRanges.js +2 -0
- package/lib/commonjs/hooks/useVisibleRanges.js.map +1 -0
- package/lib/commonjs/index.js +2 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/layouts/index.js +2 -0
- package/lib/commonjs/layouts/index.js.map +1 -0
- package/lib/commonjs/layouts/normal.js +2 -0
- package/lib/commonjs/layouts/normal.js.map +1 -0
- package/lib/commonjs/layouts/parallax.js +2 -0
- package/lib/commonjs/layouts/parallax.js.map +1 -0
- package/lib/commonjs/layouts/stack.js +2 -0
- package/lib/commonjs/layouts/stack.js.map +1 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/store/index.js +2 -0
- package/lib/commonjs/store/index.js.map +1 -0
- package/lib/commonjs/types.js +2 -0
- package/lib/commonjs/types.js.map +1 -0
- package/lib/commonjs/utils/compute-gesture-translation.js +2 -0
- package/lib/commonjs/utils/compute-gesture-translation.js.map +1 -0
- package/lib/commonjs/utils/compute-offset-if-data-changed.js +2 -0
- package/lib/commonjs/utils/compute-offset-if-data-changed.js.map +1 -0
- package/lib/commonjs/utils/compute-offset-if-size-changed.js +2 -0
- package/lib/commonjs/utils/compute-offset-if-size-changed.js.map +1 -0
- package/lib/commonjs/utils/compute-offset-if-sizes-changed.js +2 -0
- package/lib/commonjs/utils/compute-offset-if-sizes-changed.js.map +1 -0
- package/lib/commonjs/utils/computed-with-auto-fill-data.js +2 -0
- package/lib/commonjs/utils/computed-with-auto-fill-data.js.map +1 -0
- package/lib/commonjs/utils/deal-with-animation.js +2 -0
- package/lib/commonjs/utils/deal-with-animation.js.map +1 -0
- package/lib/commonjs/utils/handleroffset-direction.js +2 -0
- package/lib/commonjs/utils/handleroffset-direction.js.map +1 -0
- package/lib/commonjs/utils/log.js +2 -0
- package/lib/commonjs/utils/log.js.map +1 -0
- package/lib/commonjs/utils/sanitize-animation-style.js +2 -0
- package/lib/commonjs/utils/sanitize-animation-style.js.map +1 -0
- package/lib/commonjs/utils/size-resolver.js +2 -0
- package/lib/commonjs/utils/size-resolver.js.map +1 -0
- package/lib/module/components/Carousel.js +2 -0
- package/lib/module/components/Carousel.js.map +1 -0
- package/lib/module/components/CarouselLayout.js +2 -0
- package/lib/module/components/CarouselLayout.js.map +1 -0
- package/lib/module/components/ItemLayout.js +2 -0
- package/lib/module/components/ItemLayout.js.map +1 -0
- package/lib/module/components/ItemRenderer.js +2 -0
- package/lib/module/components/ItemRenderer.js.map +1 -0
- package/lib/module/components/LazyView.js +2 -0
- package/lib/module/components/LazyView.js.map +1 -0
- package/lib/module/components/Pagination/Basic/PaginationItem.js +2 -0
- package/lib/module/components/Pagination/Basic/PaginationItem.js.map +1 -0
- package/lib/module/components/Pagination/Basic/index.js +2 -0
- package/lib/module/components/Pagination/Basic/index.js.map +1 -0
- package/lib/module/components/Pagination/Custom/PaginationItem.js +2 -0
- package/lib/module/components/Pagination/Custom/PaginationItem.js.map +1 -0
- package/lib/module/components/Pagination/Custom/index.js +2 -0
- package/lib/module/components/Pagination/Custom/index.js.map +1 -0
- package/lib/module/components/Pagination/index.js +2 -0
- package/lib/module/components/Pagination/index.js.map +1 -0
- package/lib/module/components/ScrollViewGesture.js +2 -0
- package/lib/module/components/ScrollViewGesture.js.map +1 -0
- package/lib/module/constants/index.js +2 -0
- package/lib/module/constants/index.js.map +1 -0
- package/lib/module/hooks/useAutoPlay.js +2 -0
- package/lib/module/hooks/useAutoPlay.js.map +1 -0
- package/lib/module/hooks/useCarouselController.js +2 -0
- package/lib/module/hooks/useCarouselController.js.map +1 -0
- package/lib/module/hooks/useCheckMounted.js +2 -0
- package/lib/module/hooks/useCheckMounted.js.map +1 -0
- package/lib/module/hooks/useCommonVariables.js +2 -0
- package/lib/module/hooks/useCommonVariables.js.map +1 -0
- package/lib/module/hooks/useInitProps.js +2 -0
- package/lib/module/hooks/useInitProps.js.map +1 -0
- package/lib/module/hooks/useLayoutConfig.js +2 -0
- package/lib/module/hooks/useLayoutConfig.js.map +1 -0
- package/lib/module/hooks/useOffsetX.js +2 -0
- package/lib/module/hooks/useOffsetX.js.map +1 -0
- package/lib/module/hooks/useOnProgressChange.js +2 -0
- package/lib/module/hooks/useOnProgressChange.js.map +1 -0
- package/lib/module/hooks/usePanGestureProxy.js +2 -0
- package/lib/module/hooks/usePanGestureProxy.js.map +1 -0
- package/lib/module/hooks/usePropsErrorBoundary.js +2 -0
- package/lib/module/hooks/usePropsErrorBoundary.js.map +1 -0
- package/lib/module/hooks/useSizeResolver.js +2 -0
- package/lib/module/hooks/useSizeResolver.js.map +1 -0
- package/lib/module/hooks/useUpdateGestureConfig.js +2 -0
- package/lib/module/hooks/useUpdateGestureConfig.js.map +1 -0
- package/lib/module/hooks/useVisibleRanges.js +2 -0
- package/lib/module/hooks/useVisibleRanges.js.map +1 -0
- package/lib/module/index.js +2 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/layouts/index.js +2 -0
- package/lib/module/layouts/index.js.map +1 -0
- package/lib/module/layouts/normal.js +2 -0
- package/lib/module/layouts/normal.js.map +1 -0
- package/lib/module/layouts/parallax.js +2 -0
- package/lib/module/layouts/parallax.js.map +1 -0
- package/lib/module/layouts/stack.js +2 -0
- package/lib/module/layouts/stack.js.map +1 -0
- package/lib/module/store/index.js +2 -0
- package/lib/module/store/index.js.map +1 -0
- package/lib/module/types.js +2 -0
- package/lib/module/types.js.map +1 -0
- package/lib/module/utils/compute-gesture-translation.js +2 -0
- package/lib/module/utils/compute-gesture-translation.js.map +1 -0
- package/lib/module/utils/compute-offset-if-data-changed.js +2 -0
- package/lib/module/utils/compute-offset-if-data-changed.js.map +1 -0
- package/lib/module/utils/compute-offset-if-size-changed.js +2 -0
- package/lib/module/utils/compute-offset-if-size-changed.js.map +1 -0
- package/lib/module/utils/compute-offset-if-sizes-changed.js +2 -0
- package/lib/module/utils/compute-offset-if-sizes-changed.js.map +1 -0
- package/lib/module/utils/computed-with-auto-fill-data.js +2 -0
- package/lib/module/utils/computed-with-auto-fill-data.js.map +1 -0
- package/lib/module/utils/deal-with-animation.js +2 -0
- package/lib/module/utils/deal-with-animation.js.map +1 -0
- package/lib/module/utils/handleroffset-direction.js +2 -0
- package/lib/module/utils/handleroffset-direction.js.map +1 -0
- package/lib/module/utils/log.js +2 -0
- package/lib/module/utils/log.js.map +1 -0
- package/lib/module/utils/sanitize-animation-style.js +2 -0
- package/lib/module/utils/sanitize-animation-style.js.map +1 -0
- package/lib/module/utils/size-resolver.js +2 -0
- package/lib/module/utils/size-resolver.js.map +1 -0
- package/lib/typescript/components/Carousel.d.ts +8 -0
- package/lib/typescript/components/Carousel.d.ts.map +1 -0
- package/lib/typescript/components/CarouselLayout.d.ts +6 -0
- package/lib/typescript/components/CarouselLayout.d.ts.map +1 -0
- package/lib/typescript/components/ItemLayout.d.ts +15 -0
- package/lib/typescript/components/ItemLayout.d.ts.map +1 -0
- package/lib/typescript/components/ItemRenderer.d.ts +24 -0
- package/lib/typescript/components/ItemRenderer.d.ts.map +1 -0
- package/lib/typescript/components/LazyView.d.ts +8 -0
- package/lib/typescript/components/LazyView.d.ts.map +1 -0
- package/lib/typescript/components/Pagination/Basic/PaginationItem.d.ts +29 -0
- package/lib/typescript/components/Pagination/Basic/PaginationItem.d.ts.map +1 -0
- package/lib/typescript/components/Pagination/Basic/index.d.ts +23 -0
- package/lib/typescript/components/Pagination/Basic/index.d.ts.map +1 -0
- package/lib/typescript/components/Pagination/Custom/PaginationItem.d.ts +35 -0
- package/lib/typescript/components/Pagination/Custom/PaginationItem.d.ts.map +1 -0
- package/lib/typescript/components/Pagination/Custom/index.d.ts +26 -0
- package/lib/typescript/components/Pagination/Custom/index.d.ts.map +1 -0
- package/lib/typescript/components/Pagination/index.d.ts +5 -0
- package/lib/typescript/components/Pagination/index.d.ts.map +1 -0
- package/lib/typescript/components/ScrollViewGesture.d.ts +19 -0
- package/lib/typescript/components/ScrollViewGesture.d.ts.map +1 -0
- package/lib/typescript/constants/index.d.ts +9 -0
- package/lib/typescript/constants/index.d.ts.map +1 -0
- package/lib/typescript/hooks/useAutoPlay.d.ts +12 -0
- package/lib/typescript/hooks/useAutoPlay.d.ts.map +1 -0
- package/lib/typescript/hooks/useCarouselController.d.ts +28 -0
- package/lib/typescript/hooks/useCarouselController.d.ts.map +1 -0
- package/lib/typescript/hooks/useCheckMounted.d.ts +3 -0
- package/lib/typescript/hooks/useCheckMounted.d.ts.map +1 -0
- package/lib/typescript/hooks/useCommonVariables.d.ts +13 -0
- package/lib/typescript/hooks/useCommonVariables.d.ts.map +1 -0
- package/lib/typescript/hooks/useInitProps.d.ts +17 -0
- package/lib/typescript/hooks/useInitProps.d.ts.map +1 -0
- package/lib/typescript/hooks/useLayoutConfig.d.ts +10 -0
- package/lib/typescript/hooks/useLayoutConfig.d.ts.map +1 -0
- package/lib/typescript/hooks/useOffsetX.d.ts +24 -0
- package/lib/typescript/hooks/useOffsetX.d.ts.map +1 -0
- package/lib/typescript/hooks/useOnProgressChange.d.ts +14 -0
- package/lib/typescript/hooks/useOnProgressChange.d.ts.map +1 -0
- package/lib/typescript/hooks/usePanGestureProxy.d.ts +10 -0
- package/lib/typescript/hooks/usePanGestureProxy.d.ts.map +1 -0
- package/lib/typescript/hooks/usePropsErrorBoundary.d.ts +5 -0
- package/lib/typescript/hooks/usePropsErrorBoundary.d.ts.map +1 -0
- package/lib/typescript/hooks/useSizeResolver.d.ts +27 -0
- package/lib/typescript/hooks/useSizeResolver.d.ts.map +1 -0
- package/lib/typescript/hooks/useUpdateGestureConfig.d.ts +6 -0
- package/lib/typescript/hooks/useUpdateGestureConfig.d.ts.map +1 -0
- package/lib/typescript/hooks/useVisibleRanges.d.ts +23 -0
- package/lib/typescript/hooks/useVisibleRanges.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +7 -0
- package/lib/typescript/index.d.ts.map +1 -0
- package/lib/typescript/layouts/index.d.ts +11 -0
- package/lib/typescript/layouts/index.d.ts.map +1 -0
- package/lib/typescript/layouts/normal.d.ts +16 -0
- package/lib/typescript/layouts/normal.d.ts.map +1 -0
- package/lib/typescript/layouts/parallax.d.ts +50 -0
- package/lib/typescript/layouts/parallax.d.ts.map +1 -0
- package/lib/typescript/layouts/stack.d.ts +38 -0
- package/lib/typescript/layouts/stack.d.ts.map +1 -0
- package/lib/typescript/store/index.d.ts +38 -0
- package/lib/typescript/store/index.d.ts.map +1 -0
- package/lib/typescript/types.d.ts +326 -0
- package/lib/typescript/types.d.ts.map +1 -0
- package/lib/typescript/utils/compute-gesture-translation.d.ts +9 -0
- package/lib/typescript/utils/compute-gesture-translation.d.ts.map +1 -0
- package/lib/typescript/utils/compute-offset-if-data-changed.d.ts +9 -0
- package/lib/typescript/utils/compute-offset-if-data-changed.d.ts.map +1 -0
- package/lib/typescript/utils/compute-offset-if-size-changed.d.ts +6 -0
- package/lib/typescript/utils/compute-offset-if-size-changed.d.ts.map +1 -0
- package/lib/typescript/utils/compute-offset-if-sizes-changed.d.ts +14 -0
- package/lib/typescript/utils/compute-offset-if-sizes-changed.d.ts.map +1 -0
- package/lib/typescript/utils/computed-with-auto-fill-data.d.ts +23 -0
- package/lib/typescript/utils/computed-with-auto-fill-data.d.ts.map +1 -0
- package/lib/typescript/utils/deal-with-animation.d.ts +3 -0
- package/lib/typescript/utils/deal-with-animation.d.ts.map +1 -0
- package/lib/typescript/utils/handleroffset-direction.d.ts +4 -0
- package/lib/typescript/utils/handleroffset-direction.d.ts.map +1 -0
- package/lib/typescript/utils/log.d.ts +7 -0
- package/lib/typescript/utils/log.d.ts.map +1 -0
- package/lib/typescript/utils/sanitize-animation-style.d.ts +3 -0
- package/lib/typescript/utils/sanitize-animation-style.d.ts.map +1 -0
- package/lib/typescript/utils/size-resolver.d.ts +87 -0
- package/lib/typescript/utils/size-resolver.d.ts.map +1 -0
- package/package.json +151 -0
- package/src/components/Carousel.test.tsx +1153 -0
- package/src/components/Carousel.tsx +35 -0
- package/src/components/CarouselLayout.tsx +231 -0
- package/src/components/ItemLayout.tsx +217 -0
- package/src/components/ItemRenderer.tsx +114 -0
- package/src/components/LazyView.test.tsx +61 -0
- package/src/components/LazyView.tsx +14 -0
- package/src/components/Pagination/Basic/PaginationItem.tsx +149 -0
- package/src/components/Pagination/Basic/index.tsx +98 -0
- package/src/components/Pagination/Custom/PaginationItem.tsx +166 -0
- package/src/components/Pagination/Custom/index.tsx +111 -0
- package/src/components/Pagination/Pagination.test.tsx +178 -0
- package/src/components/Pagination/index.tsx +7 -0
- package/src/components/ScrollViewGesture.tsx +577 -0
- package/src/components/rnr-demo.test.tsx +53 -0
- package/src/constants/index.ts +11 -0
- package/src/hooks/useAutoPlay.test.ts +194 -0
- package/src/hooks/useAutoPlay.ts +58 -0
- package/src/hooks/useCarouselController.test.tsx +1158 -0
- package/src/hooks/useCarouselController.tsx +525 -0
- package/src/hooks/useCheckMounted.test.ts +47 -0
- package/src/hooks/useCheckMounted.ts +14 -0
- package/src/hooks/useCommonVariables.test.tsx +384 -0
- package/src/hooks/useCommonVariables.ts +202 -0
- package/src/hooks/useInitProps.test.tsx +134 -0
- package/src/hooks/useInitProps.ts +111 -0
- package/src/hooks/useLayoutConfig.test.tsx +247 -0
- package/src/hooks/useLayoutConfig.ts +30 -0
- package/src/hooks/useOffsetX.test.ts +110 -0
- package/src/hooks/useOffsetX.ts +109 -0
- package/src/hooks/useOnProgressChange.test.tsx +207 -0
- package/src/hooks/useOnProgressChange.ts +105 -0
- package/src/hooks/usePanGestureProxy.test.tsx +368 -0
- package/src/hooks/usePanGestureProxy.ts +144 -0
- package/src/hooks/usePropsErrorBoundary.ts +138 -0
- package/src/hooks/useSizeResolver.test.tsx +112 -0
- package/src/hooks/useSizeResolver.ts +106 -0
- package/src/hooks/useUpdateGestureConfig.test.ts +89 -0
- package/src/hooks/useUpdateGestureConfig.ts +14 -0
- package/src/hooks/useVisibleRanges.test.tsx +366 -0
- package/src/hooks/useVisibleRanges.tsx +123 -0
- package/src/index.tsx +13 -0
- package/src/layouts/index.tsx +12 -0
- package/src/layouts/normal.ts +32 -0
- package/src/layouts/parallax.test.ts +239 -0
- package/src/layouts/parallax.ts +83 -0
- package/src/layouts/stack.test.ts +252 -0
- package/src/layouts/stack.ts +306 -0
- package/src/store/index.test.tsx +314 -0
- package/src/store/index.tsx +66 -0
- package/src/types.ts +348 -0
- package/src/utils/compute-gesture-translation.test.ts +70 -0
- package/src/utils/compute-gesture-translation.ts +29 -0
- package/src/utils/compute-offset-if-data-changed.test.ts +133 -0
- package/src/utils/compute-offset-if-data-changed.ts +44 -0
- package/src/utils/compute-offset-if-size-changed.test.ts +78 -0
- package/src/utils/compute-offset-if-size-changed.ts +14 -0
- package/src/utils/compute-offset-if-sizes-changed.test.ts +74 -0
- package/src/utils/compute-offset-if-sizes-changed.ts +44 -0
- package/src/utils/computed-with-auto-fill-data.test.ts +298 -0
- package/src/utils/computed-with-auto-fill-data.ts +92 -0
- package/src/utils/deal-with-animation.test.ts +181 -0
- package/src/utils/deal-with-animation.ts +17 -0
- package/src/utils/handleroffset-direction.test.ts +124 -0
- package/src/utils/handleroffset-direction.ts +18 -0
- package/src/utils/index.test.ts +90 -0
- package/src/utils/log.test.ts +134 -0
- package/src/utils/log.ts +12 -0
- package/src/utils/sanitize-animation-style.test.ts +40 -0
- package/src/utils/sanitize-animation-style.ts +20 -0
- package/src/utils/size-resolver.test.ts +193 -0
- package/src/utils/size-resolver.ts +216 -0
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type { SharedValue } from "react-native-reanimated";
|
|
2
|
+
import { Extrapolation, interpolate, useDerivedValue } from "react-native-reanimated";
|
|
3
|
+
|
|
4
|
+
import type { SizeResolver } from "../utils/size-resolver";
|
|
5
|
+
import type { IVisibleRanges } from "./useVisibleRanges";
|
|
6
|
+
|
|
7
|
+
export interface IOpts {
|
|
8
|
+
index: number;
|
|
9
|
+
/**
|
|
10
|
+
* Uniform-mode item size. Used by the legacy callsites and tests that pass a
|
|
11
|
+
* scalar. When `resolver` is provided it takes precedence.
|
|
12
|
+
*/
|
|
13
|
+
size: number;
|
|
14
|
+
handlerOffset: SharedValue<number>;
|
|
15
|
+
dataLength: number;
|
|
16
|
+
type?: "positive" | "negative";
|
|
17
|
+
viewCount?: number;
|
|
18
|
+
loop?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Optional `SizeResolver`. When supplied, position math reads per-item
|
|
21
|
+
* offsets from the prefix-sum table instead of multiplying by `size`. Pass
|
|
22
|
+
* `undefined` for legacy uniform behavior.
|
|
23
|
+
*/
|
|
24
|
+
resolver?: SizeResolver;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const useOffsetX = (opts: IOpts, visibleRanges: IVisibleRanges) => {
|
|
28
|
+
const {
|
|
29
|
+
handlerOffset,
|
|
30
|
+
index,
|
|
31
|
+
size,
|
|
32
|
+
loop,
|
|
33
|
+
dataLength,
|
|
34
|
+
type = "positive",
|
|
35
|
+
viewCount: _viewCount,
|
|
36
|
+
resolver,
|
|
37
|
+
} = opts;
|
|
38
|
+
|
|
39
|
+
const ITEM_LENGTH = dataLength;
|
|
40
|
+
const VALID_LENGTH = ITEM_LENGTH - 1;
|
|
41
|
+
|
|
42
|
+
const viewCount = _viewCount ?? Math.round((ITEM_LENGTH - 1) / 2);
|
|
43
|
+
const positiveCount = type === "positive" ? viewCount : VALID_LENGTH - viewCount;
|
|
44
|
+
|
|
45
|
+
const x = useDerivedValue(() => {
|
|
46
|
+
// Resolve per-item geometry. In uniform mode we mirror the historical
|
|
47
|
+
// `size * index` math; in variable mode we read from the prefix-sum table
|
|
48
|
+
// via the resolver. Both branches converge on the same 7-anchor wrap
|
|
49
|
+
// interpolation, just with different inputs.
|
|
50
|
+
const useResolver = resolver?.variable === true;
|
|
51
|
+
const totalLength = useResolver ? resolver!.total.value : size * ITEM_LENGTH;
|
|
52
|
+
const itemSize = useResolver ? resolver!.itemSize(index) : size;
|
|
53
|
+
const halfItem = 0.5 * itemSize;
|
|
54
|
+
|
|
55
|
+
if ((!useResolver && !size) || (useResolver && totalLength <= 0)) return 0;
|
|
56
|
+
|
|
57
|
+
const { negativeRange, positiveRange } = visibleRanges.value;
|
|
58
|
+
const inWindow =
|
|
59
|
+
(index >= negativeRange[0] && index <= negativeRange[1]) ||
|
|
60
|
+
(index >= positiveRange[0] && index <= positiveRange[1]);
|
|
61
|
+
if (!inWindow) return Number.MAX_SAFE_INTEGER;
|
|
62
|
+
|
|
63
|
+
// Natural start position of this item along the scroll axis.
|
|
64
|
+
let startPos = useResolver ? resolver!.offsetAt(index) : size * index;
|
|
65
|
+
if (index > positiveCount) {
|
|
66
|
+
// Item is past the forward edge of the viewing window — wrap it to the
|
|
67
|
+
// negative side so it can re-enter from the other end during a loop.
|
|
68
|
+
startPos = useResolver
|
|
69
|
+
? resolver!.offsetAt(index) - totalLength
|
|
70
|
+
: (index - ITEM_LENGTH) * size;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Cutoffs for the wrap-around. In uniform mode these are offsets to the
|
|
74
|
+
// window edges; in variable mode they're offsets to the (positiveCount)-th
|
|
75
|
+
// item's start.
|
|
76
|
+
const MAX = useResolver ? resolver!.offsetAt(positiveCount) : positiveCount * size;
|
|
77
|
+
const MIN = useResolver
|
|
78
|
+
? -(totalLength - resolver!.offsetAt(positiveCount + 1))
|
|
79
|
+
: -((VALID_LENGTH - positiveCount) * size);
|
|
80
|
+
|
|
81
|
+
if (loop) {
|
|
82
|
+
const inputRange = [
|
|
83
|
+
-totalLength,
|
|
84
|
+
MIN - halfItem - startPos - Number.MIN_VALUE,
|
|
85
|
+
MIN - halfItem - startPos,
|
|
86
|
+
0,
|
|
87
|
+
MAX + halfItem - startPos,
|
|
88
|
+
MAX + halfItem - startPos + Number.MIN_VALUE,
|
|
89
|
+
totalLength,
|
|
90
|
+
];
|
|
91
|
+
|
|
92
|
+
const outputRange = [
|
|
93
|
+
startPos,
|
|
94
|
+
MAX + halfItem - Number.MIN_VALUE,
|
|
95
|
+
MIN - halfItem,
|
|
96
|
+
startPos,
|
|
97
|
+
MAX + halfItem,
|
|
98
|
+
MIN - halfItem + Number.MIN_VALUE,
|
|
99
|
+
startPos,
|
|
100
|
+
];
|
|
101
|
+
|
|
102
|
+
return interpolate(handlerOffset.value, inputRange, outputRange, Extrapolation.CLAMP);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return handlerOffset.value + (useResolver ? resolver!.offsetAt(index) : size * index);
|
|
106
|
+
}, [loop, dataLength, viewCount, type, size, visibleRanges, handlerOffset, resolver]);
|
|
107
|
+
|
|
108
|
+
return x;
|
|
109
|
+
};
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { useSharedValue } from "react-native-reanimated";
|
|
2
|
+
|
|
3
|
+
import { renderHook } from "@testing-library/react-hooks";
|
|
4
|
+
|
|
5
|
+
import { useOnProgressChange } from "./useOnProgressChange";
|
|
6
|
+
|
|
7
|
+
// Mock Reanimated and Easing
|
|
8
|
+
jest.mock("react-native-reanimated", () => {
|
|
9
|
+
let reactionCallback: ((value: any) => void) | null = null;
|
|
10
|
+
|
|
11
|
+
return {
|
|
12
|
+
useSharedValue: jest.fn((initialValue) => ({
|
|
13
|
+
value: initialValue,
|
|
14
|
+
})),
|
|
15
|
+
useAnimatedReaction: jest.fn((deps, cb) => {
|
|
16
|
+
reactionCallback = cb;
|
|
17
|
+
const depsResult = deps();
|
|
18
|
+
cb(depsResult);
|
|
19
|
+
return () => {
|
|
20
|
+
reactionCallback = null;
|
|
21
|
+
};
|
|
22
|
+
}),
|
|
23
|
+
Easing: {
|
|
24
|
+
bezier: () => ({
|
|
25
|
+
factory: () => 0,
|
|
26
|
+
}),
|
|
27
|
+
},
|
|
28
|
+
// Export the helper function for testing
|
|
29
|
+
__triggerReaction: (value: any) => {
|
|
30
|
+
if (reactionCallback) reactionCallback(value);
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
jest.mock("react-native-worklets", () => {
|
|
36
|
+
const mockScheduleOnRN = jest.fn((fn, ...args) => fn(...args));
|
|
37
|
+
return {
|
|
38
|
+
scheduleOnRN: mockScheduleOnRN,
|
|
39
|
+
mockScheduleOnRN,
|
|
40
|
+
};
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
// Mock computedOffsetXValueWithAutoFillData
|
|
44
|
+
jest.mock("../utils/computed-with-auto-fill-data", () => ({
|
|
45
|
+
computedOffsetXValueWithAutoFillData: jest.fn(({ value }) => value),
|
|
46
|
+
}));
|
|
47
|
+
|
|
48
|
+
describe("useOnProgressChange", () => {
|
|
49
|
+
const mockOffsetX = useSharedValue(0);
|
|
50
|
+
const mockSizeReady = useSharedValue(true);
|
|
51
|
+
const mockOnProgressChange = jest.fn();
|
|
52
|
+
const { __triggerReaction } = jest.requireMock("react-native-reanimated");
|
|
53
|
+
|
|
54
|
+
beforeEach(() => {
|
|
55
|
+
jest.clearAllMocks();
|
|
56
|
+
mockOffsetX.value = 0;
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("should handle progress change with function callback", () => {
|
|
60
|
+
renderHook(() =>
|
|
61
|
+
useOnProgressChange({
|
|
62
|
+
size: 300,
|
|
63
|
+
sizeReady: mockSizeReady,
|
|
64
|
+
autoFillData: false,
|
|
65
|
+
loop: false,
|
|
66
|
+
offsetX: mockOffsetX,
|
|
67
|
+
rawDataLength: 5,
|
|
68
|
+
onProgressChange: mockOnProgressChange,
|
|
69
|
+
})
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
mockOffsetX.value = -300; // Move to next slide
|
|
73
|
+
__triggerReaction({ offset: mockOffsetX.value, ready: mockSizeReady.value });
|
|
74
|
+
expect(mockOnProgressChange).toHaveBeenCalledWith(-300, 1);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("should handle progress change with shared value", () => {
|
|
78
|
+
const progressValue = useSharedValue(0);
|
|
79
|
+
renderHook(() =>
|
|
80
|
+
useOnProgressChange({
|
|
81
|
+
size: 300,
|
|
82
|
+
sizeReady: mockSizeReady,
|
|
83
|
+
autoFillData: false,
|
|
84
|
+
loop: false,
|
|
85
|
+
offsetX: mockOffsetX,
|
|
86
|
+
rawDataLength: 5,
|
|
87
|
+
onProgressChange: progressValue,
|
|
88
|
+
})
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
mockOffsetX.value = -300; // Move to next slide
|
|
92
|
+
__triggerReaction({ offset: mockOffsetX.value, ready: mockSizeReady.value });
|
|
93
|
+
expect(progressValue.value).toBe(1);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("should handle loop mode", () => {
|
|
97
|
+
renderHook(() =>
|
|
98
|
+
useOnProgressChange({
|
|
99
|
+
size: 300,
|
|
100
|
+
sizeReady: mockSizeReady,
|
|
101
|
+
autoFillData: false,
|
|
102
|
+
loop: true,
|
|
103
|
+
offsetX: mockOffsetX,
|
|
104
|
+
rawDataLength: 5,
|
|
105
|
+
onProgressChange: mockOnProgressChange,
|
|
106
|
+
})
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
mockOffsetX.value = -1500; // Move to last slide
|
|
110
|
+
__triggerReaction({ offset: mockOffsetX.value, ready: mockSizeReady.value });
|
|
111
|
+
expect(mockOnProgressChange).toHaveBeenCalledWith(-1500, 5);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("should handle autoFillData mode", () => {
|
|
115
|
+
renderHook(() =>
|
|
116
|
+
useOnProgressChange({
|
|
117
|
+
size: 300,
|
|
118
|
+
sizeReady: mockSizeReady,
|
|
119
|
+
autoFillData: true,
|
|
120
|
+
loop: false,
|
|
121
|
+
offsetX: mockOffsetX,
|
|
122
|
+
rawDataLength: 3,
|
|
123
|
+
onProgressChange: mockOnProgressChange,
|
|
124
|
+
})
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
mockOffsetX.value = -300; // Move to next slide
|
|
128
|
+
__triggerReaction({ offset: mockOffsetX.value, ready: mockSizeReady.value });
|
|
129
|
+
expect(mockOnProgressChange).toHaveBeenCalledWith(-300, 1);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("should clamp values when not in loop mode", () => {
|
|
133
|
+
renderHook(() =>
|
|
134
|
+
useOnProgressChange({
|
|
135
|
+
size: 300,
|
|
136
|
+
sizeReady: mockSizeReady,
|
|
137
|
+
autoFillData: false,
|
|
138
|
+
loop: false,
|
|
139
|
+
offsetX: mockOffsetX,
|
|
140
|
+
rawDataLength: 3,
|
|
141
|
+
onProgressChange: mockOnProgressChange,
|
|
142
|
+
})
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
mockOffsetX.value = 300; // Try to move before first slide
|
|
146
|
+
__triggerReaction({ offset: mockOffsetX.value, ready: mockSizeReady.value });
|
|
147
|
+
expect(mockOnProgressChange).toHaveBeenCalledWith(0, 0);
|
|
148
|
+
|
|
149
|
+
mockOffsetX.value = -900; // Try to move after last slide
|
|
150
|
+
__triggerReaction({ offset: mockOffsetX.value, ready: mockSizeReady.value });
|
|
151
|
+
expect(mockOnProgressChange).toHaveBeenCalledWith(-600, 2);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("should handle positive offset values", () => {
|
|
155
|
+
renderHook(() =>
|
|
156
|
+
useOnProgressChange({
|
|
157
|
+
size: 300,
|
|
158
|
+
sizeReady: mockSizeReady,
|
|
159
|
+
autoFillData: false,
|
|
160
|
+
loop: true,
|
|
161
|
+
offsetX: mockOffsetX,
|
|
162
|
+
rawDataLength: 5,
|
|
163
|
+
onProgressChange: mockOnProgressChange,
|
|
164
|
+
})
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
mockOffsetX.value = 300; // Move backwards
|
|
168
|
+
__triggerReaction({ offset: mockOffsetX.value, ready: mockSizeReady.value });
|
|
169
|
+
expect(mockOnProgressChange).toHaveBeenCalledWith(300, 4);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("should not call onProgressChange if not provided", () => {
|
|
173
|
+
renderHook(() =>
|
|
174
|
+
useOnProgressChange({
|
|
175
|
+
size: 300,
|
|
176
|
+
sizeReady: mockSizeReady,
|
|
177
|
+
autoFillData: false,
|
|
178
|
+
loop: false,
|
|
179
|
+
offsetX: mockOffsetX,
|
|
180
|
+
rawDataLength: 5,
|
|
181
|
+
})
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
mockOffsetX.value = -300; // Move to next slide
|
|
185
|
+
__triggerReaction({ offset: mockOffsetX.value, ready: mockSizeReady.value });
|
|
186
|
+
// Should not throw any errors or call any callbacks
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it("should not trigger progress change when size is not ready", () => {
|
|
190
|
+
const mockSizeNotReady = useSharedValue(false);
|
|
191
|
+
renderHook(() =>
|
|
192
|
+
useOnProgressChange({
|
|
193
|
+
size: 300,
|
|
194
|
+
sizeReady: mockSizeNotReady,
|
|
195
|
+
autoFillData: false,
|
|
196
|
+
loop: false,
|
|
197
|
+
offsetX: mockOffsetX,
|
|
198
|
+
rawDataLength: 5,
|
|
199
|
+
onProgressChange: mockOnProgressChange,
|
|
200
|
+
})
|
|
201
|
+
);
|
|
202
|
+
|
|
203
|
+
mockOffsetX.value = -300; // Move to next slide
|
|
204
|
+
__triggerReaction({ offset: mockOffsetX.value, ready: mockSizeNotReady.value });
|
|
205
|
+
expect(mockOnProgressChange).not.toHaveBeenCalled();
|
|
206
|
+
});
|
|
207
|
+
});
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import type { SharedValue } from "react-native-reanimated";
|
|
2
|
+
import { useAnimatedReaction } from "react-native-reanimated";
|
|
3
|
+
import { scheduleOnRN } from "react-native-worklets";
|
|
4
|
+
|
|
5
|
+
import type { TCarouselProps } from "../types";
|
|
6
|
+
import { computedOffsetXValueWithAutoFillData } from "../utils/computed-with-auto-fill-data";
|
|
7
|
+
import type { SizeResolver } from "../utils/size-resolver";
|
|
8
|
+
|
|
9
|
+
export function useOnProgressChange(
|
|
10
|
+
opts: {
|
|
11
|
+
size: number;
|
|
12
|
+
sizeReady: SharedValue<boolean>;
|
|
13
|
+
autoFillData: boolean;
|
|
14
|
+
loop: boolean;
|
|
15
|
+
offsetX: SharedValue<number>;
|
|
16
|
+
rawDataLength: number;
|
|
17
|
+
sizeResolver: SizeResolver;
|
|
18
|
+
variableSize: boolean;
|
|
19
|
+
} & Pick<TCarouselProps, "onProgressChange">
|
|
20
|
+
) {
|
|
21
|
+
const {
|
|
22
|
+
autoFillData,
|
|
23
|
+
loop,
|
|
24
|
+
offsetX,
|
|
25
|
+
size,
|
|
26
|
+
rawDataLength,
|
|
27
|
+
onProgressChange,
|
|
28
|
+
sizeReady,
|
|
29
|
+
sizeResolver,
|
|
30
|
+
variableSize,
|
|
31
|
+
} = opts;
|
|
32
|
+
|
|
33
|
+
// remember `isFunc` here because we can't accurately check typeof
|
|
34
|
+
// from within useAnimatedReaction because its code has been workletized;
|
|
35
|
+
// the `onProgressChange` value will be typeof "object" from within
|
|
36
|
+
// the worklet code even if it's a function.
|
|
37
|
+
const isFunc = typeof onProgressChange === "function";
|
|
38
|
+
|
|
39
|
+
useAnimatedReaction(
|
|
40
|
+
() => ({ offset: offsetX.value, ready: sizeReady.value }),
|
|
41
|
+
({ offset, ready }) => {
|
|
42
|
+
if (!ready) return;
|
|
43
|
+
|
|
44
|
+
if (variableSize) {
|
|
45
|
+
// Variable-size progress: integer part is the current item index,
|
|
46
|
+
// fractional part is the proportion scrolled into that item. We
|
|
47
|
+
// intentionally keep `offsetProgress` in raw pixels (it has always
|
|
48
|
+
// been documented as "Total of offset distance").
|
|
49
|
+
const total = sizeResolver.total.value;
|
|
50
|
+
if (total <= 0) return;
|
|
51
|
+
// Normalise into one loop length and convert to a positive-leaning
|
|
52
|
+
// offset so the math mirrors the uniform branch.
|
|
53
|
+
let value = offset;
|
|
54
|
+
if (!loop) {
|
|
55
|
+
const lastItemStart = sizeResolver.offsetAt(rawDataLength - 1);
|
|
56
|
+
value = Math.max(-lastItemStart, Math.min(value, 0));
|
|
57
|
+
}
|
|
58
|
+
const absOffset = Math.abs(value);
|
|
59
|
+
const idx = sizeResolver.indexAt(absOffset);
|
|
60
|
+
const itemStart = sizeResolver.offsetAt(idx);
|
|
61
|
+
const itemSize = sizeResolver.itemSize(idx);
|
|
62
|
+
const fraction = itemSize > 0 ? (absOffset - itemStart) / itemSize : 0;
|
|
63
|
+
let absoluteProgress = idx + fraction;
|
|
64
|
+
if (value > 0) absoluteProgress = rawDataLength - absoluteProgress;
|
|
65
|
+
|
|
66
|
+
if (onProgressChange) {
|
|
67
|
+
if (isFunc) {
|
|
68
|
+
scheduleOnRN(onProgressChange, value, absoluteProgress);
|
|
69
|
+
} else {
|
|
70
|
+
(onProgressChange as SharedValue<number>).value = absoluteProgress;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const safeSize = size > 0 ? size : 0;
|
|
77
|
+
if (safeSize <= 0) return;
|
|
78
|
+
|
|
79
|
+
let value = computedOffsetXValueWithAutoFillData({
|
|
80
|
+
value: offset,
|
|
81
|
+
rawDataLength,
|
|
82
|
+
size: safeSize,
|
|
83
|
+
autoFillData,
|
|
84
|
+
loop,
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
if (!loop) {
|
|
88
|
+
value = Math.max(-((rawDataLength - 1) * safeSize), Math.min(value, 0));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
let absoluteProgress = Math.abs(value / safeSize);
|
|
92
|
+
|
|
93
|
+
if (value > 0) absoluteProgress = rawDataLength - absoluteProgress;
|
|
94
|
+
|
|
95
|
+
if (onProgressChange) {
|
|
96
|
+
if (isFunc) {
|
|
97
|
+
scheduleOnRN(onProgressChange, value, absoluteProgress);
|
|
98
|
+
} else {
|
|
99
|
+
(onProgressChange as SharedValue<number>).value = absoluteProgress;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
[loop, autoFillData, rawDataLength, onProgressChange, size, variableSize, sizeResolver]
|
|
104
|
+
);
|
|
105
|
+
}
|