-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstyle.js
More file actions
55 lines (49 loc) · 1.27 KB
/
style.js
File metadata and controls
55 lines (49 loc) · 1.27 KB
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
import eventManager from './eventManager.js';
// Custom CSS classes are great.
export function newStyle(plugin = false) {
let loaded = false;
const el = document.createElement('style');
function appendStyle() {
if (el.parentElement) return;
if (plugin) el.dataset.underscriptPlugin = plugin;
else el.dataset.underscript = '';
document.head.append(el);
}
eventManager.on(':preload', () => {
loaded = true;
});
function add(...styles) {
const hasChildren = styles.length || el.children.length;
if (loaded && hasChildren) appendStyle();
else if (!loaded) eventManager.once(':preload', () => appendStyle());
return wrapper(append(styles));
}
function append(styles = [], nodes = []) {
styles.flat().forEach((s) => {
const node = document.createTextNode(s);
nodes.push(node);
el.appendChild(node);
});
return nodes;
}
function wrapper(nodes = []) {
return {
remove() {
nodes.splice(0)
.forEach((node) => node.remove());
return this;
},
replace(...styles) {
return this.remove().append(styles);
},
append(...styles) {
append(styles, nodes);
return this;
},
};
}
return {
add,
};
}
export default newStyle();