@fountain-ui/lab 2.0.0-beta.14 → 2.0.0-beta.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. package/build/commonjs/Carousel/Carousel.js +24 -29
  2. package/build/commonjs/Carousel/Carousel.js.map +1 -1
  3. package/build/commonjs/Carousel/CarouselProps.js.map +1 -1
  4. package/build/commonjs/Carousel/animation/createDefaultScrollAnimation.js +2 -2
  5. package/build/commonjs/Carousel/animation/createDefaultScrollAnimation.js.map +1 -1
  6. package/build/commonjs/Carousel/animation/parallaxItemStyleFactory.js +2 -2
  7. package/build/commonjs/Carousel/animation/parallaxItemStyleFactory.js.map +1 -1
  8. package/build/commonjs/Carousel/components/ItemView.js +4 -4
  9. package/build/commonjs/Carousel/components/ItemView.js.map +1 -1
  10. package/build/commonjs/Carousel/components/ScrollViewGesture.js +6 -6
  11. package/build/commonjs/Carousel/components/ScrollViewGesture.js.map +1 -1
  12. package/build/commonjs/Carousel/{hooks → components}/useItemInterpolation.js +6 -4
  13. package/build/commonjs/Carousel/components/useItemInterpolation.js.map +1 -0
  14. package/build/commonjs/Carousel/hooks/index.js +0 -16
  15. package/build/commonjs/Carousel/hooks/index.js.map +1 -1
  16. package/build/commonjs/Carousel/hooks/useIndexController.js +28 -44
  17. package/build/commonjs/Carousel/hooks/useIndexController.js.map +1 -1
  18. package/build/commonjs/Carousel/hooks/useItemVisibilityStore.js +12 -12
  19. package/build/commonjs/Carousel/hooks/useItemVisibilityStore.js.map +1 -1
  20. package/build/commonjs/Carousel/hooks/usePagingAnimation.js +72 -56
  21. package/build/commonjs/Carousel/hooks/usePagingAnimation.js.map +1 -1
  22. package/build/commonjs/Carousel/tick.js +16 -0
  23. package/build/commonjs/Carousel/tick.js.map +1 -0
  24. package/build/commonjs/Carousel/types.js +1 -0
  25. package/build/commonjs/Carousel/types.js.map +1 -1
  26. package/build/commonjs/ViewPager/ChildrenMemoizedPage.js +44 -35
  27. package/build/commonjs/ViewPager/ChildrenMemoizedPage.js.map +1 -1
  28. package/build/commonjs/ViewPager/ViewPagerNative.js +2 -3
  29. package/build/commonjs/ViewPager/ViewPagerNative.js.map +1 -1
  30. package/build/commonjs/ViewPager/usePageStore.js +5 -0
  31. package/build/commonjs/ViewPager/usePageStore.js.map +1 -1
  32. package/build/commonjs/ViewabilityTrackerView/measureViewability.js +6 -6
  33. package/build/commonjs/ViewabilityTrackerView/measureViewability.js.map +1 -1
  34. package/build/module/Carousel/Carousel.js +23 -30
  35. package/build/module/Carousel/Carousel.js.map +1 -1
  36. package/build/module/Carousel/CarouselProps.js.map +1 -1
  37. package/build/module/Carousel/animation/createDefaultScrollAnimation.js +2 -2
  38. package/build/module/Carousel/animation/createDefaultScrollAnimation.js.map +1 -1
  39. package/build/module/Carousel/animation/parallaxItemStyleFactory.js +2 -2
  40. package/build/module/Carousel/animation/parallaxItemStyleFactory.js.map +1 -1
  41. package/build/module/Carousel/components/ItemView.js +3 -3
  42. package/build/module/Carousel/components/ItemView.js.map +1 -1
  43. package/build/module/Carousel/components/ScrollViewGesture.js +6 -6
  44. package/build/module/Carousel/components/ScrollViewGesture.js.map +1 -1
  45. package/build/module/Carousel/{hooks → components}/useItemInterpolation.js +3 -3
  46. package/build/module/Carousel/components/useItemInterpolation.js.map +1 -0
  47. package/build/module/Carousel/hooks/index.js +0 -2
  48. package/build/module/Carousel/hooks/index.js.map +1 -1
  49. package/build/module/Carousel/hooks/useIndexController.js +29 -40
  50. package/build/module/Carousel/hooks/useIndexController.js.map +1 -1
  51. package/build/module/Carousel/hooks/useItemVisibilityStore.js +10 -11
  52. package/build/module/Carousel/hooks/useItemVisibilityStore.js.map +1 -1
  53. package/build/module/Carousel/hooks/usePagingAnimation.js +73 -56
  54. package/build/module/Carousel/hooks/usePagingAnimation.js.map +1 -1
  55. package/build/module/Carousel/tick.js +6 -0
  56. package/build/module/Carousel/tick.js.map +1 -0
  57. package/build/module/Carousel/types.js +1 -0
  58. package/build/module/Carousel/types.js.map +1 -1
  59. package/build/module/ViewPager/ChildrenMemoizedPage.js +44 -35
  60. package/build/module/ViewPager/ChildrenMemoizedPage.js.map +1 -1
  61. package/build/module/ViewPager/ViewPagerNative.js +2 -3
  62. package/build/module/ViewPager/ViewPagerNative.js.map +1 -1
  63. package/build/module/ViewPager/usePageStore.js +5 -0
  64. package/build/module/ViewPager/usePageStore.js.map +1 -1
  65. package/build/module/ViewabilityTrackerView/measureViewability.js +2 -2
  66. package/build/module/ViewabilityTrackerView/measureViewability.js.map +1 -1
  67. package/build/typescript/Carousel/CarouselProps.d.ts +2 -2
  68. package/build/typescript/Carousel/components/ScrollViewGesture.d.ts +2 -2
  69. package/build/typescript/Carousel/{hooks → components}/useItemInterpolation.d.ts +0 -0
  70. package/build/typescript/Carousel/hooks/index.d.ts +0 -2
  71. package/build/typescript/Carousel/hooks/useIndexController.d.ts +3 -3
  72. package/build/typescript/Carousel/hooks/useItemVisibilityStore.d.ts +3 -3
  73. package/build/typescript/Carousel/hooks/usePagingAnimation.d.ts +5 -7
  74. package/build/typescript/Carousel/tick.d.ts +2 -0
  75. package/build/typescript/Carousel/types.d.ts +10 -2
  76. package/package.json +3 -3
  77. package/src/Carousel/Carousel.tsx +19 -31
  78. package/src/Carousel/CarouselProps.ts +9 -2
  79. package/src/Carousel/animation/createDefaultScrollAnimation.ts +2 -2
  80. package/src/Carousel/animation/parallaxItemStyleFactory.ts +1 -1
  81. package/src/Carousel/components/ItemView.tsx +3 -3
  82. package/src/Carousel/components/ScrollViewGesture.tsx +8 -7
  83. package/src/Carousel/{hooks → components}/useItemInterpolation.ts +3 -3
  84. package/src/Carousel/hooks/index.ts +0 -2
  85. package/src/Carousel/hooks/useIndexController.tsx +36 -47
  86. package/src/Carousel/hooks/useItemVisibilityStore.ts +14 -14
  87. package/src/Carousel/hooks/usePagingAnimation.ts +104 -64
  88. package/src/Carousel/tick.ts +6 -0
  89. package/src/Carousel/types.ts +14 -2
  90. package/src/ViewPager/ChildrenMemoizedPage.tsx +46 -39
  91. package/src/ViewPager/ViewPagerNative.tsx +3 -3
  92. package/src/ViewPager/usePageStore.ts +6 -0
  93. package/src/ViewabilityTrackerView/measureViewability.ts +1 -3
  94. package/build/commonjs/Carousel/hooks/useDimensionChangeReaction.js +0 -23
  95. package/build/commonjs/Carousel/hooks/useDimensionChangeReaction.js.map +0 -1
  96. package/build/commonjs/Carousel/hooks/useItemInterpolation.js.map +0 -1
  97. package/build/module/Carousel/hooks/useDimensionChangeReaction.js +0 -14
  98. package/build/module/Carousel/hooks/useDimensionChangeReaction.js.map +0 -1
  99. package/build/module/Carousel/hooks/useItemInterpolation.js.map +0 -1
  100. package/build/typescript/Carousel/hooks/useDimensionChangeReaction.d.ts +0 -7
  101. package/src/Carousel/hooks/useDimensionChangeReaction.ts +0 -25
