Last active
May 30, 2024 14:12
-
-
Save tronghieu60s/187cc7b982b25d4817331d684af538c5 to your computer and use it in GitHub Desktop.
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
/** | |
* Design belike this video: | |
* https://youtu.be/zM3l9jpt5PU | |
*/ | |
import React, {useEffect} from 'react'; | |
import {Animated, Dimensions, Easing, StyleSheet, View as DefaultView} from 'react-native'; | |
import LinearGradient from 'react-native-linear-gradient'; | |
const width = Dimensions.get('window').width; | |
const AnimatedNewLG = Animated.createAnimatedComponent(LinearGradient); | |
type Props = { | |
style?: DefaultView['props']['style']; | |
speed?: number; | |
backgroundColor?: string; | |
highlightColor?: string; | |
}; | |
export default function Skeleton(props: Props) { | |
const { | |
style = {}, | |
speed = 1000, | |
backgroundColor = '#E1E9EE', | |
highlightColor = '#F2F8FC', | |
} = props; | |
const animatedValue = new Animated.Value(0); | |
useEffect(() => { | |
let currentSpeed = speed; | |
if (speed < 300) { | |
currentSpeed = 300; | |
} | |
Animated.loop( | |
Animated.timing(animatedValue, { | |
toValue: 1, | |
duration: currentSpeed, | |
easing: Easing.linear, | |
useNativeDriver: true, | |
}), | |
).start(); | |
}); | |
const translateX = animatedValue.interpolate({ | |
inputRange: [0, 1], | |
outputRange: [0, width], | |
}); | |
return ( | |
<DefaultView style={[{overflow: 'hidden', backgroundColor}, style]}> | |
<AnimatedNewLG | |
colors={[ | |
backgroundColor, | |
highlightColor, | |
backgroundColor, | |
highlightColor, | |
]} | |
start={{x: 0, y: 0}} | |
end={{x: 1, y: 0}} | |
style={{...StyleSheet.absoluteFillObject, transform: [{translateX}]}} | |
/> | |
</DefaultView> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Skeleton for OCD user 😒