clava 0.7.1 → 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/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # clava
2
2
 
3
+ ## 0.7.2
4
+
5
+ ### Computed defaults read settled values in extension chains
6
+
7
+ A computed `defaultVariants` entry that returns `defaultValue` in a base recipe no longer hides the value that an extending [`cv`](https://clava.style/docs/reference/cv) recipe computes for the same variant. Other computed defaults and [`refine`](https://clava.style/docs/reference/refine) callbacks in the chain used to read the base value, so a recipe could settle on output that contradicted its own variants. They now read the value that the chain settles on.
8
+
9
+ As a result, some chains now reach the re-run limit and log its warning in development. This happens when computed defaults contradict each other, which used to settle without a warning. It also happens when two computed defaults in one recipe change each other's input on every run, as they already did when the base default was static.
10
+
11
+ ### Other updates
12
+
13
+ - Fixed declaration emit errors (TS4023, TS4058, and TS2883) when a project with `declaration: true` exports the [`cv`](https://clava.style/docs/reference/cv) or [`cx`](https://clava.style/docs/reference/cx) that [`create`](https://clava.style/docs/reference/create) returns, [`splitProps`](https://clava.style/docs/reference/split-props), a recipe's `.jsx`, `.html`, `.htmlObj`, `.class`, or `.style` helper, or the result of `.jsx.style()` or `.htmlObj.style()`. The package now exports the [`ModalRecipe`](https://clava.style/docs/reference/modal-recipe), [`RecipeProps`](https://clava.style/docs/reference/recipe-props), [`SplitPropsFunction`](https://clava.style/docs/reference/split-props-function), [`JSXCSSProperties`](https://clava.style/docs/reference/jsx-css-properties), and [`HTMLCSSProperties`](https://clava.style/docs/reference/html-css-properties) types.
14
+ - Fixed [`getVariants`](https://clava.style/docs/reference/getVariants) returning a value that an extended recipe disables with `null` when a computed default or [`refine`](https://clava.style/docs/reference/refine) callback sets that value after the extended recipe runs.
15
+ - Fixed [`getVariants`](https://clava.style/docs/reference/getVariants) returning variants that render doesn't apply when a recipe disables a value and has an extend without callbacks, in a chain with computed defaults or [`refine`](https://clava.style/docs/reference/refine).
16
+ - Fixed [`getVariants`](https://clava.style/docs/reference/getVariants) returning values that an extended recipe disables with `null` when the chain has no computed defaults or [`refine`](https://clava.style/docs/reference/refine).
17
+ - Fixed a type error when calling [`cv`](https://clava.style/docs/reference/cv) inline inside `extend`, such as `cv({ extend: [cv({ variants })] })`.
18
+ - Fixed [`cv`](https://clava.style/docs/reference/cv) dropping a variant or value that a recipe defines again after a recipe it extends disabled it with `null`. The new definition now applies, and [`getVariants`](https://clava.style/docs/reference/getVariants) reports the value.
19
+ - Fixed [`cv`](https://clava.style/docs/reference/cv) types to keep the other values and variants when a recipe in `extend` disables a value or variant that another recipe in `extend` shares.
20
+
3
21
  ## 0.7.1
4
22
 
5
23
  ### Union recipes accept only shared variants
package/README.md CHANGED
@@ -326,6 +326,24 @@ const plainButton = cv({
326
326
  });
327
327
  ```
328
328
 
329
+ Extends run in order, so a recipe listed before this one in `extend` can still add a class for the removed value.
330
+
331
+ A recipe that extends a disabled variant or value can define it again. The new definition applies, and the disabled definitions from the recipes it extends stay removed:
332
+
333
+ ```ts
334
+ const loudIconButton = cv({
335
+ extend: [iconButton],
336
+ variants: {
337
+ intent: {
338
+ brand: "loud-brand",
339
+ },
340
+ },
341
+ });
342
+
343
+ loudIconButton({ intent: "brand" }).class;
344
+ // "button icon-button button-lg loud-brand"
345
+ ```
346
+
329
347
  You can extend any recipe mode, including `baseButton.jsx`, `baseButton.html`, and `baseButton.htmlObj`.
330
348
 
331
349
  ### Constraining Recipe Props
@@ -333,13 +351,13 @@ You can extend any recipe mode, including `baseButton.jsx`, `baseButton.html`, a
333
351
  Use `RecipeLike<Base, R>` to accept recipes that supply every base variant with compatible input and output types. A recipe can be defined independently, extend another recipe, or compose mode helpers. The constraint keeps the base variants readable inside a generic component and preserves the selected recipe's variant props, `variantKeys`, and `propKeys`, including the mode-specific key lists.
334
352
 
335
353
  ```tsx
336
- import type { ComponentProps } from "react";
337
354
  import {
338
355
  type RecipeLike,
339
356
  type VariantPropsWithRecipe,
340
357
  cv,
341
358
  splitProps,
342
359
  } from "clava";
360
+ import type { ComponentProps } from "react";
343
361
 
344
362
  const disclosure = cv({ variants: { $open: "open" } });
345
363
 
@@ -460,15 +478,17 @@ const input = cv({
460
478
 
461
479
  `setVariants()` overrides explicit props. `addClass()` and `addStyle()` append output without changing resolved variant values. `getVariants()` includes values changed by computed `defaultVariants` and `setVariants()`.
462
480
 
463
- When a computed default or `refine` callback changes variants, Clava re-runs the refine chain so later reads see the latest values. Re-runs are capped at 50 iterations, after which Clava stops and logs a warning in development.
481
+ When a computed default or `refine` callback changes variants, Clava re-runs the refine chain so later reads see the latest values. When a computed default changes a variant that another computed default computes later in the same run, only those later computed defaults receive the new value. Other callbacks keep the value from the previous run, so returning `defaultValue` from a base recipe doesn't hide the value that an extending recipe computes. A value that a recipe extending the first computed default's recipe disables is still written to the variants that all callbacks read, so that recipe removes it as before.
482
+
483
+ Re-runs are capped at 50 iterations, after which Clava stops and logs a warning in development. This happens when callbacks keep changing the variants that other callbacks read, such as computed defaults that contradict each other.
464
484
 
465
485
  ## Splitting Props
466
486
 
467
487
  Use `splitProps()` to separate variant/style props from DOM or framework props without manually maintaining prop-name lists.
468
488
 
469
489
  ```tsx
470
- import type { ComponentProps } from "react";
471
490
  import { type VariantProps, cv, splitProps } from "clava";
491
+ import type { ComponentProps } from "react";
472
492
 
473
493
  const button = cv({
474
494
  class: "button",
@@ -514,8 +534,8 @@ const [dataProps, variantProps, otherProps] = splitProps(
514
534
  Use `.jsx` for React components because it returns `className` and a camelCase style object.
515
535
 
516
536
  ```tsx
517
- import type { ComponentProps } from "react";
518
537
  import { type VariantProps, cv, splitProps } from "clava";
538
+ import type { ComponentProps } from "react";
519
539
 
520
540
  const button = cv({
521
541
  class: "button",
@@ -542,8 +562,8 @@ function Button(props: ButtonProps) {
542
562
  Use `.htmlObj` for Solid components when you want `class` and hyphenated style object output.
543
563
 
544
564
  ```tsx
545
- import type { ComponentProps } from "solid-js";
546
565
  import { type VariantProps, cv, splitProps } from "clava";
566
+ import type { ComponentProps } from "solid-js";
547
567
 
548
568
  const button = cv({
549
569
  class: "button",
@@ -599,8 +619,8 @@ Use `RecipeLike<typeof recipe, R>` to constrain a generic component's recipe pro
599
619
  Use `VariantProps<typeof recipe>` to add a Clava recipe's variant props to framework component props.
600
620
 
601
621
  ```ts
602
- import type { ComponentProps } from "react";
603
622
  import type { VariantProps } from "clava";
623
+ import type { ComponentProps } from "react";
604
624
 
605
625
  interface ButtonProps
606
626
  extends ComponentProps<"button">, VariantProps<typeof button> {}
@@ -641,7 +661,7 @@ const button: Recipe<{ size: { sm: string; lg: string } }> = cv({
641
661
  });
642
662
  ```
643
663
 
644
- The package also exports `ClassValue`, `StyleValue`, `StyleClassProps`, `StyleClassValue`, `JSXProps`, `HTMLProps`, `HTMLObjProps`, and `CVConfig`.
664
+ The package also exports `ClassValue`, `StyleValue`, `StyleClassProps`, `StyleClassValue`, `JSXProps`, `HTMLProps`, `HTMLObjProps`, `JSXCSSProperties`, `HTMLCSSProperties`, and `CVConfig`.
645
665
 
646
666
  ## Comparison
647
667
 
@@ -715,9 +735,13 @@ Both groups resolve one fixed prop set on every iteration, so every package with
715
735
 
716
736
  `RecipeLike<Base, R>` constrains `R` to a structurally compatible recipe while preserving variant props and key arrays in generic components. See [Constraining Recipe Props](#constraining-recipe-props).
717
737
 
738
+ `ModalRecipe<V, R>` is the type of `recipe.jsx`, `recipe.html`, and `recipe.htmlObj`, where `V` is the resolved variant map and `R` is the output props.
739
+
740
+ `RecipeProps<V>` is the type of the props that a recipe accepts, where `V` is the resolved variant map.
741
+
718
742
  `VariantPropsWithRecipe<Base, R, Name = "recipe">` combines the selected recipe's variant props with a recipe prop. The prop is optional when `R` is assignable to `Base` and required otherwise. `Name` selects the prop name.
719
743
 
720
- `splitProps(props, source1, ...sources)` returns one object per source plus a final rest object.
744
+ `splitProps(props, source1, ...sources)` returns one object per source plus a final rest object. `SplitPropsFunction` is its type.
721
745
 
722
746
  `cx(...classes)` joins class values with `clsx` and applies the factory's `transformClass`.
723
747
 
package/dist/index.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import { ClassValue as ClassValue$1 } from "clsx";
2
2
  import * as CSS from "csstype";
3
- //#region src/types.d.ts
4
3
  /**
5
4
  * A class value accepted by Clava. It supports strings, numbers, booleans,
6
5
  * nullish values, and nested arrays.
@@ -20,7 +19,31 @@ type ClassValue = string | number | boolean | null | undefined | void | ClassVal
20
19
  interface CustomProperties {
21
20
  [key: `--${string}`]: string | number;
22
21
  }
22
+ /**
23
+ * The style object returned by a recipe's `.jsx()` mode. It uses camelCase CSS
24
+ * property names and supports CSS custom properties.
25
+ *
26
+ * @example
27
+ * ```ts
28
+ * import { type JSXCSSProperties, cv } from "clava";
29
+ *
30
+ * const button = cv({ style: { fontSize: "16px" } });
31
+ * const style: JSXCSSProperties = button.jsx.style();
32
+ * ```
33
+ */
23
34
  interface JSXCSSProperties extends CSS.Properties<string | number>, CustomProperties {}
35
+ /**
36
+ * The style object returned by a recipe's `.htmlObj()` mode. It uses hyphenated
37
+ * CSS property names and supports CSS custom properties.
38
+ *
39
+ * @example
40
+ * ```ts
41
+ * import { type HTMLCSSProperties, cv } from "clava";
42
+ *
43
+ * const button = cv({ style: { fontSize: "16px" } });
44
+ * const style: HTMLCSSProperties = button.htmlObj.style();
45
+ * ```
46
+ */
24
47
  interface HTMLCSSProperties extends CSS.PropertiesHyphen<string | number>, CustomProperties {}
25
48
  type StyleProperty = JSXCSSProperties | HTMLCSSProperties | CSS.Properties<string | number> | CSS.PropertiesHyphen<string | number> | string;
26
49
  /**
@@ -90,6 +113,19 @@ type RecipeResult = JSXProps | HTMLProps | HTMLObjProps | StyleClassProps;
90
113
  type AllRecipeResultKeys = keyof JSXProps | keyof HTMLProps | keyof HTMLObjProps;
91
114
  type RecipeResultValue<K extends AllRecipeResultKeys> = K extends "style" ? StyleProperty : K extends "className" ? string : K extends "class" ? string : never;
92
115
  type NullableRecipeResult = { [K in AllRecipeResultKeys]?: RecipeResultValue<K> | null; };
116
+ /**
117
+ * The props that a recipe accepts for the variant map `V`: its variant values
118
+ * plus `class`, `className`, and `style`.
119
+ *
120
+ * @example
121
+ * ```ts
122
+ * import type { RecipeProps } from "clava";
123
+ *
124
+ * type Variants = { size: { sm: string; lg: string } };
125
+ *
126
+ * const props: RecipeProps<Variants> = { size: "sm", class: "button" };
127
+ * ```
128
+ */
93
129
  type RecipeProps<V = {}> = VariantValues<V> & NullableRecipeResult;
94
130
  type GetVariants<V> = (variants?: VariantValues<V>) => VariantValues<V>;
95
131
  type RecipePropKey<R extends RecipeResult> = keyof R | (R extends StyleClassProps ? "className" : never);
@@ -115,8 +151,36 @@ type SourceDefaults<S> = S extends {
115
151
  type SourceResultWithStyling<T, S> = Pick<T, Extract<keyof T, SourceKeys<S>>> & Omit<SourceDefaults<S>, keyof T>;
116
152
  type SourceResultWithoutStyling<T, S> = IsRecipe<S> extends true ? Pick<T, Extract<keyof T, SourceVariantKeys<S>>> & Omit<SourceDefaults<S>, keyof T> : Pick<T, Extract<keyof T, SourceKeys<S>>>;
117
153
  type BuildSourceResults<T, Sources extends readonly KeySource[], StylingClaimed extends boolean, UsedKeys> = Sources extends readonly [infer Current extends KeySource, ...infer Rest extends readonly KeySource[]] ? [StylingClaimed extends true ? SourceResultWithoutStyling<T, Current> : SourceResultWithStyling<T, Current>, ...BuildSourceResults<T, Rest, StylingClaimed extends true ? true : IsRecipe<Current>, UsedKeys | SourceKeys<Current>>] : [Omit<T, UsedKeys & (string | number | symbol)>];
154
+ /**
155
+ * The type of `splitProps`. The first key source is required.
156
+ *
157
+ * @example
158
+ * ```ts
159
+ * import { type SplitPropsFunction, splitProps } from "clava";
160
+ *
161
+ * const split: SplitPropsFunction = splitProps;
162
+ * ```
163
+ */
118
164
  type SplitPropsFunction = <T, const S1 extends KeySource, const Sources extends readonly KeySource[]>(props: T, source1: S1, ...sources: Sources) => SplitPropsFunctionResult<T, S1, Sources>;
119
165
  type SplitPropsFunctionResult<T, S1 extends KeySource, Sources extends readonly KeySource[]> = [SourceResultWithStyling<T, S1>, ...BuildSourceResults<T, Sources, IsRecipe<S1>, SourceKeys<S1>>];
166
+ /**
167
+ * A callable Clava recipe with one output mode, such as the `.jsx`, `.html`,
168
+ * and `.htmlObj` helpers of a recipe returned by `cv()`.
169
+ *
170
+ * @example
171
+ * ```ts
172
+ * import { type JSXProps, type ModalRecipe, cv } from "clava";
173
+ *
174
+ * const button = cv({
175
+ * variants: {
176
+ * size: { sm: "button-sm", lg: "button-lg" },
177
+ * },
178
+ * });
179
+ *
180
+ * const jsxButton: ModalRecipe<{ size: { sm: string; lg: string } }, JSXProps> =
181
+ * button.jsx;
182
+ * ```
183
+ */
120
184
  interface ModalRecipe<V, R extends RecipeResult> {
121
185
  (props?: RecipeProps<V>): R;
122
186
  class: (props?: RecipeProps<V>) => string;
@@ -150,6 +214,9 @@ interface Recipe<V extends Variants = {}, E extends AnyRecipe[] = [], R extends
150
214
  htmlObj: ModalRecipe<MergeVariants<V, E>, HTMLObjProps>;
151
215
  }
152
216
  type AnyRecipe = Recipe<any, any, any> | ModalRecipe<any, any>;
217
+ type InferredRecipe<V extends Variants, E extends AnyRecipe[]> = {
218
+ recipe: Recipe<V, E>;
219
+ }[[V, E] extends [unknown, unknown] ? "recipe" : never];
153
220
  type RecipeVariantKeys<T> = T extends {
154
221
  variantKeys: (infer Key extends string)[];
155
222
  } ? Key : never;
@@ -184,9 +251,13 @@ type RecipeLike<Base extends Recipe<any, any, any>, Candidate> = [MissingRecipeV
184
251
  html: RecipeLikeMode<Base["html"], Candidate>;
185
252
  htmlObj: RecipeLikeMode<Base["htmlObj"], Candidate>;
186
253
  } : never;
187
- type MergeExtendedVariants<T> = T extends readonly [infer First, ...infer Rest] ? ExtractVariants<First> & MergeExtendedVariants<Rest> : {};
254
+ type NormalizeSiblingValue<T> = T extends null ? never : VariantValue;
255
+ type NormalizeSiblingDefinition<T> = T extends null ? never : T extends undefined | ((...args: any[]) => any) | readonly unknown[] ? T : T extends Record<string, any> ? { [K in keyof T]: NormalizeSiblingValue<T[K]>; } : string;
256
+ type NormalizeSiblingVariants<T> = { [K in keyof T]: NormalizeSiblingDefinition<T[K]>; };
257
+ type MergeSiblingVariants<T> = T extends readonly [infer First, ...infer Rest] ? NormalizeSiblingVariants<ExtractVariants<First>> & MergeSiblingVariants<Rest> : {};
258
+ type MergeExtendedVariants<T> = T extends readonly [infer Only] ? ExtractVariants<Only> : MergeSiblingVariants<T>;
188
259
  type ExtractVariants<T> = T extends ModalRecipe<infer V, any> ? V : {};
189
- type MergeVariantDefinition<Child, Parent> = Child extends ((...args: any[]) => any) ? Child : Parent extends ((...args: any[]) => any) ? Child : Child extends Record<string, any> ? Parent extends Record<string, any> ? Omit<Parent, keyof Child> & Child : Child : Child;
260
+ type MergeVariantDefinition<Child, Parent> = Child extends ((...args: any[]) => any) ? Child : [Parent] extends [never] ? Child : Parent extends ((...args: any[]) => any) ? Child : Child extends Record<string, any> ? Parent extends Record<string, any> ? Omit<Parent, keyof Child> & Child : Child : Child;
190
261
  type MergeVariantMaps<Child, Parent> = Omit<Parent, keyof Child> & Child & { [K in keyof Child & keyof Parent]: MergeVariantDefinition<Child[K], Parent[K]>; };
191
262
  type MergeVariants<V, E extends AnyRecipe[]> = MergeVariantMaps<NoInfer<V>, MergeExtendedVariants<E>>;
192
263
  type StringToBoolean<T> = T extends "true" | "false" ? boolean : T;
@@ -244,8 +315,6 @@ type Variant$1 = ClassValue | Record<string, VariantValue> | ((value: any) => Va
244
315
  type Variants = Record<string, Variant$1>;
245
316
  type NullablePartial<T> = T extends Record<string, any> ? { [K in keyof T]?: T[K] | null; } : T | null;
246
317
  type ExtendableVariants<V extends Variants, E extends AnyRecipe[]> = V & { [K in keyof MergeExtendedVariants<E>]?: NullablePartial<MergeExtendedVariants<E>[K]> | Variant$1; };
247
- //#endregion
248
- //#region src/index.d.ts
249
318
  /**
250
319
  * Extracts the variant props inferred for a Clava recipe. Use it to add a
251
320
  * recipe's variant props to framework component props.
@@ -382,16 +451,15 @@ export declare const splitProps: SplitPropsFunction;
382
451
  * Creates the cv and cx functions.
383
452
  */
384
453
  export declare function create(params?: CreateParams): {
385
- cv: <V extends Variants = {}, const E extends AnyRecipe[] = []>(config?: CVConfig<V, E>) => Recipe<V, E>;
386
- cx: (...classes: ClassValue$1[]) => string;
454
+ cv: <V extends Variants = {}, const E extends AnyRecipe[] = []>(config?: CVConfig<V, E>) => InferredRecipe<V, E>;
455
+ cx: typeof cx;
387
456
  };
388
- export declare const cv: <V extends Variants = {}, const E extends AnyRecipe[] = []>(config?: CVConfig<V, E>) => Recipe<V, E>;
457
+ export declare const cv: <V extends Variants = {}, const E extends AnyRecipe[] = []>(config?: CVConfig<V, E>) => InferredRecipe<V, E>;
389
458
  /**
390
459
  * Joins class values without applying a class transform.
391
460
  *
392
461
  * This behaves like the `cx` function returned by `create()` with no options.
393
462
  */
394
463
  export declare function cx(...classes: ClassValue$1[]): string;
395
- //#endregion
396
- export type { ClassValue, HTMLObjProps, HTMLProps, JSXProps, Recipe, RecipeLike, StyleClassProps, StyleClassValue, StyleValue };
464
+ export type { ClassValue, HTMLCSSProperties, HTMLObjProps, HTMLProps, JSXCSSProperties, JSXProps, ModalRecipe, Recipe, RecipeLike, RecipeProps, SplitPropsFunction, StyleClassProps, StyleClassValue, StyleValue };
397
465
  //# sourceMappingURL=index.d.ts.map