-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathGmailStyleSwipeableRow.tsx
More file actions
119 lines (110 loc) · 3.07 KB
/
GmailStyleSwipeableRow.tsx
File metadata and controls
119 lines (110 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import React, { ReactNode, useRef } from 'react';
import { StyleSheet, I18nManager } from 'react-native';
import { RectButton } from 'react-native-gesture-handler';
import Animated, {
Extrapolation,
SharedValue,
interpolate,
useAnimatedStyle,
} from 'react-native-reanimated';
import Swipeable, {
SwipeableMethods,
} from 'react-native-gesture-handler/ReanimatedSwipeable';
interface LeftActionProps {
dragX: SharedValue<number>;
swipeableRef: React.RefObject<SwipeableMethods | null>;
}
const LeftAction = ({ dragX, swipeableRef }: LeftActionProps) => {
const animatedStyle = useAnimatedStyle(() => ({
transform: [
{
scale: interpolate(dragX.value, [0, 80], [0, 1], Extrapolation.CLAMP),
},
],
}));
return (
<RectButton
style={styles.leftAction}
onPress={() => swipeableRef.current!.close()}>
{/* Change it to some icons */}
<Animated.View style={[styles.actionIcon, animatedStyle]} />
</RectButton>
);
};
const renderLeftActions = (
_: any,
progress: SharedValue<number>,
swipeableRef: React.RefObject<SwipeableMethods | null>
) => <LeftAction dragX={progress} swipeableRef={swipeableRef} />;
interface RightActionProps {
dragX: SharedValue<number>;
swipeableRef: React.RefObject<SwipeableMethods | null>;
}
const RightAction = ({ dragX, swipeableRef }: RightActionProps) => {
const animatedStyle = useAnimatedStyle(() => ({
transform: [
{
scale: interpolate(dragX.value, [-80, 0], [1, 0], Extrapolation.CLAMP),
},
],
}));
return (
<RectButton
style={styles.rightAction}
onPress={() => swipeableRef.current!.close()}>
{/* Change it to some icons */}
<Animated.View style={[styles.actionIcon, animatedStyle]} />
</RectButton>
);
};
const renderRightActions = (
_: any,
progress: SharedValue<number>,
swipeableRef: React.RefObject<SwipeableMethods | null>
) => <RightAction dragX={progress} swipeableRef={swipeableRef} />;
interface GmailStyleSwipeableRowProps {
children?: ReactNode;
}
export default function GmailStyleSwipeableRow({
children,
}: GmailStyleSwipeableRowProps) {
const swipeableRow = useRef<SwipeableMethods>(null);
return (
<Swipeable
ref={swipeableRow}
friction={2}
leftThreshold={80}
enableTrackpadTwoFingerGesture
rightThreshold={40}
renderLeftActions={(_, progress) =>
renderLeftActions(_, progress, swipeableRow)
}
renderRightActions={(_, progress) =>
renderRightActions(_, progress, swipeableRow)
}>
{children}
</Swipeable>
);
}
const styles = StyleSheet.create({
leftAction: {
flex: 1,
backgroundColor: '#388e3c',
justifyContent: 'flex-end',
alignItems: 'center',
flexDirection: I18nManager.isRTL ? 'row' : 'row-reverse',
},
actionIcon: {
width: 30,
marginHorizontal: 10,
backgroundColor: 'plum',
height: 20,
},
rightAction: {
alignItems: 'center',
flexDirection: I18nManager.isRTL ? 'row-reverse' : 'row',
backgroundColor: '#dd2c00',
flex: 1,
justifyContent: 'flex-end',
},
});