@momo-kits/swipe 0.168.1-beta.2 → 0.168.1-rn.88.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/SwipeView.tsx +93 -22
  2. package/package.json +7 -7
package/SwipeView.tsx CHANGED
@@ -5,28 +5,78 @@ import React, {
5
5
  useImperativeHandle,
6
6
  useRef,
7
7
  } from 'react';
8
- import {Animated, Dimensions, TouchableHighlight, View} from 'react-native';
9
- import {Swipeable} from 'react-native-gesture-handler';
8
+ import {Dimensions, TouchableHighlight, View} from 'react-native';
9
+ // RN 0.88 / gesture-handler v3: legacy `Swipeable` đã bị xoá → dùng ReanimatedSwipeable
10
+ import ReanimatedSwipeable, {
11
+ SwipeDirection,
12
+ type SwipeableMethods,
13
+ } from 'react-native-gesture-handler/ReanimatedSwipeable';
14
+ import Animated, {
15
+ Extrapolation,
16
+ interpolate,
17
+ type SharedValue,
18
+ useAnimatedStyle,
19
+ } from 'react-native-reanimated';
10
20
  import {ApplicationContext, Colors, Icon, Text} from '@momo-kits/foundation';
11
21
  import {SwipeableRef, SwipeAction, SwipeProps} from './types';
12
22
  import styles from './styles';
13
23
 
14
24
  const ACTION_WIDTH = 56;
15
25
 
