UNPKG

@nex-ui/system

Version:

A lightweight and performant styling library based on Emotion, focusing on component architecture and developer experience.

65 lines (61 loc) 2.44 kB
'use strict'; 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 variantSection = selections[key]; if (!(Array.isArray(compoundCheck[key]) && compoundCheck[key]?.includes(variantSection) || compoundCheck[key] === variantSection)) { return false; } } } return true; } function createRuntimeFn(options) { const { mainStyles, variants = {}, compoundVariants = [], defaultVariants = {} } = options; function splitVariantProps(props) { const variantKeys = Object.keys(variants); const result = {}; variantKeys.forEach((key)=>{ if (props[key] !== undefined) result[key] = props[key]; }); return result; } function runtimeFn(variantsProps = {}) { let mergedStyles = { ...mainStyles }; const selections = { ...defaultVariants, ...variantsProps }; for(const variantKey in selections){ // istanbul ignore if if (!Object.hasOwn(selections, variantKey)) continue; const variantSection = selections[variantKey]; if (variantSection !== null) { let selection = variantSection; if (typeof selection === 'boolean') { selection = selection === true ? 'true' : 'false'; } mergedStyles = utils$1.merge({}, mergedStyles, variants[variantKey][selection]); } } for(const compoundVariantKey in compoundVariants){ // istanbul ignore if if (!Object.hasOwn(compoundVariants, compoundVariantKey)) continue; const compoundVariantValue = compoundVariants[compoundVariantKey]; const { css: compoundVariantCSS, ...compoundCheck } = compoundVariantValue; 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;