clava 0.7.0 → 0.7.2

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.
package/src/types.ts CHANGED
@@ -28,9 +28,33 @@ interface CustomProperties {
28
28
  [key: `--${string}`]: string | number;
29
29
  }
30
30
 
31
+ /**
32
+ * The style object returned by a recipe's `.jsx()` mode. It uses camelCase CSS
33
+ * property names and supports CSS custom properties.
34
+ *
35
+ * @example
36
+ * ```ts
37
+ * import { type JSXCSSProperties, cv } from "clava";
38
+ *
39
+ * const button = cv({ style: { fontSize: "16px" } });
40
+ * const style: JSXCSSProperties = button.jsx.style();
41
+ * ```
42
+ */
31
43
  export interface JSXCSSProperties
32
44
  extends CSS.Properties<string | number>, CustomProperties {}
33
45
 
46
+ /**
47
+ * The style object returned by a recipe's `.htmlObj()` mode. It uses hyphenated
48
+ * CSS property names and supports CSS custom properties.
49
+ *
50
+ * @example
51
+ * ```ts
52
+ * import { type HTMLCSSProperties, cv } from "clava";
53
+ *
54
+ * const button = cv({ style: { fontSize: "16px" } });
55
+ * const style: HTMLCSSProperties = button.htmlObj.style();
56
+ * ```
57
+ */
34
58
  export interface HTMLCSSProperties
35
59
  extends CSS.PropertiesHyphen<string | number>, CustomProperties {}
36
60
 
@@ -143,6 +167,19 @@ export type NullableRecipeResult = {
143
167
  [K in AllRecipeResultKeys]?: RecipeResultValue<K> | null;
144
168
  };
145
169
 
170
+ /**
171
+ * The props that a recipe accepts for the variant map `V`: its variant values
172
+ * plus `class`, `className`, and `style`.
173
+ *
174
+ * @example
175
+ * ```ts
176
+ * import type { RecipeProps } from "clava";
177
+ *
178
+ * type Variants = { size: { sm: string; lg: string } };
179
+ *
180
+ * const props: RecipeProps<Variants> = { size: "sm", class: "button" };
181
+ * ```
182
+ */
146
183
  export type RecipeProps<V = {}> = VariantValues<V> & NullableRecipeResult;
147
184
 
148
185
  export type GetVariants<V> = (variants?: VariantValues<V>) => VariantValues<V>;
@@ -222,7 +259,16 @@ type BuildSourceResults<
222
259
  ]
223
260
  : [Omit<T, UsedKeys & (string | number | symbol)>];
224
261
 
225
- // Standalone splitProps function type - first source is required
262
+ /**
263
+ * The type of `splitProps`. The first key source is required.
264
+ *
265
+ * @example
266
+ * ```ts
267
+ * import { type SplitPropsFunction, splitProps } from "clava";
268
+ *
269
+ * const split: SplitPropsFunction = splitProps;
270
+ * ```
271
+ */
226
272
  export type SplitPropsFunction = <
227
273
  T,
228
274
  const S1 extends KeySource,
@@ -245,6 +291,24 @@ type SplitPropsFunctionResult<
245
291
  ...BuildSourceResults<T, Sources, IsRecipe<S1>, SourceKeys<S1>>,
246
292
  ];
247
293
 