@@ -4,8 +4,8 @@ import type { AutoplayController, StartPagingAnimation } from '../types';
4
4
  export interface ScrollViewGestureProps {
5
5
  autoplayController: AutoplayController;
6
6
  children: ReactNode;
7
- finalizeAnimation: () => void;
8
- offsetTx: Animated.Value;
7
+ interruptAnimation: () => void;
8
+ translateX: Animated.Value;
9
9
  scrollEnabled: boolean;
10
10
  startPagingAnimation: StartPagingAnimation;
11
11
  }
@@ -1,7 +1,5 @@
1
1
  export { default as useAutoplayController } from './useAutoplayController';
2
- export { default as useDimensionChangeReaction } from './useDimensionChangeReaction';
3
2
  export { default as useIndexController } from './useIndexController';
4
- export { default as useItemInterpolation } from './useItemInterpolation';
5
3
  export { default as useLoopedData } from './useLoopedData';
6
4
  export { default as usePagingAnimation } from './usePagingAnimation';
7
5
  export { default as useItemVisibilityStore } from './useItemVisibilityStore';
@@ -1,10 +1,10 @@
1
- import { Animated } from 'react-native';
2
1
  import type { IndexController } from '../types';
3
2
  export interface UseIndexControllerParameters {
4
- controlledTx: Animated.AnimatedValue;
5
3
  initialIndex: number;
6
4
  itemWidth: number;
5
+ numberOfData: number;
7
6
  numberOfOriginalData: number;
8
- onIndexChange?: (newIndex: number) => void;
7
+ onIndexChange?: (itemIndex: number) => void;
8
+ onPositionChange?: (position: number) => void;
9
9
  }
