-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
FlipCard.js
160 lines (145 loc) · 3.6 KB
/
FlipCard.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
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
import React from 'react';
import { Pressable, SafeAreaView, View, StyleSheet, Text } from 'react-native';
import Animated, {
interpolate,
useAnimatedStyle,
useSharedValue,
withTiming,
} from 'react-native-reanimated';
const RegularContent = () => {
return (
<View style={regularContentStyles.card}>
<Text>Regular content ✨</Text>
</View>
);
};
const regularContentStyles = StyleSheet.create({
card: {
flex: 1,
backgroundColor: '#b6cff7',
borderRadius: 16,
justifyContent: 'center',
alignItems: 'center',
},
});
const FlippedContent = () => {
return (
<View style={flippedContentStyles.card}>
<Text>Flipped content 🚀</Text>
</View>
);
};
const flippedContentStyles = StyleSheet.create({
card: {
flex: 1,
backgroundColor: '#baeee5',
borderRadius: 16,
justifyContent: 'center',
alignItems: 'center',
},
});
const FlipCard = ({
isFlipped,
cardStyle,
direction = 'y',
duration = 500,
RegularContent,
FlippedContent,
}) => {
const isDirectionX = direction === 'x';
const regularCardAnimatedStyle = useAnimatedStyle(() => {
const spinValue = interpolate(Number(isFlipped.value), [0, 1], [0, 180]);
const rotateValue = withTiming(`${spinValue}deg`, { duration });
return {
transform: [
isDirectionX ? { rotateX: rotateValue } : { rotateY: rotateValue },
],
};
});
const flippedCardAnimatedStyle = useAnimatedStyle(() => {
const spinValue = interpolate(Number(isFlipped.value), [0, 1], [180, 360]);
const rotateValue = withTiming(`${spinValue}deg`, { duration });
return {
transform: [
isDirectionX ? { rotateX: rotateValue } : { rotateY: rotateValue },
],
};
});
return (
<View>
<Animated.View
style={[
flipCardStyles.regularCard,
cardStyle,
regularCardAnimatedStyle,
]}>
{RegularContent}
</Animated.View>
<Animated.View
style={[
flipCardStyles.flippedCard,
cardStyle,
flippedCardAnimatedStyle,
]}>
{FlippedContent}
</Animated.View>
</View>
);
};
const flipCardStyles = StyleSheet.create({
regularCard: {
position: 'absolute',
zIndex: 1,
},
flippedCard: {
backfaceVisibility: 'hidden',
zIndex: 2,
},
});
export default function App() {
const isFlipped = useSharedValue(false);
const handlePress = () => {
isFlipped.value = !isFlipped.value;
};
return (
<SafeAreaView style={styles.container}>
<FlipCard
isFlipped={isFlipped}
cardStyle={styles.flipCard}
FlippedContent={<FlippedContent />}
RegularContent={<RegularContent />}
/>
<View style={styles.buttonContainer}>
<Pressable style={styles.toggleButton} onPress={handlePress}>
<Text style={styles.toggleButtonText}>Toggle card</Text>
</Pressable>
</View>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
height: 300,
alignItems: 'center',
justifyContent: 'center',
},
buttonContainer: {
marginTop: 16,
justifyContent: 'center',
alignItems: 'center',
},
toggleButton: {
backgroundColor: '#b58df1',
padding: 12,
borderRadius: 48,
},
toggleButtonText: {
color: '#fff',
textAlign: 'center',
},
flipCard: {
width: 150,
height: 200,
},
});