mount-style
Version:
A simple function for mounting css style codes
42 lines (39 loc) • 915 B
JavaScript
;
/**
* Dynamic loading of css styles
*
* @param src - css string
* @param option - parameters: attrs, props
* @returns - results
*/
function mountStyle(css, option) {
if (option === void 0) {
option = {};
}
if (!css) throw new Error('[mountStyle]: css string is required');
var attrs = option.attrs,
props = option.props;
return new Promise(function (resolve) {
var dom = document.createElement('style');
var attr, prop;
if (attrs) {
for (attr in attrs) {
dom[attr] = attrs[attr];
}
}
if (props) {
for (prop in props) {
dom[prop] = props[prop];
}
}
dom.type = 'text/css';
try {
dom.appendChild(document.createTextNode(css));
} catch (ex) {
dom.textContent = css;
}
document.getElementsByTagName('head')[0].appendChild(dom);
resolve(true);
});
}
module.exports = mountStyle;