Last active
May 19, 2021 16:58
-
-
Save enzomanuelmangano/b9a409fe6171c4c073d13bd77315b080 to your computer and use it in GitHub Desktop.
We are animating the backgroundColor
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 { 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, | |
}; | |
}); | |
return ( | |
<Animated.View style={[styles.container, rStyle]}> | |
<Switch | |
value={theme === 'dark'} | |
onValueChange={(toggled) => { | |
setTheme(toggled ? 'dark' : 'light'); | |
}} | |
trackColor={SWITCH_TRACK_COLOR} | |
thumbColor={'violet'} | |
/> | |
</Animated.View> | |
); | |
} | |
const styles = StyleSheet.create({ | |
container: { | |
flex: 1, | |
alignItems: 'center', | |
justifyContent: 'center', | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment