UNPKG

@chakra-ui/react

Version:

Responsive and accessible React UI components built with React and Emotion

129 lines (126 loc) • 4.37 kB
"use strict"; import { compact } from '../utils/compact.js'; import { cx } from '../utils/cx.js'; import { mapEntries } from '../utils/entries.js'; import { memo } from '../utils/memo.js'; import { mergeWith } from '../utils/merge.js'; import { omit } from '../utils/omit.js'; import { splitProps } from '../utils/split-props.js'; import { uniq } from '../utils/uniq.js'; import { walkObject } from '../utils/walk-object.js'; import { createCssFn } from './css.js'; import { EMPTY_OBJECT } from './singleton.js'; const defaults = (conf) => ({ base: EMPTY_OBJECT, variants: EMPTY_OBJECT, defaultVariants: EMPTY_OBJECT, compoundVariants: [], ...conf }); function createRecipeFn(options) { const { css, conditions, normalize, normalizeValue, layers } = options; const normalizeSelections = (selections) => walkObject(selections, normalizeValue, { stop: (value) => Array.isArray(value) }); function createCompoundVariantFn(compoundVariants) { const rules = compoundVariants.map((compoundVariant) => ({ styles: compoundVariant.css, match: Object.entries(omit(compoundVariant, ["css"])).map( ([name, value]) => [name, Array.isArray(value) ? value : [value]] ) })); return function matchCompoundVariants(selections) { let result = EMPTY_OBJECT; for (const { styles, match } of rules) { const matches = match.every( ([name, values]) => values.includes(selections[name]) ); if (matches) result = css(result, styles); } return result; }; } function cva(config = {}) { const defaultsConfig = defaults(config); const { base, defaultVariants, compoundVariants } = defaultsConfig; const matchCompoundVariants = createCompoundVariantFn(compoundVariants); const variants = mapEntries(defaultsConfig.variants, (key, obj) => [ key, mapEntries(obj, (optionKey, styles) => [optionKey, normalize(styles)]) ]); const getVariantCss = createCssFn({ conditions, normalize: normalizeSelections, transform(prop, value) { return variants[prop]?.[value]; } }); const resolve = memo(function resolve2(props = {}) { const selections = { ...defaultVariants, ...compact(props) }; const variantCss = normalize(base); mergeWith(variantCss, getVariantCss(selections)); return layers.wrap( "recipes", css(variantCss, matchCompoundVariants(selections)) ); }); const variantKeys = Object.keys(variants); const splitVariantProps = (props) => { const restProps = omit(props, ["recipe"]); const [recipeProps, localProps] = 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 = mapEntries(variants, (key, value) => [ key, Object.keys(value) ]); const cvaFn = memo(function cvaFn2(props) { return css(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) ?? EMPTY_OBJECT; function mergeCva(cvaA, cvaB) { const a = defaults(toRecipeConfig(cvaA)); const b = defaults(toRecipeConfig(cvaB)); const variantKeys = uniq(Object.keys(a.variants), Object.keys(b.variants)); const base = mergeWith({}, a.base, b.base); const variants = Object.fromEntries( variantKeys.map((key) => [ key, mergeWith({}, a.variants[key], b.variants[key]) ]) ); const defaultVariants = mergeWith({}, a.defaultVariants, b.defaultVariants); const compoundVariants = [...a.compoundVariants, ...b.compoundVariants]; const className = cx(a.className, b.className); return { className, base, variants, defaultVariants, compoundVariants }; } export { createRecipeFn };