File size: 1,493 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
"use client"

import { useMemo } from "react"
import type { ConfigSlotRecipes } from "./generated/recipes.gen"
import { useChakraContext } from "./provider"
import type {
  RecipeVariantMap,
  RecipeVariantProps,
  SlotRecipeConfig,
  SystemSlotRecipeFn,
} from "./recipe.types"

export type SlotRecipeKey = keyof ConfigSlotRecipes | (string & {})

export type SlotRecipeFn<K extends SlotRecipeKey> =
  K extends keyof ConfigSlotRecipes
    ? ConfigSlotRecipes[K]
    : SystemSlotRecipeFn<string, {}, {}>

export interface UseSlotRecipeOptions<K extends SlotRecipeKey> {
  key?: K | undefined
  recipe?: SlotRecipeConfig | undefined
}

export function useSlotRecipe<
  Options extends { key: SlotRecipeKey; recipe?: SlotRecipeConfig | undefined },
>(
  options: Options,
): Options["key"] extends keyof ConfigSlotRecipes
  ? ConfigSlotRecipes[Options["key"]]
  : SystemSlotRecipeFn<string, {}, {}>

export function useSlotRecipe<Options extends { recipe: SlotRecipeConfig }>(
  options: Options,
): Options["recipe"] extends SlotRecipeConfig<infer S, infer T>
  ? SystemSlotRecipeFn<
      S,
      RecipeVariantProps<Options["recipe"]>,
      RecipeVariantMap<T>
    >
  : never

export function useSlotRecipe(options: any): any {
  const { key, recipe: recipeProp } = options
  const sys = useChakraContext()
  return useMemo((): any => {
    const recipe = recipeProp || (key != null ? sys.getSlotRecipe(key) : {})
    return sys.sva(structuredClone(recipe))
  }, [key, recipeProp, sys])
}