10
10
  export default function useIndexController(params: UseIndexControllerParameters): IndexController;
@@ -1,7 +1,7 @@
1
- import type { ItemVisibilityStore } from '../types';
1
+ import type { ItemVisibilityStore, OnPositionChange } from '../types';
2
2
  export interface Parameters {
3
- currentIndex: number;
3
+ initialIndex: number;
4
4
  numberOfData: number;
5
5
  windowSize: number;
6
6
  }
7
- export default function useItemVisibilityStore(params: Parameters): ItemVisibilityStore;
7
+ export default function useItemVisibilityStore(params: Parameters): [ItemVisibilityStore, OnPositionChange];
@@ -1,18 +1,16 @@
1
1
  import { Animated } from 'react-native';
2
- import type { CreateScrollAnimation, GetCurrentIndex, StartPagingAnimation } from '../types';
2
+ import type { CreateScrollAnimation, IndexController, StartPagingAnimation } from '../types';
3
3
  export interface PagingAnimationParameters {
4
- controlledTx: Animated.Value;
5
4
  createScrollAnimation: CreateScrollAnimation;
6
- getCurrentIndex: GetCurrentIndex;
7
5
  itemWidth: number;
8
- lastIndex: number;
6
+ indexController: IndexController;
9
7
  loop: boolean;
10
8
  numberOfData: number;
11
- offsetTx: Animated.Value;
9
+ offsetX: Animated.Value;
10
+ translateX: Animated.Value;
12
11
  }
13
12
  export interface UsePagingAnimation {
14
- finalizeAnimation: () => void;
15
- globalInterpolation: Animated.AnimatedInterpolation;
13
+ interruptAnimation: () => void;
16
14
  startPagingAnimation: StartPagingAnimation;
17
15
  }
18
16
  export default function usePagingAnimation(params: PagingAnimationParameters): UsePagingAnimation;
