-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
104 lines (89 loc) · 2.16 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
/* @flow */
import * as React from 'react';
type PreloaderProps = {
fadeDuration?: number,
className?: string,
children?: React.Node,
style: {
[string]: mixed
}
};
type PreloaderState = {
loaded: boolean
};
type PlaceholderProps = {
children?: React.Node
};
const defaultStyle = {
opacity: 1,
zIndex: 999,
backgroundColor: 'white',
height: '100vh',
width: '100vw',
position: 'fixed',
top: 0,
left: 0,
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
};
class Preloader extends React.Component<PreloaderProps, PreloaderState> {
state = { loaded: false };
ref: ?HTMLDivElement;
componentDidMount = () => {
window.requestAnimationFrame(this.checkReadyState);
};
checkReadyState = () => {
if (document.readyState === 'complete' && this.ref) {
this.ref.style.opacity = '0';
setTimeout(() => {
this.setState({ loaded: true });
this.ref = null;
}, this.props.fadeDuration || 200);
} else {
window.requestAnimationFrame(this.checkReadyState);
}
};
render() {
const { style, className, children, fadeDuration } = this.props;
const PlaceholderComponent = React.Children.toArray(children)
.find(({ type }) => type.displayName === 'PreloadingPlaceholder');
if (!PlaceholderComponent) {
console.warn('react-preloading-screen:', 'No <Placeholder> component found in children of <Preloader>. Preloader is not in effect.');
return children;
}
const cleanChildren = React.Children.map(
children,
(child) =>
(child.type.displayName === 'PreloadingPlaceholder' ? null : child)
);
return (
<React.Fragment>
{cleanChildren}
{
this.state.loaded
? null
: (
<div
style={{
...defaultStyle,
transition: `opacity ${fadeDuration || 200 / 1000}s ease`,
...style,
}}
className={className}
ref={(ref) => {
this.ref = ref;
}}
>
{PlaceholderComponent}
</div>
)}
</React.Fragment>
);
}
}
const Placeholder = ({ children }: PlaceholderProps) => (
<React.Fragment>{children}</React.Fragment>
);
Placeholder.displayName = 'PreloadingPlaceholder';
export { Preloader, Placeholder };