File size: 5,003 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 |
import type { SystemContext } from "@chakra-ui/react"
import { pretty } from "./pretty.js"
import { capitalize, isBooleanValue, unionType } from "./shared.js"
export async function generateRecipe(sys: SystemContext, strict = true) {
const theme = sys._config.theme ?? {}
const sysRecipes = theme.recipes ?? {}
const sysSlotRecipes = theme.slotRecipes ?? {}
const recipes = Object.keys(sysRecipes).map((key) => {
const recipe = sysRecipes[key]
const variantKeyMap = sys.cva(recipe).variantMap
const upperName = capitalize(key)
const str = `
export interface ${upperName}Variant {
${Object.keys(variantKeyMap)
.map((key) => {
const def = Reflect.get(recipe.defaultVariants ?? {}, key)
const jsDoc = def
? `/** @default ${JSON.stringify(def)} */\n`
: ""
const values = variantKeyMap[key]
if (values.every(isBooleanValue)) {
return `${jsDoc}${key}?: boolean | undefined`
}
return `${jsDoc}${key}?: ${unionType(values)} | undefined`
})
.join("\n")}
}
export type ${upperName}VariantProps = {
[K in keyof ${upperName}Variant]?: ConditionalValue<${upperName}Variant[K]> | undefined
}
export type ${upperName}VariantMap = {
[K in keyof ${upperName}Variant]: Array<${upperName}Variant[K]>
}
`
return str
})
const recipeKeys = Object.keys(sysRecipes)
const recipeRecord = `
export interface ConfigRecipes {
${
recipeKeys.length
? Object.keys(sysRecipes)
.map((key) => {
const upperName = capitalize(key)
return `${key}: SystemRecipeFn<${upperName}VariantProps, ${upperName}VariantMap>`
})
.join("\n")
: "[key: string]: SystemRecipeFn<any>"
}
}
`
const recipeResult = [recipes.join("\n"), recipeRecord].join("\n")
const slotRecipeKeys = Object.keys(sysSlotRecipes)
const slotRecipes = slotRecipeKeys.map((key) => {
const recipe = sysSlotRecipes[key]
const variantKeyMap = sys.sva(recipe).variantMap
const upperName = capitalize(key)
const str = `
// ${upperName}
export type ${upperName}Slot = ${unionType(recipe.slots ?? [])}
export interface ${upperName}Variant {
${Object.keys(variantKeyMap)
.map((key) => {
const def = Reflect.get(recipe.defaultVariants ?? {}, key)
const jsDoc = def
? `/** @default ${JSON.stringify(def)} */\n`
: ""
const values = variantKeyMap[key]
if (values.every(isBooleanValue)) {
return `${jsDoc}${key}?: boolean | undefined`
}
return `${jsDoc}${key}?: ${unionType(values, !strict)} | undefined`
})
.join("\n")}
}
export type ${upperName}VariantProps = {
[K in keyof ${upperName}Variant]?: ConditionalValue<${upperName}Variant[K]> | undefined
}
export type ${upperName}VariantMap = {
[K in keyof ${upperName}Variant]: Array<${upperName}Variant[K]>
}
`
return str
})
const slotRecipeRecord = `
export interface ConfigSlotRecipes {
${
slotRecipeKeys.length
? slotRecipeKeys
.map((key) => {
const upperName = capitalize(key)
return `${key}: SystemSlotRecipeFn<${upperName}Slot, ${upperName}VariantProps, ${upperName}VariantMap>`
})
.join("\n")
: "[key: string]: SystemSlotRecipeFn<string, any>"
}
}
export interface ConfigRecipeSlots {
${
slotRecipeKeys.length
? slotRecipeKeys
.map((key) => `${key}: ${capitalize(key)}Slot`)
.join("\n")
: "[key: string]: string"
}
}
`
const slotRecipeResult = [slotRecipes.join("\n"), slotRecipeRecord].join("\n")
return pretty(
[
'import type { RecipeDefinition, SlotRecipeDefinition, SystemRecipeFn, SystemSlotRecipeFn } from "../recipe.types"',
'import type { ConditionalValue } from "../css.types"',
recipeResult,
slotRecipeResult,
`
export type SlotRecipeRecord<T, K> = T extends keyof ConfigRecipeSlots
? Record<ConfigRecipeSlots[T], K>
: Record<string, K>
export type SlotRecipeProps<T> = T extends keyof ConfigSlotRecipes
? ConfigSlotRecipes[T]["__type"] & { recipe?: SlotRecipeDefinition | undefined }
: { recipe?: SlotRecipeDefinition | undefined }
export type RecipeProps<T> = T extends keyof ConfigRecipes
? ConfigRecipes[T]["__type"] & { recipe?: RecipeDefinition | undefined }
: { recipe?: RecipeDefinition | undefined }
`,
].join("\n"),
)
}
|