-
Notifications
You must be signed in to change notification settings - Fork 1
/
FloatingChatHead.js
84 lines (79 loc) · 2.03 KB
/
FloatingChatHead.js
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
import React from 'react';
import {
View,
Text,
StyleSheet,
Animated,
PanResponder,
Dimensions,
} from 'react-native';
const {height, width} = Dimensions.get('window');
const index = () => {
const pan = React.useRef(new Animated.ValueXY()).current;
const scale = React.useRef(new Animated.Value(1)).current;
const panResponder = React.useRef(
PanResponder.create({
onMoveShouldSetPanResponder: () => true,
onPanResponderGrant: () => {
pan.setOffset({
x: pan.x._value,
y: pan.y._value,
});
pan.setValue({x: 0, y: 0});
Animated.spring(scale, {
toValue: 1.2,
friction: 2,
tension: 100,
useNativeDriver: true,
}).start();
},
onPanResponderMove: Animated.event([null, {dx: pan.x, dy: pan.y}], {
useNativeDriver: false,
}),
onPanResponderRelease: (_, gesture) => {
if (gesture.moveY - 30 < 0 || gesture.moveY + 60 > height) {
pan.y.setValue(0);
}
pan.flattenOffset();
if (gesture.moveX > width / 2) pan.x.setValue(0);
else pan.x.setValue(-width + 60 + 60 / 1.25);
Animated.spring(scale, {
toValue: 1,
friction: 2,
tension: 100,
useNativeDriver: true,
}).start();
},
}),
).current;
return (
<View style={styles.container}>
<Animated.View
style={{
transform: [...pan.getTranslateTransform(), {scale}],
}}
{...panResponder.panHandlers}>
<View
style={{
width: 60,
height: 60,
borderRadius: 30,
backgroundColor: 'rgba(0,0,0,.5)',
alignItems: 'center',
justifyContent: 'center',
}}>
<Text style={{color: 'white', fontSize: 35}}>+</Text>
</View>
</Animated.View>
</View>
);
};
export default index;
const styles = StyleSheet.create({
container: {
flex: 1,
position: 'absolute',
bottom: 25,
right: 25,
},
});