forked from enzomanuelmangano/animate-with-reanimated
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
117 lines (103 loc) · 2.57 KB
/
App.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
import { StatusBar } from 'expo-status-bar';
import React, { useState } from 'react';
import { Dimensions, StyleSheet, Switch, Text, View } from 'react-native';
import Animated, {
interpolateColor,
useAnimatedStyle,
useDerivedValue,
withTiming,
} from 'react-native-reanimated';
const Colors = {
dark: {
background: '#1E1E1E',
circle: '#252525',
text: '#F8F8F8',
},
light: {
background: '#F8F8F8',
circle: '#FFF',
text: '#1E1E1E',
},
};
const SWITCH_TRACK_COLOR = {
true: 'rgba(256, 0, 256, 0.2)',
false: 'rgba(0,0,0,0.1)',
};
type Theme = 'light' | 'dark';
export default function App() {
const [theme, setTheme] = useState<Theme>('light');
// const progress = useSharedValue(0);
const progress = useDerivedValue(() => {
return theme === 'dark' ? withTiming(1) : withTiming(0);
}, [theme]);
const rStyle = useAnimatedStyle(() => {
const backgroundColor = interpolateColor(
progress.value,
[0, 1],
[Colors.light.background, Colors.dark.background]
);
return { backgroundColor };
});
const rCircleStyle = useAnimatedStyle(() => {
const backgroundColor = interpolateColor(
progress.value,
[0, 1],
[Colors.light.circle, Colors.dark.circle]
);
return { backgroundColor };
});
const rTextStyle = useAnimatedStyle(() => {
const color = interpolateColor(
progress.value,
[0, 1],
[Colors.light.text, Colors.dark.text]
);
return { color };
});
return (
<Animated.View style={[styles.container, rStyle]}>
<Animated.Text style={[styles.text, rTextStyle]}>Theme</Animated.Text>
<Animated.View style={[styles.circle, rCircleStyle]}>
<Switch
value={theme === 'dark'}
onValueChange={(toggled) => {
setTheme(toggled ? 'dark' : 'light');
}}
trackColor={SWITCH_TRACK_COLOR}
thumbColor={'violet'}
/>
</Animated.View>
</Animated.View>
);
}
const SIZE = Dimensions.get('window').width * 0.7;
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
circle: {
width: SIZE,
height: SIZE,
backgroundColor: '#FFF',
alignItems: 'center',
justifyContent: 'center',
borderRadius: SIZE / 2,
shadowOffset: {
width: 0,
height: 20,
},
shadowRadius: 10,
shadowOpacity: 0.1,
elevation: 8,
},
text: {
fontSize: 70,
textTransform: 'uppercase',
fontWeight: '700',
letterSpacing: 14,
marginBottom: 35,
},
});