@pandacss/generator 1.5.0 → 1.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/dist/index.js +122 -41
  2. package/dist/index.mjs +122 -41
  3. package/package.json +8 -8
package/dist/index.js CHANGED
@@ -1228,12 +1228,33 @@ function generatePreactCreateStyleContext(ctx) {
1228
1228
  ${ctx.file.import("cx, css, sva", "../css/index")}
1229
1229
  ${ctx.file.import(factoryName, "./factory")}
1230
1230
  ${ctx.file.import("getDisplayName", "./factory-helper")}
1231
- import { createContext, useContext, createElement, forwardRef } from 'preact/compat'
1231
+ import { createContext } from 'preact'
1232
+ import { useContext } from 'preact/hooks'
1233
+ import { createElement, forwardRef } from 'preact/compat'
1234
+
1235
+ function createSafeContext(contextName) {
1236
+ const Context = createContext(undefined)
1237
+ const useStyleContext = (componentName, slot) => {
1238
+ const context = useContext(Context)
1239
+ if (context === undefined) {
1240
+ const componentInfo = componentName ? \`Component "\${componentName}"\` : 'A component'
1241
+ const slotInfo = slot ? \` (slot: "\${slot}")\` : ''
1242
+
1243
+ throw new Error(
1244
+ \`\${componentInfo}\${slotInfo} cannot access \${contextName} because it's missing its Provider.\`
1245
+ )
1246
+ }
1247
+ return context
1248
+ }
1249
+ return [Context, useStyleContext]
1250
+ }
1232
1251
 
1233
-
1234
1252
  export function createStyleContext(recipe) {
1235
- const StyleContext = createContext({})
1236
1253
  const isConfigRecipe = '__recipe__' in recipe
1254
+ const recipeName = isConfigRecipe && recipe.__name__ ? recipe.__name__ : undefined
1255
+ const contextName = recipeName ? \`createStyleContext("\${recipeName}")\` : 'createStyleContext'
1256
+
1257
+ const [StyleContext, useStyleContext] = createSafeContext(contextName)
1237
1258
  const svaFn = isConfigRecipe ? recipe : sva(recipe.config)
1238
1259
 
1239
1260
  const getResolvedProps = (props, slotStyles) => {
@@ -1273,7 +1294,7 @@ function generatePreactCreateStyleContext(ctx) {
1273
1294
  const withProvider = (Component, slot, options) => {
1274
1295
  const StyledComponent = ${factoryName}(Component, {}, options)
1275
1296
 
1276
- const WithProvider = forwardRef((props, ref) => {
1297
+ const WithProvider = forwardRef(function WithProvider(props, ref) {
1277
1298
  const [variantProps, restProps] = svaFn.splitVariantProps(props)
1278
1299
 
1279
1300
  const slotStyles = isConfigRecipe ? svaFn(variantProps) : svaFn.raw(variantProps)
@@ -1299,9 +1320,10 @@ function generatePreactCreateStyleContext(ctx) {
1299
1320
 
1300
1321
  const withContext = (Component, slot, options) => {
1301
1322
  const StyledComponent = ${factoryName}(Component, {}, options)
1323
+ const componentName = getDisplayName(Component)
1302
1324
 
1303
- const WithContext = forwardRef((props, ref) => {
1304
- const slotStyles = useContext(StyleContext)
1325
+ const WithContext = forwardRef(function WithContext(props, ref) {
1326
+ const slotStyles = useStyleContext(componentName, slot)
1305
1327
 
1306
1328
  const propsWithClass = { ...props, className: props.className ?? options?.defaultProps?.className }
1307
1329
  const resolvedProps = getResolvedProps(propsWithClass, slotStyles[slot])
@@ -1312,7 +1334,6 @@ function generatePreactCreateStyleContext(ctx) {
1312
1334
  })
1313
1335
  })
1314
1336
 
1315
- const componentName = getDisplayName(Component)
1316
1337
  WithContext.displayName = \`withContext(\${componentName})\`
1317
1338
 
1318
1339
  return WithContext
@@ -1328,7 +1349,7 @@ function generatePreactCreateStyleContext(ctx) {
1328
1349
  dts: import_outdent17.outdent`
1329
1350
  ${ctx.file.importType("SlotRecipeRuntimeFn, RecipeVariantProps", "../types/recipe")}
1330
1351
  ${ctx.file.importType("JsxHTMLProps, JsxStyleProps, Assign", "../types/system-types")}
1331
- ${ctx.file.importType("JsxFactoryOptions, DataAttrs", "../types/jsx")}
1352
+ ${ctx.file.importType("JsxFactoryOptions, DataAttrs, AsProps", "../types/jsx")}
1332
1353
  import type { ComponentType, ComponentProps, JSX } from 'preact/compat'
1333
1354
 
1334
1355
  interface UnstyledProps {
@@ -1352,7 +1373,7 @@ function generatePreactCreateStyleContext(ctx) {
1352
1373
  type InferSlot<R extends SlotRecipe> = R extends SlotRecipeFn ? R['__slot'] : R extends SvaFn<infer S> ? S : never
1353
1374
 
1354
1375
  type StyleContextProvider<T extends ElementType, R extends SlotRecipe> = ComponentType<
1355
- JsxHTMLProps<ComponentProps<T> & UnstyledProps, Assign<RecipeVariantProps<R>, JsxStyleProps>>
1376
+ JsxHTMLProps<ComponentProps<T> & UnstyledProps & AsProps, Assign<RecipeVariantProps<R>, JsxStyleProps>>
1356
1377
  >
1357
1378
 
1358
1379
  type StyleContextRootProvider<T extends ElementType, R extends SlotRecipe> = ComponentType<
@@ -1360,7 +1381,7 @@ function generatePreactCreateStyleContext(ctx) {
1360
1381
  >
1361
1382
 
1362
1383
  type StyleContextConsumer<T extends ElementType> = ComponentType<
1363
- JsxHTMLProps<ComponentProps<T> & UnstyledProps, JsxStyleProps>
1384
+ JsxHTMLProps<ComponentProps<T> & UnstyledProps & AsProps, JsxStyleProps>
1364
1385
  >
1365
1386
 
1366
1387
  export interface StyleContext<R extends SlotRecipe> {
@@ -2126,9 +2147,29 @@ function generateReactCreateStyleContext(ctx) {
2126
2147
  ${ctx.file.import("getDisplayName", "./factory-helper")}
2127
2148
  import { createContext, useContext, createElement, forwardRef } from 'react'
2128
2149
 
2150
+ function createSafeContext(contextName) {
2151
+ const Context = createContext(undefined)
2152
+ const useStyleContext = (componentName, slot) => {
2153
+ const context = useContext(Context)
2154
+ if (context === undefined) {
2155
+ const componentInfo = componentName ? \`Component "\${componentName}"\` : 'A component'
2156
+ const slotInfo = slot ? \` (slot: "\${slot}")\` : ''
2157
+
2158
+ throw new Error(
2159
+ \`\${componentInfo}\${slotInfo} cannot access \${contextName} because it's missing its Provider.\`
2160
+ )
2161
+ }
2162
+ return context
2163
+ }
2164
+ return [Context, useStyleContext]
2165
+ }
2166
+
2129
2167
  export function createStyleContext(recipe) {
2130
- const StyleContext = createContext({})
2131
2168
  const isConfigRecipe = '__recipe__' in recipe
2169
+ const recipeName = isConfigRecipe && recipe.__name__ ? recipe.__name__ : undefined
2170
+ const contextName = recipeName ? \`createStyleContext("\${recipeName}")\` : 'createStyleContext'
2171
+
2172
+ const [StyleContext, useStyleContext] = createSafeContext(contextName)
2132
2173
  const svaFn = isConfigRecipe ? recipe : sva(recipe.config)
2133
2174
 
2134
2175
  const getResolvedProps = (props, slotStyles) => {
@@ -2194,9 +2235,10 @@ function generateReactCreateStyleContext(ctx) {
2194
2235
 
2195
2236
  const withContext = (Component, slot, options) => {
2196
2237
  const StyledComponent = ${factoryName}(Component, {}, options)
2238
+ const componentName = getDisplayName(Component)
2197
2239
 
2198
2240
  const WithContext = forwardRef((props, ref) => {
2199
- const slotStyles = useContext(StyleContext)
2241
+ const slotStyles = useStyleContext(componentName, slot)
2200
2242
 
2201
2243
  const propsWithClass = { ...props, className: props.className ?? options?.defaultProps?.className }
2202
2244
  const resolvedProps = getResolvedProps(propsWithClass, slotStyles[slot])
@@ -2207,7 +2249,6 @@ function generateReactCreateStyleContext(ctx) {
2207
2249
  })
2208
2250
  })
2209
2251
 
2210
- const componentName = getDisplayName(Component)
2211
2252
  WithContext.displayName = \`withContext(\${componentName})\`
2212
2253
 
2213
2254
  return WithContext
@@ -2223,7 +2264,7 @@ function generateReactCreateStyleContext(ctx) {
2223
2264
  dts: import_outdent28.outdent`
2224
2265
  ${ctx.file.importType("SlotRecipeRuntimeFn, RecipeVariantProps", "../types/recipe")}
2225
2266
  ${ctx.file.importType("JsxHTMLProps, JsxStyleProps, Assign", "../types/system-types")}
2226
- ${ctx.file.importType("JsxFactoryOptions, ComponentProps, DataAttrs", "../types/jsx")}
2267
+ ${ctx.file.importType("JsxFactoryOptions, ComponentProps, DataAttrs, AsProps", "../types/jsx")}
2227
2268
  import type { ComponentType, ElementType } from 'react'
2228
2269
 
2229
2270
  interface UnstyledProps {
@@ -2245,7 +2286,7 @@ function generateReactCreateStyleContext(ctx) {
2245
2286
  }
2246
2287
 
2247
2288
  type StyleContextProvider<T extends ElementType, R extends SlotRecipe> = ComponentType<
2248
- JsxHTMLProps<ComponentProps<T> & UnstyledProps, Assign<RecipeVariantProps<R>, JsxStyleProps>>
2289
+ JsxHTMLProps<ComponentProps<T> & UnstyledProps & AsProps, Assign<RecipeVariantProps<R>, JsxStyleProps>>
2249
2290
  >
2250
2291
 
2251
2292
  type StyleContextRootProvider<T extends ElementType, R extends SlotRecipe> = ComponentType<
@@ -2253,7 +2294,7 @@ function generateReactCreateStyleContext(ctx) {
2253
2294
  >
2254
2295
 
2255
2296
  type StyleContextConsumer<T extends ElementType> = ComponentType<
2256
- JsxHTMLProps<ComponentProps<T> & UnstyledProps, JsxStyleProps>
2297
+ JsxHTMLProps<ComponentProps<T> & UnstyledProps & AsProps, JsxStyleProps>
2257
2298
  >
2258
2299
 
2259
2300
  export interface StyleContext<R extends SlotRecipe> {
@@ -2414,12 +2455,15 @@ function generateSolidJsxFactory(ctx) {
2414
2455
  return forwardFn(prop, cvaFn.variantKeys)
2415
2456
  }
2416
2457
 
2417
- const defaultProps = Object.assign(
2418
- options.dataAttr && configOrCva.__name__
2458
+ const getDefaultProps = () => {
2459
+ const baseDefaults = options.dataAttr && configOrCva.__name__
2419
2460
  ? { 'data-recipe': configOrCva.__name__ }
2420
- : {},
2421
- options.defaultProps
2422
- )
2461
+ : {}
2462
+ const defaults = typeof options.defaultProps === 'function'
2463
+ ? options.defaultProps()
2464
+ : options.defaultProps
2465
+ return Object.assign(baseDefaults, defaults)
2466
+ }
2423
2467
 
2424
2468
  const __cvaFn__ = composeCvaFn(element.__cva__, cvaFn)
2425
2469
  const __shouldForwardProps__ = composeShouldForwardProps(
@@ -2430,7 +2474,7 @@ function generateSolidJsxFactory(ctx) {
2430
2474
  const ${componentName} = (props) => {
2431
2475
  const mergedProps = mergeProps(
2432
2476
  { as: element.__base__ || element },
2433
- defaultProps,
2477
+ getDefaultProps(),
2434
2478
  props
2435
2479
  )
2436
2480
 
@@ -2620,12 +2664,9 @@ ${ctx.file.importType(upperName, "../types/jsx")}
2620
2664
  export declare const ${factoryName}: ${upperName}
2621
2665
  `,
2622
2666
  jsxType: import_outdent33.outdent`
2623
- import type { ComponentProps, Component, JSX } from 'solid-js'
2667
+ import type { Accessor, ComponentProps, Component, JSX } from 'solid-js'
2624
2668
  ${ctx.file.importType("RecipeDefinition, RecipeSelection, RecipeVariantRecord", "./recipe")}
2625
- ${ctx.file.importType(
2626
- "Assign, DistributiveOmit, DistributiveUnion, JsxHTMLProps, JsxStyleProps, Pretty",
2627
- "./system-types"
2628
- )}
2669
+ ${ctx.file.importType("Assign, DistributiveUnion, JsxHTMLProps, JsxStyleProps, Pretty", "./system-types")}
2629
2670
 
2630
2671
  interface Dict {
2631
2672
  [k: string]: unknown
@@ -2658,9 +2699,11 @@ interface RecipeFn {
2658
2699
  __type: any
2659
2700
  }
2660
2701
 
2702
+ export type MaybeAccessor<T> = T | Accessor<T>
2703
+
2661
2704
  export interface JsxFactoryOptions<TProps extends Dict> {
2662
2705
  dataAttr?: boolean
2663
- defaultProps?: Partial<TProps> & DataAttrs
2706
+ defaultProps?: MaybeAccessor<Partial<TProps> & DataAttrs>
2664
2707
  shouldForwardProp?: (prop: string, variantKeys: string[]) => boolean
2665
2708
  forwardProps?: string[]
2666
2709
  }
@@ -2706,9 +2749,29 @@ function generateSolidCreateStyleContext(ctx) {
2706
2749
  import { createComponent, mergeProps } from 'solid-js/web'
2707
2750
  import { createContext, createMemo, splitProps, useContext } from 'solid-js'
2708
2751
 
2752
+ function createSafeContext(contextName) {
2753
+ const Context = createContext(undefined)
2754
+ const useStyleContext = (componentName, slot) => {
2755
+ const context = useContext(Context)
2756
+ if (context === undefined) {
2757
+ const componentInfo = componentName ? \`Component "\${componentName}"\` : 'A component'
2758
+ const slotInfo = slot ? \` (slot: "\${slot}")\` : ''
2759
+
2760
+ throw new Error(
2761
+ \`\${componentInfo}\${slotInfo} cannot access \${contextName} because it's missing its Provider.\`
2762
+ )
2763
+ }
2764
+ return context
2765
+ }
2766
+ return [Context, useStyleContext]
2767
+ }
2768
+
2709
2769
  export function createStyleContext(recipe) {
2710
- const StyleContext = createContext({})
2711
2770
  const isConfigRecipe = '__recipe__' in recipe
2771
+ const recipeName = isConfigRecipe && recipe.__name__ ? recipe.__name__ : undefined
2772
+ const contextName = recipeName ? \`createStyleContext("\${recipeName}")\` : 'createStyleContext'
2773
+
2774
+ const [StyleContext, useStyleContext] = createSafeContext(contextName)
2712
2775
  const svaFn = isConfigRecipe ? recipe : sva(recipe.config)
2713
2776
 
2714
2777
  const getResolvedProps = (props, slotStyles) => {
@@ -2735,7 +2798,10 @@ function generateSolidCreateStyleContext(ctx) {
2735
2798
 
2736
2799
  const mergedProps = createMemo(() => {
2737
2800
  if (!options?.defaultProps) return propsWithoutChildren
2738
- return { ...options.defaultProps, ...propsWithoutChildren }
2801
+ const defaults = typeof options.defaultProps === 'function'
2802
+ ? options.defaultProps()
2803
+ : options.defaultProps
2804
+ return { ...defaults, ...propsWithoutChildren }
2739
2805
  })
2740
2806
 
2741
2807
  return createComponent(StyleContext.Provider, {
@@ -2807,9 +2873,10 @@ function generateSolidCreateStyleContext(ctx) {
2807
2873
 
2808
2874
  const withContext = (Component, slot, options) => {
2809
2875
  const StyledComponent = ${factoryName}(Component, {}, options)
2876
+ const componentName = getDisplayName(Component)
2810
2877
 
2811
2878
  const WithContext = (props) => {
2812
- const slotStyles = useContext(StyleContext)
2879
+ const slotStyles = useStyleContext(componentName, slot)
2813
2880
  const [local, propsWithoutChildren] = splitProps(props, ["children"])
2814
2881
 
2815
2882
  const resolvedProps = createMemo(() => {
@@ -2832,7 +2899,6 @@ function generateSolidCreateStyleContext(ctx) {
2832
2899
  )
2833
2900
  }
2834
2901
 
2835
- const componentName = getDisplayName(Component)
2836
2902
  WithContext.displayName = \`withContext(\${componentName})\`
2837
2903
  return WithContext
2838
2904
  }
@@ -2847,7 +2913,7 @@ function generateSolidCreateStyleContext(ctx) {
2847
2913
  dts: import_outdent34.outdent`
2848
2914
  ${ctx.file.importType("SlotRecipeRuntimeFn, RecipeVariantProps", "../types/recipe")}
2849
2915
  ${ctx.file.importType("JsxHTMLProps, JsxStyleProps, Assign", "../types/system-types")}
2850
- ${ctx.file.importType("JsxFactoryOptions, DataAttrs", "../types/jsx")}
2916
+ ${ctx.file.importType("JsxFactoryOptions, DataAttrs, MaybeAccessor, AsProps", "../types/jsx")}
2851
2917
  import type { Component, JSX, ComponentProps } from 'solid-js'
2852
2918
 
2853
2919
  interface UnstyledProps {
@@ -2855,7 +2921,7 @@ function generateSolidCreateStyleContext(ctx) {
2855
2921
  }
2856
2922
 
2857
2923
  interface WithProviderOptions<P> {
2858
- defaultProps?: (Partial<P> & DataAttrs) | undefined
2924
+ defaultProps?: MaybeAccessor<Partial<P> & DataAttrs> | undefined
2859
2925
  }
2860
2926
 
2861
2927
  type ElementType = keyof JSX.IntrinsicElements | Component<any>
@@ -2875,7 +2941,7 @@ function generateSolidCreateStyleContext(ctx) {
2875
2941
  : never
2876
2942
 
2877
2943
  type StyleContextProvider<T extends ElementType, R extends SlotRecipe> = Component<
2878
- JsxHTMLProps<ComponentProps<T> & UnstyledProps, Assign<RecipeVariantProps<R>, JsxStyleProps>>
2944
+ JsxHTMLProps<ComponentProps<T> & UnstyledProps & AsProps, Assign<RecipeVariantProps<R>, JsxStyleProps>>
2879
2945
  >
2880
2946
 
2881
2947
  type StyleContextRootProvider<T extends ElementType, R extends SlotRecipe> = Component<
@@ -2883,7 +2949,7 @@ function generateSolidCreateStyleContext(ctx) {
2883
2949
  >
2884
2950
 
2885
2951
  type StyleContextConsumer<T extends ElementType> = Component<
2886
- JsxHTMLProps<ComponentProps<T> & UnstyledProps, JsxStyleProps>
2952
+ JsxHTMLProps<ComponentProps<T> & UnstyledProps & AsProps, JsxStyleProps>
2887
2953
  >
2888
2954
 
2889
2955
  export interface StyleContext<R extends SlotRecipe> {
@@ -3343,7 +3409,22 @@ function generateVueCreateStyleContext(ctx) {
3343
3409
  export function createStyleContext(recipe) {
3344
3410
  const StyleContext = Symbol('StyleContext')
3345
3411
  const isConfigRecipe = '__recipe__' in recipe
3412
+ const recipeName = isConfigRecipe && recipe.__name__ ? recipe.__name__ : undefined
3413
+ const contextName = recipeName ? \`createStyleContext("\${recipeName}")\` : 'createStyleContext'
3346
3414
  const svaFn = isConfigRecipe ? recipe : sva(recipe.config)
3415
+
3416
+ function useStyleContext(componentName, slot) {
3417
+ const context = inject(StyleContext)
3418
+ if (context === undefined) {
3419
+ const componentInfo = componentName ? \`Component "\${componentName}"\` : 'A component'
3420
+ const slotInfo = slot ? \` (slot: "\${slot}")\` : ''
3421
+
3422
+ throw new Error(
3423
+ \`\${componentInfo}\${slotInfo} cannot access \${contextName} because it's missing its Provider.\`
3424
+ )
3425
+ }
3426
+ return context
3427
+ }
3347
3428
 
3348
3429
  const getResolvedProps = (props, slotStyles) => {
3349
3430
  const { unstyled, ...restProps } = props
@@ -3426,6 +3507,7 @@ function generateVueCreateStyleContext(ctx) {
3426
3507
 
3427
3508
  const withContext = (Component, slot, options) => {
3428
3509
  const StyledComponent = ${factoryName}(Component, {}, options)
3510
+ const componentName = getDisplayName(Component)
3429
3511
 
3430
3512
  const WithContext = defineComponent({
3431
3513
  props: ["unstyled"],
@@ -3436,7 +3518,7 @@ function generateVueCreateStyleContext(ctx) {
3436
3518
  propsWithClass.class = propsWithClass.class ?? options?.defaultProps?.class
3437
3519
  return propsWithClass
3438
3520
  })
3439
- const slotStyles = inject(StyleContext)
3521
+ const slotStyles = useStyleContext(componentName, slot)
3440
3522
 
3441
3523
  return () => {
3442
3524
  const resolvedProps = getResolvedProps(props.value, slotStyles.value[slot])
@@ -3446,7 +3528,6 @@ function generateVueCreateStyleContext(ctx) {
3446
3528
  },
3447
3529
  })
3448
3530
 
3449
- const componentName = getDisplayName(Component)
3450
3531
  WithContext.displayName = \`withContext(\${componentName})\`
3451
3532
 
3452
3533
  return WithContext
@@ -3462,7 +3543,7 @@ function generateVueCreateStyleContext(ctx) {
3462
3543
  dts: import_outdent40.outdent`
3463
3544
  ${ctx.file.importType("SlotRecipeRuntimeFn, RecipeVariantProps", "../types/recipe")}
3464
3545
  ${ctx.file.importType("JsxHTMLProps, JsxStyleProps, Assign", "../types/system-types")}
3465
- ${ctx.file.importType("JsxFactoryOptions, DataAttrs", "../types/jsx")}
3546
+ ${ctx.file.importType("JsxFactoryOptions, DataAttrs, AsProps", "../types/jsx")}
3466
3547
  import type { Component, FunctionalComponent, NativeElements } from 'vue'
3467
3548
 
3468
3549
  interface UnstyledProps {
@@ -3499,7 +3580,7 @@ function generateVueCreateStyleContext(ctx) {
3499
3580
  : never
3500
3581
 
3501
3582
  type StyleContextProvider<T extends ElementType, R extends SlotRecipe> = FunctionalComponent<
3502
- JsxHTMLProps<ComponentProps<T> & UnstyledProps & VModelProps, Assign<RecipeVariantProps<R>, JsxStyleProps>>
3583
+ JsxHTMLProps<ComponentProps<T> & UnstyledProps & AsProps & VModelProps, Assign<RecipeVariantProps<R>, JsxStyleProps>>
3503
3584
  >
3504
3585
 
3505
3586
  type StyleContextRootProvider<T extends ElementType, R extends SlotRecipe> = FunctionalComponent<
@@ -3507,7 +3588,7 @@ function generateVueCreateStyleContext(ctx) {
3507
3588
  >
3508
3589
 
3509
3590
  type StyleContextConsumer<T extends ElementType> = FunctionalComponent<
3510
- JsxHTMLProps<ComponentProps<T> & UnstyledProps & VModelProps, JsxStyleProps>
3591
+ JsxHTMLProps<ComponentProps<T> & UnstyledProps & AsProps & VModelProps, JsxStyleProps>
3511
3592
  >
3512
3593
 
3513
3594
  export interface StyleContext<R extends SlotRecipe> {
package/dist/index.mjs CHANGED
@@ -1192,12 +1192,33 @@ function generatePreactCreateStyleContext(ctx) {
1192
1192
  ${ctx.file.import("cx, css, sva", "../css/index")}
1193
1193
  ${ctx.file.import(factoryName, "./factory")}
1194
1194
  ${ctx.file.import("getDisplayName", "./factory-helper")}
1195
- import { createContext, useContext, createElement, forwardRef } from 'preact/compat'
1195
+ import { createContext } from 'preact'
1196
+ import { useContext } from 'preact/hooks'
1197
+ import { createElement, forwardRef } from 'preact/compat'
1198
+
1199
+ function createSafeContext(contextName) {
1200
+ const Context = createContext(undefined)
1201
+ const useStyleContext = (componentName, slot) => {
1202
+ const context = useContext(Context)
1203
+ if (context === undefined) {
1204
+ const componentInfo = componentName ? \`Component "\${componentName}"\` : 'A component'
1205
+ const slotInfo = slot ? \` (slot: "\${slot}")\` : ''
1206
+
1207
+ throw new Error(
1208
+ \`\${componentInfo}\${slotInfo} cannot access \${contextName} because it's missing its Provider.\`
1209
+ )
1210
+ }
1211
+ return context
1212
+ }
1213
+ return [Context, useStyleContext]
1214
+ }
1196
1215
 
1197
-
1198
1216
  export function createStyleContext(recipe) {
1199
- const StyleContext = createContext({})
1200
1217
  const isConfigRecipe = '__recipe__' in recipe
1218
+ const recipeName = isConfigRecipe && recipe.__name__ ? recipe.__name__ : undefined
1219
+ const contextName = recipeName ? \`createStyleContext("\${recipeName}")\` : 'createStyleContext'
1220
+
1221
+ const [StyleContext, useStyleContext] = createSafeContext(contextName)
1201
1222
  const svaFn = isConfigRecipe ? recipe : sva(recipe.config)
1202
1223
 
1203
1224
  const getResolvedProps = (props, slotStyles) => {
@@ -1237,7 +1258,7 @@ function generatePreactCreateStyleContext(ctx) {
1237
1258
  const withProvider = (Component, slot, options) => {
1238
1259
  const StyledComponent = ${factoryName}(Component, {}, options)
1239
1260
 
1240
- const WithProvider = forwardRef((props, ref) => {
1261
+ const WithProvider = forwardRef(function WithProvider(props, ref) {
1241
1262
  const [variantProps, restProps] = svaFn.splitVariantProps(props)
1242
1263
 
1243
1264
  const slotStyles = isConfigRecipe ? svaFn(variantProps) : svaFn.raw(variantProps)
@@ -1263,9 +1284,10 @@ function generatePreactCreateStyleContext(ctx) {
1263
1284
 
1264
1285
  const withContext = (Component, slot, options) => {
1265
1286
  const StyledComponent = ${factoryName}(Component, {}, options)
1287
+ const componentName = getDisplayName(Component)
1266
1288
 
1267
- const WithContext = forwardRef((props, ref) => {
1268
- const slotStyles = useContext(StyleContext)
1289
+ const WithContext = forwardRef(function WithContext(props, ref) {
1290
+ const slotStyles = useStyleContext(componentName, slot)
1269
1291
 
1270
1292
  const propsWithClass = { ...props, className: props.className ?? options?.defaultProps?.className }
1271
1293
  const resolvedProps = getResolvedProps(propsWithClass, slotStyles[slot])
@@ -1276,7 +1298,6 @@ function generatePreactCreateStyleContext(ctx) {
1276
1298
  })
1277
1299
  })
1278
1300
 
1279
- const componentName = getDisplayName(Component)
1280
1301
  WithContext.displayName = \`withContext(\${componentName})\`
1281
1302
 
1282
1303
  return WithContext
@@ -1292,7 +1313,7 @@ function generatePreactCreateStyleContext(ctx) {
1292
1313
  dts: outdent17`
1293
1314
  ${ctx.file.importType("SlotRecipeRuntimeFn, RecipeVariantProps", "../types/recipe")}
1294
1315
  ${ctx.file.importType("JsxHTMLProps, JsxStyleProps, Assign", "../types/system-types")}
1295
- ${ctx.file.importType("JsxFactoryOptions, DataAttrs", "../types/jsx")}
1316
+ ${ctx.file.importType("JsxFactoryOptions, DataAttrs, AsProps", "../types/jsx")}
1296
1317
  import type { ComponentType, ComponentProps, JSX } from 'preact/compat'
1297
1318
 
1298
1319
  interface UnstyledProps {
@@ -1316,7 +1337,7 @@ function generatePreactCreateStyleContext(ctx) {
1316
1337
  type InferSlot<R extends SlotRecipe> = R extends SlotRecipeFn ? R['__slot'] : R extends SvaFn<infer S> ? S : never
1317
1338
 
1318
1339
  type StyleContextProvider<T extends ElementType, R extends SlotRecipe> = ComponentType<
1319
- JsxHTMLProps<ComponentProps<T> & UnstyledProps, Assign<RecipeVariantProps<R>, JsxStyleProps>>
1340
+ JsxHTMLProps<ComponentProps<T> & UnstyledProps & AsProps, Assign<RecipeVariantProps<R>, JsxStyleProps>>
1320
1341
  >
1321
1342
 
1322
1343
  type StyleContextRootProvider<T extends ElementType, R extends SlotRecipe> = ComponentType<
@@ -1324,7 +1345,7 @@ function generatePreactCreateStyleContext(ctx) {
1324
1345
  >
1325
1346
 
1326
1347
  type StyleContextConsumer<T extends ElementType> = ComponentType<
1327
- JsxHTMLProps<ComponentProps<T> & UnstyledProps, JsxStyleProps>
1348
+ JsxHTMLProps<ComponentProps<T> & UnstyledProps & AsProps, JsxStyleProps>
1328
1349
  >
1329
1350
 
1330
1351
  export interface StyleContext<R extends SlotRecipe> {
@@ -2090,9 +2111,29 @@ function generateReactCreateStyleContext(ctx) {
2090
2111
  ${ctx.file.import("getDisplayName", "./factory-helper")}
2091
2112
  import { createContext, useContext, createElement, forwardRef } from 'react'
2092
2113
 
2114
+ function createSafeContext(contextName) {
2115
+ const Context = createContext(undefined)
2116
+ const useStyleContext = (componentName, slot) => {
2117
+ const context = useContext(Context)
2118
+ if (context === undefined) {
2119
+ const componentInfo = componentName ? \`Component "\${componentName}"\` : 'A component'
2120
+ const slotInfo = slot ? \` (slot: "\${slot}")\` : ''
2121
+
2122
+ throw new Error(
2123
+ \`\${componentInfo}\${slotInfo} cannot access \${contextName} because it's missing its Provider.\`
2124
+ )
2125
+ }
2126
+ return context
2127
+ }
2128
+ return [Context, useStyleContext]
2129
+ }
2130
+
2093
2131
  export function createStyleContext(recipe) {
2094
- const StyleContext = createContext({})
2095
2132
  const isConfigRecipe = '__recipe__' in recipe
2133
+ const recipeName = isConfigRecipe && recipe.__name__ ? recipe.__name__ : undefined
2134
+ const contextName = recipeName ? \`createStyleContext("\${recipeName}")\` : 'createStyleContext'
2135
+
2136
+ const [StyleContext, useStyleContext] = createSafeContext(contextName)
2096
2137
  const svaFn = isConfigRecipe ? recipe : sva(recipe.config)
2097
2138
 
2098
2139
  const getResolvedProps = (props, slotStyles) => {
@@ -2158,9 +2199,10 @@ function generateReactCreateStyleContext(ctx) {
2158
2199
 
2159
2200
  const withContext = (Component, slot, options) => {
2160
2201
  const StyledComponent = ${factoryName}(Component, {}, options)
2202
+ const componentName = getDisplayName(Component)
2161
2203
 
2162
2204
  const WithContext = forwardRef((props, ref) => {
2163
- const slotStyles = useContext(StyleContext)
2205
+ const slotStyles = useStyleContext(componentName, slot)
2164
2206
 
2165
2207
  const propsWithClass = { ...props, className: props.className ?? options?.defaultProps?.className }
2166
2208
  const resolvedProps = getResolvedProps(propsWithClass, slotStyles[slot])
@@ -2171,7 +2213,6 @@ function generateReactCreateStyleContext(ctx) {
2171
2213
  })
2172
2214
  })
2173
2215
 
2174
- const componentName = getDisplayName(Component)
2175
2216
  WithContext.displayName = \`withContext(\${componentName})\`
2176
2217
 
2177
2218
  return WithContext
@@ -2187,7 +2228,7 @@ function generateReactCreateStyleContext(ctx) {
2187
2228
  dts: outdent28`
2188
2229
  ${ctx.file.importType("SlotRecipeRuntimeFn, RecipeVariantProps", "../types/recipe")}
2189
2230
  ${ctx.file.importType("JsxHTMLProps, JsxStyleProps, Assign", "../types/system-types")}
2190
- ${ctx.file.importType("JsxFactoryOptions, ComponentProps, DataAttrs", "../types/jsx")}
2231
+ ${ctx.file.importType("JsxFactoryOptions, ComponentProps, DataAttrs, AsProps", "../types/jsx")}
2191
2232
  import type { ComponentType, ElementType } from 'react'
2192
2233
 
2193
2234
  interface UnstyledProps {
@@ -2209,7 +2250,7 @@ function generateReactCreateStyleContext(ctx) {
2209
2250
  }
2210
2251
 
2211
2252
  type StyleContextProvider<T extends ElementType, R extends SlotRecipe> = ComponentType<
2212
- JsxHTMLProps<ComponentProps<T> & UnstyledProps, Assign<RecipeVariantProps<R>, JsxStyleProps>>
2253
+ JsxHTMLProps<ComponentProps<T> & UnstyledProps & AsProps, Assign<RecipeVariantProps<R>, JsxStyleProps>>
2213
2254
  >
2214
2255
 
2215
2256
  type StyleContextRootProvider<T extends ElementType, R extends SlotRecipe> = ComponentType<
@@ -2217,7 +2258,7 @@ function generateReactCreateStyleContext(ctx) {
2217
2258
  >
2218
2259
 
2219
2260
  type StyleContextConsumer<T extends ElementType> = ComponentType<
2220
- JsxHTMLProps<ComponentProps<T> & UnstyledProps, JsxStyleProps>
2261
+ JsxHTMLProps<ComponentProps<T> & UnstyledProps & AsProps, JsxStyleProps>
2221
2262
  >
2222
2263
 
2223
2264
  export interface StyleContext<R extends SlotRecipe> {
@@ -2378,12 +2419,15 @@ function generateSolidJsxFactory(ctx) {
2378
2419
  return forwardFn(prop, cvaFn.variantKeys)
2379
2420
  }
2380
2421
 
2381
- const defaultProps = Object.assign(
2382
- options.dataAttr && configOrCva.__name__
2422
+ const getDefaultProps = () => {
2423
+ const baseDefaults = options.dataAttr && configOrCva.__name__
2383
2424
  ? { 'data-recipe': configOrCva.__name__ }
2384
- : {},
2385
- options.defaultProps
2386
- )
2425
+ : {}
2426
+ const defaults = typeof options.defaultProps === 'function'
2427
+ ? options.defaultProps()
2428
+ : options.defaultProps
2429
+ return Object.assign(baseDefaults, defaults)
2430
+ }
2387
2431
 
2388
2432
  const __cvaFn__ = composeCvaFn(element.__cva__, cvaFn)
2389
2433
  const __shouldForwardProps__ = composeShouldForwardProps(
@@ -2394,7 +2438,7 @@ function generateSolidJsxFactory(ctx) {
2394
2438
  const ${componentName} = (props) => {
2395
2439
  const mergedProps = mergeProps(
2396
2440
  { as: element.__base__ || element },
2397
- defaultProps,
2441
+ getDefaultProps(),
2398
2442
  props
2399
2443
  )
2400
2444
 
@@ -2584,12 +2628,9 @@ ${ctx.file.importType(upperName, "../types/jsx")}
2584
2628
  export declare const ${factoryName}: ${upperName}
2585
2629
  `,
2586
2630
  jsxType: outdent33`
2587
- import type { ComponentProps, Component, JSX } from 'solid-js'
2631
+ import type { Accessor, ComponentProps, Component, JSX } from 'solid-js'
2588
2632
  ${ctx.file.importType("RecipeDefinition, RecipeSelection, RecipeVariantRecord", "./recipe")}
2589
- ${ctx.file.importType(
2590
- "Assign, DistributiveOmit, DistributiveUnion, JsxHTMLProps, JsxStyleProps, Pretty",
2591
- "./system-types"
2592
- )}
2633
+ ${ctx.file.importType("Assign, DistributiveUnion, JsxHTMLProps, JsxStyleProps, Pretty", "./system-types")}
2593
2634
 
2594
2635
  interface Dict {
2595
2636
  [k: string]: unknown
@@ -2622,9 +2663,11 @@ interface RecipeFn {
2622
2663
  __type: any
2623
2664
  }
2624
2665
 
2666
+ export type MaybeAccessor<T> = T | Accessor<T>
2667
+
2625
2668
  export interface JsxFactoryOptions<TProps extends Dict> {
2626
2669
  dataAttr?: boolean
2627
- defaultProps?: Partial<TProps> & DataAttrs
2670
+ defaultProps?: MaybeAccessor<Partial<TProps> & DataAttrs>
2628
2671
  shouldForwardProp?: (prop: string, variantKeys: string[]) => boolean
2629
2672
  forwardProps?: string[]
2630
2673
  }
@@ -2670,9 +2713,29 @@ function generateSolidCreateStyleContext(ctx) {
2670
2713
  import { createComponent, mergeProps } from 'solid-js/web'
2671
2714
  import { createContext, createMemo, splitProps, useContext } from 'solid-js'
2672
2715
 
2716
+ function createSafeContext(contextName) {
2717
+ const Context = createContext(undefined)
2718
+ const useStyleContext = (componentName, slot) => {
2719
+ const context = useContext(Context)
2720
+ if (context === undefined) {
2721
+ const componentInfo = componentName ? \`Component "\${componentName}"\` : 'A component'
2722
+ const slotInfo = slot ? \` (slot: "\${slot}")\` : ''
2723
+
2724
+ throw new Error(
2725
+ \`\${componentInfo}\${slotInfo} cannot access \${contextName} because it's missing its Provider.\`
2726
+ )
2727
+ }
2728
+ return context
2729
+ }
2730
+ return [Context, useStyleContext]
2731
+ }
2732
+
2673
2733
  export function createStyleContext(recipe) {
2674
- const StyleContext = createContext({})
2675
2734
  const isConfigRecipe = '__recipe__' in recipe
2735
+ const recipeName = isConfigRecipe && recipe.__name__ ? recipe.__name__ : undefined
2736
+ const contextName = recipeName ? \`createStyleContext("\${recipeName}")\` : 'createStyleContext'
2737
+
2738
+ const [StyleContext, useStyleContext] = createSafeContext(contextName)
2676
2739
  const svaFn = isConfigRecipe ? recipe : sva(recipe.config)
2677
2740
 
2678
2741
  const getResolvedProps = (props, slotStyles) => {
@@ -2699,7 +2762,10 @@ function generateSolidCreateStyleContext(ctx) {
2699
2762
 
2700
2763
  const mergedProps = createMemo(() => {
2701
2764
  if (!options?.defaultProps) return propsWithoutChildren
2702
- return { ...options.defaultProps, ...propsWithoutChildren }
2765
+ const defaults = typeof options.defaultProps === 'function'
2766
+ ? options.defaultProps()
2767
+ : options.defaultProps
2768
+ return { ...defaults, ...propsWithoutChildren }
2703
2769
  })
2704
2770
 
2705
2771
  return createComponent(StyleContext.Provider, {
@@ -2771,9 +2837,10 @@ function generateSolidCreateStyleContext(ctx) {
2771
2837
 
2772
2838
  const withContext = (Component, slot, options) => {
2773
2839
  const StyledComponent = ${factoryName}(Component, {}, options)
2840
+ const componentName = getDisplayName(Component)
2774
2841
 
2775
2842
  const WithContext = (props) => {
2776
- const slotStyles = useContext(StyleContext)
2843
+ const slotStyles = useStyleContext(componentName, slot)
2777
2844
  const [local, propsWithoutChildren] = splitProps(props, ["children"])
2778
2845
 
2779
2846
  const resolvedProps = createMemo(() => {
@@ -2796,7 +2863,6 @@ function generateSolidCreateStyleContext(ctx) {
2796
2863
  )
2797
2864
  }
2798
2865
 
2799
- const componentName = getDisplayName(Component)
2800
2866
  WithContext.displayName = \`withContext(\${componentName})\`
2801
2867
  return WithContext
2802
2868
  }
@@ -2811,7 +2877,7 @@ function generateSolidCreateStyleContext(ctx) {
2811
2877
  dts: outdent34`
2812
2878
  ${ctx.file.importType("SlotRecipeRuntimeFn, RecipeVariantProps", "../types/recipe")}
2813
2879
  ${ctx.file.importType("JsxHTMLProps, JsxStyleProps, Assign", "../types/system-types")}
2814
- ${ctx.file.importType("JsxFactoryOptions, DataAttrs", "../types/jsx")}
2880
+ ${ctx.file.importType("JsxFactoryOptions, DataAttrs, MaybeAccessor, AsProps", "../types/jsx")}
2815
2881
  import type { Component, JSX, ComponentProps } from 'solid-js'
2816
2882
 
2817
2883
  interface UnstyledProps {
@@ -2819,7 +2885,7 @@ function generateSolidCreateStyleContext(ctx) {
2819
2885
  }
2820
2886
 
2821
2887
  interface WithProviderOptions<P> {
2822
- defaultProps?: (Partial<P> & DataAttrs) | undefined
2888
+ defaultProps?: MaybeAccessor<Partial<P> & DataAttrs> | undefined
2823
2889
  }
2824
2890
 
2825
2891
  type ElementType = keyof JSX.IntrinsicElements | Component<any>
@@ -2839,7 +2905,7 @@ function generateSolidCreateStyleContext(ctx) {
2839
2905
  : never
2840
2906
 
2841
2907
  type StyleContextProvider<T extends ElementType, R extends SlotRecipe> = Component<
2842
- JsxHTMLProps<ComponentProps<T> & UnstyledProps, Assign<RecipeVariantProps<R>, JsxStyleProps>>
2908
+ JsxHTMLProps<ComponentProps<T> & UnstyledProps & AsProps, Assign<RecipeVariantProps<R>, JsxStyleProps>>
2843
2909
  >
2844
2910
 
2845
2911
  type StyleContextRootProvider<T extends ElementType, R extends SlotRecipe> = Component<
@@ -2847,7 +2913,7 @@ function generateSolidCreateStyleContext(ctx) {
2847
2913
  >
2848
2914
 
2849
2915
  type StyleContextConsumer<T extends ElementType> = Component<
2850
- JsxHTMLProps<ComponentProps<T> & UnstyledProps, JsxStyleProps>
2916
+ JsxHTMLProps<ComponentProps<T> & UnstyledProps & AsProps, JsxStyleProps>
2851
2917
  >
2852
2918
 
2853
2919
  export interface StyleContext<R extends SlotRecipe> {
@@ -3307,7 +3373,22 @@ function generateVueCreateStyleContext(ctx) {
3307
3373
  export function createStyleContext(recipe) {
3308
3374
  const StyleContext = Symbol('StyleContext')
3309
3375
  const isConfigRecipe = '__recipe__' in recipe
3376
+ const recipeName = isConfigRecipe && recipe.__name__ ? recipe.__name__ : undefined
3377
+ const contextName = recipeName ? \`createStyleContext("\${recipeName}")\` : 'createStyleContext'
3310
3378
  const svaFn = isConfigRecipe ? recipe : sva(recipe.config)
3379
+
3380
+ function useStyleContext(componentName, slot) {
3381
+ const context = inject(StyleContext)
3382
+ if (context === undefined) {
3383
+ const componentInfo = componentName ? \`Component "\${componentName}"\` : 'A component'
3384
+ const slotInfo = slot ? \` (slot: "\${slot}")\` : ''
3385
+
3386
+ throw new Error(
3387
+ \`\${componentInfo}\${slotInfo} cannot access \${contextName} because it's missing its Provider.\`
3388
+ )
3389
+ }
3390
+ return context
3391
+ }
3311
3392
 
3312
3393
  const getResolvedProps = (props, slotStyles) => {
3313
3394
  const { unstyled, ...restProps } = props
@@ -3390,6 +3471,7 @@ function generateVueCreateStyleContext(ctx) {
3390
3471
 
3391
3472
  const withContext = (Component, slot, options) => {
3392
3473
  const StyledComponent = ${factoryName}(Component, {}, options)
3474
+ const componentName = getDisplayName(Component)
3393
3475
 
3394
3476
  const WithContext = defineComponent({
3395
3477
  props: ["unstyled"],
@@ -3400,7 +3482,7 @@ function generateVueCreateStyleContext(ctx) {
3400
3482
  propsWithClass.class = propsWithClass.class ?? options?.defaultProps?.class
3401
3483
  return propsWithClass
3402
3484
  })
3403
- const slotStyles = inject(StyleContext)
3485
+ const slotStyles = useStyleContext(componentName, slot)
3404
3486
 
3405
3487
  return () => {
3406
3488
  const resolvedProps = getResolvedProps(props.value, slotStyles.value[slot])
@@ -3410,7 +3492,6 @@ function generateVueCreateStyleContext(ctx) {
3410
3492
  },
3411
3493
  })
3412
3494
 
3413
- const componentName = getDisplayName(Component)
3414
3495
  WithContext.displayName = \`withContext(\${componentName})\`
3415
3496
 
3416
3497
  return WithContext
@@ -3426,7 +3507,7 @@ function generateVueCreateStyleContext(ctx) {
3426
3507
  dts: outdent40`
3427
3508
  ${ctx.file.importType("SlotRecipeRuntimeFn, RecipeVariantProps", "../types/recipe")}
3428
3509
  ${ctx.file.importType("JsxHTMLProps, JsxStyleProps, Assign", "../types/system-types")}
3429
- ${ctx.file.importType("JsxFactoryOptions, DataAttrs", "../types/jsx")}
3510
+ ${ctx.file.importType("JsxFactoryOptions, DataAttrs, AsProps", "../types/jsx")}
3430
3511
  import type { Component, FunctionalComponent, NativeElements } from 'vue'
3431
3512
 
3432
3513
  interface UnstyledProps {
@@ -3463,7 +3544,7 @@ function generateVueCreateStyleContext(ctx) {
3463
3544
  : never
3464
3545
 
3465
3546
  type StyleContextProvider<T extends ElementType, R extends SlotRecipe> = FunctionalComponent<
3466
- JsxHTMLProps<ComponentProps<T> & UnstyledProps & VModelProps, Assign<RecipeVariantProps<R>, JsxStyleProps>>
3547
+ JsxHTMLProps<ComponentProps<T> & UnstyledProps & AsProps & VModelProps, Assign<RecipeVariantProps<R>, JsxStyleProps>>
3467
3548
  >
3468
3549
 
3469
3550
  type StyleContextRootProvider<T extends ElementType, R extends SlotRecipe> = FunctionalComponent<
@@ -3471,7 +3552,7 @@ function generateVueCreateStyleContext(ctx) {
3471
3552
  >
3472
3553
 
3473
3554
  type StyleContextConsumer<T extends ElementType> = FunctionalComponent<
3474
- JsxHTMLProps<ComponentProps<T> & UnstyledProps & VModelProps, JsxStyleProps>
3555
+ JsxHTMLProps<ComponentProps<T> & UnstyledProps & AsProps & VModelProps, JsxStyleProps>
3475
3556
  >
3476
3557
 
3477
3558
  export interface StyleContext<R extends SlotRecipe> {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pandacss/generator",
3
- "version": "1.5.0",
3
+ "version": "1.5.1",
4
4
  "description": "The css generator for css panda",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -37,13 +37,13 @@
37
37
  "outdent": " ^0.8.0",
38
38
  "pluralize": "8.0.0",
39
39
  "postcss": "8.5.6",
40
- "ts-pattern": "5.8.0",
41
- "@pandacss/core": "1.5.0",
42
- "@pandacss/is-valid-prop": "^1.5.0",
43
- "@pandacss/logger": "1.5.0",
44
- "@pandacss/shared": "1.5.0",
45
- "@pandacss/token-dictionary": "1.5.0",
46
- "@pandacss/types": "1.5.0"
40
+ "ts-pattern": "5.9.0",
41
+ "@pandacss/core": "1.5.1",
42
+ "@pandacss/is-valid-prop": "^1.5.1",
43
+ "@pandacss/logger": "1.5.1",
44
+ "@pandacss/shared": "1.5.1",
45
+ "@pandacss/token-dictionary": "1.5.1",
46
+ "@pandacss/types": "1.5.1"
47
47
  },
48
48
  "devDependencies": {
49
49
  "@types/pluralize": "0.0.33"