-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloading.js
77 lines (68 loc) · 2.33 KB
/
loading.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
// This file is a bit ugly to optimize first-paint:
// 1. No external resources blocking loader display
// 2. Terser friendly (no multi-line JS templates)
const getSVG = () => {
const rect = (_, index) => `<rect x="-4" y="24" ry="10" width="8" height="16" transform="rotate(${index * 30})" style="animation-delay:${(index - 11) / 10}s" />`;
return '' +
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="-50 -50 100 100">' +
'<style>' +
'@keyframes cycle{' +
'from{opacity:0}' +
'to{opacity:1}' +
'}' +
'rect{' +
'fill:#fffc;' +
'animation:cycle 1.2s infinite' +
'}' +
'</style>' +
Array.from({length: 12})
.map(rect)
.join('') +
'</svg>';
};
const createElement = (tag, properties) => Object.assign(document.createElement(tag), properties);
const createStyle = textContent => createElement('style', {textContent});
const tagName = 'cfware-loading';
customElements.define(tagName, class CFWareLoading extends HTMLElement {
constructor() {
super();
this.attachShadow({mode: 'open'}).append(
createStyle(
':host{' +
'display:grid;' +
'grid:1fr auto auto 1fr/1fr;' +
'opacity:1;' +
'background:#333;' +
'color:#ddd;' +
'user-select:none;' +
'font-size:1.2rem;' +
'line-height:1;' +
'text-align:center' +
'}' +
'img{' +
'width:4rem;' +
'margin:auto' +
'}'
),
createElement('div'),
createElement('img', {
alt: '',
src: `data:image/svg+xml;base64,${btoa(getSVG())}`
}),
'Loading'
);
}
});
document.head.prepend(createStyle(
'html,body{' +
'display:grid;' +
'height:100%;' +
'margin:0;' +
'overflow:hidden' +
'}' +
'body[loaded] ' + tagName + ',' +
'body:not([loaded]) :not(' + tagName + '){' +
'display:none' +
'}'
));
document.body.prepend(createElement(tagName));