Created
May 19, 2021 17:24
-
-
Save enzomanuelmangano/2fd93f3c6e24cf1cdd634075aa0c5970 to your computer and use it in GitHub Desktop.
Final code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from 'react'; | |
import { Dimensions, StyleSheet } from 'react-native'; | |
import { Switch } from 'react-native-gesture-handler'; | |
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 = useDerivedValue(() => { | |
return withTiming(theme === 'dark' ? 1 : 0); | |
}); | |
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, | |
alignItems: 'center', | |
justifyContent: 'center', | |
}, | |
circle: { | |
width: SIZE, | |
height: SIZE, | |
borderRadius: SIZE / 2, | |
alignItems: 'center', | |
justifyContent: 'center', | |
shadowOpacity: 0.05, | |
shadowRadius: 15, | |
shadowOffset: { | |
height: 20, | |
width: 0, | |
}, | |
shadowColor: 'black', | |
backgroundColor: '#fff', | |
elevation: 8, | |
}, | |
text: { | |
fontSize: 70, | |
fontWeight: '700', | |
letterSpacing: 14, | |
textTransform: 'uppercase', | |
marginBottom: 35, | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment