-
Notifications
You must be signed in to change notification settings - Fork 41
/
Shimmer.js
145 lines (137 loc) · 4.6 KB
/
Shimmer.js
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
import React, { Component } from 'react';
import PropTypes from 'prop-types'
import { View, StyleSheet, Animated, Platform } from 'react-native';
import LinearGradient from 'react-native-linear-gradient'
class CustomLinearGradient extends Component {
render() {
const { locationStart, colorShimmer, widthShimmer } = this.props;
return (
<LinearGradient
colors={colorShimmer}
style={{ flex: 1 }}
start={{ x: -1, y: 0.5 }}
end={{ x: 2, y: 0.5 }}
locations={[locationStart + widthShimmer, locationStart + 0.5 + widthShimmer / 2, locationStart + 1]}
/>
);
}
}
CustomLinearGradient.propTypes = {
locationStart: PropTypes.any,
colorShimmer: PropTypes.array,
widthShimmer: PropTypes.number,
};
Animated.LinearGradient = Animated.createAnimatedComponent(CustomLinearGradient);
class Shimmer extends Component {
constructor(props) {
super(props);
this.state = {
visible: false,
beginShimmerPosition: new Animated.Value(-1),
};
}
componentDidMount() {
const { autoRun } = this.props;
if (autoRun) {
this.loopAnimated();
}
}
loopAnimated() {
const shimmerAnimated = this.getAnimated();
const { visible } = this.props;
shimmerAnimated.start(() => {
if (!visible) {
this.loopAnimated();
}
})
}
getAnimated = () => {
this.state.beginShimmerPosition.setValue(-1);
return Animated.timing(this.state.beginShimmerPosition, {
toValue: 1,
duration: this.props.duration,
});
}
render() {
const { width, reverse, height, colorShimmer, style, widthShimmer, children, visible, backgroundColorBehindBorder, hasBorder } = this.props;
let beginPostioner = -0.7;
let endPosition = 0.7;
if (reverse) {
beginPostioner = 0.7;
endPosition = -0.7;
}
const newValue = this.state.beginShimmerPosition.interpolate({
inputRange: [-1, 1],
outputRange: [beginPostioner, endPosition],
});
return (
<View style={!visible
? [{ height, width }, styles.container, style]
: []
}
>
{!visible
? (
<View style={{ flex: 1 }}>
<Animated.LinearGradient
locationStart={newValue}
colorShimmer={colorShimmer}
widthShimmer={widthShimmer}
/>
<View style={{ width: 0, height: 0 }}>
{this.props.children}
</View>
{((style && style.borderRadius) || hasBorder) && Platform.OS === 'android'
? <View style={{
position: 'absolute',
top: -40,
bottom: -40,
right: -40,
left: -40,
borderRadius: width / 2 + 40 / 2,
borderWidth: 40,
borderColor: backgroundColorBehindBorder,
}}
/>
: null}
</View>
)
: children
}
</View>
);
}
}
Shimmer.defaultProps = {
width: 200,
height: 15,
widthShimmer: 0.7,
duration: 1000,
colorShimmer: ['#ebebeb', '#c5c5c5', '#ebebeb'],
reverse: false,
autoRun: false,
visible: false,
backgroundColorBehindBorder: 'white',
hasBorder: false,
};
const styles = StyleSheet.create({
container: {
overflow: 'hidden',
},
});
Shimmer.propTypes = {
width: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
height: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
widthShimmer: PropTypes.number,
duration: PropTypes.number,
colorShimmer: PropTypes.array,
reverse: PropTypes.bool,
autoRun: PropTypes.bool,
visible: PropTypes.bool,
children: PropTypes.any,
style: PropTypes.any,
backgroundColor: PropTypes.string,
backgroundColorBehindBorder: PropTypes.string,
hasBorder: PropTypes.bool,
};
export default Shimmer;