-
Notifications
You must be signed in to change notification settings - Fork 0
/
Loading.tsx
71 lines (62 loc) · 1.63 KB
/
Loading.tsx
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
import React from 'react';
import { Animated, Easing, StyleProp, ViewStyle } from 'react-native';
import { useThemeContext } from '../contexts/ThemeContext';
import { getScaleFactor } from '../styles/createScaleFactor';
import { LocalIcon, LocalIconName } from './Icon';
type LoadingProps = {
name?: LocalIconName;
size?: number;
color?: string;
style?: StyleProp<ViewStyle>;
};
const sf = getScaleFactor();
export default function Loading({
name = 'loading',
size = sf(24),
color,
style,
}: LoadingProps): JSX.Element {
const { colors } = useThemeContext();
return (
<Rotate style={style}>
<LocalIcon name={name} size={size} color={color ?? colors.primary} />
</Rotate>
);
}
const useLoopAnimated = (duration: number, useNativeDriver = true) => {
const animated = React.useRef(new Animated.Value(0)).current;
React.useEffect(() => {
Animated.loop(
Animated.timing(animated, {
toValue: 1,
duration,
useNativeDriver,
easing: Easing.inOut(Easing.linear),
}),
{ resetBeforeIteration: true }
).start();
return () => {
animated.stopAnimation();
animated.setValue(0);
};
}, [animated, duration, useNativeDriver]);
return animated;
};
const Rotate = ({
children,
style,
}: React.PropsWithChildren<{ style: StyleProp<ViewStyle> }>) => {
const loop = useLoopAnimated(1000);
const rotate = loop.interpolate({
inputRange: [0, 1],
outputRange: ['0deg', '360deg'],
});
return (
<Animated.View
pointerEvents="none"
style={[style, { transform: [{ rotate }] }]}
>
{children}
</Animated.View>
);
};