1 | const styleMap = {}
|
2 |
|
3 | export const checkAndWriteIntoHead = () => {
|
4 | Object.keys(styleMap).forEach(wrapper => {
|
5 | const style = styleMap[wrapper]
|
6 | if (style.count > 0) {
|
7 |
|
8 | if (!document.getElementById(wrapper)) {
|
9 | const styleTag = document.createElement('style')
|
10 | styleTag.innerHTML = style.css
|
11 | styleTag.setAttribute('id', wrapper)
|
12 | document.getElementsByTagName('head')[0].appendChild(styleTag)
|
13 | }
|
14 | } else {
|
15 |
|
16 | if (document.getElementById(wrapper)) {
|
17 | document.getElementById(wrapper).remove()
|
18 | }
|
19 | }
|
20 | })
|
21 | }
|
22 |
|
23 | export const append = (style) => {
|
24 | if (Array.isArray(style))
|
25 | return style.forEach(theStyle => append(theStyle))
|
26 |
|
27 |
|
28 | if (!styleMap[style.wrapper]) {
|
29 | styleMap[style.wrapper] = {
|
30 | css: style.css,
|
31 | count: 1
|
32 | }
|
33 | } else {
|
34 | styleMap[style.wrapper].count++
|
35 | }
|
36 |
|
37 | if (__CLIENT__)
|
38 | checkAndWriteIntoHead()
|
39 | }
|
40 |
|
41 | export const remove = (style) => {
|
42 | if (Array.isArray(style))
|
43 | return style.forEach(theStyle => remove(theStyle))
|
44 |
|
45 | if (styleMap[style.wrapper]) {
|
46 | styleMap[style.wrapper].count--
|
47 | }
|
48 | }
|
49 |
|
50 | export const get = () => styleMap
|