@fountain-ui/lab 2.0.0-beta.13 → 2.0.0-beta.16
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 +27 -30
- package/build/commonjs/Carousel/Carousel.js.map +1 -1
- package/build/commonjs/Carousel/animation/createDefaultScrollAnimation.js +2 -2
- package/build/commonjs/Carousel/animation/createDefaultScrollAnimation.js.map +1 -1
- package/build/commonjs/Carousel/animation/parallaxItemStyleFactory.js +2 -2
- package/build/commonjs/Carousel/animation/parallaxItemStyleFactory.js.map +1 -1
- package/build/commonjs/Carousel/components/ItemView.js +2 -2
- package/build/commonjs/Carousel/components/ItemView.js.map +1 -1
- package/build/commonjs/Carousel/components/ScrollViewGesture.js +6 -6
- package/build/commonjs/Carousel/components/ScrollViewGesture.js.map +1 -1
- package/build/commonjs/Carousel/{hooks → components}/useItemInterpolation.js +6 -4
- package/build/commonjs/Carousel/components/useItemInterpolation.js.map +1 -0
- package/build/commonjs/Carousel/hooks/index.js +0 -16
- package/build/commonjs/Carousel/hooks/index.js.map +1 -1
- package/build/commonjs/Carousel/hooks/useIndexController.js +23 -45
- package/build/commonjs/Carousel/hooks/useIndexController.js.map +1 -1
- package/build/commonjs/Carousel/hooks/useItemVisibilityStore.js +12 -12
- package/build/commonjs/Carousel/hooks/useItemVisibilityStore.js.map +1 -1
- package/build/commonjs/Carousel/hooks/usePagingAnimation.js +72 -56
- package/build/commonjs/Carousel/hooks/usePagingAnimation.js.map +1 -1
- package/build/commonjs/Carousel/tick.js +16 -0
- package/build/commonjs/Carousel/tick.js.map +1 -0
- package/build/commonjs/Carousel/types.js +1 -0
- package/build/commonjs/Carousel/types.js.map +1 -1
- package/build/commonjs/ViewPager/ChildrenMemoizedPage.js +53 -47
- package/build/commonjs/ViewPager/ChildrenMemoizedPage.js.map +1 -1
- package/build/commonjs/ViewPager/InternalContext.js +17 -0
- package/build/commonjs/ViewPager/InternalContext.js.map +1 -0
- package/build/commonjs/ViewPager/ViewPagerNative.js +40 -17
- package/build/commonjs/ViewPager/ViewPagerNative.js.map +1 -1
- package/build/commonjs/ViewPager/ViewPagerProps.js.map +1 -1
- package/build/commonjs/ViewPager/ViewPagerWeb.js +19 -8
- package/build/commonjs/ViewPager/ViewPagerWeb.js.map +1 -1
- package/build/commonjs/ViewPager/index.js.map +1 -1
- package/build/commonjs/ViewPager/types.js +6 -0
- package/build/commonjs/ViewPager/types.js.map +1 -0
- package/build/commonjs/ViewPager/usePageStore.js +35 -0
- package/build/commonjs/ViewPager/usePageStore.js.map +1 -0
- package/build/commonjs/ViewPager/utils.js.map +1 -1
- package/build/commonjs/ViewabilityTrackerView/measureViewability.js +6 -6
- package/build/commonjs/ViewabilityTrackerView/measureViewability.js.map +1 -1
- package/build/commonjs/hooks/useUnstableCollapsibleAppBar.js +1 -1
- package/build/commonjs/hooks/useUnstableCollapsibleAppBar.js.map +1 -1
- package/build/module/Carousel/Carousel.js +27 -32
- package/build/module/Carousel/Carousel.js.map +1 -1
- package/build/module/Carousel/animation/createDefaultScrollAnimation.js +2 -2
- package/build/module/Carousel/animation/createDefaultScrollAnimation.js.map +1 -1
- package/build/module/Carousel/animation/parallaxItemStyleFactory.js +2 -2
- package/build/module/Carousel/animation/parallaxItemStyleFactory.js.map +1 -1
- package/build/module/Carousel/components/ItemView.js +1 -1
- package/build/module/Carousel/components/ItemView.js.map +1 -1
- package/build/module/Carousel/components/ScrollViewGesture.js +6 -6
- package/build/module/Carousel/components/ScrollViewGesture.js.map +1 -1
- package/build/module/Carousel/{hooks → components}/useItemInterpolation.js +3 -3
- package/build/module/Carousel/components/useItemInterpolation.js.map +1 -0
- package/build/module/Carousel/hooks/index.js +0 -2
- package/build/module/Carousel/hooks/index.js.map +1 -1
- package/build/module/Carousel/hooks/useIndexController.js +23 -39
- package/build/module/Carousel/hooks/useIndexController.js.map +1 -1
- package/build/module/Carousel/hooks/useItemVisibilityStore.js +10 -11
- package/build/module/Carousel/hooks/useItemVisibilityStore.js.map +1 -1
- package/build/module/Carousel/hooks/usePagingAnimation.js +73 -56
- package/build/module/Carousel/hooks/usePagingAnimation.js.map +1 -1
- package/build/module/Carousel/tick.js +6 -0
- package/build/module/Carousel/tick.js.map +1 -0
- package/build/module/Carousel/types.js +1 -0
- package/build/module/Carousel/types.js.map +1 -1
- package/build/module/ViewPager/ChildrenMemoizedPage.js +53 -47
- package/build/module/ViewPager/ChildrenMemoizedPage.js.map +1 -1
- package/build/module/ViewPager/InternalContext.js +7 -0
- package/build/module/ViewPager/InternalContext.js.map +1 -0
- package/build/module/ViewPager/ViewPagerNative.js +38 -17
- package/build/module/ViewPager/ViewPagerNative.js.map +1 -1
- package/build/module/ViewPager/ViewPagerProps.js.map +1 -1
- package/build/module/ViewPager/ViewPagerWeb.js +16 -8
- package/build/module/ViewPager/ViewPagerWeb.js.map +1 -1
- package/build/module/ViewPager/index.js.map +1 -1
- package/build/module/ViewPager/types.js +2 -0
- package/build/module/ViewPager/types.js.map +1 -0
- package/build/module/ViewPager/usePageStore.js +25 -0
- package/build/module/ViewPager/usePageStore.js.map +1 -0
- package/build/module/ViewPager/utils.js.map +1 -1
- package/build/module/ViewabilityTrackerView/measureViewability.js +2 -2
- package/build/module/ViewabilityTrackerView/measureViewability.js.map +1 -1
- package/build/module/hooks/useUnstableCollapsibleAppBar.js +1 -1
- package/build/module/hooks/useUnstableCollapsibleAppBar.js.map +1 -1
- package/build/typescript/Carousel/components/ScrollViewGesture.d.ts +2 -2
- package/build/typescript/Carousel/{hooks → components}/useItemInterpolation.d.ts +0 -0
- package/build/typescript/Carousel/hooks/index.d.ts +0 -2
- package/build/typescript/Carousel/hooks/useIndexController.d.ts +0 -2
- package/build/typescript/Carousel/hooks/useItemVisibilityStore.d.ts +5 -2
- package/build/typescript/Carousel/hooks/usePagingAnimation.d.ts +5 -7
- package/build/typescript/Carousel/tick.d.ts +2 -0
- package/build/typescript/Carousel/types.d.ts +4 -2
- package/build/typescript/ViewPager/ChildrenMemoizedPage.d.ts +1 -1
- package/build/typescript/ViewPager/InternalContext.d.ts +7 -0
- package/build/typescript/ViewPager/ViewPagerNative.d.ts +2 -2
- package/build/typescript/ViewPager/ViewPagerProps.d.ts +4 -22
- package/build/typescript/ViewPager/ViewPagerWeb.d.ts +2 -2
- package/build/typescript/ViewPager/index.d.ts +2 -1
- package/build/typescript/ViewPager/types.d.ts +19 -0
- package/build/typescript/ViewPager/usePageStore.d.ts +2 -0
- package/build/typescript/ViewPager/utils.d.ts +1 -1
- package/package.json +3 -3
- package/src/Carousel/Carousel.tsx +25 -34
- package/src/Carousel/animation/createDefaultScrollAnimation.ts +2 -2
- package/src/Carousel/animation/parallaxItemStyleFactory.ts +1 -1
- package/src/Carousel/components/ItemView.tsx +1 -1
- package/src/Carousel/components/ScrollViewGesture.tsx +8 -7
- package/src/Carousel/{hooks → components}/useItemInterpolation.ts +3 -3
- package/src/Carousel/hooks/index.ts +0 -2
- package/src/Carousel/hooks/useIndexController.tsx +25 -47
- package/src/Carousel/hooks/useItemVisibilityStore.ts +17 -13
- package/src/Carousel/hooks/usePagingAnimation.ts +104 -64
- package/src/Carousel/tick.ts +6 -0
- package/src/Carousel/types.ts +6 -2
- package/src/ViewPager/ChildrenMemoizedPage.tsx +53 -50
- package/src/ViewPager/InternalContext.ts +13 -0
- package/src/ViewPager/ViewPagerNative.tsx +53 -39
- package/src/ViewPager/ViewPagerProps.ts +4 -27
- package/src/ViewPager/ViewPagerWeb.tsx +23 -18
- package/src/ViewPager/index.ts +2 -1
- package/src/ViewPager/types.ts +24 -0
- package/src/ViewPager/usePageStore.ts +30 -0
- package/src/ViewPager/utils.tsx +1 -1
- package/src/ViewabilityTrackerView/measureViewability.ts +1 -3
- package/src/hooks/useUnstableCollapsibleAppBar.ts +1 -1
- package/build/commonjs/Carousel/hooks/useDimensionChangeReaction.js +0 -23
- package/build/commonjs/Carousel/hooks/useDimensionChangeReaction.js.map +0 -1
- package/build/commonjs/Carousel/hooks/useItemInterpolation.js.map +0 -1
- package/build/module/Carousel/hooks/useDimensionChangeReaction.js +0 -14
- package/build/module/Carousel/hooks/useDimensionChangeReaction.js.map +0 -1
- package/build/module/Carousel/hooks/useItemInterpolation.js.map +0 -1
- package/build/typescript/Carousel/hooks/useDimensionChangeReaction.d.ts +0 -7
- package/src/Carousel/hooks/useDimensionChangeReaction.ts +0 -25
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["SimpleItemVisibilityStore","constructor","initialValue","store","subscribe","listener","listeners","push","index","indexOf","splice","dispatch","ranges","id","removeAllListeners","length","normalize","windowSize","numberOfData","
|
|
1
|
+
{"version":3,"names":["SimpleItemVisibilityStore","constructor","initialValue","store","subscribe","listener","listeners","push","index","indexOf","splice","dispatch","ranges","id","removeAllListeners","length","normalize","windowSize","numberOfData","makeVisibleIndexRanges","ws","firstIndex","lastIndex","halfNumberOfAdjacent","Math","floor","leftSide","rightSide","leftRanges","mod","rightRanges","useItemVisibilityStore","params","initialIndex","initialRange","useState","useRef","current","onIndexChange","useCallback","newIndex","newRanges","useEffect"],"sources":["useItemVisibilityStore.ts"],"sourcesContent":["import { useCallback, useEffect, useRef, useState } from 'react';\nimport { mod } from '@fountain-ui/utils';\nimport type { ItemVisibilityStore, VisibleIndexRanges } from '../types';\n\nexport interface Parameters {\n initialIndex: number;\n numberOfData: number;\n windowSize: number;\n}\n\nexport interface OnIndexChange {\n (newIndex: number): void;\n}\n\nclass SimpleItemVisibilityStore implements ItemVisibilityStore {\n\n private store: VisibleIndexRanges;\n\n constructor(initialValue: VisibleIndexRanges) {\n this.store = initialValue;\n }\n\n private listeners: Array<(ranges: VisibleIndexRanges) => void> = [];\n\n subscribe(listener: (ranges: VisibleIndexRanges) => void) {\n this.listeners.push(listener);\n\n listener(this.store);\n\n return () => {\n const index = this.listeners.indexOf(listener);\n this.listeners.splice(index, 1);\n };\n }\n\n dispatch(ranges: VisibleIndexRanges): void {\n this.store = ranges;\n for (const id in this.listeners) {\n const listener = this.listeners[id];\n listener?.(ranges);\n }\n }\n\n removeAllListeners(): void {\n this.listeners.splice(0, this.listeners.length);\n }\n\n}\n\nfunction normalize(windowSize: number, numberOfData: number): number {\n if (windowSize <= 0) {\n return numberOfData;\n }\n if (windowSize > 0 && windowSize % 2 === 0) {\n return windowSize + 1;\n }\n return windowSize;\n}\n\nfunction makeVisibleIndexRanges(numberOfData: number, windowSize: number, index: number): VisibleIndexRanges {\n const ws = normalize(windowSize, numberOfData);\n\n const firstIndex = 0;\n const lastIndex = numberOfData - 1;\n const halfNumberOfAdjacent = Math.floor(ws / 2);\n\n const leftSide = index - halfNumberOfAdjacent;\n const rightSide = index + halfNumberOfAdjacent;\n\n const leftRanges = leftSide >= 0\n ? [[leftSide, index]]\n : [[firstIndex, index], [mod(leftSide, numberOfData), lastIndex]];\n\n const rightRanges = rightSide < numberOfData\n ? [[index, rightSide]]\n : [[index, lastIndex], [firstIndex, mod(rightSide, numberOfData)]];\n\n // @ts-ignore\n return [\n ...leftRanges,\n ...rightRanges,\n ];\n}\n\nexport default function useItemVisibilityStore(params: Parameters): [ItemVisibilityStore, OnIndexChange] {\n const {\n initialIndex,\n numberOfData,\n windowSize,\n } = params;\n\n const [initialRange] = useState(() => {\n return makeVisibleIndexRanges(numberOfData, windowSize, initialIndex);\n });\n\n const store = useRef(new SimpleItemVisibilityStore(initialRange)).current;\n\n const onIndexChange: OnIndexChange = useCallback((newIndex) => {\n const newRanges = makeVisibleIndexRanges(numberOfData, windowSize, newIndex);\n\n store.dispatch(newRanges);\n }, [numberOfData, windowSize]);\n\n useEffect(() => {\n return () => {\n store.removeAllListeners();\n };\n }, []);\n\n return [store, onIndexChange];\n};\n"],"mappings":";;;;;;;AAAA;;AACA;;;;AAaA,MAAMA,yBAAN,CAA+D;EAI3DC,WAAW,CAACC,YAAD,EAAmC;IAAA;;IAAA,mCAImB,EAJnB;;IAC1C,KAAKC,KAAL,GAAaD,YAAb;EACH;;EAIDE,SAAS,CAACC,QAAD,EAAiD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AM;IACZ,OAAO,MAAM;MACTvC,KAAK,CAACW,kBAAN;IACH,CAFD;EAGH,CAJD,EAIG,EAJH;EAMA,OAAO,CAACX,KAAD,EAAQmC,aAAR,CAAP;AACH;;AAAA"}
|
|
@@ -7,8 +7,6 @@ exports.default = usePagingAnimation;
|
|
|
7
7
|
|
|
8
8
|
var _react = require("react");
|
|
9
9
|
|
|
10
|
-
var _reactNative = require("react-native");
|
|
11
|
-
|
|
12
10
|
function directionToValue(itemWidth) {
|
|
13
11
|
return function (direction) {
|
|
14
12
|
switch (direction) {
|
|
@@ -24,73 +22,89 @@ function directionToValue(itemWidth) {
|
|
|
24
22
|
};
|
|
25
23
|
}
|
|
26
24
|
|
|
25
|
+
function toValueCompensator(itemWidth) {
|
|
26
|
+
return function (toValue, currentOffset) {
|
|
27
|
+
const remainder = Math.abs(currentOffset % itemWidth);
|
|
28
|
+
const halfOfItemWidth = Math.abs(itemWidth / 2);
|
|
29
|
+
const compensateVector = remainder > halfOfItemWidth ? remainder - itemWidth : remainder;
|
|
30
|
+
const direction = currentOffset > 0 ? -1 : 1;
|
|
31
|
+
return toValue + direction * compensateVector;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
27
35
|
function usePagingAnimation(params) {
|
|
28
36
|
const {
|
|
29
|
-
controlledTx,
|
|
30
37
|
createScrollAnimation,
|
|
31
|
-
getCurrentIndex,
|
|
32
38
|
itemWidth,
|
|
33
|
-
|
|
39
|
+
indexController,
|
|
34
40
|
loop,
|
|
35
41
|
numberOfData,
|
|
36
|
-
|
|
42
|
+
offsetX,
|
|
43
|
+
translateX
|
|
37
44
|
} = params;
|
|
38
|
-
const
|
|
45
|
+
const {
|
|
46
|
+
getCurrentIndex,
|
|
47
|
+
lastIndex,
|
|
48
|
+
notifyAnimationState,
|
|
49
|
+
notifyOffsetHasChanged
|
|
50
|
+
} = indexController;
|
|
39
51
|
const toValueRef = (0, _react.useRef)(0);
|
|
40
|
-
const
|
|
41
|
-
(0, _react.
|
|
42
|
-
|
|
43
|
-
|
|
52
|
+
const currentOffsetRef = (0, _react.useRef)(0);
|
|
53
|
+
const isAnimatingRef = (0, _react.useRef)(false);
|
|
54
|
+
const maxWidth = Math.abs(numberOfData * itemWidth);
|
|
55
|
+
const ensureOffsetBoundary = (0, _react.useCallback)(offset => {
|
|
56
|
+
if (loop) {
|
|
57
|
+
const isCloseToEnd = Math.abs(offset) >= maxWidth - itemWidth;
|
|
58
|
+
|
|
59
|
+
if (isCloseToEnd) {
|
|
60
|
+
const signOfOffset = offset > 0 ? 1 : -1;
|
|
61
|
+
return offset + -signOfOffset * maxWidth;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
44
64
|
|
|
45
|
-
|
|
46
|
-
|
|
65
|
+
return offset % maxWidth;
|
|
66
|
+
}, [itemWidth, loop, maxWidth]);
|
|
67
|
+
const requireNewOffset = (0, _react.useCallback)(newOffset => {
|
|
68
|
+
const nextOffset = ensureOffsetBoundary(newOffset);
|
|
69
|
+
currentOffsetRef.current = nextOffset;
|
|
70
|
+
offsetX.setValue(nextOffset);
|
|
71
|
+
toValueRef.current = 0;
|
|
72
|
+
translateX.setValue(0);
|
|
73
|
+
}, [ensureOffsetBoundary, offsetX, translateX]);
|
|
74
|
+
const interruptAnimation = (0, _react.useCallback)(() => {
|
|
75
|
+
if (!isAnimatingRef.current) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
47
78
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
79
|
+
translateX.stopAnimation(lastValue => {
|
|
80
|
+
isAnimatingRef.current = false;
|
|
81
|
+
const prevOffset = currentOffsetRef.current;
|
|
82
|
+
const totalOffset = prevOffset + lastValue;
|
|
83
|
+
notifyOffsetHasChanged(totalOffset);
|
|
84
|
+
notifyAnimationState('interrupted');
|
|
85
|
+
requireNewOffset(totalOffset);
|
|
53
86
|
});
|
|
54
|
-
|
|
55
|
-
controlledTx.removeListener(subscriptionId);
|
|
56
|
-
};
|
|
57
|
-
}, [numberOfData, itemWidth]);
|
|
87
|
+
}, [requireNewOffset, translateX]);
|
|
58
88
|
const finalizeAnimation = (0, _react.useCallback)(() => {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
const resetBoundary = () => {
|
|
69
|
-
controlledTx.setOffset(toValueRef.current);
|
|
70
|
-
controlledTx.flattenOffset(); // FIXME: react-native-web bug maybe?
|
|
71
|
-
// `AnimatedValue.flattenOffset()` does not trigger any event listener.
|
|
72
|
-
// Accessing value directly via `_value` is dangerous but working on web (`useNativeDriver` always false).
|
|
73
|
-
// So setting same value with `value.setValue(value._value)` will trigger event listener.
|
|
74
|
-
|
|
75
|
-
if (_reactNative.Platform.OS === 'web') {
|
|
76
|
-
// @ts-ignore
|
|
77
|
-
controlledTx.setValue(controlledTx._value);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
offsetTx.setValue(0);
|
|
81
|
-
toValueRef.current = 0;
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
stopUnfinishedSnapAnimation();
|
|
85
|
-
resetBoundary();
|
|
86
|
-
}, [controlledTx]);
|
|
89
|
+
notifyAnimationState('finished');
|
|
90
|
+
isAnimatingRef.current = false;
|
|
91
|
+
const prevOffset = currentOffsetRef.current;
|
|
92
|
+
const toValue = toValueRef.current;
|
|
93
|
+
const totalOffset = prevOffset + toValue;
|
|
94
|
+
requireNewOffset(totalOffset);
|
|
95
|
+
}, [notifyAnimationState, requireNewOffset]);
|
|
87
96
|
const startPagingAnimation = (0, _react.useCallback)((type, config) => {
|
|
97
|
+
if (isAnimatingRef.current) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
88
101
|
const configWithDefaults = {
|
|
89
102
|
animated: true,
|
|
90
103
|
...config
|
|
91
104
|
};
|
|
92
105
|
const currentIndex = getCurrentIndex();
|
|
93
106
|
const getValueByDirectionOnAllAdjacentItemsVisible = directionToValue(itemWidth);
|
|
107
|
+
const compensateToValue = toValueCompensator(itemWidth);
|
|
94
108
|
|
|
95
109
|
const getValueByDirectionalPagingOnLoopDisabled = _config => {
|
|
96
110
|
const {
|
|
@@ -130,14 +144,16 @@ function usePagingAnimation(params) {
|
|
|
130
144
|
return distance * direction;
|
|
131
145
|
};
|
|
132
146
|
|
|
133
|
-
const
|
|
147
|
+
const wantedToValue = type === 'directional' // @ts-ignore
|
|
134
148
|
? getValueByDirectionalPaging(configWithDefaults) // @ts-ignore
|
|
135
149
|
: getValueByIndexPaging(configWithDefaults);
|
|
150
|
+
const toValue = compensateToValue(wantedToValue, currentOffsetRef.current);
|
|
136
151
|
toValueRef.current = toValue;
|
|
152
|
+
isAnimatingRef.current = true;
|
|
153
|
+
notifyOffsetHasChanged(currentOffsetRef.current + toValue);
|
|
137
154
|
|
|
138
155
|
if (configWithDefaults.animated) {
|
|
139
|
-
const animation = createScrollAnimation(
|
|
140
|
-
animationRef.current = animation;
|
|
156
|
+
const animation = createScrollAnimation(translateX, toValue);
|
|
141
157
|
animation.start(_ref2 => {
|
|
142
158
|
let {
|
|
143
159
|
finished
|
|
@@ -147,13 +163,13 @@ function usePagingAnimation(params) {
|
|
|
147
163
|
finalizeAnimation();
|
|
148
164
|
}
|
|
149
165
|
});
|
|
166
|
+
notifyAnimationState('started');
|
|
150
167
|
} else {
|
|
151
168
|
finalizeAnimation();
|
|
152
169
|
}
|
|
153
|
-
}, [createScrollAnimation, getCurrentIndex, finalizeAnimation, itemWidth, lastIndex, loop]);
|
|
170
|
+
}, [createScrollAnimation, getCurrentIndex, finalizeAnimation, itemWidth, lastIndex, loop, notifyAnimationState, notifyOffsetHasChanged]);
|
|
154
171
|
return {
|
|
155
|
-
|
|
156
|
-
finalizeAnimation,
|
|
172
|
+
interruptAnimation,
|
|
157
173
|
startPagingAnimation
|
|
158
174
|
};
|
|
159
175
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["directionToValue","itemWidth","direction","usePagingAnimation","params","controlledTx","createScrollAnimation","getCurrentIndex","lastIndex","loop","numberOfData","offsetTx","animationRef","useRef","toValueRef","globalInterpolation","useMemo","Animated","add","useEffect","subscriptionId","addListener","value","currentTx","maxWidth","Math","abs","round","setValue","removeListener","finalizeAnimation","useCallback","stopUnfinishedSnapAnimation","current","stop","resetBoundary","setOffset","flattenOffset","Platform","OS","_value","startPagingAnimation","type","config","configWithDefaults","animated","currentIndex","getValueByDirectionOnAllAdjacentItemsVisible","getValueByDirectionalPagingOnLoopDisabled","_config","isOriginatedFromGesture","getValueByDirectionalPaging","_configWithDefaults","getValueByIndexPaging","index","distance","toValue","animation","start","finished"],"sources":["usePagingAnimation.ts"],"sourcesContent":["import { useCallback, useEffect, useMemo, useRef } from 'react';\nimport { Animated, Platform } from 'react-native';\nimport type {\n CreateScrollAnimation,\n DirectionalPagingAnimationConfig,\n GetCurrentIndex,\n IndexPagingAnimationConfig,\n PagingAnimationConfig,\n PagingAnimationType,\n PagingDirection,\n StartPagingAnimation,\n} from '../types';\n\nexport interface PagingAnimationParameters {\n controlledTx: Animated.Value;\n createScrollAnimation: CreateScrollAnimation;\n getCurrentIndex: GetCurrentIndex;\n itemWidth: number;\n lastIndex: number;\n loop: boolean;\n numberOfData: number;\n offsetTx: Animated.Value;\n}\n\nexport interface UsePagingAnimation {\n finalizeAnimation: () => void;\n globalInterpolation: Animated.AnimatedInterpolation;\n startPagingAnimation: StartPagingAnimation;\n}\n\nfunction directionToValue(itemWidth: number) {\n return function (direction: PagingDirection): number {\n switch (direction) {\n case 'next':\n return -itemWidth;\n case 'prev':\n return itemWidth;\n case 'stay':\n return 0;\n }\n };\n}\n\nexport default function usePagingAnimation(params: PagingAnimationParameters): UsePagingAnimation {\n const {\n controlledTx,\n createScrollAnimation,\n getCurrentIndex,\n itemWidth,\n lastIndex,\n loop,\n numberOfData,\n offsetTx,\n } = params;\n\n const animationRef = useRef<Animated.CompositeAnimation | null>(null);\n const toValueRef = useRef<number>(0);\n\n const globalInterpolation = useMemo(\n () => Animated.add(controlledTx, offsetTx),\n [controlledTx, offsetTx],\n );\n\n useEffect(() => {\n const subscriptionId = controlledTx.addListener((value) => {\n const currentTx = value.value;\n\n // Prevent infinite loop\n if (currentTx !== 0) {\n const maxWidth = numberOfData * itemWidth;\n\n if (Math.abs(Math.round(currentTx)) === Math.round(maxWidth)) {\n // reset position\n controlledTx.setValue(0);\n }\n }\n });\n\n return () => {\n controlledTx.removeListener(subscriptionId);\n };\n }, [numberOfData, itemWidth]);\n\n const finalizeAnimation = useCallback(() => {\n const stopUnfinishedSnapAnimation = () => {\n if (animationRef.current) {\n animationRef.current?.stop();\n animationRef.current = null;\n }\n };\n\n const resetBoundary = () => {\n controlledTx.setOffset(toValueRef.current);\n controlledTx.flattenOffset();\n\n // FIXME: react-native-web bug maybe?\n // `AnimatedValue.flattenOffset()` does not trigger any event listener.\n // Accessing value directly via `_value` is dangerous but working on web (`useNativeDriver` always false).\n // So setting same value with `value.setValue(value._value)` will trigger event listener.\n if (Platform.OS === 'web') {\n // @ts-ignore\n controlledTx.setValue(controlledTx._value);\n }\n\n offsetTx.setValue(0);\n toValueRef.current = 0;\n };\n\n stopUnfinishedSnapAnimation();\n\n resetBoundary();\n }, [controlledTx]);\n\n const startPagingAnimation = useCallback((type: PagingAnimationType, config: PagingAnimationConfig) => {\n const configWithDefaults: PagingAnimationConfig = {\n animated: true,\n ...config,\n };\n\n const currentIndex = getCurrentIndex();\n\n const getValueByDirectionOnAllAdjacentItemsVisible = directionToValue(itemWidth);\n\n const getValueByDirectionalPagingOnLoopDisabled = (_config: DirectionalPagingAnimationConfig): number => {\n const { direction, isOriginatedFromGesture } = _config;\n\n if (currentIndex === 0 && direction === 'prev') {\n return isOriginatedFromGesture\n ? getValueByDirectionOnAllAdjacentItemsVisible('stay')\n : -lastIndex * itemWidth; // last position\n } else if (currentIndex === lastIndex && direction === 'next') {\n return isOriginatedFromGesture\n ? getValueByDirectionOnAllAdjacentItemsVisible('stay')\n : lastIndex * itemWidth; // first position\n }\n return getValueByDirectionOnAllAdjacentItemsVisible(direction);\n };\n\n const getValueByDirectionalPaging = (_config: DirectionalPagingAnimationConfig): number => {\n const _configWithDefaults: DirectionalPagingAnimationConfig = {\n isOriginatedFromGesture: false,\n ..._config,\n };\n\n return loop\n ? getValueByDirectionOnAllAdjacentItemsVisible(_configWithDefaults.direction)\n : getValueByDirectionalPagingOnLoopDisabled(_configWithDefaults);\n };\n\n const getValueByIndexPaging = ({ index }: IndexPagingAnimationConfig): number => {\n if (index < 0 || index > lastIndex || index === currentIndex) {\n // no animation if index is invalid or equals to current index\n return 0;\n }\n\n const distance = Math.abs(currentIndex - index) * itemWidth;\n const direction = index > currentIndex ? -1 : 1;\n\n return distance * direction;\n };\n\n const toValue = type === 'directional'\n // @ts-ignore\n ? getValueByDirectionalPaging(configWithDefaults)\n // @ts-ignore\n : getValueByIndexPaging(configWithDefaults);\n\n toValueRef.current = toValue;\n\n if (configWithDefaults.animated) {\n const animation = createScrollAnimation(offsetTx, toValue);\n animationRef.current = animation;\n animation.start(({ finished }) => {\n if (finished) {\n finalizeAnimation();\n }\n });\n } else {\n finalizeAnimation();\n }\n }, [\n createScrollAnimation,\n getCurrentIndex,\n finalizeAnimation,\n itemWidth,\n lastIndex,\n loop,\n ]);\n\n return {\n globalInterpolation,\n finalizeAnimation,\n startPagingAnimation,\n };\n};\n"],"mappings":";;;;;;;AAAA;;AACA;;AA6BA,SAASA,gBAAT,CAA0BC,SAA1B,EAA6C;EACzC,OAAO,UAAUC,SAAV,EAA8C;IACjD,QAAQA,SAAR;MACI,KAAK,MAAL;QACI,OAAO,CAACD,SAAR;;MACJ,KAAK,MAAL;QACI,OAAOA,SAAP;;MACJ,KAAK,MAAL;QACI,OAAO,CAAP;IANR;EAQH,CATD;AAUH;;AAEc,SAASE,kBAAT,CAA4BC,MAA5B,EAAmF;EAC9F,MAAM;IACFC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D,EAKCO,SALD,EAMCC,IAND,CAnE0B,CAA7B;EA4EA,OAAO;IACHM,mBADG;IAEHe,iBAFG;IAGHW;EAHG,CAAP;AAKH;;AAAA"}
|
|
1
|
+
{"version":3,"names":["directionToValue","itemWidth","direction","toValueCompensator","toValue","currentOffset","remainder","Math","abs","halfOfItemWidth","compensateVector","usePagingAnimation","params","createScrollAnimation","indexController","loop","numberOfData","offsetX","translateX","getCurrentIndex","lastIndex","notifyAnimationState","notifyOffsetHasChanged","toValueRef","useRef","currentOffsetRef","isAnimatingRef","maxWidth","ensureOffsetBoundary","useCallback","offset","isCloseToEnd","signOfOffset","requireNewOffset","newOffset","nextOffset","current","setValue","interruptAnimation","stopAnimation","lastValue","prevOffset","totalOffset","finalizeAnimation","startPagingAnimation","type","config","configWithDefaults","animated","currentIndex","getValueByDirectionOnAllAdjacentItemsVisible","compensateToValue","getValueByDirectionalPagingOnLoopDisabled","_config","isOriginatedFromGesture","getValueByDirectionalPaging","_configWithDefaults","getValueByIndexPaging","index","distance","wantedToValue","animation","start","finished"],"sources":["usePagingAnimation.ts"],"sourcesContent":["import { useCallback, useRef } from 'react';\nimport { Animated } from 'react-native';\nimport type {\n CreateScrollAnimation,\n DirectionalPagingAnimationConfig,\n IndexController,\n IndexPagingAnimationConfig,\n PagingAnimationConfig,\n PagingAnimationType,\n PagingDirection,\n StartPagingAnimation,\n} from '../types';\n\nexport interface PagingAnimationParameters {\n createScrollAnimation: CreateScrollAnimation;\n itemWidth: number;\n indexController: IndexController;\n loop: boolean;\n numberOfData: number;\n offsetX: Animated.Value;\n translateX: Animated.Value;\n}\n\nexport interface UsePagingAnimation {\n interruptAnimation: () => void;\n startPagingAnimation: StartPagingAnimation;\n}\n\nfunction directionToValue(itemWidth: number) {\n return function (direction: PagingDirection): number {\n switch (direction) {\n case 'next':\n return -itemWidth;\n case 'prev':\n return itemWidth;\n case 'stay':\n return 0;\n }\n };\n}\n\nfunction toValueCompensator(itemWidth: number) {\n return function (toValue: number, currentOffset: number): number {\n const remainder = Math.abs(currentOffset % itemWidth);\n\n const halfOfItemWidth = Math.abs(itemWidth / 2);\n const compensateVector = remainder > halfOfItemWidth\n ? remainder - itemWidth\n : remainder;\n\n const direction = currentOffset > 0 ? -1 : 1;\n\n return toValue + (direction * compensateVector);\n };\n}\n\nexport default function usePagingAnimation(params: PagingAnimationParameters): UsePagingAnimation {\n const {\n createScrollAnimation,\n itemWidth,\n indexController,\n loop,\n numberOfData,\n offsetX,\n translateX,\n } = params;\n\n const {\n getCurrentIndex,\n lastIndex,\n notifyAnimationState,\n notifyOffsetHasChanged,\n } = indexController;\n\n const toValueRef = useRef<number>(0);\n const currentOffsetRef = useRef<number>(0);\n\n const isAnimatingRef = useRef<boolean>(false);\n\n const maxWidth = Math.abs(numberOfData * itemWidth);\n\n const ensureOffsetBoundary: (offset: number) => number = useCallback((offset: number) => {\n if (loop) {\n const isCloseToEnd = Math.abs(offset) >= (maxWidth - itemWidth);\n if (isCloseToEnd) {\n const signOfOffset = offset > 0 ? 1 : -1;\n return offset + (-signOfOffset * maxWidth);\n }\n }\n\n return offset % maxWidth;\n }, [itemWidth, loop, maxWidth]);\n\n const requireNewOffset = useCallback((newOffset: number) => {\n const nextOffset = ensureOffsetBoundary(newOffset);\n\n currentOffsetRef.current = nextOffset;\n offsetX.setValue(nextOffset);\n\n toValueRef.current = 0;\n translateX.setValue(0);\n }, [\n ensureOffsetBoundary,\n offsetX,\n translateX,\n ]);\n\n const interruptAnimation = useCallback(() => {\n if (!isAnimatingRef.current) {\n return;\n }\n\n translateX.stopAnimation(lastValue => {\n isAnimatingRef.current = false;\n\n const prevOffset = currentOffsetRef.current;\n const totalOffset = prevOffset + lastValue;\n\n notifyOffsetHasChanged(totalOffset);\n notifyAnimationState('interrupted');\n\n requireNewOffset(totalOffset);\n });\n }, [requireNewOffset, translateX]);\n\n const finalizeAnimation = useCallback(() => {\n notifyAnimationState('finished');\n\n isAnimatingRef.current = false;\n\n const prevOffset = currentOffsetRef.current;\n const toValue = toValueRef.current;\n const totalOffset = prevOffset + toValue;\n\n requireNewOffset(totalOffset);\n }, [\n notifyAnimationState,\n requireNewOffset,\n ]);\n\n const startPagingAnimation = useCallback((type: PagingAnimationType, config: PagingAnimationConfig) => {\n if (isAnimatingRef.current) {\n return;\n }\n\n const configWithDefaults: PagingAnimationConfig = {\n animated: true,\n ...config,\n };\n\n const currentIndex = getCurrentIndex();\n\n const getValueByDirectionOnAllAdjacentItemsVisible = directionToValue(itemWidth);\n const compensateToValue = toValueCompensator(itemWidth);\n\n const getValueByDirectionalPagingOnLoopDisabled = (_config: DirectionalPagingAnimationConfig): number => {\n const { direction, isOriginatedFromGesture } = _config;\n\n if (currentIndex === 0 && direction === 'prev') {\n return isOriginatedFromGesture\n ? getValueByDirectionOnAllAdjacentItemsVisible('stay')\n : -lastIndex * itemWidth; // last position\n } else if (currentIndex === lastIndex && direction === 'next') {\n return isOriginatedFromGesture\n ? getValueByDirectionOnAllAdjacentItemsVisible('stay')\n : lastIndex * itemWidth; // first position\n }\n return getValueByDirectionOnAllAdjacentItemsVisible(direction);\n };\n\n const getValueByDirectionalPaging = (_config: DirectionalPagingAnimationConfig): number => {\n const _configWithDefaults: DirectionalPagingAnimationConfig = {\n isOriginatedFromGesture: false,\n ..._config,\n };\n\n return loop\n ? getValueByDirectionOnAllAdjacentItemsVisible(_configWithDefaults.direction)\n : getValueByDirectionalPagingOnLoopDisabled(_configWithDefaults);\n };\n\n const getValueByIndexPaging = ({ index }: IndexPagingAnimationConfig): number => {\n if (index < 0 || index > lastIndex || index === currentIndex) {\n // no animation if index is invalid or equals to current index\n return 0;\n }\n\n const distance = Math.abs(currentIndex - index) * itemWidth;\n const direction = index > currentIndex ? -1 : 1;\n\n return distance * direction;\n };\n\n const wantedToValue = type === 'directional'\n // @ts-ignore\n ? getValueByDirectionalPaging(configWithDefaults)\n // @ts-ignore\n : getValueByIndexPaging(configWithDefaults);\n\n const toValue = compensateToValue(wantedToValue, currentOffsetRef.current);\n\n toValueRef.current = toValue;\n isAnimatingRef.current = true;\n\n notifyOffsetHasChanged(currentOffsetRef.current + toValue);\n\n if (configWithDefaults.animated) {\n const animation = createScrollAnimation(translateX, toValue);\n\n animation.start(({ finished }) => {\n if (finished) {\n finalizeAnimation();\n }\n });\n\n notifyAnimationState('started');\n } else {\n finalizeAnimation();\n }\n }, [\n createScrollAnimation,\n getCurrentIndex,\n finalizeAnimation,\n itemWidth,\n lastIndex,\n loop,\n notifyAnimationState,\n notifyOffsetHasChanged,\n ]);\n\n return {\n interruptAnimation,\n startPagingAnimation,\n };\n};\n"],"mappings":";;;;;;;AAAA;;AA4BA,SAASA,gBAAT,CAA0BC,SAA1B,EAA6C;EACzC,OAAO,UAAUC,SAAV,EAA8C;IACjD,QAAQA,SAAR;MACI,KAAK,MAAL;QACI,OAAO,CAACD,SAAR;;MACJ,KAAK,MAAL;QACI,OAAOA,SAAP;;MACJ,KAAK,MAAL;QACI,OAAO,CAAP;IANR;EAQH,CATD;AAUH;;AAED,SAASE,kBAAT,CAA4BF,SAA5B,EAA+C;EAC3C,OAAO,UAAUG,OAAV,EAA2BC,aAA3B,EAA0D;IAC7D,MAAMC,SAAS,GAAGC,IAAI,CAACC,GAAL,CAASH,aAAa,GAAGJ,SAAzB,CAAlB;IAEA,MAAMQ,eAAe,GAAGF,IAAI,CAACC,GAAL,CAASP,SAAS,GAAG,CAArB,CAAxB;IACA,MAAMS,gBAAgB,GAAGJ,SAAS,GAAGG,eAAZ,GACnBH,SAAS,GAAGL,SADO,GAEnBK,SAFN;IAIA,MAAMJ,SAAS,GAAGG,aAAa,GAAG,CAAhB,GAAoB,CAAC,CAArB,GAAyB,CAA3C;IAEA,OAAOD,OAAO,GAAIF,SAAS,GAAGQ,gBAA9B;EACH,CAXD;AAYH;;AAEc,SAASC,kBAAT,CAA4BC,MAA5B,EAAmF;EAC9F,MAAM;IACFC,qBADE;IAEFZ,SAFE;IAGFa,eAHE;IAIFC,IAJE;IAKFC,YALE;IAMFC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EAIhBU,qBAAqB,CAACV,kBAAD,CAJ3B;IAMA,MAAM3C,OAAO,GAAG+C,iBAAiB,CAACS,aAAD,EAAgBnC,gBAAgB,CAACW,OAAjC,CAAjC;IAEAb,UAAU,CAACa,OAAX,GAAqBhC,OAArB;IACAsB,cAAc,CAACU,OAAf,GAAyB,IAAzB;IAEAd,sBAAsB,CAACG,gBAAgB,CAACW,OAAjB,GAA2BhC,OAA5B,CAAtB;;IAEA,IAAI2C,kBAAkB,CAACC,QAAvB,EAAiC;MAC7B,MAAMa,SAAS,GAAGhD,qBAAqB,CAACK,UAAD,EAAad,OAAb,CAAvC;MAEAyD,SAAS,CAACC,KAAV,CAAgB,SAAkB;QAAA,IAAjB;UAAEC;QAAF,CAAiB;;QAC9B,IAAIA,QAAJ,EAAc;UACVpB,iBAAiB;QACpB;MACJ,CAJD;MAMAtB,oBAAoB,CAAC,SAAD,CAApB;IACH,CAVD,MAUO;MACHsB,iBAAiB;IACpB;EACJ,CA/E4B,EA+E1B,CACC9B,qBADD,EAECM,eAFD,EAGCwB,iBAHD,EAIC1C,SAJD,EAKCmB,SALD,EAMCL,IAND,EAOCM,oBAPD,EAQCC,sBARD,CA/E0B,CAA7B;EA0FA,OAAO;IACHgB,kBADG;IAEHM;EAFG,CAAP;AAIH;;AAAA"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _utils = require("@fountain-ui/utils");
|
|
9
|
+
|
|
10
|
+
var _default = (0, _utils.logger)('Carousel', {
|
|
11
|
+
enabled: __DEV__,
|
|
12
|
+
format: 'diff'
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
exports.default = _default;
|
|
16
|
+
//# sourceMappingURL=tick.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["logger","enabled","__DEV__","format"],"sources":["tick.ts"],"sourcesContent":["import { logger } from '@fountain-ui/utils';\n\nexport default logger('Carousel', {\n enabled: __DEV__,\n format: 'diff',\n});\n"],"mappings":";;;;;;;AAAA;;eAEe,IAAAA,aAAA,EAAO,UAAP,EAAmB;EAC9BC,OAAO,EAAEC,OADqB;EAE9BC,MAAM,EAAE;AAFsB,CAAnB,C"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["directions"],"sources":["types.ts"],"sourcesContent":["import type { ReactElement } from 'react';\nimport type { Animated, ViewProps } from 'react-native';\n\nconst directions = ['next', 'prev', 'stay'] as const;\n\nexport type PagingDirection = (typeof directions)[number];\n\nexport type ItemHeight = number | 'auto';\n\nexport interface RenderItem<T> {\n (info: { item: T, index: number, interpolation: Animated.AnimatedInterpolation }): ReactElement | null;\n}\n\nexport interface CreateScrollAnimation {\n (aValue: Animated.AnimatedValue, toValue: number): Animated.CompositeAnimation;\n}\n\nexport interface CreateItemStyle {\n (itemInterpolation: Animated.AnimatedInterpolation, itemWidth: number): Animated.AnimatedProps<ViewProps['style']>;\n}\n\nexport interface GetCurrentIndex {\n (): number;\n}\n\nexport interface IndexController {\n
|
|
1
|
+
{"version":3,"names":["directions","animationStates"],"sources":["types.ts"],"sourcesContent":["import type { ReactElement } from 'react';\nimport type { Animated, ViewProps } from 'react-native';\n\nconst directions = ['next', 'prev', 'stay'] as const;\n\nexport type PagingDirection = (typeof directions)[number];\n\nconst animationStates = ['started', 'finished', 'interrupted'] as const;\n\nexport type AnimationState = (typeof animationStates)[number];\n\nexport type ItemHeight = number | 'auto';\n\nexport interface RenderItem<T> {\n (info: { item: T, index: number, interpolation: Animated.AnimatedInterpolation }): ReactElement | null;\n}\n\nexport interface CreateScrollAnimation {\n (aValue: Animated.AnimatedValue, toValue: number): Animated.CompositeAnimation;\n}\n\nexport interface CreateItemStyle {\n (itemInterpolation: Animated.AnimatedInterpolation, itemWidth: number): Animated.AnimatedProps<ViewProps['style']>;\n}\n\nexport interface GetCurrentIndex {\n (): number;\n}\n\nexport interface IndexController {\n getCurrentIndex: GetCurrentIndex;\n lastIndex: number;\n notifyAnimationState: (state: AnimationState) => void;\n notifyOffsetHasChanged: (offset: number) => void;\n}\n\nexport type PagingAnimationType = 'directional' | 'index';\n\nexport interface BasePagingAnimationConfig {\n animated?: boolean;\n}\n\nexport interface DirectionalPagingAnimationConfig extends BasePagingAnimationConfig {\n direction: PagingDirection;\n isOriginatedFromGesture?: boolean;\n}\n\nexport interface IndexPagingAnimationConfig extends BasePagingAnimationConfig {\n index: number;\n}\n\nexport type PagingAnimationConfig = DirectionalPagingAnimationConfig | IndexPagingAnimationConfig;\n\nexport interface StartPagingAnimation {\n (type: PagingAnimationType, config: PagingAnimationConfig): void;\n}\n\nexport type VisibleIndexRanges = Array<[number, number]>;\n\nexport interface StoreSubscription {\n (): void;\n}\n\nexport interface ItemVisibilityStore {\n dispatch: (ranges: VisibleIndexRanges) => void;\n subscribe: (listener: (ranges: VisibleIndexRanges) => void) => StoreSubscription;\n removeAllListeners: () => void;\n}\n\nexport interface AutoplayController {\n pause: () => void;\n resume: () => void;\n}\n\nexport interface ScrollToOption {\n index: number;\n animated?: boolean;\n}\n\nexport interface CarouselInstance {\n /**\n * Get current visible item index.\n */\n getCurrentIndex: GetCurrentIndex;\n\n /**\n * Scroll to next visible item.\n */\n next: () => void;\n\n /**\n * Scroll to previous visible item.\n */\n prev: () => void;\n\n /**\n * Scroll to desired indexed item.\n * Invalid index is ignored.\n */\n scrollTo: (option: ScrollToOption) => void;\n}\n"],"mappings":";;;;;AAGA,MAAMA,UAAU,GAAG,CAAC,MAAD,EAAS,MAAT,EAAiB,MAAjB,CAAnB;AAIA,MAAMC,eAAe,GAAG,CAAC,SAAD,EAAY,UAAZ,EAAwB,aAAxB,CAAxB"}
|
|
@@ -9,12 +9,12 @@ var _react = _interopRequireWildcard(require("react"));
|
|
|
9
9
|
|
|
10
10
|
var _reactNative = require("react-native");
|
|
11
11
|
|
|
12
|
-
var _reactNativeReanimated = require("react-native-reanimated");
|
|
13
|
-
|
|
14
12
|
var _core = require("@fountain-ui/core");
|
|
15
13
|
|
|
16
14
|
var _PageStateContext = _interopRequireDefault(require("./PageStateContext"));
|
|
17
15
|
|
|
16
|
+
var _InternalContext = _interopRequireDefault(require("./InternalContext"));
|
|
17
|
+
|
|
18
18
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
19
19
|
|
|
20
20
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
@@ -34,66 +34,72 @@ const styles = _core.StyleSheet.create({
|
|
|
34
34
|
function Page(props) {
|
|
35
35
|
const {
|
|
36
36
|
index,
|
|
37
|
+
initialPage,
|
|
37
38
|
children,
|
|
38
39
|
loading,
|
|
39
|
-
offscreenPageRerenderLimit
|
|
40
|
-
sharedIndex
|
|
40
|
+
offscreenPageRerenderLimit
|
|
41
41
|
} = props;
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
42
|
+
const {
|
|
43
|
+
pageStore
|
|
44
|
+
} = (0, _react.useContext)(_InternalContext.default);
|
|
45
|
+
const computePageDescription = (0, _react.useCallback)(page => {
|
|
46
|
+
const activeIndex = page;
|
|
45
47
|
const isActive = index === activeIndex;
|
|
48
|
+
const shouldRerender = index >= activeIndex - offscreenPageRerenderLimit && index <= activeIndex + offscreenPageRerenderLimit;
|
|
49
|
+
const becomeNeighbor = shouldRerender && !isActive;
|
|
50
|
+
return {
|
|
51
|
+
isActive,
|
|
52
|
+
becomeNeighbor
|
|
53
|
+
};
|
|
54
|
+
}, [index]);
|
|
55
|
+
const [initialState] = (0, _react.useState)(() => {
|
|
56
|
+
const {
|
|
57
|
+
isActive,
|
|
58
|
+
becomeNeighbor
|
|
59
|
+
} = computePageDescription(initialPage);
|
|
46
60
|
|
|
47
61
|
if (loading === 'eager') {
|
|
48
62
|
return {
|
|
49
|
-
isActive,
|
|
50
|
-
|
|
63
|
+
active: isActive,
|
|
64
|
+
loaded: true
|
|
51
65
|
};
|
|
52
66
|
}
|
|
53
67
|
|
|
54
|
-
const isLoaded = index >= activeIndex - offscreenPageRerenderLimit && index <= activeIndex + offscreenPageRerenderLimit;
|
|
55
68
|
return {
|
|
56
|
-
isActive,
|
|
57
|
-
|
|
69
|
+
active: isActive,
|
|
70
|
+
loaded: isActive || becomeNeighbor
|
|
58
71
|
};
|
|
59
|
-
};
|
|
72
|
+
}); // `Bailing out of a state update` is not working as expected.
|
|
60
73
|
|
|
61
|
-
const
|
|
62
|
-
const
|
|
74
|
+
const pageStateRef = (0, _react.useRef)(initialState);
|
|
75
|
+
const [, forceRender] = (0, _react.useReducer)(s => s + 1, 0);
|
|
76
|
+
const {
|
|
77
|
+
active,
|
|
78
|
+
loaded
|
|
79
|
+
} = pageStateRef.current;
|
|
80
|
+
const content = loaded ? children : null;
|
|
81
|
+
(0, _react.useEffect)(() => {
|
|
82
|
+
return pageStore.subscribe(newPage => {
|
|
83
|
+
const {
|
|
84
|
+
isActive,
|
|
85
|
+
becomeNeighbor
|
|
86
|
+
} = computePageDescription(newPage);
|
|
87
|
+
const currentState = pageStateRef.current;
|
|
88
|
+
const newState = {
|
|
89
|
+
active: isActive,
|
|
90
|
+
loaded: isActive || becomeNeighbor ? true : currentState.loaded
|
|
91
|
+
};
|
|
63
92
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
})
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
const onBecomeNeighbor = () => {
|
|
72
|
-
setPageState(prevState => ({ ...prevState,
|
|
73
|
-
isLoaded: true
|
|
74
|
-
}));
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
(0, _reactNativeReanimated.useAnimatedReaction)(() => {
|
|
78
|
-
const activeIndex = sharedIndex.value;
|
|
79
|
-
const isActive = index === activeIndex;
|
|
80
|
-
const shouldRerender = index >= activeIndex - offscreenPageRerenderLimit && index <= activeIndex + offscreenPageRerenderLimit;
|
|
81
|
-
const becomeNeighbor = shouldRerender && !isActive;
|
|
82
|
-
return {
|
|
83
|
-
isActive,
|
|
84
|
-
becomeNeighbor
|
|
85
|
-
};
|
|
86
|
-
}, (nextState, prevState) => {
|
|
87
|
-
if (nextState.isActive !== (prevState === null || prevState === void 0 ? void 0 : prevState.isActive)) {
|
|
88
|
-
(0, _reactNativeReanimated.runOnJS)(onActiveStateChange)(nextState.isActive);
|
|
89
|
-
} else if (nextState.becomeNeighbor) {
|
|
90
|
-
(0, _reactNativeReanimated.runOnJS)(onBecomeNeighbor)();
|
|
91
|
-
}
|
|
92
|
-
}, [index]);
|
|
93
|
+
if (currentState.active !== newState.active || currentState.loaded !== newState.loaded) {
|
|
94
|
+
pageStateRef.current = newState;
|
|
95
|
+
forceRender();
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
}, [pageStore, computePageDescription]);
|
|
93
99
|
const contextValue = (0, _react.useMemo)(() => ({
|
|
94
|
-
isActive:
|
|
95
|
-
}), [
|
|
96
|
-
const style = _reactNative.Platform.OS === 'web' ?
|
|
100
|
+
isActive: active
|
|
101
|
+
}), [active]);
|
|
102
|
+
const style = _reactNative.Platform.OS === 'web' ? active ? _core.StyleSheet.absoluteFill : styles.none : styles.fill;
|
|
97
103
|
return /*#__PURE__*/_react.default.createElement(_reactNative.View, {
|
|
98
104
|
collapsable: false,
|
|
99
105
|
style: style
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["styles","StyleSheet","create","fill","width","height","none","display","Page","props","index","children","loading","offscreenPageRerenderLimit","
|
|
1
|
+
{"version":3,"names":["styles","StyleSheet","create","fill","width","height","none","display","Page","props","index","initialPage","children","loading","offscreenPageRerenderLimit","pageStore","useContext","InternalContext","computePageDescription","useCallback","page","activeIndex","isActive","shouldRerender","becomeNeighbor","initialState","useState","active","loaded","pageStateRef","useRef","forceRender","useReducer","s","current","content","useEffect","subscribe","newPage","currentState","newState","contextValue","useMemo","style","Platform","OS","absoluteFill","memo","prevProps","nextProps","rerenderKey"],"sources":["ChildrenMemoizedPage.tsx"],"sourcesContent":["import React, { memo, useCallback, useContext, useEffect, useMemo, useReducer, useRef, useState } from 'react';\nimport { Platform, View } from 'react-native';\nimport { StyleSheet } from '@fountain-ui/core';\nimport type { PageProps } from './types';\nimport PageStateContext from './PageStateContext';\nimport InternalContext from './InternalContext';\n\nconst styles = StyleSheet.create({\n fill: { width: '100%', height: '100%' },\n none: { display: 'none' },\n});\n\ninterface InternalPageDescription {\n isActive: boolean;\n becomeNeighbor: boolean;\n}\n\ninterface InternalPageState {\n active: boolean;\n loaded: boolean;\n}\n\nfunction Page(props: PageProps) {\n const {\n index,\n initialPage,\n children,\n loading,\n offscreenPageRerenderLimit,\n } = props;\n\n const { pageStore } = useContext(InternalContext);\n\n const computePageDescription: (page: number) => InternalPageDescription = useCallback((page: number) => {\n const activeIndex = page;\n\n const isActive = index === activeIndex;\n\n const shouldRerender = index >= activeIndex - offscreenPageRerenderLimit\n && index <= activeIndex + offscreenPageRerenderLimit;\n\n const becomeNeighbor = shouldRerender && !isActive;\n\n return { isActive, becomeNeighbor };\n }, [index]);\n\n const [initialState] = useState<InternalPageState>(() => {\n const { isActive, becomeNeighbor } = computePageDescription(initialPage);\n\n if (loading === 'eager') {\n return { active: isActive, loaded: true };\n }\n\n return { active: isActive, loaded: isActive || becomeNeighbor };\n });\n\n // `Bailing out of a state update` is not working as expected.\n const pageStateRef = useRef<InternalPageState>(initialState);\n const [, forceRender] = useReducer((s) => s + 1, 0);\n\n const { active, loaded } = pageStateRef.current;\n\n const content = loaded ? children : null;\n\n useEffect(() => {\n return pageStore.subscribe(newPage => {\n const { isActive, becomeNeighbor } = computePageDescription(newPage);\n\n const currentState = pageStateRef.current;\n\n const newState: InternalPageState = {\n active: isActive,\n loaded: isActive || becomeNeighbor ? true : currentState.loaded,\n };\n\n if (\n currentState.active !== newState.active\n || currentState.loaded !== newState.loaded\n ) {\n pageStateRef.current = newState;\n\n forceRender();\n }\n });\n }, [pageStore, computePageDescription]);\n\n const contextValue = useMemo(() => ({\n isActive: active,\n }), [active]);\n\n const style = Platform.OS === 'web'\n ? (active ? StyleSheet.absoluteFill : styles.none)\n : styles.fill;\n\n return (\n <View\n collapsable={false}\n style={style}\n >\n <PageStateContext.Provider value={contextValue}>\n {content}\n </PageStateContext.Provider>\n </View>\n );\n}\n\nexport default memo(Page, (prevProps, nextProps) => {\n if (prevProps.rerenderKey !== nextProps.rerenderKey) {\n return false;\n }\n\n return prevProps.index !== nextProps.index;\n});\n"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;AAEA;;AACA;;;;;;;;AAEA,MAAMA,MAAM,GAAGC,gBAAA,CAAWC,MAAX,CAAkB;EAC7BC,IAAI,EAAE;IAAEC,KAAK,EAAE,MAAT;IAAiBC,MAAM,EAAE;EAAzB,CADuB;EAE7BC,IAAI,EAAE;IAAEC,OAAO,EAAE;EAAX;AAFuB,CAAlB,CAAf;;AAeA,SAASC,IAAT,CAAcC,KAAd,EAAgC;EAC5B,MAAM;IACFC,KADE;IAEFC,WAFE;IAGFC,QAHE;IAIFC,OAJE;IAKFC;EALE,IAMFL,KANJ;EAQA,MAAM;IAAEM;EAAF,IAAgB,IAAAC,iBAAA,EAAWC,wBAAX,CAAtB;EAEA,MAAMC,sBAAiE,GAAG,IAAAC,kBAAA,EAAaC,IAAD,IAAkB;IACpG,MAAMC,WAAW,GAAGD,IAApB;IAEA,MAAME,QAAQ,GAAGZ,KAAK,KAAKW,WAA3B;IAEA,MAAME,cAAc,GAAGb,KAAK,IAAIW,WAAW,GAAGP,0BAAvB,IAChBJ,KAAK,IAAIW,WAAW,GAAGP,0BAD9B;IAGA,MAAMU,cAAc,GAAGD,cAAc,IAAI,CAACD,QAA1C;IAEA,OAAO;MA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,CAACK,OAAb,GAAuBM,QAAvB;QAEAT,WAAW;MACd;IACJ,CAlBM,CAAP;EAmBH,CApBD,EAoBG,CAAChB,SAAD,EAAYG,sBAAZ,CApBH;EAsBA,MAAMuB,YAAY,GAAG,IAAAC,cAAA,EAAQ,OAAO;IAChCpB,QAAQ,EAAEK;EADsB,CAAP,CAAR,EAEjB,CAACA,MAAD,CAFiB,CAArB;EAIA,MAAMgB,KAAK,GAAGC,qBAAA,CAASC,EAAT,KAAgB,KAAhB,GACPlB,MAAM,GAAG1B,gBAAA,CAAW6C,YAAd,GAA6B9C,MAAM,CAACM,IADnC,GAERN,MAAM,CAACG,IAFb;EAIA,oBACI,6BAAC,iBAAD;IACI,WAAW,EAAE,KADjB;IAEI,KAAK,EAAEwC;EAFX,gBAII,6BAAC,yBAAD,CAAkB,QAAlB;IAA2B,KAAK,EAAEF;EAAlC,GACKN,OADL,CAJJ,CADJ;AAUH;;4BAEc,IAAAY,WAAA,EAAKvC,IAAL,EAAW,CAACwC,SAAD,EAAYC,SAAZ,KAA0B;EAChD,IAAID,SAAS,CAACE,WAAV,KAA0BD,SAAS,CAACC,WAAxC,EAAqD;IACjD,OAAO,KAAP;EACH;;EAED,OAAOF,SAAS,CAACtC,KAAV,KAAoBuC,SAAS,CAACvC,KAArC;AACH,CANc,C"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _react = require("react");
|
|
9
|
+
|
|
10
|
+
var _core = require("@fountain-ui/core");
|
|
11
|
+
|
|
12
|
+
const InternalContext = /*#__PURE__*/(0, _react.createContext)({
|
|
13
|
+
pageStore: new _core.MockStore()
|
|
14
|
+
});
|
|
15
|
+
var _default = InternalContext;
|
|
16
|
+
exports.default = _default;
|
|
17
|
+
//# sourceMappingURL=InternalContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["InternalContext","createContext","pageStore","MockStore"],"sources":["InternalContext.ts"],"sourcesContent":["import { createContext } from 'react';\nimport type { MonoStore } from '@fountain-ui/core';\nimport { MockStore } from '@fountain-ui/core';\n\nexport interface InternalContextValue {\n pageStore: MonoStore<number>;\n}\n\nconst InternalContext = createContext<InternalContextValue>({\n pageStore: new MockStore(),\n});\n\nexport default InternalContext;\n"],"mappings":";;;;;;;AAAA;;AAEA;;AAMA,MAAMA,eAAe,gBAAG,IAAAC,oBAAA,EAAoC;EACxDC,SAAS,EAAE,IAAIC,eAAJ;AAD6C,CAApC,CAAxB;eAIeH,e"}
|