UNPKG

@platform/css

Version:

Helpers for working with inline CSS.

36 lines (35 loc) 945 B
import { is } from '../common'; export function init() { const importStylesheet = (url) => { if (!is.browser) { return res; } if (exists('style', url)) { return res; } const head = document.head; const style = document.createElement('style'); style.type = 'text/css'; const css = `@import url('${url}')`; style.dataset.url = url; style.appendChild(document.createTextNode(css)); head.appendChild(style); return res; }; const res = { importStylesheet, }; return res; } function exists(tag, url) { return is.browser ? Boolean(findByUrl(tag, url)) : false; } function findByUrl(tag, url) { if (is.browser) { const items = Array.from(document.getElementsByTagName(tag)); return items.find(style => style.dataset.url === url); } else { return undefined; } }