@amaui/style
Version:
CSS in JS styling solution
56 lines (55 loc) • 2.95 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const isEnvironment_1 = __importDefault(require("@amaui/utils/isEnvironment"));
class AmauiStyleRenderer {
make(attributes = { element: {}, data: {} }, version = 'style') {
// Append to the bottom of head element
if ((0, isEnvironment_1.default)('browser')) {
const element = window.document.createElement(version);
Object.keys((attributes === null || attributes === void 0 ? void 0 : attributes.element) || {}).forEach(attribute => element.setAttribute(attribute, attributes.element[attribute]));
// Add attributes
Object.keys((attributes === null || attributes === void 0 ? void 0 : attributes.data) || {}).forEach(attribute => {
element[attribute] = attributes.data[attribute];
element.setAttribute(attribute.indexOf('data-') === 0 ? attribute : `data-${attribute}`, attributes.data[attribute]);
});
return element;
}
}
add(value, priority = 'lower', attributes) {
var _a, _b;
// Append to the bottom of head element
if ((0, isEnvironment_1.default)('browser')) {
const styleSheets = window.document.styleSheets;
if (!styleSheets.length || priority === 'upper')
window.document.head.append(value);
else {
const reset = Array.from(styleSheets).find(item => item.ownerNode.method === 'reset');
let pure = Array.from(styleSheets).filter(item => item.ownerNode.method === 'pure');
pure = pure[pure.length - 1];
if (((_a = attributes === null || attributes === void 0 ? void 0 : attributes.data) === null || _a === void 0 ? void 0 : _a.method) === 'reset' ||
(priority === 'lower' && !(pure || reset)))
window.document.head.insertBefore(value, ((_b = styleSheets[0]) === null || _b === void 0 ? void 0 : _b.ownerNode) || null);
else if (priority === 'lower') {
if (pure)
window.document.head.insertBefore(value, pure.ownerNode.nextElementSibling);
else if (reset)
window.document.head.insertBefore(value, reset.ownerNode.nextElementSibling);
}
else
window.document.head.append(value);
}
return value;
}
}
remove(value) {
let element = value;
if (value === null || value === void 0 ? void 0 : value.ownerNode)
element = element.ownerNode;
if (element === null || element === void 0 ? void 0 : element.remove)
element.remove();
}
}
exports.default = AmauiStyleRenderer;