forked from willikay11/React-Native-Skeleton-Loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
171 lines (145 loc) · 4.62 KB
/
index.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
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
171
import React, { Component } from 'react';
import {
View,
Animated,
Easing,
} from 'react-native'
import PropTypes from 'prop-types'
const defaultRows = 1;
const defaultColor = '#dfdfdf';
const defaultHighlightColor = '#c8c8c8';
const defaultCircleSize = 100;
const defaultSquareSize = 100;
const defaultRectangleHeight = 15;
export default class Skeleton extends Component {
constructor () {
super();
this.state = {
fadeAnim: new Animated.Value(0),
}
this.fadeOut = this.fadeOut.bind(this);
this.fadeIn = this.fadeIn.bind(this);
}
componentDidMount () {
this.fadeIn();
}
fadeIn () {
Animated.timing(
this.state.fadeAnim,
{
toValue: 1,
duration: 1000,
easing: Easing.linear
}
).start(this.fadeOut);
}
fadeOut() {
this.setState({ fadeAnim: new Animated.Value(1) },
() => {
Animated.timing(
this.state.fadeAnim,
{
toValue: 0.2,
duration: 1000,
easing: Easing.linear
}
).start(this.fadeIn);
})
}
render () {
let { fadeAnim } = this.state;
let { type, size, color, highlightColor, height, rows, loading, children } = this.props;
if (type === 'square') {
return (
<Square
fadeAnim={fadeAnim}
loading={loading}
children={children}
color={color ? color : defaultColor}
highlightColor={highlightColor ? highlightColor : defaultHighlightColor}
size={size ? parseInt(size, 10) : defaultSquareSize}
/>
);
}
if (type === 'circle') {
return (
<Circle
fadeAnim={fadeAnim}
loading={loading}
children={children}
color={color ? color : defaultColor}
highlightColor={highlightColor ? highlightColor : defaultHighlightColor}
size={size ? parseInt(size, 10) : defaultCircleSize}
/>
);
}
if (type === 'rectangle') {
let rowCount = defaultRows;
if (rows > 0) {
rowCount = parseInt(rows);
}
return <Rectangle
fadeAnim={fadeAnim}
loading={loading}
children={children}
rows={rowCount}
color={color ? color : defaultColor}
highlightColor={highlightColor ? highlightColor : defaultHighlightColor}
height={height ? parseInt(height, 10) : defaultRectangleHeight}
/>
}
return null;
}
}
Square = (props) => {
if (props.loading) {
return (
<View style={{ backgroundColor: props.color, height: props.size, width: props.size }}>
<Animated.View style={{ opacity: props.fadeAnim }} >
<View style = {{ backgroundColor: props.highlightColor, height: props.size, width: props.size }}>
</View>
</Animated.View>
</View>
);
}
return props.children ? props.children : null;
}
const Rectangle = (props) => {
if(props.loading) {
let rectangles = [];
for (let i = 0; i < props.rows; i++) {
rectangles.push(
<View key={i} style={{ backgroundColor: props.color, marginLeft: 5, marginBottom: 10 }}>
<Animated.View style={{ opacity: props.fadeAnim }} >
<View style = {{ backgroundColor: props.highlightColor, height: props.height }}>
</View>
</Animated.View>
</View>
);
}
return rectangles;
}
return props.children ? props.children : null;
}
const Circle = (props) => {
if(props.loading) {
return (
<View style={{ backgroundColor: props.color, height: props.size, width: props.size, borderRadius: parseInt(props.size, 10)/2 }}>
<Animated.View style={{ opacity: props.fadeAnim }} >
<View style = {{ backgroundColor: props.highlightColor, height: props.size, width: props.size, borderRadius: parseInt(props.size, 10)/2 }}>
</View>
</Animated.View>
</View>
);
}
return props.children ? props.children : null;
}
Skeleton.propTypes = {
type: PropTypes.oneOf(['rectangle', 'square', 'circle']).isRequired,
loading: PropTypes.bool.isRequired,
size: PropTypes.number,
height: PropTypes.number,
color: PropTypes.string,
highlightColor: PropTypes.string,
rows: PropTypes.number,
}