-
-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathActivityIndicator.tsx
More file actions
73 lines (66 loc) · 1.69 KB
/
Copy pathActivityIndicator.tsx
File metadata and controls
73 lines (66 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import React, { useEffect } from 'react';
import { ViewStyle } from 'react-native';
import Animated, {
useAnimatedStyle,
useSharedValue,
withRepeat,
withTiming,
Easing,
cancelAnimation,
} from 'react-native-reanimated';
import Svg, { Circle } from 'react-native-svg';
interface ActivityIndicatorProps {
size?: number;
color?: string;
style?: ViewStyle;
}
const ActivityIndicator: React.FC<ActivityIndicatorProps> = ({
size = 32,
color = '#000000',
style,
}) => {
const rotation = useSharedValue(0);
useEffect(() => {
rotation.value = withRepeat(
withTiming(360, {
duration: 1000,
easing: Easing.linear,
}),
-1 // Infinite repeat
);
return () => cancelAnimation(rotation);
}, [rotation]);
const animatedStyle = useAnimatedStyle(() => {
return {
transform: [{ rotateZ: `${rotation.value}deg` }],
};
});
const strokeWidth = Math.max(2, size * 0.1);
const radius = (size - strokeWidth) / 2;
const circumference = 2 * Math.PI * radius;
// 75% filled means 0.75 * circumference is drawn, the rest is gap
const strokeDasharray = [circumference * 0.75, circumference];
return (
<Animated.View
style={[
{ width: size, height: size, justifyContent: 'center', alignItems: 'center' },
animatedStyle,
style,
]}
>
<Svg width={size} height={size}>
<Circle
cx={size / 2}
cy={size / 2}
r={radius}
stroke={color}
strokeWidth={strokeWidth}
strokeDasharray={strokeDasharray}
strokeLinecap="round"
fill="transparent"
/>
</Svg>
</Animated.View>
);
};
export default ActivityIndicator;