@tdb/util
Version:
Shared helpers and utilities.
39 lines (35 loc) • 1 kB
text/typescript
import { IS_BROWSER } from '../../constants';
/**
* Adds an `@import` stylesheet reference to the <head>
* if it does not already exist.
*/
export function importStylesheet(url: string) {
if (!IS_BROWSER) {
return;
}
if (exists('style', url)) {
return; // NB: Only add to the document once.
}
// const head = document.head || document.getElementsByTagName('head')[0];
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);
}
/**
* INTERNAL
*/
function exists(tag: 'style', url: string) {
return IS_BROWSER ? Boolean(findByUrl(tag, url)) : false;
}
function findByUrl(tag: 'style', url: string) {
if (IS_BROWSER) {
const items = Array.from(document.getElementsByTagName(tag));
return items.find(style => style.dataset.url === url);
} else {
return undefined;
}
}