util-helpers
Version:
29 lines (26 loc) • 892 B
JavaScript
;
function injectStyle(css, options) {
var _a = options || {}, _b = _a.container, container = _b === void 0 ? document.head || document.getElementsByTagName('head')[0] || document.body : _b, _c = _a.insertAt, insertAt = _c === void 0 ? 'top' : _c, onBefore = _a.onBefore;
var style = document.createElement('style');
if (style.styleSheet) {
style.styleSheet.cssText = css;
}
else {
style.appendChild(document.createTextNode(css));
}
if (typeof onBefore === 'function') {
onBefore(style);
}
var atTop = insertAt === 'top';
if (atTop && container.prepend) {
container.prepend(style);
}
else if (atTop && container.firstChild) {
container.insertBefore(style, container.firstChild);
}
else {
container.appendChild(style);
}
return style;
}
module.exports = injectStyle;