-
Notifications
You must be signed in to change notification settings - Fork 170
/
style.ts
37 lines (35 loc) · 1.16 KB
/
style.ts
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
import util from '../../shared/util.ts'
export function removeCSS(id: string) {
const { document } = window as any
Array.from(document.head.children).forEach((el: any) => {
if (el.getAttribute('data-module-id') === id) {
document.head.removeChild(el)
}
})
}
export function applyCSS(id: string, css: string) {
if (util.inDeno()) {
return { id, css }
} else {
const { document } = window as any
const ssrStyle = Array.from<any>(document.head.children).find((el: any) => {
return el.getAttribute('data-module-id') === id && el.hasAttribute('ssr')
})
if (ssrStyle) {
ssrStyle.removeAttribute('ssr')
} else {
const prevStyleEls = Array.from(document.head.children).filter((el: any) => {
return el.getAttribute('data-module-id') === id
})
const styleEl = document.createElement('style')
styleEl.type = 'text/css'
styleEl.appendChild(document.createTextNode(css))
styleEl.setAttribute('data-module-id', id)
document.head.appendChild(styleEl)
if (prevStyleEls.length > 0) {
prevStyleEls.forEach(el => document.head.removeChild(el))
}
return styleEl
}
}
}