@momo-kits/swipe 0.77.8 → 0.78.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.
Files changed (3) hide show
  1. package/index.tsx +127 -113
  2. package/package.json +1 -1
  3. package/types.ts +5 -5
package/index.tsx CHANGED
@@ -1,130 +1,144 @@
1
- import React, {FC, forwardRef, useContext} from 'react';
1
+ import React, {
2
+ forwardRef,
3
+ ForwardRefRenderFunction,
4
+ useContext,
5
+ useImperativeHandle,
6
+ useRef,
7
+ } from 'react';
2
8
  import {Animated, Dimensions, TouchableHighlight, View} from 'react-native';
3
9
  import {Swipeable} from 'react-native-gesture-handler';
4
10
  import {ApplicationContext, Colors, Icon, Text} from '@momo-kits/foundation';
5
- import {SwipeAction, SwipeProps} from './types';
11
+ import {SwipeableRef, SwipeAction, SwipeProps} from './types';
6
12
  import styles from './styles';
7
13
 
8
14
  const ACTION_WIDTH = 56;
9
15
 
10
- const Swipe: FC<SwipeProps> = forwardRef(
11
- ({children, leftActions, rightActions, ...props}, ref) => {
12
- const {theme} = useContext(ApplicationContext);
13
- const screenWidth = Dimensions.get('window').width;
16
+ const Swipe: ForwardRefRenderFunction<SwipeableRef, SwipeProps> = (
17
+ {children, leftActions, rightActions, ...props},
18
+ ref,
19
+ ) => {
20
+ const {theme} = useContext(ApplicationContext);
21
+ const screenWidth = Dimensions.get('window').width;
22
+ const swipeableRef = useRef<Swipeable>(null);
14
23
 
15
- const renderActionItem = (
16
- action: SwipeAction,
17
- index: number,
18
- length: number,
19
- direction: 'left' | 'right',
20
- dragX: Animated.AnimatedInterpolation,
21
- ) => {
22
- const {
23
- label,
24
- icon,
25
- onPress,
26
- backgroundColor = theme.colors.highlight.primary,
27
- } = action;
24
+ const renderActionItem = (
25
+ action: SwipeAction,
26
+ index: number,
27
+ length: number,
28
+ direction: 'left' | 'right',
29
+ dragX: Animated.AnimatedInterpolation,
30
+ ) => {
31
+ const {
32
+ label,
33
+ icon,
34
+ onPress,
35
+ backgroundColor = theme.colors.highlight.primary,
36
+ } = action;
28
37
 
29
- let inputRange = [0, ACTION_WIDTH * length];
30
- let outputRange = [-ACTION_WIDTH * (index + 1), 0];
38
+ let inputRange = [0, ACTION_WIDTH * length];
39
+ let outputRange = [-ACTION_WIDTH * (index + 1), 0];
31
40
 
32
- let zIndex = -index;
41
+ let zIndex = -index;
33
42
 
34
- if (direction === 'right') {
35
- inputRange = [-ACTION_WIDTH * length, 0];
36
- outputRange = [0, ACTION_WIDTH * (length - index)];
37
- zIndex = index;
38
- }
39
-
40
- const translateX = dragX.interpolate({
41
- inputRange,
42
- outputRange,
43
- extrapolate: 'clamp',
44
- });
43
+ if (direction === 'right') {
44
+ inputRange = [-ACTION_WIDTH * length, 0];
45
+ outputRange = [0, ACTION_WIDTH * (length - index)];
46
+ zIndex = index;
47
+ }
45
48
 
46
- return (
47
- <TouchableHighlight
48
- underlayColor={Colors.black_01}
49
- activeOpacity={0.8}
50
- style={{zIndex: zIndex}}
51
- onPress={onPress}>
52
- <Animated.View
53
- style={[
54
- styles.action,
55
- {
56
- backgroundColor,
57
- transform: [{translateX}],
58
- width: ACTION_WIDTH,
59
- },
60
- ]}>
61
- {!!icon && <Icon source={icon} color={Colors.black_01} />}
62
- <Text
63
- style={styles.textCenter}
64
- numberOfLines={1}
65
- color={Colors.black_01}
66
- typography={'label_s'}>
67
- {label}
68
- </Text>
69
- </Animated.View>
70
- </TouchableHighlight>
71
- );
72
- };
49
+ const translateX = dragX.interpolate({
50
+ inputRange,
51
+ outputRange,
52
+ extrapolate: 'clamp',
53
+ });
73
54
 
74
- const renderActions = (
75
- actions: SwipeAction[] = [],
76
- direction: 'left' | 'right',
77
- dragX: Animated.AnimatedInterpolation,
78
- ) => {
79
- let overlayPosition: {[key: string]: number} = {left: 0};
80
- if (direction === 'right') {
81
- overlayPosition = {right: 0};
82
- }
83
- return (
84
- <View style={styles.row}>
85
- <View
86
- style={[
87
- {
88
- width: screenWidth,
89
- backgroundColor: theme.colors.background.surface,
90
- height: '100%',
91
- zIndex: -10,
92
- position: 'absolute',
93
- },
94
- overlayPosition,
95
- ]}
96
- />
97
- {actions?.map((item, index) => {
98
- //maximum 3 actions
99
- if (index < 3) {
100
- return renderActionItem(
101
- item,
102
- index,
103
- actions?.length,
104
- direction,
105
- dragX,
106
- );
107
- }
108
- })}
109
- </View>
110
- );
111
- };
55
+ return (
56
+ <TouchableHighlight
57
+ underlayColor={Colors.black_01}
58
+ activeOpacity={0.8}
59
+ style={{zIndex: zIndex}}
60
+ onPress={onPress}>
61
+ <Animated.View
62
+ style={[
63
+ styles.action,
64
+ {
65
+ backgroundColor,
66
+ transform: [{translateX}],
67
+ width: ACTION_WIDTH,
68
+ },
69
+ ]}>
70
+ {!!icon && <Icon source={icon} color={Colors.black_01} />}
71
+ <Text
72
+ style={styles.textCenter}
73
+ numberOfLines={1}
74
+ color={Colors.black_01}
75
+ typography={'label_s'}>
76
+ {label}
77
+ </Text>
78
+ </Animated.View>
79
+ </TouchableHighlight>
80
+ );
81
+ };
112
82
 
83
+ const renderActions = (
84
+ actions: SwipeAction[] = [],
85
+ direction: 'left' | 'right',
86
+ dragX: Animated.AnimatedInterpolation,
87
+ ) => {
88
+ let overlayPosition: {[key: string]: number} = {left: 0};
89
+ if (direction === 'right') {
90
+ overlayPosition = {right: 0};
91
+ }
113
92
  return (
114
- <Swipeable
115
- {...props}
116
- containerStyle={styles.container}
117
- ref={ref}
118
- renderRightActions={(progressAnimatedValue, dragAnimatedValue) =>
119
- renderActions(rightActions, 'right', dragAnimatedValue)
120
- }
121
- renderLeftActions={(progressAnimatedValue, dragAnimatedValue) =>
122
- renderActions(leftActions, 'left', dragAnimatedValue)
123
- }>
124
- {children}
125
- </Swipeable>
93
+ <View style={styles.row}>
94
+ <View
95
+ style={[
96
+ {
97
+ width: screenWidth,
98
+ backgroundColor: theme.colors.background.surface,
99
+ height: '100%',
100
+ zIndex: -10,
101
+ position: 'absolute',
102
+ },
103
+ overlayPosition,
104
+ ]}
105
+ />
106
+ {actions?.map((item, index) => {
107
+ //maximum 3 actions
108
+ if (index < 3) {
109
+ return renderActionItem(
110
+ item,
111
+ index,
112
+ actions?.length,
113
+ direction,
114
+ dragX,
115
+ );
116
+ }
117
+ })}
118
+ </View>
126
119
  );
127
- },
128
- );
120
+ };
121
+
122
+ useImperativeHandle(ref, () => ({
123
+ openLeft: () => swipeableRef?.current?.openLeft(),
124
+ openRight: () => swipeableRef?.current?.openRight(),
125
+ close: () => swipeableRef?.current?.close(),
126
+ }));
127
+
128
+ return (
129
+ <Swipeable
130
+ {...props}
131
+ containerStyle={styles.container}
132
+ ref={swipeableRef}
133
+ renderRightActions={(progressAnimatedValue, dragAnimatedValue) =>
134
+ renderActions(rightActions, 'right', dragAnimatedValue)
135
+ }
136
+ renderLeftActions={(progressAnimatedValue, dragAnimatedValue) =>
137
+ renderActions(leftActions, 'left', dragAnimatedValue)
138
+ }>
139
+ {children}
140
+ </Swipeable>
141
+ );
142
+ };
129
143
 
130
- export default Swipe;
144
+ export default forwardRef(Swipe);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momo-kits/swipe",
3
- "version": "0.77.8",
3
+ "version": "0.78.2",
4
4
  "private": false,
5
5
  "main": "index.tsx",
6
6
  "dependencies": {},
package/types.ts CHANGED
@@ -90,10 +90,10 @@ export type SwipeProps = {
90
90
  * about to close and return to its original position.
91
91
  */
92
92
  onSwipeableWillClose?: () => void;
93
+ };
93
94
 
94
- /**
95
- * Optional. A ref for the underlying `Swipeable` component from `react-native-gesture-handler`.
96
- * Can be used for manually triggering actions, like closing the swipeable surface.
97
- */
98
- ref?: React.ForwardedRef<Swipeable>;
95
+ export type SwipeableRef = {
96
+ openLeft: () => void;
97
+ openRight: () => void;
98
+ close: () => void;
99
99
  };