@@ -0,0 +1,2 @@
1
+ declare const _default: (key: string, message?: string | undefined) => void;
2
+ export default _default;
@@ -2,6 +2,8 @@ import type { ReactElement } from 'react';
2
2
  import type { Animated, ViewProps } from 'react-native';
3
3
  declare const directions: readonly ["next", "prev", "stay"];
4
4
  export declare type PagingDirection = (typeof directions)[number];
5
+ declare const animationStates: readonly ["started", "finished", "interrupted"];
6
+ export declare type AnimationState = (typeof animationStates)[number];
5
7
  export declare type ItemHeight = number | 'auto';
6
8
  export interface RenderItem<T> {
7
9
  (info: {
@@ -19,11 +21,17 @@ export interface CreateItemStyle {
19
21
  export interface GetCurrentIndex {
20
22
  (): number;
21
23
  }
24
+ export interface OnIndexChange {
25
+ (itemIndex: number): void;
26
+ }
27
+ export interface OnPositionChange {
28
+ (position: number): void;
29
+ }
22
30
  export interface IndexController {
23
- currentIndex: number;
24
31
  getCurrentIndex: GetCurrentIndex;
25
32
  lastIndex: number;
26
- monitorElement: ReactElement;
33
+ notifyAnimationState: (state: AnimationState) => void;
34
+ notifyOffsetHasChanged: (offset: number) => void;
27
35
  }
28
36
  export declare type PagingAnimationType = 'directional' | 'index';
29
37
  export interface BasePagingAnimationConfig {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fountain-ui/lab",
3
- "version": "2.0.0-beta.14",
3
+ "version": "2.0.0-beta.17",
4
4
  "private": false,
5
5
  "author": "Fountain-UI Team",
6
6
  "description": "Incubator for Fountain-UI React components.",
@@ -18,7 +18,7 @@
18
18
  "@emotion/react": "^11.10.0",
19
19
  "@emotion/styled": "^11.10.0",
20
20
  "@fountain-ui/icons": "^2.0.0-beta.6",
21
- "@fountain-ui/utils": "^2.0.0-beta.3",
21
+ "@fountain-ui/utils": "^2.0.0-beta.4",
22
22
  "react-native-calendars": "1.1267.0"
23
23
  },
24
24
  "peerDependencies": {
@@ -70,5 +70,5 @@
70
70
  "publishConfig": {
71
71
  "access": "public"
72
72
  },
73
- "gitHead": "bd0ccbf7db51f1418dffb2da36fe167654a58f7e"
73
+ "gitHead": "973591c61fab2561cea9f30d03e802a24817b5f8"
74
74
  }
@@ -5,7 +5,6 @@ import type CarouselProps from './CarouselProps';
5
5
  import type { CarouselInstance } from './types';
6
6
  import {
7
7
  useAutoplayController,
8
- useDimensionChangeReaction,
9
8
  useIndexController,
10
9
  useItemVisibilityStore,
11
10
  useLoopedData,
@@ -36,41 +35,38 @@ const Carousel = forwardRef<CarouselInstance, CarouselProps>(function Carousel(p
36
35
  const data = useLoopedData(originalData, loop);
37
36
 
38
37
  const initialTx = itemWidth * initialIndex;
39
- const controlledTx = useRef(new Animated.Value(initialTx)).current;
40
- const offsetTx = useRef(new Animated.Value(0)).current;
38
+ const offsetX = useRef(new Animated.Value(initialTx)).current;
39
+ const translateX = useRef(new Animated.Value(0)).current;
40
+ const globalInterpolation = Animated.add(offsetX, translateX);
41
41
 
42
- const {
43
- currentIndex,
44
- getCurrentIndex,
45
- lastIndex,
46
- monitorElement,
47
- } = useIndexController({
48
- controlledTx,
42
+ const [itemVisibilityStore, onPositionChange] = useItemVisibilityStore({
43
+ initialIndex,
44
+ numberOfData: data.length,
45
+ windowSize,
46
+ });
47
+
48
+ const indexController = useIndexController({
49
49
  initialIndex,
50
50
  itemWidth,
51
+ numberOfData: data.length,
51
52
  numberOfOriginalData: originalData.length,
52
53
  onIndexChange,
54
+ onPositionChange,
53
55
  });
54
56
 
55
- const itemVisibilityStore = useItemVisibilityStore({
56
- currentIndex,
57
- numberOfData: data.length,
58
- windowSize,
59
- });
57
+ const { getCurrentIndex } = indexController;
60
58
 
61
59
  const {
62
- finalizeAnimation,
63
- globalInterpolation,
60
+ interruptAnimation,
64
61
  startPagingAnimation,
65
62
  } = usePagingAnimation({
66
- controlledTx,
67
63
  createScrollAnimation,
68
- getCurrentIndex,
69
64
  itemWidth,
70
- lastIndex,
65
+ indexController,
71
66
  loop,
72
67
  numberOfData: data.length,
73
- offsetTx,
68
+ offsetX,
69
+ translateX,
74
70
  });
75
71
 
76
72
  const autoplayController = useAutoplayController({
@@ -79,12 +75,6 @@ const Carousel = forwardRef<CarouselInstance, CarouselProps>(function Carousel(p
79
75
  startPagingAnimation,
80
76
  });
81
77
 
82
- useDimensionChangeReaction({
83
- controlledTx,
84
- currentIndex,
85
- itemWidth,
86
- });
87
-
88
78
  useImperativeHandle(
89
79
  ref,
90
80
  () => ({
@@ -116,8 +106,6 @@ const Carousel = forwardRef<CarouselInstance, CarouselProps>(function Carousel(p
116
106
 
117
107
  return (
118
108
  <InternalContext.Provider value={contextValue}>
119
- {monitorElement}
120
-
121
109
  <ViewabilityTrackerView
122
110
  enabled={autoplay && !disableSmartAutoplay}
123
111
  measurementIntervalMillis={Math.max(3000, autoplayInterval)}
@@ -131,8 +119,8 @@ const Carousel = forwardRef<CarouselInstance, CarouselProps>(function Carousel(p
131
119
  >
132
120
  <ScrollViewGesture
133
121
  autoplayController={autoplayController}
134
- finalizeAnimation={finalizeAnimation}
135
- offsetTx={offsetTx}
122
+ interruptAnimation={interruptAnimation}
123
+ translateX={translateX}
136
124
  scrollEnabled={scrollEnabled}
137
125
  startPagingAnimation={startPagingAnimation}
138
126
  >
@@ -1,6 +1,13 @@
1
1
  import type { RefObject } from 'react';
2
2
  import type { ComponentProps } from '@fountain-ui/core';
3
- import type { CarouselInstance, CreateItemStyle, CreateScrollAnimation, ItemHeight, RenderItem } from './types';
3
+ import type {
4
+ CarouselInstance,
5
+ CreateItemStyle,
6
+ CreateScrollAnimation,
7
+ ItemHeight,
8
+ OnIndexChange,
9
+ RenderItem,
10
+ } from './types';
4
11
 
5
12
  export default interface CarouselProps<ItemT = any> extends ComponentProps<{
6
13
  ref?: RefObject<CarouselInstance>;
@@ -66,7 +73,7 @@ export default interface CarouselProps<ItemT = any> extends ComponentProps<{
66
73
  /**
67
74
  * Callback fired when an index is changed.
68
75
  */
69
- onIndexChange?: (newIndex: number) => void;
76
+ onIndexChange?: OnIndexChange;
70
77
 
71
78
  /**
72
79
  * Takes an item from data and renders it into the list.
@@ -6,8 +6,8 @@ export default function createDefaultScrollAnimation(
6
6
  ): Animated.CompositeAnimation {
7
7
  return Animated.timing(animatedValue, {
8
8
  toValue: toValue,
9
- duration: 350,
10
- easing: Easing.bezier(0.25, 1, 0.5, 1),
9
+ duration: 180,
10
+ easing: Easing.bezier(0.2, 0.2, 0.2, 1),
11
11
  useNativeDriver: true,
12
12
  });
13
13
  };
@@ -25,8 +25,8 @@ export default function parallaxItemStyleFactory(config: ParallaxAnimationConfig
25
25
  adjacentItemScale,
26
26
  scrollingOffset,
27
27
  }: Required<ParallaxAnimationConfig> = {
28
- ...config,
29
28
  ...defaultParallaxAnimationConfig,
29
+ ...config,
30
30
  };
31
31
 
32
32
  const createItemStyle: CreateItemStyle = (itemInterpolation, itemWidth) => {
@@ -3,7 +3,7 @@ import React, { useContext, useEffect, useMemo, useState } from 'react';
3
3
  import type { ViewProps } from 'react-native';
4
4
  import { Animated } from 'react-native';
5
5
  import { StyleSheet } from '@fountain-ui/core';
6
- import { useItemInterpolation } from '../hooks';
6
+ import useItemInterpolation from './useItemInterpolation';
7
7
  import InternalContext from './InternalContext';
8
8
 
9
9
  export interface ItemViewProps {
@@ -32,7 +32,7 @@ export default function ItemView(props: ItemViewProps) {
32
32
 
33
33
  const itemStyle = useMemo(
34
34
  () => createItemStyle(interpolation, itemWidth),
35
- [createItemStyle, interpolation],
35
+ [createItemStyle, interpolation, itemWidth],
36
36
  );
37
37
 
38
38
  useEffect(() => {
@@ -40,7 +40,7 @@ export default function ItemView(props: ItemViewProps) {
40
40
  const nextVisible = ranges.some(([from, to]) => index >= from && index <= to);
41
41
  setVisible(nextVisible);
42
42
  });
43
- }, [itemVisibilityStore]);
43
+ }, [index, itemVisibilityStore]);
44
44
 
45
45
  return (
46
46
  <Animated.View
@@ -8,8 +8,8 @@ import type { AutoplayController, PagingDirection, StartPagingAnimation } from '
8
8
  export interface ScrollViewGestureProps {
9
9
  autoplayController: AutoplayController;
10
10
  children: ReactNode;
11
- finalizeAnimation: () => void;
12
- offsetTx: Animated.Value,
11
+ interruptAnimation: () => void;
12
+ translateX: Animated.Value,
13
13
  scrollEnabled: boolean;
14
14
  startPagingAnimation: StartPagingAnimation;
15
15
  }
@@ -23,6 +23,7 @@ const activeOffsetX: number[] = [-ACTIVE_OFFSET_ABS_X, ACTIVE_OFFSET_ABS_X];
23
23
  const endAnimationStates: Readonly<GestureHandlerState[]> = [
24
24
  GestureHandlerState.CANCELLED,
25
25
  GestureHandlerState.END,
26
+ GestureHandlerState.FAILED,
26
27
  ];
27
28
 
28
29
  function shouldScrollToAdjacent(translationX: number, velocityX: number): boolean {
@@ -38,8 +39,8 @@ export default function ScrollViewGesture(props: ScrollViewGestureProps) {
38
39
  const {
39
40
  autoplayController,
40
41
  children,
41
- finalizeAnimation,
42
- offsetTx,
42
+ interruptAnimation,
43
+ translateX,
43
44
  scrollEnabled,
44
45
  startPagingAnimation,
45
46
  } = props;
@@ -49,11 +50,11 @@ export default function ScrollViewGesture(props: ScrollViewGestureProps) {
49
50
  const handleGestureBegin = useCallback(() => {
50
51
  pauseAutoplay();
51
52
 
52
- finalizeAnimation();
53
- }, [finalizeAnimation, pauseAutoplay]);
53
+ interruptAnimation();
54
+ }, [interruptAnimation, pauseAutoplay]);
54
55
 
55
56
  const handleGestureEvent = useCallback(Animated.event(
56
- [{ nativeEvent: { translationX: offsetTx } }],
57
+ [{ nativeEvent: { translationX: translateX } }],
57
58
  { useNativeDriver: true },
58
59
  ), []);
59
60
 
@@ -1,6 +1,6 @@
1
1
  import { useContext, useMemo } from 'react';
2
2
  import { Animated } from 'react-native';
3
- import { InternalContext } from '../components';
3
+ import InternalContext from './InternalContext';
4
4
 
5
5
  const OVERSCROLL_FRICTION_FACTOR = 4;
6
6
 
@@ -94,9 +94,9 @@ export default function useItemInterpolation(index: number): Animated.AnimatedIn
94
94
  ? interpolationConfigOnLoop
95
95
  : interpolationConfigOnNoLoop;
96
96
 
97
- const offsetX = globalInterpolation.interpolate(interpolationConfig);
97
+ const localOffsetX = globalInterpolation.interpolate(interpolationConfig);
98
98
 
99
- return Animated.divide(offsetX, itemWidth);
99
+ return Animated.divide(localOffsetX, itemWidth);
100
100
  }, [
101
101
  globalInterpolation,
102
102
  interpolationConfigOnLoop,
@@ -1,7 +1,5 @@
1
1
  export { default as useAutoplayController } from './useAutoplayController';
2
- export { default as useDimensionChangeReaction } from './useDimensionChangeReaction';
3
2
  export { default as useIndexController } from './useIndexController';
4
- export { default as useItemInterpolation } from './useItemInterpolation';
5
3
  export { default as useLoopedData } from './useLoopedData';
6
4
  export { default as usePagingAnimation } from './usePagingAnimation';
7
5
  export { default as useItemVisibilityStore } from './useItemVisibilityStore';
@@ -1,76 +1,65 @@
1
- import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
2
- import { Animated } from 'react-native';
3
- import type { IndexController } from '../types';
1
+ import { useCallback } from 'react';
2
+ import { useImperativeState } from '@fountain-ui/core';
3
+ import { mod } from '@fountain-ui/utils';
4
+ import type { AnimationState, IndexController } from '../types';
4
5
 
5
6
  export interface UseIndexControllerParameters {
6
- controlledTx: Animated.AnimatedValue;
7
7
  initialIndex: number;
8
8
  itemWidth: number;
9
+ numberOfData: number;
9
10
  numberOfOriginalData: number;
10
- onIndexChange?: (newIndex: number) => void;
11
+ onIndexChange?: (itemIndex: number) => void;
12
+ onPositionChange?: (position: number) => void;
11
13
  }
12
14
 
13
- const normalizeIndex = (maybeIndex: number, numberOfData: number): number =>
14
- Math.abs(Math.floor(maybeIndex)) % numberOfData;
15
-
16
15
  export default function useIndexController(params: UseIndexControllerParameters): IndexController {
17
16
  const {
18
- controlledTx,
19
17
  initialIndex,
20
18
  itemWidth,
19
+ numberOfData,
21
20
  numberOfOriginalData,
22
21
  onIndexChange,
22
+ onPositionChange,
23
23
  } = params;
24
24
 
25
- const indexRef = useRef<number>(initialIndex);
26
- const [index, setIndex] = useState<number>(indexRef.current);
25
+ const currentIndex = useImperativeState(initialIndex);
26
+ const currentPosition = useImperativeState(initialIndex);
27
+
28
+ const notifyAnimationState = useCallback((state: AnimationState) => {
29
+ if (state === 'finished' || state === 'interrupted') {
30
+ if (currentIndex.hasChanged()) {
31
+ onIndexChange?.(currentIndex.get());
32
+ }
27
33
 
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);
34
+ if (currentPosition.hasChanged()) {
35
+ onPositionChange?.(currentPosition.get());
36
+ }
37
+ }
33
38
  }, [
34
- controlledTx,
35
- itemWidth,
36
- numberOfOriginalData,
39
+ onIndexChange,
40
+ onPositionChange,
37
41
  ]);
38
42
 
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);
43
+ const notifyOffsetHasChanged = useCallback((offset: number) => {
44
+ const roundedOffset = Math.round(offset / itemWidth) * itemWidth;
46
45
 
47
- onIndexChange?.(newIndex);
48
- }
49
- });
46
+ // To prevent floating point problem, make sure index is integer type.
47
+ const nextIndex = Math.floor(mod((-roundedOffset / itemWidth), numberOfOriginalData));
48
+ currentIndex.set(nextIndex);
50
49
 
51
- return () => {
52
- maybeIndex.removeListener(subscriptionId);
53
- };
50
+ // To prevent floating point problem, make sure index is integer type.
51
+ const nextPosition = Math.floor(mod((-roundedOffset / itemWidth), numberOfData));
52
+ currentPosition.set(nextPosition);
54
53
  }, [
55
- maybeIndex,
54
+ itemWidth,
55
+ numberOfData,
56
56
  numberOfOriginalData,
57
- onIndexChange,
58
57
  ]);
59
58
 
60
- const getCurrentIndex = useCallback(() => indexRef.current, []);
61
-
62
59
  return {
63
- currentIndex: index,
64
- getCurrentIndex,
60
+ getCurrentIndex: currentIndex.get,
65
61
  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
- ),
62
+ notifyAnimationState,
63
+ notifyOffsetHasChanged,
75
64
  };
76
65
  };
@@ -1,8 +1,9 @@
1
- import { useEffect, useRef, useState } from 'react';
2
- import type { ItemVisibilityStore, VisibleIndexRanges } from '../types';
1
+ import { useCallback, useEffect, useRef, useState } from 'react';
2
+ import { mod } from '@fountain-ui/utils';
3
+ import type { ItemVisibilityStore, OnPositionChange, VisibleIndexRanges } from '../types';
3
4
 
4
5
  export interface Parameters {
5
- currentIndex: number;
6
+ initialIndex: number;
6
7
  numberOfData: number;
7
8
  windowSize: number;
8
9
  }
@@ -52,10 +53,6 @@ function normalize(windowSize: number, numberOfData: number): number {
52
53
  return windowSize;
53
54
  }
54
55
 
55
- function mod(value: number, modulo: number): number {
56
- return ((value % modulo) + modulo) % modulo;
57
- }
58
-
59
56
  function makeVisibleIndexRanges(numberOfData: number, windowSize: number, index: number): VisibleIndexRanges {
60
57
  const ws = normalize(windowSize, numberOfData);
61
58
 
@@ -81,21 +78,24 @@ function makeVisibleIndexRanges(numberOfData: number, windowSize: number, index:
81
78
  ];
82
79
  }
83
80
 
84
- export default function useItemVisibilityStore(params: Parameters): ItemVisibilityStore {
81
+ export default function useItemVisibilityStore(params: Parameters): [ItemVisibilityStore, OnPositionChange] {
85
82
  const {
86
- currentIndex,
83
+ initialIndex,
87
84
  numberOfData,
88
85
  windowSize,
89
86
  } = params;
90
87
 
91
- const [initialRange] = useState(() => makeVisibleIndexRanges(numberOfData, windowSize, currentIndex));
88
+ const [initialRange] = useState(() => {
89
+ return makeVisibleIndexRanges(numberOfData, windowSize, initialIndex);
90
+ });
91
+
92
92
  const store = useRef(new SimpleItemVisibilityStore(initialRange)).current;
93
93
 
94
- useEffect(() => {
95
- const newRanges = makeVisibleIndexRanges(numberOfData, windowSize, currentIndex);
94
+ const onPositionChange: OnPositionChange = useCallback((position) => {
95
+ const newRanges = makeVisibleIndexRanges(numberOfData, windowSize, position);
96
96
 
97
97
  store.dispatch(newRanges);
98
- }, [currentIndex, numberOfData, windowSize]);
98
+ }, [numberOfData, windowSize]);
99
99
 
100
100
  useEffect(() => {
101
101
  return () => {
@@ -103,5 +103,5 @@ export default function useItemVisibilityStore(params: Parameters): ItemVisibili
103
103
  };
104
104
  }, []);
105
105
 
106
- return store;
106
+ return [store, onPositionChange];
107
107
  };