-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.tsx
92 lines (74 loc) · 2.39 KB
/
index.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import React from "react";
import { View, StyleSheet } from "react-native";
import MaskedView from "@react-native-masked-view/masked-view";
import { LinearGradient } from "react-native-linear-gradient";
import Reanimated,{ useSharedValue, withRepeat, useAnimatedStyle, withTiming, interpolate } from "react-native-reanimated";
interface SkeletonProps {
/**
* background of the loader componenet hexcode
*/
background: string,
/**
* highlight color of the loader component hexcode
*/
highlight: string
}
const SkeletonLoading: React.FC<SkeletonProps> = ({
children,
background,
highlight
}) => {
const [layout, setLayout] = React.useState();
const shared = useSharedValue(0);
const animStyle = useAnimatedStyle(() => {
const x = interpolate( shared.value, [0, 1], [layout ? -layout.width : 0, layout ? layout.width : 0], )
return {
transform: [ { translateX: x }, ]
}
});
React.useEffect(() => {
shared.value = withRepeat(
withTiming(1, { duration: 1000 }),
Infinity,
);
}, []);
if (!layout) {
return (
<View onLayout={event => setLayout(event.nativeEvent.layout)}>
{children}
</View>
);
}
return(
<MaskedView
maskElement={children}
style={{ width: layout.width, height: layout.height }}
>
<View style={[styles.container, { backgroundColor: background }]} />
<Reanimated.View
style={[StyleSheet.absoluteFill, animStyle]}
>
<MaskedView
style={StyleSheet.absoluteFill}
maskElement={
<LinearGradient
start={{ x: 0, y: 0 }}
end={{ x: 1, y: 0 }}
style={StyleSheet.absoluteFill}
colors={['transparent', 'black', 'transparent']}
/>
}
>
<View style={[ StyleSheet.absoluteFill, { backgroundColor: highlight } ]} />
</MaskedView>
</Reanimated.View>
</MaskedView>
)
}
export default SkeletonLoading;
const styles = StyleSheet.create({
container: {
flexGrow: 1,
overflow: 'hidden'
}
})