-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathChatHeadsExample.tsx
183 lines (169 loc) · 4.36 KB
/
ChatHeadsExample.tsx
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import React from 'react';
import { View, Dimensions, StyleSheet } from 'react-native';
import Animated, {
useSharedValue,
useAnimatedStyle,
useAnimatedGestureHandler,
useDerivedValue,
withSpring,
} from 'react-native-reanimated';
import {
PanGestureHandler,
PanGestureHandlerGestureEvent,
} from 'react-native-gesture-handler';
const { width: windowWidth, height: windowHeight } = Dimensions.get('window');
function ChatHeads({
children,
}: React.PropsWithChildren<Record<never, never>>) {
const transX = useSharedValue(0);
const transY = useSharedValue(0);
type AnimatedGHContext = {
startX: number;
startY: number;
};
const gestureHandler = useAnimatedGestureHandler<
PanGestureHandlerGestureEvent,
AnimatedGHContext
>({
onStart: (_, ctx) => {
ctx.startX = transX.value;
ctx.startY = transY.value;
},
onActive: (event, ctx) => {
transX.value = ctx.startX + event.translationX;
transY.value = ctx.startY + event.translationY;
},
onEnd: (event) => {
const width = windowWidth - 100 - 40; // minus margins & width
const height = windowHeight - 100 - 40; // minus margins & height
const toss = 0.2;
function clamp(value: number, min: number, max: number) {
return Math.min(Math.max(value, min), max);
}
const targetX = clamp(transX.value + toss * event.velocityX, 0, width);
const targetY = clamp(transY.value + toss * event.velocityY, 0, height);
// return;
const top = targetY;
const bottom = height - targetY;
const left = targetX;
const right = width - targetX;
const minDistance = Math.min(top, bottom, left, right);
let snapX = targetX;
let snapY = targetY;
switch (minDistance) {
case top:
snapY = 0;
break;
case bottom:
snapY = height;
break;
case left:
snapX = 0;
break;
case right:
snapX = width;
break;
}
transX.value = withSpring(snapX, {
velocity: event.velocityX,
});
transY.value = withSpring(snapY, {
velocity: event.velocityY,
});
},
});
const stylez = useAnimatedStyle(() => {
return {
transform: [
{
translateX: transX.value,
},
{
translateY: transY.value,
},
],
};
});
const childrenArray = React.Children.toArray(children);
return (
<>
{childrenArray.length > 1 && (
<Followers
children={childrenArray.slice(1)}
transX={transX}
transY={transY}
/>
)}
<PanGestureHandler onGestureEvent={gestureHandler}>
<Animated.View style={[styles.headContainer, stylez]}>
{childrenArray[0]}
</Animated.View>
</PanGestureHandler>
</>
);
}
type FollowersProps = {
readonly transX: Animated.SharedValue<number>;
readonly transY: Animated.SharedValue<number>;
};
function Followers({
transX,
transY,
children,
}: React.PropsWithChildren<FollowersProps>) {
const myTransX = useDerivedValue(() => {
return withSpring(transX.value);
});
const myTransY = useDerivedValue(() => {
return withSpring(transY.value);
});
const stylez = useAnimatedStyle(() => {
return {
transform: [
{
translateX: myTransX.value,
},
{
translateY: myTransY.value,
},
],
};
});
const childrenArray = React.Children.toArray(children);
return (
<>
{childrenArray.length > 1 && (
<Followers
children={childrenArray.slice(1)}
transX={myTransX}
transY={myTransY}
/>
)}
<Animated.View style={[styles.headContainer, stylez]}>
{childrenArray[0]}
</Animated.View>
</>
);
}
function Main(): React.ReactElement {
return (
<View style={{ flex: 1, margin: 50 }}>
<ChatHeads>
<View style={[styles.head, { backgroundColor: 'black' }]} />
<View style={[styles.head, { backgroundColor: 'blue' }]} />
<View style={[styles.head, { backgroundColor: 'green' }]} />
<View style={[styles.head, { backgroundColor: 'yellow' }]} />
</ChatHeads>
</View>
);
}
const styles = StyleSheet.create({
head: {
width: 40,
height: 40,
},
headContainer: {
position: 'absolute',
},
});
export default Main;