@amaui/style
Version:
CSS in JS styling solution
85 lines (84 loc) • 3.6 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"));
const utils_1 = require("./utils");
const optionsDefault = {
production: ((0, isEnvironment_1.default)('browser') ||
((0, isEnvironment_1.default)('nodejs') && ['prod', 'production'].indexOf(process.env.NODE_ENV) > -1))
};
let amauiMakeClassNameInc = 0;
function makeClassName(amauiStyle, options_ = {}) {
var _a;
const options = Object.assign(Object.assign({}, optionsDefault), options_);
const prefix = ((_a = amauiStyle === null || amauiStyle === void 0 ? void 0 : amauiStyle.options) === null || _a === void 0 ? void 0 : _a.classNamePrefix) || '';
// If both dev and prod are false, then dev is true
const production = options.production !== undefined ? options.production : optionsDefault.production;
const makeNameMethodClassName = (0, utils_1.makeName)();
const makeNameMethodKeyframesName = (0, utils_1.makeName)();
const domUnique = (value) => {
const allClassNames = [...new Set(Array.from(window.document.querySelectorAll('[class]')).flatMap(item => [...item.classList]))];
return allClassNames.indexOf(value) === -1;
};
const method = (method_ = makeNameMethodClassName) => {
const made = [];
return (value_) => {
var _a, _b;
const value = {
arguments: {
value: value_
}
};
// Make a class name
// Production
if (production) {
value.value = `${prefix}${method_.next().value}`;
while (true) {
if (made.includes(value.value) ||
(((_a = options.dom) === null || _a === void 0 ? void 0 : _a.unique) && !domUnique(value.value))) {
value.value = `${prefix}${method_.next().value}`;
}
else
break;
}
}
// Development
else {
value.value = `${prefix}${value_.property}-${++amauiMakeClassNameInc}`;
while (true) {
if ((((_b = options.dom) === null || _b === void 0 ? void 0 : _b.unique) && !domUnique(value.value))) {
value.value = `${prefix}${value_ === null || value_ === void 0 ? void 0 : value_.property}-${++amauiMakeClassNameInc}`;
}
else
break;
}
}
made.push(value);
return value;
};
};
const methodClassName = method();
const methodKeyframesName = method(makeNameMethodKeyframesName);
// Add methods to subscriptions
if (amauiStyle) {
amauiStyle.subscriptions.className.name.subscribe(methodClassName);
amauiStyle.subscriptions.keyframes.name.subscribe(methodKeyframesName);
}
const remove = () => {
// Remove methods from subscriptions
if (amauiStyle) {
amauiStyle.subscriptions.className.name.unsubscribe(methodClassName);
amauiStyle.subscriptions.keyframes.name.unsubscribe(methodKeyframesName);
}
};
const response = {
methods: {
method: methodClassName,
},
remove,
};
return response;
}
exports.default = makeClassName;