@momo-kits/carousel 0.165.2-beta.1 → 0.165.2-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core/LICENSE +21 -0
- package/core/README.md +16 -0
- package/core/components/Carousel.tsx +28 -0
- package/core/components/CarouselLayout.tsx +231 -0
- package/core/components/ItemLayout.tsx +215 -0
- package/core/components/ItemRenderer.tsx +126 -0
- package/core/components/LazyView.tsx +14 -0
- package/core/components/ScrollViewGesture.tsx +443 -0
- package/core/constants/index.ts +11 -0
- package/core/hooks/useAutoPlay.ts +63 -0
- package/core/hooks/useCarouselController.tsx +295 -0
- package/core/hooks/useCheckMounted.ts +14 -0
- package/core/hooks/useCommonVariables.ts +261 -0
- package/core/hooks/useInitProps.ts +177 -0
- package/core/hooks/useLayoutConfig.ts +35 -0
- package/core/hooks/useOffsetX.ts +81 -0
- package/core/hooks/useOnProgressChange.ts +33 -0
- package/core/hooks/usePanGestureProxy.ts +144 -0
- package/core/hooks/usePropsErrorBoundary.ts +41 -0
- package/core/hooks/useUpdateGestureConfig.ts +17 -0
- package/core/hooks/useVisibleRanges.tsx +141 -0
- package/core/layouts/index.tsx +10 -0
- package/core/layouts/normal.ts +32 -0
- package/core/layouts/parallax.ts +50 -0
- package/core/layouts/stack.ts +266 -0
- package/core/public-types.ts +209 -0
- package/core/store/index.tsx +63 -0
- package/core/types.ts +32 -0
- package/core/utils/carousel-direction.ts +39 -0
- package/core/utils/carousel-math.ts +156 -0
- package/core/utils/compute-gesture-translation.ts +29 -0
- package/core/utils/compute-offset-if-size-changed.ts +14 -0
- package/core/utils/computed-with-auto-fill-data.ts +92 -0
- package/core/utils/deal-with-animation.ts +19 -0
- package/core/utils/log.ts +12 -0
- package/core/utils/sanitize-animation-style.ts +20 -0
- package/index.tsx +390 -455
- package/package.json +6 -5
- package/types.ts +123 -48
- package/animation.ts +0 -62
package/package.json
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@momo-kits/carousel",
|
|
3
|
-
"version": "0.165.2-beta.
|
|
3
|
+
"version": "0.165.2-beta.2",
|
|
4
4
|
"private": false,
|
|
5
5
|
"main": "index.tsx",
|
|
6
6
|
"peerDependencies": {
|
|
7
7
|
"@momo-kits/foundation": "latest",
|
|
8
8
|
"react": "*",
|
|
9
|
-
"react-native": "*"
|
|
9
|
+
"react-native": "*",
|
|
10
|
+
"react-native-gesture-handler": "2.30.0",
|
|
11
|
+
"react-native-reanimated": "4.1.3",
|
|
12
|
+
"react-native-worklets": "0.6.1"
|
|
10
13
|
},
|
|
11
14
|
"license": "MoMo",
|
|
12
15
|
"publishConfig": {
|
|
13
16
|
"registry": "https://registry.npmjs.org/"
|
|
14
17
|
},
|
|
15
|
-
"dependencies": {
|
|
16
|
-
"moment": "^2.24.0"
|
|
17
|
-
}
|
|
18
|
+
"dependencies": {}
|
|
18
19
|
}
|
package/types.ts
CHANGED
|
@@ -1,71 +1,146 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import type {
|
|
3
3
|
NativeScrollEvent,
|
|
4
4
|
NativeSyntheticEvent,
|
|
5
5
|
StyleProp,
|
|
6
6
|
ViewStyle,
|
|
7
7
|
} from 'react-native';
|
|
8
|
+
import type { SharedValue } from 'react-native-reanimated';
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
import type {
|
|
11
|
+
CarouselItemAnimation,
|
|
12
|
+
CarouselLayout,
|
|
13
|
+
CarouselProps as CoreCarouselProps,
|
|
14
|
+
CarouselRef as CoreCarouselRef,
|
|
15
|
+
} from './core/public-types';
|
|
16
|
+
|
|
17
|
+
export type {
|
|
18
|
+
CarouselAnimation,
|
|
19
|
+
CarouselItemAnimation,
|
|
20
|
+
CarouselLayout,
|
|
21
|
+
CarouselPanGesture,
|
|
22
|
+
CarouselProgressChangeHandler,
|
|
23
|
+
CarouselRenderItem,
|
|
24
|
+
CarouselRenderItemInfo,
|
|
25
|
+
CarouselScrollToOptions,
|
|
26
|
+
CarouselStepOptions,
|
|
27
|
+
} from './core/public-types';
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Engine props minus the three the compatibility layer widens: `renderItem`
|
|
31
|
+
* accepts a `ReactNode`, and `layout`/`itemAnimation` lose their compile-time
|
|
32
|
+
* exclusivity (the engine still throws at runtime when both are passed).
|
|
33
|
+
*/
|
|
34
|
+
type EngineProps<Item> = Omit<
|
|
35
|
+
CoreCarouselProps<Item>,
|
|
36
|
+
'renderItem' | 'layout' | 'itemAnimation'
|
|
37
|
+
>;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Props from the FlatList-based carousel. They keep working so call sites can
|
|
41
|
+
* upgrade by bumping the version alone, but new code should prefer the engine
|
|
42
|
+
* props they map onto.
|
|
43
|
+
*/
|
|
44
|
+
type LegacyProps = {
|
|
45
|
+
/** @deprecated Use `defaultIndex`. */
|
|
16
46
|
firstItem?: number;
|
|
47
|
+
/** @deprecated Use `itemSize`. */
|
|
48
|
+
itemWidth?: number;
|
|
49
|
+
/** @deprecated Use `itemSize` — this divides the container by `visibleItem`. */
|
|
50
|
+
visibleItem?: number;
|
|
51
|
+
/** @deprecated Item fills the container, which is the default. */
|
|
52
|
+
full?: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* @deprecated Use `snapMode` — `false` maps to `"none"`. Free scrolling
|
|
55
|
+
* decelerates on the gesture handler's curve rather than the platform's.
|
|
56
|
+
*/
|
|
57
|
+
enableSnap?: boolean;
|
|
58
|
+
/** @deprecated Delay before autoplay starts. No engine equivalent. */
|
|
59
|
+
autoplayDelay?: number;
|
|
60
|
+
/** @deprecated Use `layout` or `itemAnimation`. */
|
|
17
61
|
inactiveSlideOpacity?: number;
|
|
62
|
+
/** @deprecated Use `layout` or `itemAnimation`. */
|
|
18
63
|
inactiveSlideScale?: number;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
64
|
+
/** @deprecated Delay before the carousel becomes visible. */
|
|
65
|
+
apparitionDelay?: number;
|
|
66
|
+
/**
|
|
67
|
+
* @deprecated Ignored — it was already unused by the FlatList implementation.
|
|
68
|
+
* Set a gesture threshold with `onConfigurePanGesture` + `activeOffsetX`.
|
|
69
|
+
*/
|
|
70
|
+
activeSlideOffset?: number;
|
|
71
|
+
/** @deprecated Style each item inside `renderItem` instead. */
|
|
22
72
|
slideStyle?: StyleProp<ViewStyle>;
|
|
23
|
-
|
|
24
|
-
itemWidth?: number;
|
|
25
|
-
data: any[];
|
|
26
|
-
renderItem: (info: { item: any; index: number }) => React.ReactNode;
|
|
27
|
-
onScroll?: any;
|
|
73
|
+
/** @deprecated Use `onProgressChange` or `progress`. */
|
|
28
74
|
onScrollIndexChanged?: (index: number) => void;
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
75
|
+
/**
|
|
76
|
+
* @deprecated Use `onProgressChange`.
|
|
77
|
+
*
|
|
78
|
+
* Only `nativeEvent.contentOffset.x` is filled in, as it was by the FlatList
|
|
79
|
+
* implementation. With `loop` the origin differs: the offset is now relative
|
|
80
|
+
* to the first item and may go negative, where it used to count from the
|
|
81
|
+
* leading clones.
|
|
82
|
+
*/
|
|
83
|
+
onScroll?: (event: NativeSyntheticEvent<NativeScrollEvent>) => void;
|
|
84
|
+
/**
|
|
85
|
+
* @deprecated Ignored. It used to carry a real momentum event from the
|
|
86
|
+
* underlying ScrollView; there is no ScrollView and no momentum phase left to
|
|
87
|
+
* report. Use `onSnapToItem` for the settled index.
|
|
88
|
+
*/
|
|
89
|
+
onMomentumScrollEnd?: (event: NativeSyntheticEvent<NativeScrollEvent>) => void;
|
|
90
|
+
/**
|
|
91
|
+
* @deprecated Use `onConfigurePanGesture` with `onBegin`.
|
|
92
|
+
*
|
|
93
|
+
* Called with no argument — the pan gesture has no `GestureResponderEvent`.
|
|
94
|
+
*/
|
|
95
|
+
onTouchStart?: (event?: any) => void;
|
|
96
|
+
/**
|
|
97
|
+
* @deprecated Use `onConfigurePanGesture` with `onFinalize`.
|
|
98
|
+
*
|
|
99
|
+
* Called with no argument — the pan gesture has no `GestureResponderEvent`.
|
|
100
|
+
*/
|
|
101
|
+
onTouchEnd?: (event?: any) => void;
|
|
102
|
+
|
|
103
|
+
/** @deprecated Ignored. The engine loops by offset instead of cloning data. */
|
|
104
|
+
loopClonesPerSide?: number;
|
|
105
|
+
/** @deprecated Ignored. Snapping is per item — use `snapMode`. */
|
|
106
|
+
snapToInterval?: number;
|
|
107
|
+
/** @deprecated Ignored. The engine does not use a native ScrollView. */
|
|
108
|
+
disableIntervalMomentum?: boolean;
|
|
109
|
+
/** @deprecated Ignored. The engine does not use a FlatList. */
|
|
37
110
|
getItemLayout?: (
|
|
38
111
|
data: any,
|
|
39
112
|
index: number
|
|
40
113
|
) => { length: number; offset: number; index: number };
|
|
41
|
-
style?: ViewStyle;
|
|
42
|
-
visibleItem?: number;
|
|
43
|
-
contentContainerStyle?: StyleProp<ViewStyle>;
|
|
44
|
-
full?: boolean;
|
|
45
|
-
snapToInterval?: number | Animated.Value | Animated.AnimatedInterpolation<number | string> | undefined
|
|
46
114
|
};
|
|
47
115
|
|
|
48
|
-
export type
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
116
|
+
export type CarouselRenderItemInfoCompat<Item> = {
|
|
117
|
+
item: Item;
|
|
118
|
+
index: number;
|
|
119
|
+
relativeProgress: SharedValue<number>;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* `Item` defaults to `any` so `CarouselProps` stays usable without a type
|
|
124
|
+
* argument, the way the FlatList-based props type was.
|
|
125
|
+
*/
|
|
126
|
+
export type CarouselProps<Item = any> = EngineProps<Item> &
|
|
127
|
+
LegacyProps & {
|
|
128
|
+
renderItem: (info: CarouselRenderItemInfoCompat<Item>) => ReactNode;
|
|
129
|
+
layout?: CarouselLayout;
|
|
130
|
+
itemAnimation?: CarouselItemAnimation;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
export type CarouselRef = CoreCarouselRef & {
|
|
134
|
+
/** @deprecated Use `scrollTo({ index, animated })`. */
|
|
135
|
+
snapToItem: (index: number, animated?: boolean, fireCallback?: boolean) => void;
|
|
136
|
+
/** @deprecated Use `next({ animated })`. */
|
|
54
137
|
snapToNext: (animated?: boolean, fireCallback?: boolean) => void;
|
|
138
|
+
/** @deprecated Use `prev({ animated })`. */
|
|
55
139
|
snapToPrev: (animated?: boolean, fireCallback?: boolean) => void;
|
|
140
|
+
/** @deprecated Toggle the `autoplay` prop instead. */
|
|
56
141
|
startAutoplay: () => void;
|
|
142
|
+
/** @deprecated Toggle the `autoplay` prop instead. */
|
|
57
143
|
pauseAutoplay: () => void;
|
|
144
|
+
/** @deprecated Toggle the `autoplay` prop instead. */
|
|
58
145
|
stopAutoplay: () => void;
|
|
59
146
|
};
|
|
60
|
-
|
|
61
|
-
export type CarouselState = {
|
|
62
|
-
hideCarousel: boolean;
|
|
63
|
-
interpolators: any;
|
|
64
|
-
containerWidth: number;
|
|
65
|
-
itemWidth: number;
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
export type Position = {
|
|
69
|
-
start: any;
|
|
70
|
-
end: any;
|
|
71
|
-
};
|
package/animation.ts
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import {ReactNode} from 'react';
|
|
2
|
-
import {Animated} from 'react-native';
|
|
3
|
-
import {CarouselProps} from './types';
|
|
4
|
-
|
|
5
|
-
export function getInputRangeFromIndexes(
|
|
6
|
-
range: string | any[],
|
|
7
|
-
index: number,
|
|
8
|
-
itemWidth: number,
|
|
9
|
-
) {
|
|
10
|
-
const sizeRef = itemWidth;
|
|
11
|
-
const inputRange = [];
|
|
12
|
-
|
|
13
|
-
for (let i = 0; i < range.length; i++) {
|
|
14
|
-
inputRange.push((index - range[i]) * sizeRef);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return inputRange;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function defaultScrollInterpolator(index: number, itemWidth: number) {
|
|
21
|
-
const range = [1, 0, -1];
|
|
22
|
-
const inputRange = getInputRangeFromIndexes(range, index, itemWidth);
|
|
23
|
-
const outputRange = [0, 1, 0];
|
|
24
|
-
|
|
25
|
-
return {inputRange, outputRange};
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export function defaultAnimatedStyles(
|
|
29
|
-
animatedValue: Animated.Value,
|
|
30
|
-
carouselProps: Readonly<CarouselProps> & Readonly<{children?: ReactNode}>,
|
|
31
|
-
) {
|
|
32
|
-
let animatedOpacity = {};
|
|
33
|
-
let animatedScale = {};
|
|
34
|
-
const {inactiveSlideOpacity = 1, inactiveSlideScale = 1} = carouselProps;
|
|
35
|
-
|
|
36
|
-
if (inactiveSlideOpacity < 1) {
|
|
37
|
-
animatedOpacity = {
|
|
38
|
-
opacity: animatedValue.interpolate({
|
|
39
|
-
inputRange: [0, 1],
|
|
40
|
-
outputRange: [inactiveSlideOpacity, 1],
|
|
41
|
-
}),
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
if (inactiveSlideScale < 1) {
|
|
46
|
-
animatedScale = {
|
|
47
|
-
transform: [
|
|
48
|
-
{
|
|
49
|
-
scaleY: animatedValue.interpolate({
|
|
50
|
-
inputRange: [0, 1],
|
|
51
|
-
outputRange: [inactiveSlideScale, 1],
|
|
52
|
-
}),
|
|
53
|
-
},
|
|
54
|
-
],
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return {
|
|
59
|
-
...animatedOpacity,
|
|
60
|
-
...animatedScale,
|
|
61
|
-
};
|
|
62
|
-
}
|