UNPKG

@chakra-ui/react

Version:

Responsive and accessible React UI components built with React and Emotion

131 lines (127 loc) • 4.55 kB
"use strict"; 'use strict'; var compact = require('../utils/compact.cjs'); var cx = require('../utils/cx.cjs'); var entries = require('../utils/entries.cjs'); var memo = require('../utils/memo.cjs'); var merge = require('../utils/merge.cjs'); var omit = require('../utils/omit.cjs'); var splitProps = require('../utils/split-props.cjs'); var uniq = require('../utils/uniq.cjs'); var walkObject = require('../utils/walk-object.cjs'); var css = require('./css.cjs'); var singleton = require('./singleton.cjs'); const defaults = (conf) => ({ base: singleton.EMPTY_OBJECT, variants: singleton.EMPTY_OBJECT, defaultVariants: singleton.EMPTY_OBJECT, compoundVariants: [], ...conf }); function createRecipeFn(options) { const { css: css$1, conditions, normalize, normalizeValue, layers } = options; const normalizeSelections = (selections) => walkObject.walkObject(selections, normalizeValue, { stop: (value) => Array.isArray(value) }); function createCompoundVariantFn(compoundVariants) { const rules = compoundVariants.map((compoundVariant) => ({ styles: compoundVariant.css, match: Object.entries(omit.omit(compoundVariant, ["css"])).map( ([name, value]) => [name, Array.isArray(value) ? value : [value]] ) })); return function matchCompoundVariants(selections) { let result = singleton.EMPTY_OBJECT; for (const { styles, match } of rules) { const matches = match.every( ([name, values]) => values.includes(selections[name]) ); if (matches) result = css$1(result, styles); } return result; }; } function cva(config = {}) { const defaultsConfig = defaults(config); const { base, defaultVariants, compoundVariants } = defaultsConfig; const matchCompoundVariants = createCompoundVariantFn(compoundVariants); const variants = entries.mapEntries(defaultsConfig.variants, (key, obj) => [ key, entries.mapEntries(obj, (optionKey, styles) => [optionKey, normalize(styles)]) ]); const getVariantCss = css.createCssFn({ conditions, normalize: normalizeSelections, transform(prop, value) { return variants[prop]?.[value]; } }); const resolve = memo.memo(function resolve2(props = {}) { const selections = { ...defaultVariants, ...compact.compact(props) }; const variantCss = normalize(base); merge.mergeWith(variantCss, getVariantCss(selections)); return layers.wrap( "recipes", css$1(variantCss, matchCompoundVariants(selections)) ); }); const variantKeys = Object.keys(variants); const splitVariantProps = (props) => { const restProps = omit.omit(props, ["recipe"]); const [recipeProps, localProps] = splitProps.splitProps(restProps, variantKeys); const hasColorPalette = variantKeys.includes("colorPalette"); const hasOrientation = variantKeys.includes("orientation"); if (!hasColorPalette) { recipeProps.colorPalette = props.colorPalette || defaultVariants.colorPalette; } if (hasOrientation) { localProps.orientation = props.orientation; } return [recipeProps, localProps]; }; const variantMap = entries.mapEntries(variants, (key, value) => [ key, Object.keys(value) ]); const cvaFn = memo.memo(function cvaFn2(props) { return css$1(resolve(props)); }); return Object.assign(cvaFn, { className: config.className, __cva__: true, variantMap, variantKeys, raw: resolve, config, splitVariantProps, merge(other) { return cva(mergeCva(this, other)); } }); } return cva; } const toRecipeConfig = (cva) => (cva?.__cva__ ? cva.config : cva) ?? singleton.EMPTY_OBJECT; function mergeCva(cvaA, cvaB) { const a = defaults(toRecipeConfig(cvaA)); const b = defaults(toRecipeConfig(cvaB)); const variantKeys = uniq.uniq(Object.keys(a.variants), Object.keys(b.variants)); const base = merge.mergeWith({}, a.base, b.base); const variants = Object.fromEntries( variantKeys.map((key) => [ key, merge.mergeWith({}, a.variants[key], b.variants[key]) ]) ); const defaultVariants = merge.mergeWith({}, a.defaultVariants, b.defaultVariants); const compoundVariants = [...a.compoundVariants, ...b.compoundVariants]; const className = cx.cx(a.className, b.className); return { className, base, variants, defaultVariants, compoundVariants }; } exports.createRecipeFn = createRecipeFn;