@nex-ui/system
Version:
A lightweight and performant styling library based on Emotion, focusing on component architecture and developer experience.
59 lines (55 loc) • 2.13 kB
JavaScript
var utils$1 = require('@nex-ui/utils');
var utils = require('../utils.cjs');
function shouldApplyCompound(compoundCheck, selections) {
for(const key in compoundCheck){
if (Object.prototype.hasOwnProperty.call(compoundCheck, key)) {
const variantValue = selections[key];
if (!(utils$1.isArray(compoundCheck[key]) && compoundCheck[key]?.includes(variantValue) || compoundCheck[key] === variantValue)) {
return false;
}
}
}
return true;
}
function createRuntimeFn(options) {
const { mainStyles, variants = {}, compoundVariants = [], defaultVariants = {} } = options;
function splitVariantProps(props) {
const variantKeys = Object.keys(variants);
const result = {};
utils$1.forEach(variantKeys, (key)=>{
if (props[key] !== undefined) result[key] = props[key];
});
return result;
}
function runtimeFn(variantsProps = {}) {
let mergedStyles = {
...mainStyles
};
const selections = {
...defaultVariants,
...variantsProps
};
utils$1.forEach(selections, (variantSelection, variantName)=>{
if (variantSelection !== null) {
let selection = variantSelection;
if (typeof selection === 'boolean') {
selection = selection === true ? 'true' : 'false';
}
mergedStyles = utils$1.merge({}, mergedStyles, variants?.[variantName]?.[selection]);
}
});
utils$1.forEach(compoundVariants, (compoundVariant)=>{
const { css: compoundVariantCSS, ...compoundCheck } = compoundVariant;
if (shouldApplyCompound(compoundCheck, selections)) {
mergedStyles = utils$1.merge({}, mergedStyles, compoundVariantCSS);
}
});
return mergedStyles;
}
const memoized = utils.memoizeFn(runtimeFn);
// @ts-ignore
memoized.splitVariantProps = splitVariantProps;
return memoized;
}
exports.createRuntimeFn = createRuntimeFn;
;