@fountain-ui/lab 2.0.0-beta.11 → 2.0.0-beta.12
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/build/commonjs/Carousel/Carousel.js +137 -0
- package/build/commonjs/Carousel/Carousel.js.map +1 -0
- package/build/commonjs/Carousel/CarouselProps.js.map +1 -1
- package/build/commonjs/Carousel/animation/animationUtils.js +9 -0
- package/build/commonjs/Carousel/animation/animationUtils.js.map +1 -0
- package/build/commonjs/Carousel/animation/createDefaultItemStyle.js +15 -0
- package/build/commonjs/Carousel/animation/createDefaultItemStyle.js.map +1 -0
- package/build/commonjs/Carousel/animation/createDefaultScrollAnimation.js +20 -0
- package/build/commonjs/Carousel/animation/createDefaultScrollAnimation.js.map +1 -0
- package/build/commonjs/Carousel/animation/index.js +40 -0
- package/build/commonjs/Carousel/animation/index.js.map +1 -0
- package/build/commonjs/Carousel/animation/normalItemStyleFactory.js +27 -0
- package/build/commonjs/Carousel/animation/normalItemStyleFactory.js.map +1 -0
- package/build/commonjs/Carousel/animation/parallaxItemStyleFactory.js +65 -0
- package/build/commonjs/Carousel/animation/parallaxItemStyleFactory.js.map +1 -0
- package/build/commonjs/Carousel/components/InternalContext.js +29 -0
- package/build/commonjs/Carousel/components/InternalContext.js.map +1 -0
- package/build/commonjs/Carousel/components/ItemView.js +64 -0
- package/build/commonjs/Carousel/components/ItemView.js.map +1 -0
- package/build/commonjs/Carousel/components/RootView.js +68 -0
- package/build/commonjs/Carousel/components/RootView.js.map +1 -0
- package/build/commonjs/Carousel/components/ScrollViewGesture.js +76 -0
- package/build/commonjs/Carousel/components/ScrollViewGesture.js.map +1 -0
- package/build/commonjs/Carousel/components/index.js +40 -0
- package/build/commonjs/Carousel/components/index.js.map +1 -0
- package/build/commonjs/Carousel/hooks/index.js +64 -0
- package/build/commonjs/Carousel/hooks/index.js.map +1 -0
- package/build/commonjs/Carousel/hooks/useAutoplayController.js +55 -0
- package/build/commonjs/Carousel/hooks/useAutoplayController.js.map +1 -0
- package/build/commonjs/Carousel/hooks/useDimensionChangeReaction.js +23 -0
- package/build/commonjs/Carousel/hooks/useDimensionChangeReaction.js.map +1 -0
- package/build/commonjs/Carousel/hooks/useIndexController.js +70 -0
- package/build/commonjs/Carousel/hooks/useIndexController.js.map +1 -0
- package/build/commonjs/Carousel/hooks/useItemInterpolation.js +63 -0
- package/build/commonjs/Carousel/hooks/useItemInterpolation.js.map +1 -0
- package/build/commonjs/Carousel/hooks/useItemVisibilityStore.js +95 -0
- package/build/commonjs/Carousel/hooks/useItemVisibilityStore.js.map +1 -0
- package/build/commonjs/Carousel/hooks/useLoopedData.js +33 -0
- package/build/commonjs/Carousel/hooks/useLoopedData.js.map +1 -0
- package/build/commonjs/Carousel/hooks/usePagingAnimation.js +124 -0
- package/build/commonjs/Carousel/hooks/usePagingAnimation.js.map +1 -0
- package/build/commonjs/Carousel/index.js +16 -2
- package/build/commonjs/Carousel/index.js.map +1 -1
- package/build/commonjs/Carousel/types.js +7 -0
- package/build/commonjs/Carousel/types.js.map +1 -0
- package/build/commonjs/ViewPager/ChildrenMemoizedPage.js +44 -47
- package/build/commonjs/ViewPager/ChildrenMemoizedPage.js.map +1 -1
- package/build/commonjs/ViewPager/PageStateContext.js +22 -0
- package/build/commonjs/ViewPager/PageStateContext.js.map +1 -0
- package/build/commonjs/ViewPager/ViewPagerNative.js +23 -10
- package/build/commonjs/ViewPager/ViewPagerNative.js.map +1 -1
- package/build/commonjs/ViewPager/ViewPagerProps.js.map +1 -1
- package/build/commonjs/ViewPager/ViewPagerWeb.js +9 -7
- package/build/commonjs/ViewPager/ViewPagerWeb.js.map +1 -1
- package/build/commonjs/ViewPager/index.js +8 -0
- package/build/commonjs/ViewPager/index.js.map +1 -1
- package/build/commonjs/ViewPager/index.native.js +8 -0
- package/build/commonjs/ViewPager/index.native.js.map +1 -1
- package/build/commonjs/ViewPager/utils.js +3 -1
- package/build/commonjs/ViewPager/utils.js.map +1 -1
- package/build/commonjs/ViewabilityTrackerView/Viewability.js +2 -0
- package/build/commonjs/ViewabilityTrackerView/Viewability.js.map +1 -0
- package/build/commonjs/ViewabilityTrackerView/ViewabilityTrackerView.js +45 -0
- package/build/commonjs/ViewabilityTrackerView/ViewabilityTrackerView.js.map +1 -0
- package/build/commonjs/ViewabilityTrackerView/ViewabilityTrackerViewProps.js +2 -0
- package/build/commonjs/ViewabilityTrackerView/ViewabilityTrackerViewProps.js.map +1 -0
- package/build/commonjs/{Carousel/index.native.js → ViewabilityTrackerView/index.js} +3 -3
- package/build/commonjs/ViewabilityTrackerView/index.js.map +1 -0
- package/build/commonjs/ViewabilityTrackerView/measureViewability.js +42 -0
- package/build/commonjs/ViewabilityTrackerView/measureViewability.js.map +1 -0
- package/build/commonjs/ViewabilityTrackerView/useViewabilityTracker.js +88 -0
- package/build/commonjs/ViewabilityTrackerView/useViewabilityTracker.js.map +1 -0
- package/build/commonjs/hooks/index.js +16 -0
- package/build/commonjs/hooks/index.js.map +1 -1
- package/build/commonjs/hooks/useDeferredExecutor.js +45 -0
- package/build/commonjs/hooks/useDeferredExecutor.js.map +1 -0
- package/build/commonjs/hooks/useIsMounted.js +22 -0
- package/build/commonjs/hooks/useIsMounted.js.map +1 -0
- package/build/commonjs/index.js +22 -1
- package/build/commonjs/index.js.map +1 -1
- package/build/module/Carousel/Carousel.js +115 -0
- package/build/module/Carousel/Carousel.js.map +1 -0
- package/build/module/Carousel/CarouselProps.js.map +1 -1
- package/build/module/Carousel/animation/animationUtils.js +2 -0
- package/build/module/Carousel/animation/animationUtils.js.map +1 -0
- package/build/module/Carousel/animation/createDefaultItemStyle.js +3 -0
- package/build/module/Carousel/animation/createDefaultItemStyle.js.map +1 -0
- package/build/module/Carousel/animation/createDefaultScrollAnimation.js +11 -0
- package/build/module/Carousel/animation/createDefaultScrollAnimation.js.map +1 -0
- package/build/module/Carousel/animation/index.js +5 -0
- package/build/module/Carousel/animation/index.js.map +1 -0
- package/build/module/Carousel/animation/normalItemStyleFactory.js +18 -0
- package/build/module/Carousel/animation/normalItemStyleFactory.js.map +1 -0
- package/build/module/Carousel/animation/parallaxItemStyleFactory.js +55 -0
- package/build/module/Carousel/animation/parallaxItemStyleFactory.js.map +1 -0
- package/build/module/Carousel/components/InternalContext.js +19 -0
- package/build/module/Carousel/components/InternalContext.js.map +1 -0
- package/build/module/Carousel/components/ItemView.js +44 -0
- package/build/module/Carousel/components/ItemView.js.map +1 -0
- package/build/module/Carousel/components/RootView.js +47 -0
- package/build/module/Carousel/components/RootView.js.map +1 -0
- package/build/module/Carousel/components/ScrollViewGesture.js +62 -0
- package/build/module/Carousel/components/ScrollViewGesture.js.map +1 -0
- package/build/module/Carousel/components/index.js +5 -0
- package/build/module/Carousel/components/index.js.map +1 -0
- package/build/module/Carousel/hooks/index.js +8 -0
- package/build/module/Carousel/hooks/index.js.map +1 -0
- package/build/module/Carousel/hooks/useAutoplayController.js +45 -0
- package/build/module/Carousel/hooks/useAutoplayController.js.map +1 -0
- package/build/module/Carousel/hooks/useDimensionChangeReaction.js +14 -0
- package/build/module/Carousel/hooks/useDimensionChangeReaction.js.map +1 -0
- package/build/module/Carousel/hooks/useIndexController.js +54 -0
- package/build/module/Carousel/hooks/useIndexController.js.map +1 -0
- package/build/module/Carousel/hooks/useItemInterpolation.js +51 -0
- package/build/module/Carousel/hooks/useItemInterpolation.js.map +1 -0
- package/build/module/Carousel/hooks/useItemVisibilityStore.js +87 -0
- package/build/module/Carousel/hooks/useItemVisibilityStore.js.map +1 -0
- package/build/module/Carousel/hooks/useLoopedData.js +24 -0
- package/build/module/Carousel/hooks/useLoopedData.js.map +1 -0
- package/build/module/Carousel/hooks/usePagingAnimation.js +115 -0
- package/build/module/Carousel/hooks/usePagingAnimation.js.map +1 -0
- package/build/module/Carousel/index.js +2 -1
- package/build/module/Carousel/index.js.map +1 -1
- package/build/module/Carousel/types.js +3 -0
- package/build/module/Carousel/types.js.map +1 -0
- package/build/module/ViewPager/ChildrenMemoizedPage.js +40 -45
- package/build/module/ViewPager/ChildrenMemoizedPage.js.map +1 -1
- package/build/module/ViewPager/PageStateContext.js +10 -0
- package/build/module/ViewPager/PageStateContext.js.map +1 -0
- package/build/module/ViewPager/ViewPagerNative.js +26 -13
- package/build/module/ViewPager/ViewPagerNative.js.map +1 -1
- package/build/module/ViewPager/ViewPagerProps.js.map +1 -1
- package/build/module/ViewPager/ViewPagerWeb.js +10 -8
- package/build/module/ViewPager/ViewPagerWeb.js.map +1 -1
- package/build/module/ViewPager/index.js +1 -0
- package/build/module/ViewPager/index.js.map +1 -1
- package/build/module/ViewPager/index.native.js +1 -0
- package/build/module/ViewPager/index.native.js.map +1 -1
- package/build/module/ViewPager/utils.js +1 -0
- package/build/module/ViewPager/utils.js.map +1 -1
- package/build/module/ViewabilityTrackerView/Viewability.js +2 -0
- package/build/module/ViewabilityTrackerView/Viewability.js.map +1 -0
- package/build/module/ViewabilityTrackerView/ViewabilityTrackerView.js +28 -0
- package/build/module/ViewabilityTrackerView/ViewabilityTrackerView.js.map +1 -0
- package/build/module/ViewabilityTrackerView/ViewabilityTrackerViewProps.js +2 -0
- package/build/module/ViewabilityTrackerView/ViewabilityTrackerViewProps.js.map +1 -0
- package/build/module/ViewabilityTrackerView/index.js +2 -0
- package/build/module/ViewabilityTrackerView/index.js.map +1 -0
- package/build/module/ViewabilityTrackerView/measureViewability.js +34 -0
- package/build/module/ViewabilityTrackerView/measureViewability.js.map +1 -0
- package/build/module/ViewabilityTrackerView/useViewabilityTracker.js +73 -0
- package/build/module/ViewabilityTrackerView/useViewabilityTracker.js.map +1 -0
- package/build/module/hooks/index.js +2 -0
- package/build/module/hooks/index.js.map +1 -1
- package/build/module/hooks/useDeferredExecutor.js +33 -0
- package/build/module/hooks/useDeferredExecutor.js.map +1 -0
- package/build/module/hooks/useIsMounted.js +13 -0
- package/build/module/hooks/useIsMounted.js.map +1 -0
- package/build/module/index.js +2 -0
- package/build/module/index.js.map +1 -1
- package/build/typescript/Carousel/Carousel.d.ts +5 -0
- package/build/typescript/Carousel/CarouselProps.d.ts +32 -48
- package/build/typescript/Carousel/animation/animationUtils.d.ts +1 -0
- package/build/typescript/Carousel/animation/createDefaultItemStyle.d.ts +2 -0
- package/build/typescript/Carousel/animation/createDefaultScrollAnimation.d.ts +2 -0
- package/build/typescript/Carousel/animation/index.d.ts +4 -0
- package/build/typescript/Carousel/animation/normalItemStyleFactory.d.ts +2 -0
- package/build/typescript/Carousel/animation/parallaxItemStyleFactory.d.ts +9 -0
- package/build/typescript/Carousel/components/InternalContext.d.ts +14 -0
- package/build/typescript/Carousel/components/ItemView.d.ts +7 -0
- package/build/typescript/Carousel/components/RootView.d.ts +16 -0
- package/build/typescript/Carousel/components/ScrollViewGesture.d.ts +12 -0
- package/build/typescript/Carousel/components/index.d.ts +4 -0
- package/build/typescript/Carousel/hooks/index.d.ts +7 -0
- package/build/typescript/Carousel/hooks/useAutoplayController.d.ts +7 -0
- package/build/typescript/Carousel/hooks/useDimensionChangeReaction.d.ts +7 -0
- package/build/typescript/Carousel/hooks/useIndexController.d.ts +10 -0
- package/build/typescript/Carousel/hooks/useItemInterpolation.d.ts +2 -0
- package/build/typescript/Carousel/hooks/useItemVisibilityStore.d.ts +7 -0
- package/build/typescript/Carousel/hooks/useLoopedData.d.ts +1 -0
- package/build/typescript/Carousel/hooks/usePagingAnimation.d.ts +18 -0
- package/build/typescript/Carousel/index.d.ts +3 -1
- package/build/typescript/Carousel/types.d.ts +57 -0
- package/build/typescript/ViewPager/PageStateContext.d.ts +7 -0
- package/build/typescript/ViewPager/ViewPagerNative.d.ts +1 -1
- package/build/typescript/ViewPager/ViewPagerProps.d.ts +11 -0
- package/build/typescript/ViewPager/ViewPagerWeb.d.ts +1 -1
- package/build/typescript/ViewPager/index.d.ts +1 -0
- package/build/typescript/ViewPager/index.native.d.ts +1 -0
- package/build/typescript/ViewPager/utils.d.ts +1 -0
- package/build/typescript/ViewabilityTrackerView/Viewability.d.ts +3 -0
- package/build/typescript/ViewabilityTrackerView/ViewabilityTrackerView.d.ts +2 -0
- package/build/typescript/ViewabilityTrackerView/ViewabilityTrackerViewProps.d.ts +22 -0
- package/build/typescript/ViewabilityTrackerView/index.d.ts +2 -0
- package/build/typescript/ViewabilityTrackerView/measureViewability.d.ts +11 -0
- package/build/typescript/ViewabilityTrackerView/useViewabilityTracker.d.ts +11 -0
- package/build/typescript/hooks/index.d.ts +2 -0
- package/build/typescript/hooks/useDeferredExecutor.d.ts +11 -0
- package/build/typescript/hooks/useIsMounted.d.ts +4 -0
- package/build/typescript/index.d.ts +2 -0
- package/package.json +4 -19
- package/src/Carousel/Carousel.tsx +151 -0
- package/src/Carousel/CarouselProps.ts +34 -53
- package/src/Carousel/animation/animationUtils.ts +1 -0
- package/src/Carousel/animation/createDefaultItemStyle.ts +3 -0
- package/src/Carousel/animation/createDefaultScrollAnimation.ts +13 -0
- package/src/Carousel/animation/index.ts +4 -0
- package/src/Carousel/animation/normalItemStyleFactory.ts +19 -0
- package/src/Carousel/animation/parallaxItemStyleFactory.ts +79 -0
- package/src/Carousel/components/InternalContext.ts +33 -0
- package/src/Carousel/components/ItemView.tsx +56 -0
- package/src/Carousel/components/RootView.tsx +66 -0
- package/src/Carousel/components/ScrollViewGesture.tsx +80 -0
- package/src/Carousel/components/index.ts +4 -0
- package/src/Carousel/hooks/index.ts +7 -0
- package/src/Carousel/hooks/useAutoplayController.ts +54 -0
- package/src/Carousel/hooks/useDimensionChangeReaction.ts +25 -0
- package/src/Carousel/hooks/useIndexController.tsx +76 -0
- package/src/Carousel/hooks/useItemInterpolation.ts +107 -0
- package/src/Carousel/hooks/useItemVisibilityStore.ts +107 -0
- package/src/Carousel/hooks/useLoopedData.ts +26 -0
- package/src/Carousel/hooks/usePagingAnimation.ts +148 -0
- package/src/Carousel/index.ts +4 -2
- package/src/Carousel/types.ts +67 -0
- package/src/ViewPager/ChildrenMemoizedPage.tsx +48 -43
- package/src/ViewPager/PageStateContext.ts +15 -0
- package/src/ViewPager/ViewPagerNative.tsx +35 -11
- package/src/ViewPager/ViewPagerProps.ts +13 -0
- package/src/ViewPager/ViewPagerWeb.tsx +9 -5
- package/src/ViewPager/index.native.ts +1 -0
- package/src/ViewPager/index.ts +1 -0
- package/src/ViewPager/utils.tsx +2 -1
- package/src/ViewabilityTrackerView/Viewability.ts +3 -0
- package/src/ViewabilityTrackerView/ViewabilityTrackerView.tsx +33 -0
- package/src/ViewabilityTrackerView/ViewabilityTrackerViewProps.ts +25 -0
- package/src/ViewabilityTrackerView/index.ts +2 -0
- package/src/ViewabilityTrackerView/measureViewability.ts +56 -0
- package/src/ViewabilityTrackerView/useViewabilityTracker.ts +85 -0
- package/src/hooks/index.ts +2 -0
- package/src/hooks/useDeferredExecutor.ts +46 -0
- package/src/hooks/useIsMounted.ts +19 -0
- package/src/index.ts +3 -0
- package/build/commonjs/Carousel/CarouselNative.js +0 -72
- package/build/commonjs/Carousel/CarouselNative.js.map +0 -1
- package/build/commonjs/Carousel/CarouselWeb.js +0 -237
- package/build/commonjs/Carousel/CarouselWeb.js.map +0 -1
- package/build/commonjs/Carousel/index.native.js.map +0 -1
- package/build/commonjs/Carousel/utils.js +0 -19
- package/build/commonjs/Carousel/utils.js.map +0 -1
- package/build/module/Carousel/CarouselNative.js +0 -54
- package/build/module/Carousel/CarouselNative.js.map +0 -1
- package/build/module/Carousel/CarouselWeb.js +0 -215
- package/build/module/Carousel/CarouselWeb.js.map +0 -1
- package/build/module/Carousel/index.native.js +0 -2
- package/build/module/Carousel/index.native.js.map +0 -1
- package/build/module/Carousel/utils.js +0 -7
- package/build/module/Carousel/utils.js.map +0 -1
- package/build/typescript/Carousel/CarouselNative.d.ts +0 -2
- package/build/typescript/Carousel/CarouselWeb.d.ts +0 -4
- package/build/typescript/Carousel/index.native.d.ts +0 -1
- package/build/typescript/Carousel/utils.d.ts +0 -6
- package/src/Carousel/CarouselNative.tsx +0 -67
- package/src/Carousel/CarouselWeb.tsx +0 -222
- package/src/Carousel/index.native.ts +0 -1
- package/src/Carousel/utils.ts +0 -11
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { CreateItemStyle } from '../types';
|
|
2
|
+
import { itemInterpolationInputRange } from './animationUtils';
|
|
3
|
+
|
|
4
|
+
export interface ParallaxAnimationConfig {
|
|
5
|
+
parallaxActiveItemScale?: number;
|
|
6
|
+
parallaxActiveItemOpacity?: number;
|
|
7
|
+
parallaxAdjacentItemScale?: number;
|
|
8
|
+
parallaxAdjacentItemOpacity?: number;
|
|
9
|
+
parallaxScrollingOffset?: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const defaultParallaxAnimationConfig: Required<Readonly<ParallaxAnimationConfig>> = {
|
|
13
|
+
parallaxActiveItemScale: 0.9,
|
|
14
|
+
parallaxActiveItemOpacity: 1,
|
|
15
|
+
parallaxAdjacentItemScale: Math.pow(0.9, 2),
|
|
16
|
+
parallaxAdjacentItemOpacity: 0.5,
|
|
17
|
+
parallaxScrollingOffset: 50,
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export default function parallaxItemStyleFactory(config: ParallaxAnimationConfig = defaultParallaxAnimationConfig) {
|
|
21
|
+
const {
|
|
22
|
+
parallaxActiveItemScale,
|
|
23
|
+
parallaxActiveItemOpacity,
|
|
24
|
+
parallaxAdjacentItemOpacity,
|
|
25
|
+
parallaxAdjacentItemScale,
|
|
26
|
+
parallaxScrollingOffset,
|
|
27
|
+
}: Required<ParallaxAnimationConfig> = {
|
|
28
|
+
...config,
|
|
29
|
+
...defaultParallaxAnimationConfig,
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const createItemStyle: CreateItemStyle = (itemInterpolation, itemWidth) => {
|
|
33
|
+
const translate = itemInterpolation.interpolate({
|
|
34
|
+
inputRange: itemInterpolationInputRange,
|
|
35
|
+
outputRange: [
|
|
36
|
+
-itemWidth + parallaxScrollingOffset,
|
|
37
|
+
0,
|
|
38
|
+
itemWidth - parallaxScrollingOffset,
|
|
39
|
+
],
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const zIndex = itemInterpolation.interpolate({
|
|
43
|
+
inputRange: itemInterpolationInputRange,
|
|
44
|
+
outputRange: [0, itemWidth, 0],
|
|
45
|
+
extrapolate: 'clamp',
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const scale = itemInterpolation.interpolate({
|
|
49
|
+
inputRange: itemInterpolationInputRange,
|
|
50
|
+
outputRange: [
|
|
51
|
+
parallaxAdjacentItemScale,
|
|
52
|
+
parallaxActiveItemScale,
|
|
53
|
+
parallaxAdjacentItemScale,
|
|
54
|
+
],
|
|
55
|
+
extrapolate: 'clamp',
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const opacity = itemInterpolation.interpolate({
|
|
59
|
+
inputRange: itemInterpolationInputRange,
|
|
60
|
+
outputRange: [
|
|
61
|
+
parallaxAdjacentItemOpacity,
|
|
62
|
+
parallaxActiveItemOpacity,
|
|
63
|
+
parallaxAdjacentItemOpacity,
|
|
64
|
+
],
|
|
65
|
+
extrapolate: 'clamp',
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
transform: [
|
|
70
|
+
{ translateX: translate },
|
|
71
|
+
{ scale },
|
|
72
|
+
],
|
|
73
|
+
zIndex,
|
|
74
|
+
opacity,
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
return createItemStyle;
|
|
79
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { createContext } from 'react';
|
|
2
|
+
import { Animated } from 'react-native';
|
|
3
|
+
import type { CreateItemStyle, ItemVisibilityStore } from '../types';
|
|
4
|
+
|
|
5
|
+
export interface InternalContextValue<ItemT> {
|
|
6
|
+
createItemStyle: CreateItemStyle;
|
|
7
|
+
data: ReadonlyArray<ItemT>;
|
|
8
|
+
globalInterpolation: Animated.AnimatedInterpolation;
|
|
9
|
+
itemHeight: number;
|
|
10
|
+
itemWidth: number;
|
|
11
|
+
itemVisibilityStore: ItemVisibilityStore;
|
|
12
|
+
loop: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const mockItemVisibilityStore: ItemVisibilityStore = {
|
|
16
|
+
dispatch: () => void 0,
|
|
17
|
+
subscribe: () => () => void 0,
|
|
18
|
+
removeAllListeners: () => void 0,
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const initialValue: Readonly<InternalContextValue<any>> = {
|
|
22
|
+
createItemStyle: () => ({}),
|
|
23
|
+
data: [],
|
|
24
|
+
globalInterpolation: Animated.add(new Animated.Value(0), 0),
|
|
25
|
+
itemHeight: 0,
|
|
26
|
+
itemWidth: 0,
|
|
27
|
+
itemVisibilityStore: mockItemVisibilityStore,
|
|
28
|
+
loop: false,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const InternalContext = createContext<InternalContextValue<any>>(initialValue);
|
|
32
|
+
|
|
33
|
+
export default InternalContext;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { ReactElement } from 'react';
|
|
2
|
+
import React, { useContext, useEffect, useMemo, useState } from 'react';
|
|
3
|
+
import { Animated } from 'react-native';
|
|
4
|
+
import { StyleSheet } from '@fountain-ui/core';
|
|
5
|
+
import { useItemInterpolation } from '../hooks';
|
|
6
|
+
import InternalContext from './InternalContext';
|
|
7
|
+
|
|
8
|
+
export interface ItemViewProps {
|
|
9
|
+
children: (interpolation: Animated.AnimatedInterpolation) => ReactElement | null;
|
|
10
|
+
index: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export default function ItemView(props: ItemViewProps) {
|
|
14
|
+
const { children, index } = props;
|
|
15
|
+
|
|
16
|
+
const {
|
|
17
|
+
createItemStyle,
|
|
18
|
+
itemHeight,
|
|
19
|
+
itemWidth,
|
|
20
|
+
itemVisibilityStore,
|
|
21
|
+
} = useContext(InternalContext);
|
|
22
|
+
|
|
23
|
+
const [visible, setVisible] = useState(false);
|
|
24
|
+
|
|
25
|
+
const interpolation = useItemInterpolation(index);
|
|
26
|
+
|
|
27
|
+
const itemStyle = useMemo(
|
|
28
|
+
() => createItemStyle(interpolation, itemWidth),
|
|
29
|
+
[createItemStyle, interpolation],
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
return itemVisibilityStore.subscribe(ranges => {
|
|
34
|
+
const nextVisible = ranges.some(([from, to]) => index >= from && index <= to);
|
|
35
|
+
setVisible(nextVisible);
|
|
36
|
+
});
|
|
37
|
+
}, [itemVisibilityStore]);
|
|
38
|
+
|
|
39
|
+
return (
|
|
40
|
+
<Animated.View
|
|
41
|
+
children={visible ? children(interpolation) : null}
|
|
42
|
+
style={[
|
|
43
|
+
{ width: itemWidth, height: itemHeight },
|
|
44
|
+
styles.absolute,
|
|
45
|
+
// @ts-ignore
|
|
46
|
+
itemStyle,
|
|
47
|
+
]}
|
|
48
|
+
/>
|
|
49
|
+
);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const styles = StyleSheet.create({
|
|
53
|
+
absolute: {
|
|
54
|
+
position: 'absolute',
|
|
55
|
+
},
|
|
56
|
+
});
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import React, { memo, ReactElement, forwardRef } from 'react';
|
|
2
|
+
import type { ViewProps, View } from 'react-native';
|
|
3
|
+
import { Animated } from 'react-native';
|
|
4
|
+
import { StyleSheet } from '@fountain-ui/core';
|
|
5
|
+
import type { RenderItem } from '../types';
|
|
6
|
+
import ItemView from './ItemView';
|
|
7
|
+
|
|
8
|
+
const styles = StyleSheet.create({
|
|
9
|
+
root: {
|
|
10
|
+
width: '100%',
|
|
11
|
+
justifyContent: 'center',
|
|
12
|
+
},
|
|
13
|
+
horizontal: {
|
|
14
|
+
flexDirection: 'row',
|
|
15
|
+
overflow: 'hidden',
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
export type RootViewProps<ItemT> = ViewProps & {
|
|
20
|
+
data: ReadonlyArray<ItemT>;
|
|
21
|
+
itemHeight: number;
|
|
22
|
+
originalData: ReadonlyArray<ItemT>;
|
|
23
|
+
renderItem: RenderItem<ItemT>;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const RootView = forwardRef<View, RootViewProps<any>>(function RootView(props, ref) {
|
|
27
|
+
const {
|
|
28
|
+
data,
|
|
29
|
+
itemHeight,
|
|
30
|
+
originalData,
|
|
31
|
+
renderItem,
|
|
32
|
+
style,
|
|
33
|
+
...otherProps
|
|
34
|
+
} = props;
|
|
35
|
+
|
|
36
|
+
const renderItemView = (item: any, index: number): ReactElement => (
|
|
37
|
+
<ItemView
|
|
38
|
+
key={index}
|
|
39
|
+
index={index}
|
|
40
|
+
>
|
|
41
|
+
{(interpolation) => renderItem({
|
|
42
|
+
item,
|
|
43
|
+
index: index % originalData.length,
|
|
44
|
+
interpolation,
|
|
45
|
+
})}
|
|
46
|
+
</ItemView>
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
const viewStyles = [
|
|
50
|
+
styles.root,
|
|
51
|
+
styles.horizontal,
|
|
52
|
+
{ height: itemHeight },
|
|
53
|
+
style,
|
|
54
|
+
];
|
|
55
|
+
|
|
56
|
+
return (
|
|
57
|
+
<Animated.View
|
|
58
|
+
ref={ref}
|
|
59
|
+
{...otherProps}
|
|
60
|
+
children={data.map(renderItemView)}
|
|
61
|
+
style={viewStyles}
|
|
62
|
+
/>
|
|
63
|
+
);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
export default memo(RootView);
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import React, { useCallback } from 'react';
|
|
3
|
+
import { Animated } from 'react-native';
|
|
4
|
+
import type { PanGestureHandlerStateChangeEvent } from 'react-native-gesture-handler';
|
|
5
|
+
import { PanGestureHandler, State as GestureHandlerState } from 'react-native-gesture-handler';
|
|
6
|
+
import type { AutoplayController, PagingDirection, StartPagingAnimation } from '../types';
|
|
7
|
+
|
|
8
|
+
export interface ScrollViewGestureProps {
|
|
9
|
+
autoplayController: AutoplayController;
|
|
10
|
+
children: ReactNode;
|
|
11
|
+
finalizeAnimation: () => void;
|
|
12
|
+
offsetTx: Animated.Value,
|
|
13
|
+
scrollEnabled: boolean;
|
|
14
|
+
startAnimation: StartPagingAnimation;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const SCROLL_TO_ADJACENT_TX_THRESHOLD = 60;
|
|
18
|
+
const SCROLL_TO_ADJACENT_VX_THRESHOLD = 500;
|
|
19
|
+
|
|
20
|
+
const ACTIVE_OFFSET_ABS_X = 5;
|
|
21
|
+
const activeOffsetX: number[] = [-ACTIVE_OFFSET_ABS_X, ACTIVE_OFFSET_ABS_X];
|
|
22
|
+
|
|
23
|
+
const endAnimationStates: Readonly<GestureHandlerState[]> = [
|
|
24
|
+
GestureHandlerState.CANCELLED,
|
|
25
|
+
GestureHandlerState.END,
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
function shouldScrollToAdjacent(translationX: number, velocityX: number): boolean {
|
|
29
|
+
return Math.abs(translationX) >= SCROLL_TO_ADJACENT_TX_THRESHOLD
|
|
30
|
+
|| Math.abs(velocityX) >= SCROLL_TO_ADJACENT_VX_THRESHOLD;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export default function ScrollViewGesture(props: ScrollViewGestureProps) {
|
|
34
|
+
const {
|
|
35
|
+
autoplayController,
|
|
36
|
+
children,
|
|
37
|
+
finalizeAnimation,
|
|
38
|
+
offsetTx,
|
|
39
|
+
scrollEnabled,
|
|
40
|
+
startAnimation,
|
|
41
|
+
} = props;
|
|
42
|
+
|
|
43
|
+
const { pause: pauseAutoplay, resume: resumeAutoplay } = autoplayController;
|
|
44
|
+
|
|
45
|
+
const handleGestureBegin = useCallback(() => {
|
|
46
|
+
pauseAutoplay();
|
|
47
|
+
|
|
48
|
+
finalizeAnimation();
|
|
49
|
+
}, [finalizeAnimation, pauseAutoplay]);
|
|
50
|
+
|
|
51
|
+
const handleGestureEvent = useCallback(Animated.event(
|
|
52
|
+
[{ nativeEvent: { translationX: offsetTx } }],
|
|
53
|
+
{ useNativeDriver: true },
|
|
54
|
+
), []);
|
|
55
|
+
|
|
56
|
+
const handleHandlerStateChange = useCallback((event: PanGestureHandlerStateChangeEvent) => {
|
|
57
|
+
const { nativeEvent: { translationX, velocityX, state } } = event;
|
|
58
|
+
|
|
59
|
+
if (endAnimationStates.includes(state)) {
|
|
60
|
+
const userDirection: PagingDirection = shouldScrollToAdjacent(translationX, velocityX)
|
|
61
|
+
? (translationX < 0 ? 'next' : 'prev')
|
|
62
|
+
: 'stay';
|
|
63
|
+
|
|
64
|
+
startAnimation(userDirection, true);
|
|
65
|
+
|
|
66
|
+
resumeAutoplay();
|
|
67
|
+
}
|
|
68
|
+
}, [startAnimation, resumeAutoplay]);
|
|
69
|
+
|
|
70
|
+
return (
|
|
71
|
+
<PanGestureHandler
|
|
72
|
+
activeOffsetX={activeOffsetX}
|
|
73
|
+
children={children}
|
|
74
|
+
enabled={scrollEnabled}
|
|
75
|
+
onBegan={handleGestureBegin}
|
|
76
|
+
onGestureEvent={handleGestureEvent}
|
|
77
|
+
onHandlerStateChange={handleHandlerStateChange}
|
|
78
|
+
/>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { default as useAutoplayController } from './useAutoplayController';
|
|
2
|
+
export { default as useDimensionChangeReaction } from './useDimensionChangeReaction';
|
|
3
|
+
export { default as useIndexController } from './useIndexController';
|
|
4
|
+
export { default as useItemInterpolation } from './useItemInterpolation';
|
|
5
|
+
export { default as useLoopedData } from './useLoopedData';
|
|
6
|
+
export { default as usePagingAnimation } from './usePagingAnimation';
|
|
7
|
+
export { default as useItemVisibilityStore } from './useItemVisibilityStore';
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef } from 'react';
|
|
2
|
+
import type { AutoplayController, StartPagingAnimation } from '../types';
|
|
3
|
+
import { useDeferredExecutor } from '../../hooks';
|
|
4
|
+
|
|
5
|
+
export interface AutoplayConfig {
|
|
6
|
+
enabled: boolean;
|
|
7
|
+
intervalMillis: number;
|
|
8
|
+
startPagingAnimation: StartPagingAnimation;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default function useAutoplayController(config: AutoplayConfig): AutoplayController {
|
|
12
|
+
const {
|
|
13
|
+
enabled,
|
|
14
|
+
intervalMillis,
|
|
15
|
+
startPagingAnimation,
|
|
16
|
+
} = config;
|
|
17
|
+
|
|
18
|
+
const paused = useRef<boolean>(false);
|
|
19
|
+
|
|
20
|
+
const executor = useDeferredExecutor();
|
|
21
|
+
|
|
22
|
+
const play = useCallback(() => {
|
|
23
|
+
if (paused.current) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
executor.execute(() => {
|
|
28
|
+
startPagingAnimation('next', false);
|
|
29
|
+
play();
|
|
30
|
+
}, intervalMillis);
|
|
31
|
+
}, [intervalMillis, startPagingAnimation]);
|
|
32
|
+
|
|
33
|
+
const pause = useCallback(() => {
|
|
34
|
+
executor.cancel();
|
|
35
|
+
paused.current = true;
|
|
36
|
+
}, []);
|
|
37
|
+
|
|
38
|
+
const resume = useCallback(() => {
|
|
39
|
+
paused.current = false;
|
|
40
|
+
if (enabled) {
|
|
41
|
+
play();
|
|
42
|
+
}
|
|
43
|
+
}, [play, enabled]);
|
|
44
|
+
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
if (enabled) {
|
|
47
|
+
resume();
|
|
48
|
+
} else {
|
|
49
|
+
pause();
|
|
50
|
+
}
|
|
51
|
+
}, [enabled]);
|
|
52
|
+
|
|
53
|
+
return { pause, resume };
|
|
54
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
import { Animated } from 'react-native';
|
|
3
|
+
|
|
4
|
+
export interface Parameters {
|
|
5
|
+
controlledTx: Animated.AnimatedValue;
|
|
6
|
+
currentIndex: number;
|
|
7
|
+
itemWidth: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export default function useDimensionChangeReaction(params: Parameters) {
|
|
11
|
+
const {
|
|
12
|
+
controlledTx,
|
|
13
|
+
currentIndex,
|
|
14
|
+
itemWidth,
|
|
15
|
+
} = params;
|
|
16
|
+
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
const newTx = -currentIndex * itemWidth;
|
|
19
|
+
controlledTx.setValue(newTx);
|
|
20
|
+
}, [
|
|
21
|
+
controlledTx,
|
|
22
|
+
currentIndex,
|
|
23
|
+
itemWidth,
|
|
24
|
+
]);
|
|
25
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
2
|
+
import { Animated } from 'react-native';
|
|
3
|
+
import type { IndexController } from '../types';
|
|
4
|
+
|
|
5
|
+
export interface UseIndexControllerParameters {
|
|
6
|
+
controlledTx: Animated.AnimatedValue;
|
|
7
|
+
initialIndex: number;
|
|
8
|
+
itemWidth: number;
|
|
9
|
+
numberOfOriginalData: number;
|
|
10
|
+
onIndexChange?: (newIndex: number) => void;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const normalizeIndex = (maybeIndex: number, numberOfData: number): number =>
|
|
14
|
+
Math.abs(Math.floor(maybeIndex)) % numberOfData;
|
|
15
|
+
|
|
16
|
+
export default function useIndexController(params: UseIndexControllerParameters): IndexController {
|
|
17
|
+
const {
|
|
18
|
+
controlledTx,
|
|
19
|
+
initialIndex,
|
|
20
|
+
itemWidth,
|
|
21
|
+
numberOfOriginalData,
|
|
22
|
+
onIndexChange,
|
|
23
|
+
} = params;
|
|
24
|
+
|
|
25
|
+
const indexRef = useRef<number>(initialIndex);
|
|
26
|
+
const [index, setIndex] = useState<number>(indexRef.current);
|
|
27
|
+
|
|
28
|
+
const maybeIndex = useMemo(() => {
|
|
29
|
+
const negative = new Animated.Value(-1);
|
|
30
|
+
const reversedTx = Animated.multiply(controlledTx, negative);
|
|
31
|
+
const normalized = Animated.divide(reversedTx, itemWidth);
|
|
32
|
+
return Animated.modulo(normalized, numberOfOriginalData);
|
|
33
|
+
}, [
|
|
34
|
+
controlledTx,
|
|
35
|
+
itemWidth,
|
|
36
|
+
numberOfOriginalData,
|
|
37
|
+
]);
|
|
38
|
+
|
|
39
|
+
useEffect(() => {
|
|
40
|
+
const subscriptionId = maybeIndex.addListener((observedValue) => {
|
|
41
|
+
const newIndex = normalizeIndex(observedValue.value, numberOfOriginalData);
|
|
42
|
+
|
|
43
|
+
if (indexRef.current !== newIndex) {
|
|
44
|
+
indexRef.current = newIndex;
|
|
45
|
+
setIndex(newIndex);
|
|
46
|
+
|
|
47
|
+
onIndexChange?.(newIndex);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
return () => {
|
|
52
|
+
maybeIndex.removeListener(subscriptionId);
|
|
53
|
+
};
|
|
54
|
+
}, [
|
|
55
|
+
maybeIndex,
|
|
56
|
+
numberOfOriginalData,
|
|
57
|
+
onIndexChange,
|
|
58
|
+
]);
|
|
59
|
+
|
|
60
|
+
const getCurrentIndex = useCallback(() => indexRef.current, []);
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
currentIndex: index,
|
|
64
|
+
getCurrentIndex,
|
|
65
|
+
lastIndex: numberOfOriginalData - 1,
|
|
66
|
+
monitorElement: (
|
|
67
|
+
<Animated.View
|
|
68
|
+
collapsable={false}
|
|
69
|
+
style={[
|
|
70
|
+
{ zIndex: maybeIndex },
|
|
71
|
+
{ width: 1, height: 1, position: 'absolute' },
|
|
72
|
+
]}
|
|
73
|
+
/>
|
|
74
|
+
),
|
|
75
|
+
};
|
|
76
|
+
};
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { useContext, useMemo } from 'react';
|
|
2
|
+
import { Animated } from 'react-native';
|
|
3
|
+
import { InternalContext } from '../components';
|
|
4
|
+
|
|
5
|
+
const OVERSCROLL_FRICTION_FACTOR = 4;
|
|
6
|
+
|
|
7
|
+
export default function useItemInterpolation(index: number): Animated.AnimatedInterpolation {
|
|
8
|
+
const {
|
|
9
|
+
itemWidth,
|
|
10
|
+
data,
|
|
11
|
+
globalInterpolation,
|
|
12
|
+
loop,
|
|
13
|
+
} = useContext(InternalContext);
|
|
14
|
+
|
|
15
|
+
const numberOfData = data.length;
|
|
16
|
+
|
|
17
|
+
const interpolationConfigOnLoop = useMemo<Animated.InterpolationConfigType>(() => {
|
|
18
|
+
const lastIndex = numberOfData - 1;
|
|
19
|
+
const maxWidth = Math.abs(itemWidth * numberOfData);
|
|
20
|
+
const halfOfItemWidth = 0.5 * itemWidth;
|
|
21
|
+
|
|
22
|
+
const positiveCount = Math.round(lastIndex / 2);
|
|
23
|
+
const startPosition = index > positiveCount
|
|
24
|
+
? (index - numberOfData) * itemWidth
|
|
25
|
+
: itemWidth * index;
|
|
26
|
+
|
|
27
|
+
const max = positiveCount * itemWidth;
|
|
28
|
+
const min = -((lastIndex - positiveCount) * itemWidth);
|
|
29
|
+
|
|
30
|
+
const inputRange = [
|
|
31
|
+
-maxWidth,
|
|
32
|
+
min - halfOfItemWidth - startPosition - Number.MIN_VALUE,
|
|
33
|
+
min - halfOfItemWidth - startPosition,
|
|
34
|
+
0,
|
|
35
|
+
max + halfOfItemWidth - startPosition,
|
|
36
|
+
max + halfOfItemWidth - startPosition + Number.MIN_VALUE,
|
|
37
|
+
maxWidth,
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
const outputRange = [
|
|
41
|
+
startPosition,
|
|
42
|
+
max + halfOfItemWidth - Number.MIN_VALUE,
|
|
43
|
+
min - halfOfItemWidth,
|
|
44
|
+
startPosition,
|
|
45
|
+
max + halfOfItemWidth,
|
|
46
|
+
min - halfOfItemWidth + Number.MIN_VALUE,
|
|
47
|
+
startPosition,
|
|
48
|
+
];
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
inputRange,
|
|
52
|
+
outputRange,
|
|
53
|
+
extrapolate: 'clamp',
|
|
54
|
+
};
|
|
55
|
+
}, [
|
|
56
|
+
index,
|
|
57
|
+
itemWidth,
|
|
58
|
+
numberOfData,
|
|
59
|
+
]);
|
|
60
|
+
|
|
61
|
+
const interpolationConfigOnNoLoop = useMemo<Animated.InterpolationConfigType>(() => {
|
|
62
|
+
const lastIndex = numberOfData - 1;
|
|
63
|
+
const maxWidth = itemWidth * lastIndex;
|
|
64
|
+
const startPosition = itemWidth * index;
|
|
65
|
+
const friction = itemWidth / OVERSCROLL_FRICTION_FACTOR;
|
|
66
|
+
|
|
67
|
+
const inputRange = [
|
|
68
|
+
-(maxWidth + itemWidth), // overscroll
|
|
69
|
+
-maxWidth,
|
|
70
|
+
0,
|
|
71
|
+
itemWidth, // overscroll
|
|
72
|
+
];
|
|
73
|
+
|
|
74
|
+
const outputRange = [
|
|
75
|
+
-maxWidth + startPosition - friction,
|
|
76
|
+
-maxWidth + startPosition,
|
|
77
|
+
startPosition,
|
|
78
|
+
startPosition + friction,
|
|
79
|
+
];
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
inputRange,
|
|
83
|
+
outputRange,
|
|
84
|
+
extrapolate: 'clamp',
|
|
85
|
+
};
|
|
86
|
+
}, [
|
|
87
|
+
index,
|
|
88
|
+
itemWidth,
|
|
89
|
+
numberOfData,
|
|
90
|
+
]);
|
|
91
|
+
|
|
92
|
+
return useMemo(() => {
|
|
93
|
+
const interpolationConfig = loop
|
|
94
|
+
? interpolationConfigOnLoop
|
|
95
|
+
: interpolationConfigOnNoLoop;
|
|
96
|
+
|
|
97
|
+
const offsetX = globalInterpolation.interpolate(interpolationConfig);
|
|
98
|
+
|
|
99
|
+
return Animated.divide(offsetX, itemWidth);
|
|
100
|
+
}, [
|
|
101
|
+
globalInterpolation,
|
|
102
|
+
interpolationConfigOnLoop,
|
|
103
|
+
interpolationConfigOnNoLoop,
|
|
104
|
+
itemWidth,
|
|
105
|
+
loop,
|
|
106
|
+
]);
|
|
107
|
+
};
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from 'react';
|
|
2
|
+
import type { ItemVisibilityStore, VisibleIndexRanges } from '../types';
|
|
3
|
+
|
|
4
|
+
export interface Parameters {
|
|
5
|
+
currentIndex: number;
|
|
6
|
+
numberOfData: number;
|
|
7
|
+
windowSize: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
class SimpleItemVisibilityStore implements ItemVisibilityStore {
|
|
11
|
+
|
|
12
|
+
private store: VisibleIndexRanges;
|
|
13
|
+
|
|
14
|
+
constructor(initialValue: VisibleIndexRanges) {
|
|
15
|
+
this.store = initialValue;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
private listeners: Array<(ranges: VisibleIndexRanges) => void> = [];
|
|
19
|
+
|
|
20
|
+
subscribe(listener: (ranges: VisibleIndexRanges) => void) {
|
|
21
|
+
this.listeners.push(listener);
|
|
22
|
+
|
|
23
|
+
listener(this.store);
|
|
24
|
+
|
|
25
|
+
return () => {
|
|
26
|
+
const index = this.listeners.indexOf(listener);
|
|
27
|
+
this.listeners.splice(index, 1);
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
dispatch(ranges: VisibleIndexRanges): void {
|
|
32
|
+
this.store = ranges;
|
|
33
|
+
for (const id in this.listeners) {
|
|
34
|
+
const listener = this.listeners[id];
|
|
35
|
+
listener?.(ranges);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
removeAllListeners(): void {
|
|
40
|
+
this.listeners.splice(0, this.listeners.length);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function normalize(windowSize: number, numberOfData: number): number {
|
|
46
|
+
if (windowSize <= 0) {
|
|
47
|
+
return numberOfData;
|
|
48
|
+
}
|
|
49
|
+
if (windowSize > 0 && windowSize % 2 === 0) {
|
|
50
|
+
return windowSize + 1;
|
|
51
|
+
}
|
|
52
|
+
return windowSize;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function mod(value: number, modulo: number): number {
|
|
56
|
+
return ((value % modulo) + modulo) % modulo;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function makeVisibleIndexRanges(numberOfData: number, windowSize: number, index: number): VisibleIndexRanges {
|
|
60
|
+
const ws = normalize(windowSize, numberOfData);
|
|
61
|
+
|
|
62
|
+
const firstIndex = 0;
|
|
63
|
+
const lastIndex = numberOfData - 1;
|
|
64
|
+
const halfNumberOfAdjacent = Math.floor(ws / 2);
|
|
65
|
+
|
|
66
|
+
const leftSide = index - halfNumberOfAdjacent;
|
|
67
|
+
const rightSide = index + halfNumberOfAdjacent;
|
|
68
|
+
|
|
69
|
+
const leftRanges = leftSide >= 0
|
|
70
|
+
? [[leftSide, index]]
|
|
71
|
+
: [[firstIndex, index], [mod(leftSide, numberOfData), lastIndex]];
|
|
72
|
+
|
|
73
|
+
const rightRanges = rightSide < numberOfData
|
|
74
|
+
? [[index, rightSide]]
|
|
75
|
+
: [[index, lastIndex], [firstIndex, mod(rightSide, numberOfData)]];
|
|
76
|
+
|
|
77
|
+
// @ts-ignore
|
|
78
|
+
return [
|
|
79
|
+
...leftRanges,
|
|
80
|
+
...rightRanges,
|
|
81
|
+
];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export default function useItemVisibilityStore(params: Parameters): ItemVisibilityStore {
|
|
85
|
+
const {
|
|
86
|
+
currentIndex,
|
|
87
|
+
numberOfData,
|
|
88
|
+
windowSize,
|
|
89
|
+
} = params;
|
|
90
|
+
|
|
91
|
+
const [initialRange] = useState(() => makeVisibleIndexRanges(numberOfData, windowSize, currentIndex));
|
|
92
|
+
const store = useRef(new SimpleItemVisibilityStore(initialRange)).current;
|
|
93
|
+
|
|
94
|
+
useEffect(() => {
|
|
95
|
+
const newRanges = makeVisibleIndexRanges(numberOfData, windowSize, currentIndex);
|
|
96
|
+
|
|
97
|
+
store.dispatch(newRanges);
|
|
98
|
+
}, [currentIndex, numberOfData, windowSize]);
|
|
99
|
+
|
|
100
|
+
useEffect(() => {
|
|
101
|
+
return () => {
|
|
102
|
+
store.removeAllListeners();
|
|
103
|
+
};
|
|
104
|
+
}, []);
|
|
105
|
+
|
|
106
|
+
return store;
|
|
107
|
+
};
|