UNPKG

947 BJavaScriptView Raw
1import { is } from '../common';
2export function init() {
3 const importStylesheet = (url) => {
4 if (!is.browser) {
5 return res;
6 }
7 if (exists('style', url)) {
8 return res;
9 }
10 const head = document.head;
11 const style = document.createElement('style');
12 style.type = 'text/css';
13 const css = `@import url('${url}')`;
14 style.dataset.url = url;
15 style.appendChild(document.createTextNode(css));
16 head.appendChild(style);
17 return res;
18 };
19 const res = {
20 importStylesheet,
21 };
22 return res;
23}
24function exists(tag, url) {
25 return is.browser ? Boolean(findByUrl(tag, url)) : false;
26}
27function findByUrl(tag, url) {
28 if (is.browser) {
29 const items = Array.from(document.getElementsByTagName(tag));
30 return items.find((style) => style.dataset.url === url);
31 }
32 else {
33 return undefined;
34 }
35}