Last active
December 14, 2024 18:21
-
-
Save douglas-henrique/e9b61ed7603fff8232e0505f104789b9 to your computer and use it in GitHub Desktop.
Custom spinner on react native using Reanimated > 2x + Styled components
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 React, { useEffect } from 'react'; | |
import styled from 'styled-components/native'; | |
import Animated, { | |
cancelAnimation, | |
Easing, | |
useAnimatedStyle, | |
useSharedValue, | |
withRepeat, | |
withTiming, | |
} from 'react-native-reanimated'; | |
export const StyledSpinner = styled(Animated.View)` | |
height: 34px; | |
width: 34px; | |
border-radius: 90px; | |
border-width: 3px; | |
border-top-color: 'transparent'; | |
border-right-color: 'transparent'; | |
border-bottom-color:'transparent'; | |
border-left-color: 'red' | |
`; | |
export const ThemedLoadingSpinner: React.FC = () => { | |
const rotation = useSharedValue(0); | |
const animatedStyles = useAnimatedStyle(() => { | |
return { | |
transform: [ | |
{ | |
rotateZ: `${rotation.value}deg`, | |
}, | |
], | |
}; | |
}, [rotation.value]); | |
useEffect(() => { | |
rotation.value = withRepeat( | |
withTiming(360, { | |
duration: 1000, | |
easing: Easing.linear, | |
}), | |
200 | |
); | |
return () => cancelAnimation(rotation); | |
}, []); | |
return <StyledSpinner style={[animatedStyles]} />; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment