@momo-kits/swipe 0.77.5 → 0.77.8-beta.1

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 +128 -93
  2. package/package.json +1 -1
  3. package/types.ts +78 -2
package/index.tsx CHANGED
@@ -1,109 +1,144 @@
1
- import React, {FC, forwardRef, useContext} from 'react';
2
- import {Dimensions, Animated, TouchableOpacity, View} from 'react-native';
1
+ import React, {
2
+ forwardRef,
3
+ ForwardRefRenderFunction,
4
+ useContext,
5
+ useImperativeHandle,
6
+ useRef,
7
+ } from 'react';
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);
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);
13
23
 
14
- const renderActionItem = (
15
- action: SwipeAction,
16
- index: number,
17
- length: number,
18
- direction: 'left' | 'right',
19
- dragX: Animated.AnimatedInterpolation,
20
- ) => {
21
- const {
22
- label,
23
- icon,
24
- onPress,
25
- backgroundColor = theme.colors.highlight.primary,
26
- } = 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;
27
37
 
28
- let inputRange = [0, ACTION_WIDTH * length];
29
- let outputRange = [-ACTION_WIDTH * (index + 1), 0];
38
+ let inputRange = [0, ACTION_WIDTH * length];
39
+ let outputRange = [-ACTION_WIDTH * (index + 1), 0];
30
40
 
31
- let zIndex = -index;
41
+ let zIndex = -index;
32
42
 
33
- if (direction === 'right') {
34
- inputRange = [-ACTION_WIDTH * length, 0];
35
- outputRange = [0, ACTION_WIDTH * (length - index)];
36
- zIndex = index;
37
- }
38
-
39
- const translateX = dragX.interpolate({
40
- inputRange,
41
- outputRange,
42
- extrapolate: 'clamp',
43
- });
43
+ if (direction === 'right') {
44
+ inputRange = [-ACTION_WIDTH * length, 0];
45
+ outputRange = [0, ACTION_WIDTH * (length - index)];
46
+ zIndex = index;
47
+ }
44
48
 
45
- return (
46
- <TouchableOpacity style={{zIndex: zIndex}} onPress={onPress}>
47
- <Animated.View
48
- style={[
49
- styles.action,
50
- {
51
- backgroundColor,
52
- transform: [{translateX}],
53
- width: ACTION_WIDTH,
54
- },
55
- ]}>
56
- {!!icon && <Icon source={icon} color={Colors.black_01} />}
57
- <Text
58
- style={styles.textCenter}
59
- numberOfLines={1}
60
- color={Colors.black_01}
61
- typography={'label_s'}>
62
- {label}
63
- </Text>
64
- </Animated.View>
65
- </TouchableOpacity>
66
- );
67
- };
49
+ const translateX = dragX.interpolate({
50
+ inputRange,
51
+ outputRange,
52
+ extrapolate: 'clamp',
53
+ });
68
54
 
69
- const renderActions = (
70
- actions: SwipeAction[] | undefined,
71
- direction: 'left' | 'right',
72
- dragX: Animated.AnimatedInterpolation,
73
- ) => {
74
- return (
75
- <View style={styles.row}>
76
- {actions?.map((item, index) => {
77
- //maximum 3 actions
78
- if (index < 3) {
79
- return renderActionItem(
80
- item,
81
- index,
82
- actions?.length,
83
- direction,
84
- dragX,
85
- );
86
- }
87
- })}
88
- </View>
89
- );
90
- };
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
+ };
91
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
+ }
92
92
  return (
93
- <Swipeable
94
- {...props}
95
- containerStyle={styles.container}
96
- ref={ref}
97
- renderRightActions={(progressAnimatedValue, dragAnimatedValue) =>
98
- renderActions(rightActions, 'right', dragAnimatedValue)
99
- }
100
- renderLeftActions={(progressAnimatedValue, dragAnimatedValue) =>
101
- renderActions(leftActions, 'left', dragAnimatedValue)
102
- }>
103
- {children}
104
- </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>
105
119
  );
106
- },
107
- );
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
+ };
108
143
 
109
- 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.5",
3
+ "version": "0.77.8-beta.01",
4
4
  "private": false,
5
5
  "main": "index.tsx",
6
6
  "dependencies": {},
package/types.ts CHANGED
@@ -1,23 +1,99 @@
1
- import {Ref, RefObject} from 'react';
1
+ import {Ref} from 'react';
2
2
  import {Swipeable} from 'react-native-gesture-handler';
3
3
 
4
+ /**
5
+ * Represents an action item in the Swipe component.
6
+ */
4
7
  export type SwipeAction = {
8
+ /**
9
+ * The text label for the action button. This label is typically displayed on the button itself.
10
+ */
5
11
  label: string;
12
+
13
+ /**
14
+ * The icon for the action button, providing a visual representation of the action.
15
+ */
6
16
  icon: string;
17
+
18
+ /**
19
+ * The function to be called when the action button is pressed.
20
+ */
7
21
  onPress: () => void;
22
+
23
+ /**
24
+ * Optional. The background color of the action button.
25
+ * If not provided, a default color is used.
26
+ */
8
27
  backgroundColor?: string;
9
28
  };
10
29
 
30
+ /**
31
+ * Properties for the Swipe component, which allows swipeable actions on an item.
32
+ */
11
33
  export type SwipeProps = {
34
+ /**
35
+ * Optional. An array of `SwipeAction` items representing the actions available
36
+ * on swiping to the left. Each action is rendered as a button on the swipeable surface.
37
+ */
12
38
  leftActions?: SwipeAction[];
39
+
40
+ /**
41
+ * Optional. An array of `SwipeAction` items representing the actions available
42
+ * on swiping to the right. Each action is rendered as a button on the swipeable surface.
43
+ */
13
44
  rightActions?: SwipeAction[];
45
+
46
+ /**
47
+ * Optional. A callback function triggered when the swipeable surface is
48
+ * fully opened on the left side.
49
+ */
14
50
  onSwipeableLeftOpen?: () => void;
51
+
52
+ /**
53
+ * Optional. A callback function triggered when the swipeable surface is
54
+ * fully opened on the right side.
55
+ */
15
56
  onSwipeableRightOpen?: () => void;
57
+
58
+ /**
59
+ * Optional. A callback function triggered when the swipeable surface is
60
+ * fully opened, either from the left or the right.
61
+ */
16
62
  onSwipeableOpen?: () => void;
63
+
64
+ /**
65
+ * Optional. A callback function triggered when the swipeable surface has
66
+ * returned to its original position and is closed.
67
+ */
17
68
  onSwipeableClose?: () => void;
69
+
70
+ /**
71
+ * Optional. A callback function triggered when the swipeable surface is
72
+ * about to open on the left side.
73
+ */
18
74
  onSwipeableLeftWillOpen?: () => void;
75
+
76
+ /**
77
+ * Optional. A callback function triggered when the swipeable surface is
78
+ * about to open on the right side.
79
+ */
19
80
  onSwipeableRightWillOpen?: () => void;
81
+
82
+ /**
83
+ * Optional. A callback function triggered when the swipeable surface is
84
+ * about to open, either from the left or the right.
85
+ */
20
86
  onSwipeableWillOpen?: () => void;
87
+
88
+ /**
89
+ * Optional. A callback function triggered when the swipeable surface is
90
+ * about to close and return to its original position.
91
+ */
21
92
  onSwipeableWillClose?: () => void;
22
- ref?: Ref<Swipeable>;
93
+ };
94
+
95
+ export type SwipeableRef = {
96
+ openLeft: () => void;
97
+ openRight: () => void;
98
+ close: () => void;
23
99
  };