clava 0.6.4 → 0.7.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,81 @@
1
1
  # clava
2
2
 
3
+ ## 0.7.1
4
+
5
+ ### Union recipes accept only shared variants
6
+
7
+ [`VariantPropsWithRecipe`](https://clava.style/docs/reference/variant-props-with-recipe) now rejects a variant that only some members of a recipe union define. Before, a component could receive `recipe` as a union and a variant that the selected member does not define. That member then ignored the variant at runtime, and `splitProps` forwarded it to the element.
8
+
9
+ If you pass a union-typed recipe together with a variant that only some members define, narrow the recipe first so the variant belongs to a member that defines it:
10
+
11
+ ```tsx
12
+ // Error: `disclosure` does not define `$placement`.
13
+ <Disclosure recipe={isNav ? navDisclosure : disclosure} $placement="top" />;
14
+
15
+ // Narrow the recipe before passing the variant.
16
+ const element = isNav ? (
17
+ <Disclosure recipe={navDisclosure} $placement="top" />
18
+ ) : (
19
+ <Disclosure recipe={disclosure} />
20
+ );
21
+ ```
22
+
23
+ ## 0.7.0
24
+
25
+ ### Renamed Clava components to recipes
26
+
27
+ **BREAKING** if you import the [`CVComponent`](https://clava.style/docs/reference/cv) type. Replace it with [`Recipe`](https://clava.style/docs/reference/cv). The type parameters and generated class and style props stay the same.
28
+
29
+ Before:
30
+
31
+ ```ts
32
+ import type { CVComponent } from "clava";
33
+
34
+ type Button = CVComponent<{ size: { sm: string; lg: string } }>;
35
+ ```
36
+
37
+ After:
38
+
39
+ ```ts
40
+ import type { Recipe } from "clava";
41
+
42
+ type Button = Recipe<{ size: { sm: string; lg: string } }>;
43
+ ```
44
+
45
+ Use [`cv()`](https://clava.style/docs/reference/cv) to create a recipe. Documentation and development warnings now use the recipe name.
46
+
47
+ ### Structural recipe compatibility
48
+
49
+ The new [`RecipeLike`](https://clava.style/docs/reference/recipe-like) type lets a generic component accept structurally compatible recipes. Each recipe must supply every base variant with compatible input and output types. Added variants preserve their exact prop and key types.
50
+
51
+ ```ts
52
+ import { type RecipeLike, type VariantPropsWithRecipe, cv } from "clava";
53
+
54
+ const disclosure = cv({ variants: { $open: "open" } });
55
+
56
+ type DisclosureProps<
57
+ R extends RecipeLike<typeof disclosure, R> = typeof disclosure,
58
+ > = VariantPropsWithRecipe<typeof disclosure, R>;
59
+ ```
60
+
61
+ Recipes can be defined independently or through extension. Extra metadata and extension ancestry do not affect compatibility.
62
+
63
+ ### Variant props with a configurable recipe prop
64
+
65
+ Use [`VariantPropsWithRecipe`](https://clava.style/docs/reference/variant-props-with-recipe) to combine a recipe's variant props with a recipe prop. The prop is optional when the selected recipe type is assignable to the base type and required when it adds variants. The third type argument selects the prop name and defaults to `"recipe"`.
66
+
67
+ ```ts
68
+ type DisclosureProps<
69
+ R extends RecipeLike<typeof disclosure, R> = typeof disclosure,
70
+ > = VariantPropsWithRecipe<typeof disclosure, R>;
71
+
72
+ type StyledDisclosureProps<
73
+ R extends RecipeLike<typeof disclosure, R> = typeof disclosure,
74
+ > = VariantPropsWithRecipe<typeof disclosure, R, "styles">;
75
+ ```
76
+
77
+ Pass the selected recipe at runtime to apply its classes and styles. Choosing its type alone does not provide the recipe to the component.
78
+
3
79
  ## 0.6.4
4
80
 
5
81
  - Fixed shared components in [`cv`](https://clava.style/docs/reference/cv) extension chains to apply only at their first occurrence, preventing repeated base classes and variant output when several components extend the same base.
package/README.md CHANGED
@@ -17,7 +17,7 @@ import type { Variant, VariantProps } from "clava";
17
17
  - [Classes And Styles](#classes-and-styles)
18
18
  - [Variants](#variants)
19
19
  - [Function Variants](#function-variants)
20
- - [Extending Components](#extending-components)
20
+ - [Extending Recipes](#extending-recipes)
21
21
  - [Refine](#refine)
22
22
  - [Splitting Props](#splitting-props)
23
23
  - [React](#react)
@@ -43,7 +43,7 @@ yarn add clava
43
43
 
44
44
  ## Quick Start
45
45
 
46
- Use `cv()` to create a callable style component. The default callable component returns normalized `{ class, style }` props.
46
+ Use `cv()` to create a callable style recipe. The default callable recipe returns normalized `{ class, style }` props.
47
47
 
48
48
  ```ts
49
49
  import { cv } from "clava";
@@ -85,7 +85,7 @@ Input props may use `class` or `className` in any output mode. Both are appended
85
85
 
86
86
  ## Output Modes
87
87
 
88
- Every Clava component has four output modes:
88
+ Every Clava recipe has four output modes:
89
89
 
90
90
  ```ts
91
91
  const label = cv({
@@ -106,7 +106,7 @@ label.htmlObj();
106
106
  // { class: "label", style: { "font-size": "14px", "--accent": "red" } }
107
107
  ```
108
108
 
109
- Use the default callable component when you want Clava's own normalized shape. Use `.jsx()` for React-style `className` props, `.html()` when you need an HTML style string, and `.htmlObj()` when you need hyphenated CSS property names.
109
+ Use the default callable recipe when you want Clava's own normalized shape. Use `.jsx()` for React-style `className` props, `.html()` when you need an HTML style string, and `.htmlObj()` when you need hyphenated CSS property names.
110
110
 
111
111
  Each mode also exposes helpers:
112
112
 
@@ -228,7 +228,7 @@ item({ active: true, interactive: true }).class;
228
228
  // "item-active item-interactive focus-visible:ring"
229
229
  ```
230
230
 
231
- Variant values can be class values, arrays, `{ class, style }` objects, or [functions](#function-variants). Use `null` in an extending component to disable inherited variants or inherited variant values.
231
+ Variant values can be class values, arrays, `{ class, style }` objects, or [functions](#function-variants). Use `null` in an extending recipe to disable inherited variants or inherited variant values.
232
232
 
233
233
  ## Function Variants
234
234
 
@@ -263,15 +263,15 @@ grid({ columns: 3, color: null });
263
263
  // }
264
264
  ```
265
265
 
266
- Function variants can return any class value, `{ class, style }`, a default Clava component result, `null`, or `undefined`. A function variant with the same key as an extended variant replaces that inherited variant's prop type and output.
266
+ Function variants can return any class value, `{ class, style }`, a default Clava recipe result, `null`, or `undefined`. A function variant with the same key as an extended variant replaces that inherited variant's prop type and output.
267
267
 
268
- ## Extending Components
268
+ ## Extending Recipes
269
269
 
270
- Use `extend` to compose existing Clava components. Extended base classes are ordered before the child class, and extended variant output is applied before child variant output.
270
+ Use `extend` to compose existing Clava recipes. Extended base classes are ordered before the child class, and extended variant output is applied before child variant output.
271
271
 
272
- If a component appears through several `extend` paths, Clava applies it only at its first occurrence. Its classes, styles, defaults, and refinement participate through that path. For example, if `left` and `right` both extend `base`, `cv({ extend: [left, right] })` applies `base`, then `left`, then `right`. The component's JSX and HTML helpers share the same identity. Separate components can still emit identical classes.
272
+ If a recipe appears through several `extend` paths, Clava applies it only at its first occurrence. Its classes, styles, defaults, and refinement participate through that path. For example, if `left` and `right` both extend `base`, `cv({ extend: [left, right] })` applies `base`, then `left`, then `right`. The recipe's JSX and HTML helpers share the same identity. Separate recipes can still emit identical classes.
273
273
 
274
- Components created by older Clava versions retain their existing internal extension behavior. Shared ancestors inside those components can still repeat.
274
+ Recipes created by older Clava versions retain their existing internal extension behavior. Shared ancestors inside those recipes can still repeat.
275
275
 
276
276
  ```ts
277
277
  const baseButton = cv({
@@ -312,7 +312,7 @@ iconButton({ size: "sm" }).class;
312
312
  // "button icon-button button-sm button-neutral icon-button-sm"
313
313
 
314
314
  iconButton({ intent: "brand" });
315
- // TypeScript error: "brand" was disabled by the child component.
315
+ // TypeScript error: "brand" was disabled by the child recipe.
316
316
  ```
317
317
 
318
318
  Set an inherited variant to `null` to remove it entirely:
@@ -326,13 +326,93 @@ const plainButton = cv({
326
326
  });
327
327
  ```
328
328
 
329
- You can extend any component mode, including `baseButton.jsx`, `baseButton.html`, and `baseButton.htmlObj`.
329
+ You can extend any recipe mode, including `baseButton.jsx`, `baseButton.html`, and `baseButton.htmlObj`.
330
+
331
+ ### Constraining Recipe Props
332
+
333
+ 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
+
335
+ ```tsx
336
+ import type { ComponentProps } from "react";
337
+ import {
338
+ type RecipeLike,
339
+ type VariantPropsWithRecipe,
340
+ cv,
341
+ splitProps,
342
+ } from "clava";
343
+
344
+ const disclosure = cv({ variants: { $open: "open" } });
345
+
346
+ type DisclosureProps<
347
+ R extends RecipeLike<typeof disclosure, R> = typeof disclosure,
348
+ > = ComponentProps<"div"> & VariantPropsWithRecipe<typeof disclosure, R>;
349
+
350
+ function Disclosure<
351
+ R extends RecipeLike<typeof disclosure, R> = typeof disclosure,
352
+ >(props: DisclosureProps<R>) {
353
+ const { recipe = disclosure, ...rest } = props;
354
+ const [variantProps, elementProps] = splitProps(rest, recipe);
355
+ return <div {...elementProps} {...recipe.jsx(variantProps)} />;
356
+ }
357
+
358
+ const navDisclosure = cv({
359
+ variants: {
360
+ $open: "nav-open",
361
+ $placement: { top: "top-0", bottom: "bottom-0" },
362
+ },
363
+ });
364
+
365
+ <Disclosure recipe={navDisclosure} $placement="top" />;
366
+
367
+ type NavDisclosureProps = Omit<DisclosureProps<typeof navDisclosure>, "recipe">;
368
+ ```
369
+
370
+ `VariantPropsWithRecipe` combines variant props with a `recipe` prop. The prop is optional when the selected type is assignable to the base type. Selecting a type that adds variants requires its recipe, so those variants can be processed at runtime. A wrapper can omit this prop from its public props if it supplies the selected recipe itself.
371
+
372
+ You can omit the optional prop. When `R` is inferred, passing `undefined` or forwarding an optional recipe can infer `undefined` as part of `R` and fail the recipe constraint. Resolve the optional value before forwarding it:
373
+
374
+ ```tsx
375
+ function Panel({ recipe, ...props }: DisclosureProps) {
376
+ return <Disclosure {...props} recipe={recipe ?? disclosure} />;
377
+ }
378
+ ```
379
+
380
+ If a wrapper exposes added variants, use a fallback recipe that defines those variants.
381
+
382
+ The third type argument changes the prop name and defaults to `"recipe"`:
383
+
384
+ ```ts
385
+ type StyledDisclosureProps<
386
+ R extends RecipeLike<typeof disclosure, R> = typeof disclosure,
387
+ > = ComponentProps<"div"> &
388
+ VariantPropsWithRecipe<typeof disclosure, R, "styles">;
389
+ ```
390
+
391
+ The component must read `props.styles` when it uses this type. A type argument does not provide a runtime recipe. To apply different classes or styles, pass the desired recipe even if its variant types match the base. Recipe values are checked by structural assignability, so these helpers do not enforce recipe identity.
392
+
393
+ Inside generic code, the key arrays stay tied to `R`. You can read `recipe.variantKeys` and `recipe.propKeys` and use variant keys to index `VariantProps<R>` without a cast. The arrays remain properties, and a recipe with added variants is still not assignable to the base recipe's exact key-array type.
394
+
395
+ Existing variants must remain compatible with the base's inputs and resolved values. Adding a new value to an existing variant, disabling a base value, or changing its value type can fail this constraint. These recipes can still be created with `cv`; they cannot be passed where the base variant contract is required.
396
+
397
+ When `R` is a union of recipes, the variant props include only the variants that every member defines. A variant that only some members define is rejected, because the recipe selected at runtime may not process it. The check compares variant names, not their values:
398
+
399
+ ```tsx
400
+ declare const recipe: typeof navDisclosure | typeof disclosure;
401
+
402
+ <Disclosure recipe={recipe} $open />;
403
+ // Error: the disclosure member does not define `$placement`.
404
+ <Disclosure recipe={recipe} $placement="top" />;
405
+ ```
406
+
407
+ This check applies to the type of `R`. A single recipe type still accepts any object that is structurally compatible with its props, so the helpers do not enforce that the props match the recipe passed at runtime.
408
+
409
+ Compatibility depends on the recipe's shape, not its `extend` list or extra metadata. Each member of a recipe union must supply every base variant. Both the base and selected recipe must be full recipes with `jsx`, `html`, and `htmlObj` helpers, even when the component calls only one mode. A mode helper such as `disclosure.jsx` is rejected, but a recipe that extends a mode helper is accepted.
330
410
 
331
411
  ## Refine
332
412
 
333
413
  Use computed `defaultVariants` for dependent defaults. A function entry receives the current default value for that key as the first parameter and the resolved variants snapshot as the second parameter, then returns the next default value.
334
414
 
335
- Use `refine` for final variant overrides and class/style adjustments. It receives the resolved variant values for the component and can return class/style output.
415
+ Use `refine` for final variant overrides and class/style adjustments. It receives the resolved variant values for the recipe and can return class/style output.
336
416
 
337
417
  ```ts
338
418
  const toolbarButton = cv({
@@ -409,13 +489,13 @@ function Button(props: ButtonProps) {
409
489
  }
410
490
  ```
411
491
 
412
- The first component source claims variant props plus the styling props exposed by that component's `propKeys`. The base component claims `class`, `className`, and `style`; mode-specific components claim their own styling props. Later component sources receive only their variant props. Array sources receive exactly the listed keys and do not claim styling props.
492
+ The first recipe source claims variant props plus the styling props exposed by that recipe's `propKeys`. The base recipe claims `class`, `className`, and `style`; mode-specific recipes claim their own styling props. Later recipe sources receive only their variant props. Array sources receive exactly the listed keys and do not claim styling props.
413
493
 
414
494
  ```ts
415
495
  const [buttonProps, fieldProps, rest] = splitProps(props, button, field);
416
496
  // buttonProps: button variants + class/style props
417
497
  // fieldProps: field variants only
418
- // rest: props not claimed by either component
498
+ // rest: props not claimed by either recipe
419
499
 
420
500
  const [dataProps, variantProps, otherProps] = splitProps(
421
501
  props,
@@ -427,7 +507,7 @@ const [dataProps, variantProps, otherProps] = splitProps(
427
507
  // otherProps: remaining props
428
508
  ```
429
509
 
430
- `splitProps()` only moves props that are actually present in the input object. It does not inject `defaultVariants`; call `component.getVariants()` when you need resolved variant values.
510
+ `splitProps()` only moves props that are actually present in the input object. It does not inject `defaultVariants`; call `recipe.getVariants()` when you need resolved variant values.
431
511
 
432
512
  ## React
433
513
 
@@ -510,11 +590,13 @@ title().class;
510
590
  // "tw-text-lg tw-font-semibold"
511
591
  ```
512
592
 
513
- When a component created by one factory extends a component created by another factory, the extended component's transform is preserved for its own classes and the parent transform still runs on the final joined string.
593
+ When a recipe created by one factory extends a recipe created by another factory, the extended recipe's transform is preserved for its own classes and the parent transform still runs on the final joined string.
514
594
 
515
595
  ## Type Helpers
516
596
 
517
- Use `VariantProps<typeof component>` to add a Clava component's variant props to framework component props.
597
+ Use `RecipeLike<typeof recipe, R>` to constrain a generic component's recipe prop to a structurally compatible recipe. Use `VariantPropsWithRecipe<typeof recipe, R>` to combine the selected recipe's variant props with its `recipe` prop, or pass a third type argument for a custom prop name. See [Constraining Recipe Props](#constraining-recipe-props).
598
+
599
+ Use `VariantProps<typeof recipe>` to add a Clava recipe's variant props to framework component props.
518
600
 
519
601
  ```ts
520
602
  import type { ComponentProps } from "react";
@@ -524,7 +606,7 @@ interface ButtonProps
524
606
  extends ComponentProps<"button">, VariantProps<typeof button> {}
525
607
  ```
526
608
 
527
- Use `Variant<typeof component, "key">` to constrain a new variant map to the same values as another component's variant.
609
+ Use `Variant<typeof recipe, "key">` to constrain a new variant map to the same values as another recipe's variant.
528
610
 
529
611
  ```ts
530
612
  import { type Variant, cv } from "clava";
@@ -549,7 +631,17 @@ const icon = cv({
549
631
  });
550
632
  ```
551
633
 
552
- The package also exports `ClassValue`, `StyleValue`, `StyleClassProps`, `StyleClassValue`, `JSXProps`, `HTMLProps`, `HTMLObjProps`, `CVComponent`, and `CVConfig`.
634
+ Use `Recipe` when you need an explicit type for a recipe:
635
+
636
+ ```ts
637
+ import { type Recipe, cv } from "clava";
638
+
639
+ const button: Recipe<{ size: { sm: string; lg: string } }> = cv({
640
+ variants: { size: { sm: "button-sm", lg: "button-lg" } },
641
+ });
642
+ ```
643
+
644
+ The package also exports `ClassValue`, `StyleValue`, `StyleClassProps`, `StyleClassValue`, `JSXProps`, `HTMLProps`, `HTMLObjProps`, and `CVConfig`.
553
645
 
554
646
  ## Comparison
555
647
 
@@ -559,7 +651,7 @@ This table compares Clava with the packages used by the repository's alternative
559
651
  | ---------------------------------------------------- | -------------- | ------------------ | ------------------ | ------------------ | ------------------ |
560
652
  | Typed variants and default variants | Yes | Yes | Yes | Yes | Yes |
561
653
  | Boolean shorthand variants | Yes | No | No | No | No |
562
- | Component extension or composition | `extend` | No helper | `compose()` | `extend` | `extend` |
654
+ | Recipe extension or composition | `extend` | No helper | `compose()` | `extend` | `extend` |
563
655
  | Cross-variant conditions | `refine()` | `compoundVariants` | `compoundVariants` | `compoundVariants` | `compoundVariants` |
564
656
  | Function variant values | Yes | No | No | No | No |
565
657
  | Computed default variants | Yes | No | No | No | No |
@@ -593,7 +685,7 @@ The second group adds the same cross-variant conditions at the interaction and p
593
685
  | `class-variance-authority@0.7.1` | 500,557 | 2.67x slower |
594
686
  | `cva@1.0.0-beta.8` | 313,641 | 4.26x slower |
595
687
 
596
- Dividing one table by the other shows what each library charges for cross-variant conditions. `cva` charges the least at 1.30x, Clava sits at 1.79x, both `tailwind-variants` entry points are near 2.31x, and `class-variance-authority` charges the most at 3.74x. The exact ratios move by several percent between runs, so read the ordering rather than the digits, and reach for the group that matches your components rather than reading either table as the whole picture.
688
+ Dividing one table by the other shows what each library charges for cross-variant conditions. `cva` charges the least at 1.30x, Clava sits at 1.79x, both `tailwind-variants` entry points are near 2.31x, and `class-variance-authority` charges the most at 3.74x. The exact ratios move by several percent between runs, so read the ordering rather than the digits, and reach for the group that matches your recipes rather than reading either table as the whole picture.
597
689
 
598
690
  Benchmark results vary by runtime and hardware, so treat them as a reproducible snapshot of this repository's composed-variant case rather than a universal ranking. Rows within a few percent of each other sit inside run-to-run noise, so their relative order is not meaningful. That covers the two `tailwind-variants` entry points, which land within a few percent in both groups and swap order between runs.
599
691
 
@@ -601,25 +693,29 @@ Both groups resolve one fixed prop set on every iteration, so every package with
601
693
 
602
694
  ## API Summary
603
695
 
604
- `cv(config?)` creates a typed Clava component. Supported config keys are `extend`, `class`, `style`, `variants`, `defaultVariants`, and `refine`.
696
+ `cv(config?)` creates a typed Clava recipe. Supported config keys are `extend`, `class`, `style`, `variants`, `defaultVariants`, and `refine`.
697
+
698
+ `recipe(props?)` returns `{ class, style }` with normalized camelCase style keys.
699
+
700
+ `recipe.jsx(props?)` returns `{ className, style }`.
605
701
 
606
- `component(props?)` returns `{ class, style }` with normalized camelCase style keys.
702
+ `recipe.html(props?)` returns `{ class, style }`, where `style` is a CSS string.
607
703
 
608
- `component.jsx(props?)` returns `{ className, style }`.
704
+ `recipe.htmlObj(props?)` returns `{ class, style }`, where `style` is a hyphenated CSS property object.
609
705
 
610
- `component.html(props?)` returns `{ class, style }`, where `style` is a CSS string.
706
+ `recipe.class(props?)` returns only the resolved class string.
611
707
 
612
- `component.htmlObj(props?)` returns `{ class, style }`, where `style` is a hyphenated CSS property object.
708
+ `recipe.style(props?)` returns only the resolved style value for that recipe mode.
613
709
 
614
- `component.class(props?)` returns only the resolved class string.
710
+ `recipe.getVariants(props?)` returns resolved variant values after static defaults, inherited defaults, computed defaults, and `refine` updates.
615
711
 
616
- `component.style(props?)` returns only the resolved style value for that component mode.
712
+ `recipe.propKeys` lists style props plus variant props for that recipe mode.
617
713
 
618
- `component.getVariants(props?)` returns resolved variant values after static defaults, inherited defaults, computed defaults, and `refine` updates.
714
+ `recipe.variantKeys` lists only variant prop keys.
619
715
 
620
- `component.propKeys` lists style props plus variant props for that component mode.
716
+ `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).
621
717
 
622
- `component.variantKeys` lists only variant prop keys.
718
+ `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.
623
719
 
624
720
  `splitProps(props, source1, ...sources)` returns one object per source plus a final rest object.
625
721