26
+ /**
27
+ * Action item tách thành component riêng để dùng hook `useAnimatedStyle`
28
+ * (thay cho Animated.Value.interpolate của legacy Swipeable).
29
+ */
30
+ const SwipeActionItem: React.FC<{
31
+ translation: SharedValue<number>;
32
+ inputRange: number[];
33
+ outputRange: number[];
34
+ style: any;
35
+ children?: React.ReactNode;
36
+ }> = ({translation, inputRange, outputRange, style, children}) => {
37
+ const animatedStyle = useAnimatedStyle(() => ({
38
+ transform: [
39
+ {
40
+ translateX: interpolate(
41
+ translation.value,
42
+ inputRange,
43
+ outputRange,
44
+ Extrapolation.CLAMP
45
+ ),
46
+ },
47
+ ],
48
+ }));
49
+ return <Animated.View style={[style, animatedStyle]}>{children}</Animated.View>;
50
+ };
51
+
16
52
  export const Swipe: ForwardRefRenderFunction<SwipeableRef, SwipeProps> = (
17
- {children, leftActions, rightActions, height = 56, itemRadius, ...props},
53
+ {
54
+ children,
55
+ leftActions,
56
+ rightActions,
57
+ height = 56,
58
+ itemRadius,
59
+ onSwipeableLeftOpen,
60
+ onSwipeableRightOpen,
61
+ onSwipeableOpen,
62
+ onSwipeableClose,
63
+ onSwipeableLeftWillOpen,
64
+ onSwipeableRightWillOpen,
65
+ onSwipeableWillOpen,
66
+ onSwipeableWillClose,
67
+ },
18
68
  ref
19
69
  ) => {
20
70
  const {theme} = useContext(ApplicationContext);
21
71
  const screenWidth = Dimensions.get('window').width;
22
- const swipeableRef = useRef<Swipeable>(null);
72
+ const swipeableRef = useRef<SwipeableMethods>(null);
23
73
 
24
74
  const renderActionItem = (
25
75
  action: SwipeAction,
26
76
  index: number,
27
77
  length: number,
28
78
  direction: 'left' | 'right',
29
- dragX: Animated.AnimatedInterpolation<number | string>
79
+ dragX: SharedValue<number>
30
80
  ) => {
31
81
  const {
32
82
  label,
@@ -46,12 +96,6 @@ export const Swipe: ForwardRefRenderFunction<SwipeableRef, SwipeProps> = (
46
96
  zIndex = index;
47
97
  }
48
98
 
49
- const translateX = dragX.interpolate({
50
- inputRange,
51
- outputRange,
52
- extrapolate: 'clamp',
53
- });
54
-
55
99
  return (
56
100
  <TouchableHighlight
57
101
  key={`swipe-action-${direction}-${index}`}
@@ -59,12 +103,14 @@ export const Swipe: ForwardRefRenderFunction<SwipeableRef, SwipeProps> = (
59
103
  activeOpacity={0.8}
60
104
  style={{zIndex: zIndex}}
61
105
  onPress={onPress}>
62
- <Animated.View
106
+ <SwipeActionItem
107
+ translation={dragX}
108
+ inputRange={inputRange}
109
+ outputRange={outputRange}
63
110
  style={[
64
111
  styles.action,
65
112
  {
66
113
  backgroundColor,
67
- transform: [{translateX}],
68
114
  width: ACTION_WIDTH,
69
115
  borderRadius: itemRadius,
70
116
  },
@@ -77,7 +123,7 @@ export const Swipe: ForwardRefRenderFunction<SwipeableRef, SwipeProps> = (
77
123
  typography={'label_s_medium'}>
78
124
  {label}
79
125
  </Text>
80
- </Animated.View>
126
+ </SwipeActionItem>
81
127
  </TouchableHighlight>
82
128
  );
83
129
  };
@@ -85,7 +131,7 @@ export const Swipe: ForwardRefRenderFunction<SwipeableRef, SwipeProps> = (
85
131
  const renderActions = (
86
132
  actions: SwipeAction[] = [],
87
133
  direction: 'left' | 'right',
88
- dragX: Animated.AnimatedInterpolation<number | string>
134
+ dragX: SharedValue<number>
89
135
  ) => {
90
136
  let overlayPosition: {[key: string]: number} = {left: 0};
91
137
  if (direction === 'right') {
@@ -127,9 +173,34 @@ export const Swipe: ForwardRefRenderFunction<SwipeableRef, SwipeProps> = (
127
173
  close: () => swipeableRef?.current?.close(),
128
174
  }));
129
175
 
176
+ /**
177
+ * ReanimatedSwipeable trả về hướng vuốt: mở left actions (translation > 0) => SwipeDirection.RIGHT,
178
+ * mở right actions (translation < 0) => SwipeDirection.LEFT. Map lại về API cũ của @momo-kits/swipe.
179
+ */
180
+ const handleOpen = (direction: SwipeDirection) => {
181
+ if (direction === SwipeDirection.RIGHT) {
182
+ onSwipeableLeftOpen?.();
183
+ } else {
184
+ onSwipeableRightOpen?.();
185
+ }
186
+ onSwipeableOpen?.();
187
+ };
188
+
189
+ const handleWillOpen = (direction: SwipeDirection) => {
190
+ if (direction === SwipeDirection.RIGHT) {
191
+ onSwipeableLeftWillOpen?.();
192
+ } else {
193
+ onSwipeableRightWillOpen?.();
194
+ }
195
+ onSwipeableWillOpen?.();
196
+ };
197
+
130
198
  return (
131
- <Swipeable
132
- {...props}
199
+ <ReanimatedSwipeable
200
+ onSwipeableOpen={handleOpen}
201
+ onSwipeableWillOpen={handleWillOpen}
202
+ onSwipeableClose={() => onSwipeableClose?.()}
203
+ onSwipeableWillClose={() => onSwipeableWillClose?.()}
133
204
  containerStyle={[
134
205
  styles.container,
135
206
  {
@@ -137,14 +208,14 @@ export const Swipe: ForwardRefRenderFunction<SwipeableRef, SwipeProps> = (
137
208
  },
138
209
  ]}
139
210
  ref={swipeableRef}
140
- renderRightActions={(progressAnimatedValue, dragAnimatedValue) =>
141
- renderActions(rightActions, 'right', dragAnimatedValue)
211
+ renderRightActions={(_progress, translation) =>
212
+ renderActions(rightActions, 'right', translation)
142
213
  }
143
- renderLeftActions={(progressAnimatedValue, dragAnimatedValue) =>
144
- renderActions(leftActions, 'left', dragAnimatedValue)
214
+ renderLeftActions={(_progress, translation) =>
215
+ renderActions(leftActions, 'left', translation)
145
216
  }>
146
217
  {children}
147
- </Swipeable>
218
+ </ReanimatedSwipeable>
148
219
  );
149
220
  };
150
221
 
package/package.json CHANGED
@@ -1,16 +1,16 @@
1
1
  {
2
2
  "name": "@momo-kits/swipe",
3
- "version": "0.168.1-beta.2",
3
+ "version": "0.168.1-rn.88.0",
4
4
  "private": false,
5
+ "license": "MoMo",
5
6
  "main": "index.tsx",
7
+ "publishConfig": {
8
+ "registry": "https://registry.npmjs.org/"
9
+ },
10
+ "dependencies": {},
6
11
  "peerDependencies": {
7
12
  "@momo-kits/foundation": "latest",
8
13
  "react": "*",
9
14
  "react-native": "*"
10
- },
11
- "license": "MoMo",
12
- "publishConfig": {
13
- "registry": "https://registry.npmjs.org/"
14
- },
15
- "dependencies": {}
15
+ }
16
16
  }