File size: 2,945 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 |
import { type Dict, omit, splitProps } from "../utils"
import type { RecipeCreatorFn, SlotRecipeCreatorFn } from "./recipe.types"
interface Options {
cva: RecipeCreatorFn
}
const getSlotRecipes = (
config: Record<string, any> = {},
): Record<string, any> => {
const init = (slot: string) => ({
base: config.base?.[slot] ?? {},
variants: {},
defaultVariants: config.defaultVariants ?? {},
compoundVariants: config.compoundVariants
? getSlotCompoundVariant(config.compoundVariants, slot)
: [],
})
const slots = config.slots ?? []
const entries: [string, any] = slots.map((slot: any) => [slot, init(slot)])
for (const [variantsKey, variantsSpec] of Object.entries(
config.variants ?? {},
)) {
for (const [variantKey, variantSpec] of Object.entries(
variantsSpec as Record<string, any>,
)) {
entries.forEach(([slot, slotRecipe]) => {
slotRecipe.variants[variantsKey] ??= {}
slotRecipe.variants[variantsKey][variantKey] = variantSpec[slot] ?? {}
})
}
}
return Object.fromEntries(entries)
}
export const getSlotCompoundVariant = <T extends { css: any }>(
compoundVariants: T[],
slotName: string,
) =>
compoundVariants
.filter((compoundVariant) => compoundVariant.css[slotName])
.map((compoundVariant) => ({
...compoundVariant,
css: compoundVariant.css[slotName],
}))
export function createSlotRecipeFn(options: Options): SlotRecipeCreatorFn {
const { cva } = options
return function sva(config: Dict = {}): any {
const slots = Object.entries(getSlotRecipes(config)).map(
([slot, slotCva]) => [slot, cva(slotCva)],
)
function svaFn(props: Dict) {
//@ts-ignore
const result = slots.map(([slot, cvaFn]) => [slot, cvaFn(props)])
return Object.fromEntries(result)
}
const variants = (config.variants ?? {}) as Dict
const variantKeys = Object.keys(variants)
function splitVariantProps(props: Dict) {
const restProps = omit(props, ["recipe"])
const [recipeProps, localProps] = splitProps(restProps, variantKeys)
if (!variantKeys.includes("colorPalette")) {
recipeProps.colorPalette =
props.colorPalette || config.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)]),
)
let classNameMap: Record<string, string> = {}
if (config.className) {
classNameMap = Object.fromEntries(
config.slots.map((slot: string) => [
slot,
`${config.className}__${slot}`,
]),
)
}
return Object.assign(svaFn, {
variantMap,
variantKeys,
splitVariantProps,
classNameMap,
})
}
}
|