294
+ /**
295
+ * A callable Clava recipe with one output mode, such as the `.jsx`, `.html`,
296
+ * and `.htmlObj` helpers of a recipe returned by `cv()`.
297
+ *
298
+ * @example
299
+ * ```ts
300
+ * import { type JSXProps, type ModalRecipe, cv } from "clava";
301
+ *
302
+ * const button = cv({
303
+ * variants: {
304
+ * size: { sm: "button-sm", lg: "button-lg" },
305
+ * },
306
+ * });
307
+ *
308
+ * const jsxButton: ModalRecipe<{ size: { sm: string; lg: string } }, JSXProps> =
309
+ * button.jsx;
310
+ * ```
311
+ */
248
312
  export interface ModalRecipe<V, R extends RecipeResult> {
249
313
  (props?: RecipeProps<V>): R;
250
314
  class: (props?: RecipeProps<V>) => string;
@@ -285,6 +349,15 @@ export interface Recipe<
285
349
 
286
350
  export type AnyRecipe = Recipe<any, any, any> | ModalRecipe<any, any>;
287
351
 
352
+ // Resolves to `Recipe<V, E>`. TypeScript skips generic calls that return
353
+ // callable objects in the first inference pass of the outer call, so an inline
354
+ // `cv()` in `extend` left `E` at its `[]` default. An indexed access avoids
355
+ // that skip and keeps a contextual `AnyRecipe` from inferring `any`.
356
+ // https://github.com/ariakit/clava/issues/626
357
+ export type InferredRecipe<V extends Variants, E extends AnyRecipe[]> = {
358
+ recipe: Recipe<V, E>;
359
+ }[[V, E] extends [unknown, unknown] ? "recipe" : never];
360
+
288
361
  // Derive keys from the arrays, not getVariants: its base return constraint can
289
362
  // hide added keys when a generic recipe is combined with its default recipe.
290
363
  type RecipeVariantKeys<T> = T extends {
@@ -343,10 +416,36 @@ export type RecipeLike<Base extends Recipe<any, any, any>, Candidate> = [
343
416
  }
344
417
  : never;
345
418
 
346
- type MergeExtendedVariants<T> = T extends readonly [infer First, ...infer Rest]
347
- ? ExtractVariants<First> & MergeExtendedVariants<Rest>
419
+ // Intersecting siblings reduces the whole object type to never when a shared
420
+ // key has disjoint types and one of them is a unit type, such as string and
421
+ // null. Props read only whether a value is disabled, so erase the rest. An
422
+ // explicit never still disables the key for every sibling without reducing the
423
+ // object type.
424
+ type NormalizeSiblingValue<T> = T extends null ? never : VariantValue;
425
+
426
+ type NormalizeSiblingDefinition<T> = T extends null
427
+ ? never
428
+ : T extends undefined | ((...args: any[]) => any) | readonly unknown[]
429
+ ? T
430
+ : T extends Record<string, any>
431
+ ? { [K in keyof T]: NormalizeSiblingValue<T[K]> }
432
+ : string;
433
+
434
+ type NormalizeSiblingVariants<T> = {
435
+ [K in keyof T]: NormalizeSiblingDefinition<T[K]>;
436
+ };
437
+
438
+ type MergeSiblingVariants<T> = T extends readonly [infer First, ...infer Rest]
439
+ ? NormalizeSiblingVariants<ExtractVariants<First>> &
440
+ MergeSiblingVariants<Rest>
348
441
  : {};
349
442
 
443
+ // A single extend can't conflict. Skipping normalization also keeps long chains
444
+ // of single extends within the instantiation depth limit.
445
+ type MergeExtendedVariants<T> = T extends readonly [infer Only]
446
+ ? ExtractVariants<Only>
447
+ : MergeSiblingVariants<T>;
448
+
350
449
  // Returns a recipe's effective variants so an intermediate recipe's
351
450
  // static variant hides a grandparent's function variant from descendants.
352
451
  // Recipe and its mode helpers instantiate ModalRecipe with the already
@@ -360,13 +459,15 @@ type MergeVariantDefinition<Child, Parent> = Child extends (
360
459
  ...args: any[]
361
460
  ) => any
362
461
  ? Child
363
- : Parent extends (...args: any[]) => any
462
+ : [Parent] extends [never]
364
463
  ? Child
365
- : Child extends Record<string, any>
366
- ? Parent extends Record<string, any>
367
- ? Omit<Parent, keyof Child> & Child
368
- : Child
369
- : Child;
464
+ : Parent extends (...args: any[]) => any
465
+ ? Child
466
+ : Child extends Record<string, any>
467
+ ? Parent extends Record<string, any>
468
+ ? Omit<Parent, keyof Child> & Child
469
+ : Child
470
+ : Child;
370
471
 
371
472
  type MergeVariantMaps<Child, Parent> = Omit<Parent, keyof Child> &
372
473
  Child & {