-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShimmerEffect.tsx
170 lines (161 loc) · 4.26 KB
/
ShimmerEffect.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import React, {FC, useEffect} from 'react';
import {Image, StyleSheet, TextStyle, View, ViewStyle} from 'react-native';
import LinearGradient from 'react-native-linear-gradient';
import Animated, {
useSharedValue,
useAnimatedStyle,
withRepeat,
withTiming,
interpolate,
Extrapolation,
} from 'react-native-reanimated';
import {moderateScale, width} from './src/constants/Utils';
import Images from './src/constants/Images';
type Props = {
style?: ViewStyle | TextStyle;
children?: React.ReactNode;
};
export const ShimmerEffect: FC<Props> = ({style, children}) => {
const translateX = useSharedValue(-width);
// Infinite shimmer animation
useEffect(() => {
translateX.value = withRepeat(
withTiming(width, {duration: 1500}),
-1, // Infinite repetition
true, // Alternate (so it resets smoothlby)
);
}, [translateX]);
// Shimmer animated style
const shimmerAnimatedStyle = useAnimatedStyle(() => {
return {
transform: [{translateX: translateX.value}],
};
});
return (
<View style={[styles.shimmerWrapper, style]}>
<Animated.View style={[styles.shimmer, shimmerAnimatedStyle]}>
<LinearGradient
colors={[
'rgba(255,255,255,0.1)',
'rgba(255,255,255,0.5)',
'rgba(255,255,255,0)',
]}
style={styles.gradient}
start={{x: 0, y: 0.5}}
end={{x: 1, y: 0.5}}></LinearGradient>
</Animated.View>
</View>
);
};
const InstagramShimmerLoader = () => {
return (
<View style={styles.container}>
{/* Simulate Instagram Post Loader */}
<View style={styles.postContainer}>
{/* Profile Picture */}
<ShimmerEffect style={styles.profileCircle} />
{/* Username and time */}
<View style={styles.textContainer}>
<ShimmerEffect style={styles.username} />
<ShimmerEffect style={styles.time} />
</View>
</View>
{/* Simulate Image Post Loader */}
<View style={styles.postImage}>
<Image
source={Images.Skip}
style={{
position: 'absolute',
zIndex: 1,
}}
/>
<ShimmerEffect style={styles.postImage} />
</View>
{/* Simulate Caption Loader */}
<ShimmerEffect style={styles.captionLine1} />
<ShimmerEffect style={styles.captionLine2} />
{/* Simulate Like and Comment Section Loader */}
<View style={styles.likeCommentContainer}>
<ShimmerEffect style={styles.likeBar} />
<ShimmerEffect style={styles.commentBar} />
</View>
</View>
);
};
export default InstagramShimmerLoader;
const styles = StyleSheet.create({
container: {
flex: 1,
paddingHorizontal: moderateScale(16),
paddingTop: moderateScale(100),
backgroundColor: '#fff',
},
shimmerWrapper: {
overflow: 'hidden',
backgroundColor: '#e0e0e0',
},
shimmer: {
...StyleSheet.absoluteFillObject,
},
gradient: {
width: moderateScale(150),
height: '100%',
},
postContainer: {
flexDirection: 'row',
alignItems: 'center',
marginBottom: moderateScale(10),
},
profileCircle: {
width: moderateScale(50),
height: moderateScale(50),
borderRadius: moderateScale(25),
},
textContainer: {
flex: 1,
marginLeft: moderateScale(10),
},
username: {
width: moderateScale(120),
height: moderateScale(15),
borderRadius: moderateScale(8),
marginBottom: moderateScale(6),
},
time: {
width: moderateScale(80),
height: moderateScale(12),
borderRadius: moderateScale(8),
},
postImage: {
width: '100%',
height: moderateScale(250),
borderRadius: moderateScale(8),
marginBottom: moderateScale(10),
},
captionLine1: {
width: '80%',
height: moderateScale(12),
borderRadius: moderateScale(8),
marginBottom: moderateScale(6),
},
captionLine2: {
width: '60%',
height: moderateScale(12),
borderRadius: moderateScale(8),
marginBottom: moderateScale(10),
},
likeCommentContainer: {
flexDirection: 'row',
justifyContent: 'space-between',
},
likeBar: {
width: '40%',
height: moderateScale(12),
borderRadius: moderateScale(8),
},
commentBar: {
width: '40%',
height: moderateScale(12),
borderRadius: moderateScale(8),
},
});