Created
November 21, 2023 13:04
-
-
Save badalsaibo/c4a6950511e15f8767a65baa0fb3510f to your computer and use it in GitHub Desktop.
Reanimated Skeleton component
This file contains hidden or 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 { useEffect } from 'react'; | |
import { ViewStyle } from 'react-native'; | |
import Animated, { | |
interpolateColor, | |
useAnimatedStyle, | |
useSharedValue, | |
withRepeat, | |
withTiming, | |
} from 'react-native-reanimated'; | |
type SkeletonProps = { style?: ViewStyle; className?: string }; | |
const color1 = '#E1E9EE'; | |
const color2 = '#F2F8FC'; | |
const Skeleton = ({ style }: SkeletonProps) => { | |
const progress = useSharedValue(0); | |
useEffect(() => { | |
progress.value = withRepeat(withTiming(1, { duration: 1000 }), -1, true); | |
}, [progress]); | |
const animatedStyle = useAnimatedStyle(() => ({ | |
backgroundColor: interpolateColor(progress.value, [0, 1], [color1, color2]), | |
})); | |
return <Animated.View style={[style, animatedStyle]} className="h-10" />; | |
}; | |
export default Skeleton; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment