-
Notifications
You must be signed in to change notification settings - Fork 1
/
Loader.jsx
169 lines (156 loc) · 3.63 KB
/
Loader.jsx
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
/**
* TEAM: frontend_infra
* WATCHERS: notandrewkaye
* @flow strict
*/
import * as React from "react";
import {css, StyleSheet} from "aphrodite";
import latitudeColors from "./colors";
type Props = {|
/** The content that is loading */
+children?: React.Node,
/** The loader will take care of the show hide logic. Pass your loading conditional here and wrap your data with this loader. */
+loaded: boolean,
/** Whether the loader spans the full width of it's container */
+isFullWidth?: boolean,
/** Optionally specify a size for the loader. By default the loader is 50px. */
+size?: number,
/** Deprecated! Do not use the overlay prop. */
+overlay?: boolean,
|};
const LOADER_SIZE = 50;
const rotateKeyframes = {
"100%": {
transform: "rotate(360deg)",
},
};
const styles = StyleSheet.create({
center: {
display: "block",
marginLeft: "auto",
marginRight: "auto",
},
circularWrapper: {
position: "relative",
display: "inline-block",
},
loader: {
animationName: rotateKeyframes,
animationDuration: "2s",
animationTimingFunction: "linear",
animationIterationCount: "infinite",
transformOrigin: "center center",
position: "absolute",
width: "100%",
height: "100%",
top: 0,
bottom: 0,
left: 0,
right: 0,
margin: "auto",
},
overlayContainer: {
position: "relative",
},
overlayElement: {
top: 0,
right: 0,
bottom: 0,
left: 0,
backgroundColor: "rgba(255,255,255,0.78)",
},
fullWidthContainer: {
width: "100%",
},
});
type KeyframeColors = {|
+light: string,
+medium: string,
+dark: string,
|};
const cyclePrimaryKeyframes = ({light, medium, dark}: KeyframeColors) => ({
"100%, 0%": {
stroke: medium,
},
"40%": {
stroke: dark,
},
"66%": {
stroke: light,
},
"80%, 90%": {
stroke: dark,
},
});
const indefiniteKeyframes = {
"0%": {
strokeDasharray: "1, 200",
strokeDashoffset: "0",
},
"50%": {
strokeDasharray: "89, 200",
strokeDashoffset: "-35px",
},
"100%": {
strokeDasharray: "89, 200",
strokeDashoffset: "-124px",
},
};
const keyframeColors = {
light: latitudeColors.grey40,
medium: latitudeColors.grey50,
dark: latitudeColors.grey60,
};
const spinnerStyles = StyleSheet.create({
loaderPath: {
animationName: [indefiniteKeyframes, cyclePrimaryKeyframes(keyframeColors)],
animationDuration: "1.5s, 6s",
animationTimingFunction: "ease-in-out, ease-in-out",
animationIterationCount: "infinite, infinite",
strokeDashoffset: 0,
},
});
/**
* @short A pure CSS loader with an indeterminate loading animation that should be used as a placeholder element to indicate that data is loading.
* @brandStatus V2
* @status Stable
* @category Data Display
*/
function Loader({
children,
loaded,
isFullWidth = true,
size = LOADER_SIZE,
overlay = false,
}: Props) {
const loader = (
<div className={css(styles.overlayContainer)}>
<div
className={css(
styles.circularWrapper,
isFullWidth && styles.center,
overlay && styles.overlayElement
)}
style={{width: size, height: size}}
>
<svg className={css(styles.loader)} viewBox="25 25 50 50">
<circle
className={css(spinnerStyles.loaderPath)}
cx="50"
cy="50"
r="20"
fill="none"
strokeWidth="2"
strokeMiterlimit="10"
/>
</svg>
</div>
</div>
);
return (
<span className={css(isFullWidth && styles.fullWidthContainer)}>
{loaded === false ? loader : children}
</span>
);
}
export default Loader;