File size: 3,752 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
import {
type Dict,
compact,
cx,
mergeWith,
omit,
splitProps,
uniq,
} from "../utils"
import { createCssFn } from "./css"
import type { RecipeCreatorFn, RecipeDefinition } from "./recipe.types"
import type { Condition, CssFn, Layers } from "./types"
const defaults = (conf: any): Required<RecipeDefinition> => ({
base: {},
variants: {},
defaultVariants: {},
compoundVariants: [],
...conf,
})
interface Options {
normalize: (styles: Dict) => Dict
css: CssFn
conditions: Condition
layers: Layers
}
export function createRecipeFn(options: Options): RecipeCreatorFn {
const { css, conditions, normalize, layers } = options
function cva(config: Dict = {}) {
const { base, variants, defaultVariants, compoundVariants } =
defaults(config)
const getVariantCss = createCssFn({
conditions,
normalize,
transform(prop, value) {
return variants[prop]?.[value]
},
})
const resolve = (props = {}) => {
const variantSelections: Dict = normalize({
...defaultVariants,
...compact(props),
})
let variantCss = { ...base }
mergeWith(variantCss, getVariantCss(variantSelections))
const compoundVariantCss = getCompoundVariantCss(
compoundVariants,
variantSelections,
)
return layers.wrap("recipes", css(variantCss, compoundVariantCss))
}
const variantKeys = Object.keys(variants)
const splitVariantProps = (props: Dict) => {
const restProps = omit(props, ["recipe"])
const [recipeProps, localProps] = splitProps(restProps, variantKeys)
if (!variantKeys.includes("colorPalette")) {
recipeProps.colorPalette =
props.colorPalette || defaultVariants.colorPalette
}
if (variantKeys.includes("orientation")) {
;(localProps as any).orientation = props.orientation
}
return [recipeProps, localProps]
}
const variantMap = Object.fromEntries(
Object.entries(variants).map(([key, value]) => [
key,
Object.keys(value as any),
]),
)
const cvaFn = (props: any) => css(resolve(props))
return Object.assign(cvaFn, {
className: config.className,
__cva__: true,
variantMap,
variantKeys,
raw: resolve,
config,
splitVariantProps,
merge(other: any) {
return cva(mergeCva(options)(this, other))
},
})
}
function getCompoundVariantCss(cvs: any[], vm: any) {
let result = {}
cvs.forEach((cv) => {
const isMatching = Object.entries(cv).every(([key, value]) => {
if (key === "css") return true
const values = Array.isArray(value) ? value : [value]
return values.some((value) => vm[key] === value)
})
if (isMatching) {
result = css(result, cv.css)
}
})
return result
}
//@ts-expect-error
return cva
}
function mergeCva(opts: Options) {
const { css } = opts
return function mergeCva(cvaA: any, cvaB: any) {
const override = defaults(cvaB.config)
const variantKeys = uniq(cvaA.variantKeys, Object.keys(cvaB.variants))
const base = css(cvaA.base, override.base)
const variants = Object.fromEntries(
variantKeys.map((key) => [
key,
css(cvaA.config.variants[key], override.variants[key]),
]),
)
const defaultVariants = mergeWith(
cvaA.config.defaultVariants,
override.defaultVariants,
)
const compoundVariants = [
...cvaA.compoundVariants,
...override.compoundVariants,
]
const className = cx(cvaA.className, cvaB.className)
return {
className,
base,
variants,
defaultVariants,
compoundVariants,
}
}
}
|