-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathTile.tsx
More file actions
98 lines (88 loc) · 2.02 KB
/
Tile.tsx
File metadata and controls
98 lines (88 loc) · 2.02 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
import React, { useEffect, useRef } from 'react';
import { StyleSheet } from 'react-native';
import Animated, {
Easing,
interpolate,
useAnimatedStyle,
useSharedValue,
withRepeat,
withTiming,
} from 'react-native-reanimated';
export interface ColorTile {
id: string;
color: string;
}
interface TileProps {
data: ColorTile;
draggingActive: boolean;
tileSize: number;
isActive: boolean;
}
function Tile({ data, draggingActive, tileSize, isActive }: TileProps) {
const shakeAnimation = useSharedValue(0);
const tileScale = useSharedValue(1);
const draggingActivePrevious = useRef(draggingActive);
const animateOnDraggingActive = () => {
shakeAnimation.value = withRepeat(
withTiming(1, {
duration: 150,
easing: Easing.linear,
}),
-1
);
tileScale.value = withTiming(isActive ? 1.1 : 0.9, {
duration: 100,
});
};
const animateOnDraggingDisabled = () => {
tileScale.value = withTiming(1, {
duration: 100,
});
shakeAnimation.value = 0;
};
useEffect(() => {
if (draggingActive && !draggingActivePrevious.current) {
animateOnDraggingActive();
}
if (!draggingActive && draggingActivePrevious.current) {
animateOnDraggingDisabled();
}
draggingActivePrevious.current = draggingActive;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [draggingActive]);
const tileAnimatedStyle = useAnimatedStyle(() => {
return {
transform: [
{
rotateZ: `${interpolate(
shakeAnimation.value,
[0, 0.3, 0.7, 1],
[0, -3, 3, 0]
)}deg`,
},
{
scale: tileScale.value,
},
],
};
});
return (
<Animated.View
style={[
styles.tile,
{
backgroundColor: data.color,
width: tileSize,
height: tileSize,
},
tileAnimatedStyle,
]}
/>
);
}
const styles = StyleSheet.create({
tile: {
borderRadius: 16,
},
});
export default Tile;