-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathSwipeButton.js
More file actions
167 lines (154 loc) · 4.21 KB
/
SwipeButton.js
File metadata and controls
167 lines (154 loc) · 4.21 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import React from 'react';
import {StyleSheet} from 'react-native';
import LinearGradient from 'react-native-linear-gradient';
import {PanGestureHandler} from 'react-native-gesture-handler';
import Animated, {
useAnimatedGestureHandler,
useSharedValue,
useAnimatedStyle,
withSpring,
interpolate,
Extrapolate,
interpolateColor,
runOnJS,
} from 'react-native-reanimated';
import {useState} from 'react';
const BUTTON_WIDTH = 350;
const BUTTON_HEIGHT = 100;
const BUTTON_PADDING = 10;
const SWIPEABLE_DIMENSIONS = BUTTON_HEIGHT - 2 * BUTTON_PADDING;
const H_WAVE_RANGE = SWIPEABLE_DIMENSIONS + 2 * BUTTON_PADDING;
const H_SWIPE_RANGE = BUTTON_WIDTH - 2 * BUTTON_PADDING - SWIPEABLE_DIMENSIONS;
const AnimatedLinearGradient = Animated.createAnimatedComponent(LinearGradient);
const SwipeButton = ({onToggle}) => {
// Animated value for X translation
const X = useSharedValue(0);
// Toggled State
const [toggled, setToggled] = useState(false);
// Fires when animation ends
const handleComplete = (isToggled) => {
if (isToggled !== toggled) {
setToggled(isToggled);
onToggle(isToggled);
}
};
// Gesture Handler Events
const animatedGestureHandler = useAnimatedGestureHandler({
onStart: (_, ctx) => {
ctx.completed = toggled;
},
onActive: (e, ctx) => {
let newValue;
if (ctx.completed) {
newValue = H_SWIPE_RANGE + e.translationX;
} else {
newValue = e.translationX;
}
if (newValue >= 0 && newValue <= H_SWIPE_RANGE) {
X.value = newValue;
}
},
onEnd: () => {
if (X.value < BUTTON_WIDTH / 2 - SWIPEABLE_DIMENSIONS / 2) {
X.value = withSpring(0);
runOnJS(handleComplete)(false);
} else {
X.value = withSpring(H_SWIPE_RANGE);
runOnJS(handleComplete)(true);
}
},
});
const InterpolateXInput = [0, H_SWIPE_RANGE];
const AnimatedStyles = {
swipeCont: useAnimatedStyle(() => {
return {};
}),
colorWave: useAnimatedStyle(() => {
return {
width: H_WAVE_RANGE + X.value,
opacity: interpolate(X.value, InterpolateXInput, [0, 1]),
};
}),
swipeable: useAnimatedStyle(() => {
return {
backgroundColor: interpolateColor(
X.value,
[0, BUTTON_WIDTH - SWIPEABLE_DIMENSIONS - BUTTON_PADDING],
['#06d6a0', '#fff'],
),
transform: [{translateX: X.value}],
};
}),
swipeText: useAnimatedStyle(() => {
return {
opacity: interpolate(
X.value,
InterpolateXInput,
[0.7, 0],
Extrapolate.CLAMP,
),
transform: [
{
translateX: interpolate(
X.value,
InterpolateXInput,
[0, BUTTON_WIDTH / 2 - SWIPEABLE_DIMENSIONS],
Extrapolate.CLAMP,
),
},
],
};
}),
};
return (
<Animated.View style={[styles.swipeCont, AnimatedStyles.swipeCont]}>
<AnimatedLinearGradient
style={[AnimatedStyles.colorWave, styles.colorWave]}
colors={['#06d6a0', '#1b9aaa']}
start={{x: 0.0, y: 0.5}}
end={{x: 1, y: 0.5}}
/>
<PanGestureHandler onGestureEvent={animatedGestureHandler}>
<Animated.View style={[styles.swipeable, AnimatedStyles.swipeable]} />
</PanGestureHandler>
<Animated.Text style={[styles.swipeText, AnimatedStyles.swipeText]}>
Swipe Me
</Animated.Text>
</Animated.View>
);
};
const styles = StyleSheet.create({
swipeCont: {
height: BUTTON_HEIGHT,
width: BUTTON_WIDTH,
backgroundColor: '#fff',
borderRadius: BUTTON_HEIGHT,
padding: BUTTON_PADDING,
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
flexDirection: 'row',
},
colorWave: {
position: 'absolute',
left: 0,
height: BUTTON_HEIGHT,
borderRadius: BUTTON_HEIGHT,
},
swipeable: {
position: 'absolute',
left: BUTTON_PADDING,
height: SWIPEABLE_DIMENSIONS,
width: SWIPEABLE_DIMENSIONS,
borderRadius: SWIPEABLE_DIMENSIONS,
zIndex: 3,
},
swipeText: {
alignSelf: 'center',
fontSize: 20,
fontWeight: 'bold',
zIndex: 2,
color: '#1b9aaa',
},
});
export default SwipeButton;