mount-style
Version:
A simple function for mounting css style codes
51 lines (47 loc) • 1.27 kB
JavaScript
/*!
* mount-style v1.2.0
* A simple function for mounting css style codes
* (c) 2021-2023 saqqdy<https://github.com/saqqdy>
* Released under the MIT License.
*/
this.mountStyle = (function () {
'use strict';
/**
* 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);
});
}
return mountStyle;
})();