clava 0.6.3 → 0.7.0

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.
@@ -23,7 +23,7 @@ export const REFINE_UNSTABLE_TRACKING_WINDOW = 10;
23
23
  // build time can drop the entire warning machinery. The underlying `.stack`
24
24
  // string is formatted lazily on first access in every major engine (V8,
25
25
  // SpiderMonkey, JavaScriptCore), so holding the captured frame for the
26
- // lifetime of the component is cheap when no warning fires.
26
+ // lifetime of the recipe is cheap when no warning fires.
27
27
  export function captureCreationFrame(
28
28
  skipFn: Function,
29
29
  ): CreationFrame | undefined {
@@ -36,8 +36,8 @@ export function captureCreationFrame(
36
36
  // Engines without `Error.captureStackTrace` (SpiderMonkey, JavaScriptCore)
37
37
  // can't strip internal frames, but their `Error.stack` getter is still
38
38
  // lazy, so returning the Error instance defers the format cost. The
39
- // resulting trace includes 1–2 extra frames at the top from this helper and
40
- // `cv` itself.
39
+ // resulting trace includes extra frames from this helper, `buildRecipe`,
40
+ // and `cv` itself.
41
41
  return new Error();
42
42
  }
43
43
 
@@ -68,6 +68,9 @@ export function formatCreationStack(frame: CreationFrame): string | undefined {
68
68
 
69
69
  function isInternalCreationFrame(line: string): boolean {
70
70
  if (line.includes("captureCreationFrame")) return true;
71
+ if (line.startsWith("at buildRecipe ")) return true;
72
+ if (line.startsWith("at buildRecipe(")) return true;
73
+ if (line.startsWith("buildRecipe@")) return true;
71
74
  if (line.startsWith("at cv ")) return true;
72
75
  if (line.startsWith("at cv(")) return true;
73
76
  if (line.startsWith("cv@")) return true;
@@ -148,7 +151,7 @@ export function warnRefineLimit({
148
151
  if (creationFrame) {
149
152
  const creationStack = formatCreationStack(creationFrame);
150
153
  if (creationStack) {
151
- message += `\nComponent created at:\n${creationStack}`;
154
+ message += `\nRecipe created at:\n${creationStack}`;
152
155
  }
153
156
  }
154
157
  console.warn(message);
package/src/types.ts CHANGED
@@ -48,7 +48,7 @@ export type StyleProperty =
48
48
  | string;
49
49
 
50
50
  /**
51
- * The prop object returned by a component's `.jsx()` mode.
51
+ * The prop object returned by a recipe's `.jsx()` mode.
52
52
  *
53
53
  * @example
54
54
  * ```ts
@@ -64,7 +64,7 @@ export interface JSXProps {
64
64
  }
65
65
 
66
66
  /**
67
- * The prop object returned by a component's `.html()` mode. The `style` value
67
+ * The prop object returned by a recipe's `.html()` mode. The `style` value
68
68
  * is serialized as an HTML style string.
69
69
  *
70
70
  * @example
@@ -81,7 +81,7 @@ export interface HTMLProps {
81
81
  }
82
82
 
83
83
  /**
84
- * The prop object returned by a component's `.htmlObj()` mode. The `style`
84
+ * The prop object returned by a recipe's `.htmlObj()` mode. The `style`
85
85
  * value uses hyphenated CSS property names.
86
86
  *
87
87
  * @example
@@ -98,7 +98,7 @@ export interface HTMLObjProps {
98
98
  }
99
99
 
100
100
  /**
101
- * The default prop object returned by a Clava component. It uses `class`
101
+ * The default prop object returned by a Clava recipe. It uses `class`
102
102
  * rather than `className` and keeps styles as a normalized object.
103
103
  *
104
104
  * @example
@@ -120,18 +120,18 @@ export interface StyleProps {
120
120
  htmlObj: HTMLObjProps;
121
121
  }
122
122
 
123
- export type ComponentResult =
123
+ export type RecipeResult =
124
124
  | JSXProps
125
125
  | HTMLProps
126
126
  | HTMLObjProps
127
127
  | StyleClassProps;
128
128
 
129
- type AllComponentResultKeys =
129
+ type AllRecipeResultKeys =
130
130
  | keyof JSXProps
131
131
  | keyof HTMLProps
132
132
  | keyof HTMLObjProps;
133
133
 
134
- type ComponentResultValue<K extends AllComponentResultKeys> = K extends "style"
134
+ type RecipeResultValue<K extends AllRecipeResultKeys> = K extends "style"
135
135
  ? StyleProperty
136
136
  : K extends "className"
137
137
  ? string
@@ -139,31 +139,31 @@ type ComponentResultValue<K extends AllComponentResultKeys> = K extends "style"
139
139
  ? string
140
140
  : never;
141
141
 
142
- export type NullableComponentResult = {
143
- [K in AllComponentResultKeys]?: ComponentResultValue<K> | null;
142
+ export type NullableRecipeResult = {
143
+ [K in AllRecipeResultKeys]?: RecipeResultValue<K> | null;
144
144
  };
145
145
 
146
- export type ComponentProps<V = {}> = VariantValues<V> & NullableComponentResult;
146
+ export type RecipeProps<V = {}> = VariantValues<V> & NullableRecipeResult;
147
147
 
148
148
  export type GetVariants<V> = (variants?: VariantValues<V>) => VariantValues<V>;
149
149
 
150
- type ComponentPropKey<R extends ComponentResult> =
150
+ type RecipePropKey<R extends RecipeResult> =
151
151
  | keyof R
152
152
  | (R extends StyleClassProps ? "className" : never);
153
153
 
154
154
  // Key source types - what can be passed as additional parameters to splitProps
155
155
  export type KeySourceArray = readonly string[];
156
- export interface KeySourceComponent {
156
+ export interface KeySourceRecipe {
157
157
  propKeys: readonly string[];
158
158
  variantKeys: readonly string[];
159
159
  getVariants: () => Record<string, unknown>;
160
160
  }
161
- export type KeySource = KeySourceArray | KeySourceComponent;
161
+ export type KeySource = KeySourceArray | KeySourceRecipe;
162
162
 
163
- // Check if source is a component (has getVariants)
164
- type IsComponent<S> = S extends { getVariants: () => unknown } ? true : false;
163
+ // Check if source is a recipe (has getVariants)
164
+ type IsRecipe<S> = S extends { getVariants: () => unknown } ? true : false;
165
165
 
166
- // Extract keys from a source (includes class/style for components)
166
+ // Extract keys from a source (includes class/style for recipes)
167
167
  type SourceKeys<S> = S extends readonly (infer K)[]
168
168
  ? K
169
169
  : S extends { propKeys: readonly (infer K)[] }
@@ -177,28 +177,28 @@ type SourceVariantKeys<S> = S extends readonly (infer K)[]
177
177
  ? K
178
178
  : never;
179
179
 
180
- // Extract defaults from a source (components have defaults, arrays don't)
180
+ // Extract defaults from a source (recipes have defaults, arrays don't)
181
181
  type SourceDefaults<S> = S extends { getVariants: () => infer Defaults }
182
182
  ? Defaults
183
183
  : {};
184
184
 
185
185
  // Result type for a source when styling is NOT yet claimed
186
186
  // - Arrays: use listed keys (no defaults)
187
- // - Components: use full keys including class/style (with defaults)
187
+ // - Recipes: use full keys including class/style (with defaults)
188
188
  type SourceResultWithStyling<T, S> = Pick<T, Extract<keyof T, SourceKeys<S>>> &
189
189
  Omit<SourceDefaults<S>, keyof T>;
190
190
 
191
191
  // Result type for a source when styling IS already claimed
192
192
  // - Arrays: use listed keys (no defaults)
193
- // - Components: use only variant keys (with defaults)
193
+ // - Recipes: use only variant keys (with defaults)
194
194
  type SourceResultWithoutStyling<T, S> =
195
- IsComponent<S> extends true
195
+ IsRecipe<S> extends true
196
196
  ? Pick<T, Extract<keyof T, SourceVariantKeys<S>>> &
197
197
  Omit<SourceDefaults<S>, keyof T>
198
198
  : Pick<T, Extract<keyof T, SourceKeys<S>>>;
199
199
 
200
200
  // Recursive helper to build the result tuple for sources after S1
201
- // StylingClaimed: whether a component has already claimed styling
201
+ // StylingClaimed: whether a recipe has already claimed styling
202
202
  // UsedKeys: accumulator for all keys used so far (for the rest object)
203
203
  type BuildSourceResults<
204
204
  T,
@@ -216,7 +216,7 @@ type BuildSourceResults<
216
216
  ...BuildSourceResults<
217
217
  T,
218
218
  Rest,
219
- StylingClaimed extends true ? true : IsComponent<Current>,
219
+ StylingClaimed extends true ? true : IsRecipe<Current>,
220
220
  UsedKeys | SourceKeys<Current>
221
221
  >,
222
222
  ]
@@ -234,35 +234,35 @@ export type SplitPropsFunction = <
234
234
  ) => SplitPropsFunctionResult<T, S1, Sources>;
235
235
 
236
236
  // Result type for standalone splitProps function
237
- // S1: First source - uses SourceResultWithStyling (either array or first component gets styling)
238
- // Sources: Subsequent sources - use SourceResultWithStyling if no prior component, else SourceResultWithoutStyling
237
+ // S1: First source - uses SourceResultWithStyling (either array or first recipe gets styling)
238
+ // Sources: Subsequent sources - use SourceResultWithStyling if no prior recipe, else SourceResultWithoutStyling
239
239
  type SplitPropsFunctionResult<
240
240
  T,
241
241
  S1 extends KeySource,
242
242
  Sources extends readonly KeySource[],
243
243
  > = [
244
244
  SourceResultWithStyling<T, S1>,
245
- ...BuildSourceResults<T, Sources, IsComponent<S1>, SourceKeys<S1>>,
245
+ ...BuildSourceResults<T, Sources, IsRecipe<S1>, SourceKeys<S1>>,
246
246
  ];
247
247
 
248
- export interface ModalComponent<V, R extends ComponentResult> {
249
- (props?: ComponentProps<V>): R;
250
- class: (props?: ComponentProps<V>) => string;
251
- style: (props?: ComponentProps<V>) => R["style"];
248
+ export interface ModalRecipe<V, R extends RecipeResult> {
249
+ (props?: RecipeProps<V>): R;
250
+ class: (props?: RecipeProps<V>) => string;
251
+ style: (props?: RecipeProps<V>) => R["style"];
252
252
  getVariants: GetVariants<V>;
253
253
  variantKeys: (keyof V)[];
254
- propKeys: (keyof V | ComponentPropKey<R>)[];
254
+ propKeys: (keyof V | RecipePropKey<R>)[];
255
255
  }
256
256
 
257
257
  /**
258
- * A callable Clava component returned by `cv()`. It includes the default
258
+ * A callable Clava recipe returned by `cv()`. It includes the default
259
259
  * output mode plus `.jsx()`, `.html()`, and `.htmlObj()` mode helpers.
260
260
  *
261
261
  * @example
262
262
  * ```ts
263
- * import { type CVComponent, cv } from "clava";
263
+ * import { type Recipe, cv } from "clava";
264
264
  *
265
- * const button: CVComponent<{
265
+ * const button: Recipe<{
266
266
  * size: { sm: string; lg: string };
267
267
  * }> = cv({
268
268
  * variants: {
@@ -273,29 +273,85 @@ export interface ModalComponent<V, R extends ComponentResult> {
273
273
  * button.jsx({ size: "lg" });
274
274
  * ```
275
275
  */
276
- export interface CVComponent<
276
+ export interface Recipe<
277
277
  V extends Variants = {},
278
- E extends AnyComponent[] = [],
279
- R extends ComponentResult = StyleClassProps,
280
- > extends ModalComponent<MergeVariants<V, E>, R> {
281
- jsx: ModalComponent<MergeVariants<V, E>, JSXProps>;
282
- html: ModalComponent<MergeVariants<V, E>, HTMLProps>;
283
- htmlObj: ModalComponent<MergeVariants<V, E>, HTMLObjProps>;
278
+ E extends AnyRecipe[] = [],
279
+ R extends RecipeResult = StyleClassProps,
280
+ > extends ModalRecipe<MergeVariants<V, E>, R> {
281
+ jsx: ModalRecipe<MergeVariants<V, E>, JSXProps>;
282
+ html: ModalRecipe<MergeVariants<V, E>, HTMLProps>;
283
+ htmlObj: ModalRecipe<MergeVariants<V, E>, HTMLObjProps>;
284
284
  }
285
285
 
286
- export type AnyComponent =
287
- | CVComponent<any, any, any>
288
- | ModalComponent<any, any>;
286
+ export type AnyRecipe = Recipe<any, any, any> | ModalRecipe<any, any>;
287
+
288
+ // Derive keys from the arrays, not getVariants: its base return constraint can
289
+ // hide added keys when a generic recipe is combined with its default recipe.
290
+ type RecipeVariantKeys<T> = T extends {
291
+ variantKeys: (infer Key extends string)[];
292
+ }
293
+ ? Key
294
+ : never;
295
+
296
+ // Optional variant props alone cannot require every base key. Check each
297
+ // candidate separately so a union cannot hide a member's missing variants.
298
+ type MissingRecipeVariantKeys<
299
+ Base extends AnyRecipe,
300
+ Candidate,
301
+ > = Candidate extends unknown
302
+ ? Exclude<Base["variantKeys"][number], RecipeVariantKeys<Candidate>>
303
+ : never;
304
+
305
+ // Keep keys tied to the candidate while retaining the base's callable contract.
306
+ // Copying the base's key arrays would reject recipes that add variants.
307
+ interface RecipeLikeMode<Base extends AnyRecipe, Candidate> {
308
+ (props?: Parameters<Base>[0]): ReturnType<Base>;
309
+ class: Base["class"];
310
+ style: Base["style"];
311
+ getVariants: (
312
+ variants?: Parameters<Base["getVariants"]>[0],
313
+ ) => ReturnType<Base["getVariants"]> &
314
+ Partial<Record<RecipeVariantKeys<Candidate>, unknown>>;
315
+ variantKeys: RecipeVariantKeys<Candidate>[];
316
+ propKeys: (RecipeVariantKeys<Candidate> | RecipePropKey<ReturnType<Base>>)[];
317
+ }
318
+
319
+ /**
320
+ * Constrains a recipe to one that supplies every base variant and preserves
321
+ * the base's input and output types. Recipes can be defined independently or
322
+ * through extension. Added variants retain their prop and key types in generic
323
+ * code. Extra metadata does not affect compatibility.
324
+ *
325
+ * @example
326
+ * ```ts
327
+ * import { type RecipeLike, type VariantPropsWithRecipe, cv } from "clava";
328
+ *
329
+ * const disclosure = cv({ variants: { $open: "open" } });
330
+ *
331
+ * type DisclosureProps<
332
+ * R extends RecipeLike<typeof disclosure, R> = typeof disclosure,
333
+ * > = VariantPropsWithRecipe<typeof disclosure, R>;
334
+ * ```
335
+ */
336
+ export type RecipeLike<Base extends Recipe<any, any, any>, Candidate> = [
337
+ MissingRecipeVariantKeys<Base, Candidate>,
338
+ ] extends [never]
339
+ ? RecipeLikeMode<Base, Candidate> & {
340
+ jsx: RecipeLikeMode<Base["jsx"], Candidate>;
341
+ html: RecipeLikeMode<Base["html"], Candidate>;
342
+ htmlObj: RecipeLikeMode<Base["htmlObj"], Candidate>;
343
+ }
344
+ : never;
289
345
 
290
346
  type MergeExtendedVariants<T> = T extends readonly [infer First, ...infer Rest]
291
347
  ? ExtractVariants<First> & MergeExtendedVariants<Rest>
292
348
  : {};
293
349
 
294
- // Returns a component's effective variants so an intermediate component's
350
+ // Returns a recipe's effective variants so an intermediate recipe's
295
351
  // static variant hides a grandparent's function variant from descendants.
296
- // CVComponent and its mode helpers instantiate ModalComponent with the already
352
+ // Recipe and its mode helpers instantiate ModalRecipe with the already
297
353
  // merged MergeVariants<V, E>, so inferring V yields that effective chain.
298
- type ExtractVariants<T> = T extends ModalComponent<infer V, any> ? V : {};
354
+ type ExtractVariants<T> = T extends ModalRecipe<infer V, any> ? V : {};
299
355
 
300
356
  // A function value in `variants` (a function variant) replaces any inherited
301
357
  // variant for the same key. An object value merges value-by-value with an
@@ -320,7 +376,7 @@ type MergeVariantMaps<Child, Parent> = Omit<Parent, keyof Child> &
320
376
  >;
321
377
  };
322
378
 
323
- export type MergeVariants<V, E extends AnyComponent[]> = MergeVariantMaps<
379
+ export type MergeVariants<V, E extends AnyRecipe[]> = MergeVariantMaps<
324
380
  NoInfer<V>,
325
381
  MergeExtendedVariants<E>
326
382
  >;
@@ -425,7 +481,7 @@ type NullablePartial<T> =
425
481
 
426
482
  export type ExtendableVariants<
427
483
  V extends Variants,
428
- E extends AnyComponent[],
484
+ E extends AnyRecipe[],
429
485
  > = V & {
430
486
  [K in keyof MergeExtendedVariants<E>]?:
431
487
  | NullablePartial<MergeExtendedVariants<E>[K]>