UNPKG

class-variance-authority

Version:

Class Variance Authority 🧬

49 lines (47 loc) • 2.3 kB
const falsyToString = (value)=>typeof value === "boolean" ? "".concat(value) : value === 0 ? "0" : value; export const cx = function() // @ts-ignore { for(var _len = arguments.length, classes = new Array(_len), _key = 0; _key < _len; _key++){ classes[_key] = arguments[_key]; } return classes.flat(Infinity).filter(Boolean).join(" "); }; export const cva = (base, config)=>{ return (props)=>{ var ref; const className = props === null || props === void 0 ? void 0 : props.class; if ((config === null || config === void 0 ? void 0 : config.variants) == null) return cx(base, className); const { variants , defaultVariants } = config; const getVariantClassNames = Object.keys(variants).map((variant)=>{ const variantProp = props === null || props === void 0 ? void 0 : props[variant]; const defaultVariantProp = defaultVariants === null || defaultVariants === void 0 ? void 0 : defaultVariants[variant]; if (variantProp === null) return null; const variantKey = falsyToString(variantProp) || falsyToString(defaultVariantProp); return variants[variant][variantKey]; }); const propsWithoutUndefined = props && Object.entries(props).reduce((acc, param)=>{ let [key, value] = param; if (value === undefined) { return acc; } acc[key] = value; return acc; }, {}); const getCompoundVariantClassNames = config === null || config === void 0 ? void 0 : (ref = config.compoundVariants) === null || ref === void 0 ? void 0 : ref.reduce((acc, param1)=>{ let { class: classNames , ...compoundVariantOptions } = param1; if (classNames == null) return acc; return Object.entries(compoundVariantOptions).every((param)=>{ let [key, value] = param; return ({ ...defaultVariants, ...propsWithoutUndefined })[key] === value; }) ? [ ...acc, classNames ] : acc; }, []); return cx(base, getVariantClassNames, getCompoundVariantClassNames, className); }; }; //# sourceMappingURL=index.esm.js.map