-
Notifications
You must be signed in to change notification settings - Fork 169
/
style.ts
26 lines (25 loc) · 901 Bytes
/
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
const deno = typeof Deno !== "undefined" && typeof Deno.env === "object";
export function applyCSS(url: string, css: string) {
if (!deno) {
const { document } = window;
const ssrEl = Array.from<Element>(document.head.children).find((el: Element) =>
el.getAttribute("data-module-id") === url &&
el.hasAttribute("ssr")
);
if (ssrEl) {
// apply the css at next time
ssrEl.removeAttribute("ssr");
} else {
const prevEls = Array.from(document.head.children).filter((el: Element) => {
return el.getAttribute("data-module-id") === url;
});
const el = document.createElement("style");
el.setAttribute("data-module-id", url);
el.appendChild(document.createTextNode(css));
document.head.appendChild(el);
if (prevEls.length > 0) {
prevEls.forEach((el) => document.head.removeChild(el));
}
}
}
}