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.
- package/CHANGELOG.md +60 -0
- package/README.md +118 -30
- package/dist/index.d.ts +104 -44
- package/dist/index.js +568 -527
- package/dist/index.js.map +1 -1
- package/package.json +6 -6
- package/src/index.ts +1147 -1072
- package/src/refine-warning.ts +7 -4
- package/src/types.ts +104 -48
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,65 @@
|
|
|
1
1
|
# clava
|
|
2
2
|
|
|
3
|
+
## 0.7.0
|
|
4
|
+
|
|
5
|
+
### Renamed Clava components to recipes
|
|
6
|
+
|
|
7
|
+
**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.
|
|
8
|
+
|
|
9
|
+
Before:
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import type { CVComponent } from "clava";
|
|
13
|
+
|
|
14
|
+
type Button = CVComponent<{ size: { sm: string; lg: string } }>;
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
After:
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import type { Recipe } from "clava";
|
|
21
|
+
|
|
22
|
+
type Button = Recipe<{ size: { sm: string; lg: string } }>;
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Use [`cv()`](https://clava.style/docs/reference/cv) to create a recipe. Documentation and development warnings now use the recipe name.
|
|
26
|
+
|
|
27
|
+
### Structural recipe compatibility
|
|
28
|
+
|
|
29
|
+
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.
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import { type RecipeLike, type VariantPropsWithRecipe, cv } from "clava";
|
|
33
|
+
|
|
34
|
+
const disclosure = cv({ variants: { $open: "open" } });
|
|
35
|
+
|
|
36
|
+
type DisclosureProps<
|
|
37
|
+
R extends RecipeLike<typeof disclosure, R> = typeof disclosure,
|
|
38
|
+
> = VariantPropsWithRecipe<typeof disclosure, R>;
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Recipes can be defined independently or through extension. Extra metadata and extension ancestry do not affect compatibility.
|
|
42
|
+
|
|
43
|
+
### Variant props with a configurable recipe prop
|
|
44
|
+
|
|
45
|
+
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"`.
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
type DisclosureProps<
|
|
49
|
+
R extends RecipeLike<typeof disclosure, R> = typeof disclosure,
|
|
50
|
+
> = VariantPropsWithRecipe<typeof disclosure, R>;
|
|
51
|
+
|
|
52
|
+
type StyledDisclosureProps<
|
|
53
|
+
R extends RecipeLike<typeof disclosure, R> = typeof disclosure,
|
|
54
|
+
> = VariantPropsWithRecipe<typeof disclosure, R, "styles">;
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Pass the selected recipe at runtime to apply its classes and styles. Choosing its type alone does not provide the recipe to the component.
|
|
58
|
+
|
|
59
|
+
## 0.6.4
|
|
60
|
+
|
|
61
|
+
- 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.
|
|
62
|
+
|
|
3
63
|
## 0.6.3
|
|
4
64
|
|
|
5
65
|
### Improved runtime performance of the `refine` chain
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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,11 +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
|
|
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
|
|
268
|
+
## Extending Recipes
|
|
269
269
|
|
|
270
|
-
Use `extend` to compose existing Clava
|
|
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
|
+
|
|
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
|
+
|
|
274
|
+
Recipes created by older Clava versions retain their existing internal extension behavior. Shared ancestors inside those recipes can still repeat.
|
|
271
275
|
|
|
272
276
|
```ts
|
|
273
277
|
const baseButton = cv({
|
|
@@ -308,7 +312,7 @@ iconButton({ size: "sm" }).class;
|
|
|
308
312
|
// "button icon-button button-sm button-neutral icon-button-sm"
|
|
309
313
|
|
|
310
314
|
iconButton({ intent: "brand" });
|
|
311
|
-
// TypeScript error: "brand" was disabled by the child
|
|
315
|
+
// TypeScript error: "brand" was disabled by the child recipe.
|
|
312
316
|
```
|
|
313
317
|
|
|
314
318
|
Set an inherited variant to `null` to remove it entirely:
|
|
@@ -322,13 +326,81 @@ const plainButton = cv({
|
|
|
322
326
|
});
|
|
323
327
|
```
|
|
324
328
|
|
|
325
|
-
You can extend any
|
|
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
|
+
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.
|
|
326
398
|
|
|
327
399
|
## Refine
|
|
328
400
|
|
|
329
401
|
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.
|
|
330
402
|
|
|
331
|
-
Use `refine` for final variant overrides and class/style adjustments. It receives the resolved variant values for the
|
|
403
|
+
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.
|
|
332
404
|
|
|
333
405
|
```ts
|
|
334
406
|
const toolbarButton = cv({
|
|
@@ -405,13 +477,13 @@ function Button(props: ButtonProps) {
|
|
|
405
477
|
}
|
|
406
478
|
```
|
|
407
479
|
|
|
408
|
-
The first
|
|
480
|
+
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.
|
|
409
481
|
|
|
410
482
|
```ts
|
|
411
483
|
const [buttonProps, fieldProps, rest] = splitProps(props, button, field);
|
|
412
484
|
// buttonProps: button variants + class/style props
|
|
413
485
|
// fieldProps: field variants only
|
|
414
|
-
// rest: props not claimed by either
|
|
486
|
+
// rest: props not claimed by either recipe
|
|
415
487
|
|
|
416
488
|
const [dataProps, variantProps, otherProps] = splitProps(
|
|
417
489
|
props,
|
|
@@ -423,7 +495,7 @@ const [dataProps, variantProps, otherProps] = splitProps(
|
|
|
423
495
|
// otherProps: remaining props
|
|
424
496
|
```
|
|
425
497
|
|
|
426
|
-
`splitProps()` only moves props that are actually present in the input object. It does not inject `defaultVariants`; call `
|
|
498
|
+
`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.
|
|
427
499
|
|
|
428
500
|
## React
|
|
429
501
|
|
|
@@ -506,11 +578,13 @@ title().class;
|
|
|
506
578
|
// "tw-text-lg tw-font-semibold"
|
|
507
579
|
```
|
|
508
580
|
|
|
509
|
-
When a
|
|
581
|
+
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.
|
|
510
582
|
|
|
511
583
|
## Type Helpers
|
|
512
584
|
|
|
513
|
-
Use `
|
|
585
|
+
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).
|
|
586
|
+
|
|
587
|
+
Use `VariantProps<typeof recipe>` to add a Clava recipe's variant props to framework component props.
|
|
514
588
|
|
|
515
589
|
```ts
|
|
516
590
|
import type { ComponentProps } from "react";
|
|
@@ -520,7 +594,7 @@ interface ButtonProps
|
|
|
520
594
|
extends ComponentProps<"button">, VariantProps<typeof button> {}
|
|
521
595
|
```
|
|
522
596
|
|
|
523
|
-
Use `Variant<typeof
|
|
597
|
+
Use `Variant<typeof recipe, "key">` to constrain a new variant map to the same values as another recipe's variant.
|
|
524
598
|
|
|
525
599
|
```ts
|
|
526
600
|
import { type Variant, cv } from "clava";
|
|
@@ -545,7 +619,17 @@ const icon = cv({
|
|
|
545
619
|
});
|
|
546
620
|
```
|
|
547
621
|
|
|
548
|
-
|
|
622
|
+
Use `Recipe` when you need an explicit type for a recipe:
|
|
623
|
+
|
|
624
|
+
```ts
|
|
625
|
+
import { type Recipe, cv } from "clava";
|
|
626
|
+
|
|
627
|
+
const button: Recipe<{ size: { sm: string; lg: string } }> = cv({
|
|
628
|
+
variants: { size: { sm: "button-sm", lg: "button-lg" } },
|
|
629
|
+
});
|
|
630
|
+
```
|
|
631
|
+
|
|
632
|
+
The package also exports `ClassValue`, `StyleValue`, `StyleClassProps`, `StyleClassValue`, `JSXProps`, `HTMLProps`, `HTMLObjProps`, and `CVConfig`.
|
|
549
633
|
|
|
550
634
|
## Comparison
|
|
551
635
|
|
|
@@ -555,7 +639,7 @@ This table compares Clava with the packages used by the repository's alternative
|
|
|
555
639
|
| ---------------------------------------------------- | -------------- | ------------------ | ------------------ | ------------------ | ------------------ |
|
|
556
640
|
| Typed variants and default variants | Yes | Yes | Yes | Yes | Yes |
|
|
557
641
|
| Boolean shorthand variants | Yes | No | No | No | No |
|
|
558
|
-
|
|
|
642
|
+
| Recipe extension or composition | `extend` | No helper | `compose()` | `extend` | `extend` |
|
|
559
643
|
| Cross-variant conditions | `refine()` | `compoundVariants` | `compoundVariants` | `compoundVariants` | `compoundVariants` |
|
|
560
644
|
| Function variant values | Yes | No | No | No | No |
|
|
561
645
|
| Computed default variants | Yes | No | No | No | No |
|
|
@@ -589,7 +673,7 @@ The second group adds the same cross-variant conditions at the interaction and p
|
|
|
589
673
|
| `class-variance-authority@0.7.1` | 500,557 | 2.67x slower |
|
|
590
674
|
| `cva@1.0.0-beta.8` | 313,641 | 4.26x slower |
|
|
591
675
|
|
|
592
|
-
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
|
|
676
|
+
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.
|
|
593
677
|
|
|
594
678
|
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.
|
|
595
679
|
|
|
@@ -597,25 +681,29 @@ Both groups resolve one fixed prop set on every iteration, so every package with
|
|
|
597
681
|
|
|
598
682
|
## API Summary
|
|
599
683
|
|
|
600
|
-
`cv(config?)` creates a typed Clava
|
|
684
|
+
`cv(config?)` creates a typed Clava recipe. Supported config keys are `extend`, `class`, `style`, `variants`, `defaultVariants`, and `refine`.
|
|
685
|
+
|
|
686
|
+
`recipe(props?)` returns `{ class, style }` with normalized camelCase style keys.
|
|
687
|
+
|
|
688
|
+
`recipe.jsx(props?)` returns `{ className, style }`.
|
|
601
689
|
|
|
602
|
-
`
|
|
690
|
+
`recipe.html(props?)` returns `{ class, style }`, where `style` is a CSS string.
|
|
603
691
|
|
|
604
|
-
`
|
|
692
|
+
`recipe.htmlObj(props?)` returns `{ class, style }`, where `style` is a hyphenated CSS property object.
|
|
605
693
|
|
|
606
|
-
`
|
|
694
|
+
`recipe.class(props?)` returns only the resolved class string.
|
|
607
695
|
|
|
608
|
-
`
|
|
696
|
+
`recipe.style(props?)` returns only the resolved style value for that recipe mode.
|
|
609
697
|
|
|
610
|
-
`
|
|
698
|
+
`recipe.getVariants(props?)` returns resolved variant values after static defaults, inherited defaults, computed defaults, and `refine` updates.
|
|
611
699
|
|
|
612
|
-
`
|
|
700
|
+
`recipe.propKeys` lists style props plus variant props for that recipe mode.
|
|
613
701
|
|
|
614
|
-
`
|
|
702
|
+
`recipe.variantKeys` lists only variant prop keys.
|
|
615
703
|
|
|
616
|
-
`
|
|
704
|
+
`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).
|
|
617
705
|
|
|
618
|
-
`
|
|
706
|
+
`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.
|
|
619
707
|
|
|
620
708
|
`splitProps(props, source1, ...sources)` returns one object per source plus a final rest object.
|
|
621
709
|
|
package/dist/index.d.ts
CHANGED
|
@@ -24,7 +24,7 @@ interface JSXCSSProperties extends CSS.Properties<string | number>, CustomProper
|
|
|
24
24
|
interface HTMLCSSProperties extends CSS.PropertiesHyphen<string | number>, CustomProperties {}
|
|
25
25
|
type StyleProperty = JSXCSSProperties | HTMLCSSProperties | CSS.Properties<string | number> | CSS.PropertiesHyphen<string | number> | string;
|
|
26
26
|
/**
|
|
27
|
-
* The prop object returned by a
|
|
27
|
+
* The prop object returned by a recipe's `.jsx()` mode.
|
|
28
28
|
*
|
|
29
29
|
* @example
|
|
30
30
|
* ```ts
|
|
@@ -39,7 +39,7 @@ interface JSXProps {
|
|
|
39
39
|
style: JSXCSSProperties;
|
|
40
40
|
}
|
|
41
41
|
/**
|
|
42
|
-
* The prop object returned by a
|
|
42
|
+
* The prop object returned by a recipe's `.html()` mode. The `style` value
|
|
43
43
|
* is serialized as an HTML style string.
|
|
44
44
|
*
|
|
45
45
|
* @example
|
|
@@ -55,7 +55,7 @@ interface HTMLProps {
|
|
|
55
55
|
style: string;
|
|
56
56
|
}
|
|
57
57
|
/**
|
|
58
|
-
* The prop object returned by a
|
|
58
|
+
* The prop object returned by a recipe's `.htmlObj()` mode. The `style`
|
|
59
59
|
* value uses hyphenated CSS property names.
|
|
60
60
|
*
|
|
61
61
|
* @example
|
|
@@ -71,7 +71,7 @@ interface HTMLObjProps {
|
|
|
71
71
|
style: HTMLCSSProperties;
|
|
72
72
|
}
|
|
73
73
|
/**
|
|
74
|
-
* The default prop object returned by a Clava
|
|
74
|
+
* The default prop object returned by a Clava recipe. It uses `class`
|
|
75
75
|
* rather than `className` and keeps styles as a normalized object.
|
|
76
76
|
*
|
|
77
77
|
* @example
|
|
@@ -86,21 +86,21 @@ interface StyleClassProps {
|
|
|
86
86
|
class: string;
|
|
87
87
|
style: StyleValue;
|
|
88
88
|
}
|
|
89
|
-
type
|
|
90
|
-
type
|
|
91
|
-
type
|
|
92
|
-
type
|
|
93
|
-
type
|
|
89
|
+
type RecipeResult = JSXProps | HTMLProps | HTMLObjProps | StyleClassProps;
|
|
90
|
+
type AllRecipeResultKeys = keyof JSXProps | keyof HTMLProps | keyof HTMLObjProps;
|
|
91
|
+
type RecipeResultValue<K extends AllRecipeResultKeys> = K extends "style" ? StyleProperty : K extends "className" ? string : K extends "class" ? string : never;
|
|
92
|
+
type NullableRecipeResult = { [K in AllRecipeResultKeys]?: RecipeResultValue<K> | null; };
|
|
93
|
+
type RecipeProps<V = {}> = VariantValues<V> & NullableRecipeResult;
|
|
94
94
|
type GetVariants<V> = (variants?: VariantValues<V>) => VariantValues<V>;
|
|
95
|
-
type
|
|
95
|
+
type RecipePropKey<R extends RecipeResult> = keyof R | (R extends StyleClassProps ? "className" : never);
|
|
96
96
|
type KeySourceArray = readonly string[];
|
|
97
|
-
interface
|
|
97
|
+
interface KeySourceRecipe {
|
|
98
98
|
propKeys: readonly string[];
|
|
99
99
|
variantKeys: readonly string[];
|
|
100
100
|
getVariants: () => Record<string, unknown>;
|
|
101
101
|
}
|
|
102
|
-
type KeySource = KeySourceArray |
|
|
103
|
-
type
|
|
102
|
+
type KeySource = KeySourceArray | KeySourceRecipe;
|
|
103
|
+
type IsRecipe<S> = S extends {
|
|
104
104
|
getVariants: () => unknown;
|
|
105
105
|
} ? true : false;
|
|
106
106
|
type SourceKeys<S> = S extends readonly (infer K)[] ? K : S extends {
|
|
@@ -113,27 +113,27 @@ type SourceDefaults<S> = S extends {
|
|
|
113
113
|
getVariants: () => infer Defaults;
|
|
114
114
|
} ? Defaults : {};
|
|
115
115
|
type SourceResultWithStyling<T, S> = Pick<T, Extract<keyof T, SourceKeys<S>>> & Omit<SourceDefaults<S>, keyof T>;
|
|
116
|
-
type SourceResultWithoutStyling<T, S> =
|
|
117
|
-
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 :
|
|
116
|
+
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
|
+
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)>];
|
|
118
118
|
type SplitPropsFunction = <T, const S1 extends KeySource, const Sources extends readonly KeySource[]>(props: T, source1: S1, ...sources: Sources) => SplitPropsFunctionResult<T, S1, Sources>;
|
|
119
|
-
type SplitPropsFunctionResult<T, S1 extends KeySource, Sources extends readonly KeySource[]> = [SourceResultWithStyling<T, S1>, ...BuildSourceResults<T, Sources,
|
|
120
|
-
interface
|
|
121
|
-
(props?:
|
|
122
|
-
class: (props?:
|
|
123
|
-
style: (props?:
|
|
119
|
+
type SplitPropsFunctionResult<T, S1 extends KeySource, Sources extends readonly KeySource[]> = [SourceResultWithStyling<T, S1>, ...BuildSourceResults<T, Sources, IsRecipe<S1>, SourceKeys<S1>>];
|
|
120
|
+
interface ModalRecipe<V, R extends RecipeResult> {
|
|
121
|
+
(props?: RecipeProps<V>): R;
|
|
122
|
+
class: (props?: RecipeProps<V>) => string;
|
|
123
|
+
style: (props?: RecipeProps<V>) => R["style"];
|
|
124
124
|
getVariants: GetVariants<V>;
|
|
125
125
|
variantKeys: (keyof V)[];
|
|
126
|
-
propKeys: (keyof V |
|
|
126
|
+
propKeys: (keyof V | RecipePropKey<R>)[];
|
|
127
127
|
}
|
|
128
128
|
/**
|
|
129
|
-
* A callable Clava
|
|
129
|
+
* A callable Clava recipe returned by `cv()`. It includes the default
|
|
130
130
|
* output mode plus `.jsx()`, `.html()`, and `.htmlObj()` mode helpers.
|
|
131
131
|
*
|
|
132
132
|
* @example
|
|
133
133
|
* ```ts
|
|
134
|
-
* import { type
|
|
134
|
+
* import { type Recipe, cv } from "clava";
|
|
135
135
|
*
|
|
136
|
-
* const button:
|
|
136
|
+
* const button: Recipe<{
|
|
137
137
|
* size: { sm: string; lg: string };
|
|
138
138
|
* }> = cv({
|
|
139
139
|
* variants: {
|
|
@@ -144,17 +144,51 @@ interface ModalComponent<V, R extends ComponentResult> {
|
|
|
144
144
|
* button.jsx({ size: "lg" });
|
|
145
145
|
* ```
|
|
146
146
|
*/
|
|
147
|
-
interface
|
|
148
|
-
jsx:
|
|
149
|
-
html:
|
|
150
|
-
htmlObj:
|
|
147
|
+
interface Recipe<V extends Variants = {}, E extends AnyRecipe[] = [], R extends RecipeResult = StyleClassProps> extends ModalRecipe<MergeVariants<V, E>, R> {
|
|
148
|
+
jsx: ModalRecipe<MergeVariants<V, E>, JSXProps>;
|
|
149
|
+
html: ModalRecipe<MergeVariants<V, E>, HTMLProps>;
|
|
150
|
+
htmlObj: ModalRecipe<MergeVariants<V, E>, HTMLObjProps>;
|
|
151
151
|
}
|
|
152
|
-
type
|
|
152
|
+
type AnyRecipe = Recipe<any, any, any> | ModalRecipe<any, any>;
|
|
153
|
+
type RecipeVariantKeys<T> = T extends {
|
|
154
|
+
variantKeys: (infer Key extends string)[];
|
|
155
|
+
} ? Key : never;
|
|
156
|
+
type MissingRecipeVariantKeys<Base extends AnyRecipe, Candidate> = Candidate extends unknown ? Exclude<Base["variantKeys"][number], RecipeVariantKeys<Candidate>> : never;
|
|
157
|
+
interface RecipeLikeMode<Base extends AnyRecipe, Candidate> {
|
|
158
|
+
(props?: Parameters<Base>[0]): ReturnType<Base>;
|
|
159
|
+
class: Base["class"];
|
|
160
|
+
style: Base["style"];
|
|
161
|
+
getVariants: (variants?: Parameters<Base["getVariants"]>[0]) => ReturnType<Base["getVariants"]> & Partial<Record<RecipeVariantKeys<Candidate>, unknown>>;
|
|
162
|
+
variantKeys: RecipeVariantKeys<Candidate>[];
|
|
163
|
+
propKeys: (RecipeVariantKeys<Candidate> | RecipePropKey<ReturnType<Base>>)[];
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Constrains a recipe to one that supplies every base variant and preserves
|
|
167
|
+
* the base's input and output types. Recipes can be defined independently or
|
|
168
|
+
* through extension. Added variants retain their prop and key types in generic
|
|
169
|
+
* code. Extra metadata does not affect compatibility.
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* ```ts
|
|
173
|
+
* import { type RecipeLike, type VariantPropsWithRecipe, cv } from "clava";
|
|
174
|
+
*
|
|
175
|
+
* const disclosure = cv({ variants: { $open: "open" } });
|
|
176
|
+
*
|
|
177
|
+
* type DisclosureProps<
|
|
178
|
+
* R extends RecipeLike<typeof disclosure, R> = typeof disclosure,
|
|
179
|
+
* > = VariantPropsWithRecipe<typeof disclosure, R>;
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
type RecipeLike<Base extends Recipe<any, any, any>, Candidate> = [MissingRecipeVariantKeys<Base, Candidate>] extends [never] ? RecipeLikeMode<Base, Candidate> & {
|
|
183
|
+
jsx: RecipeLikeMode<Base["jsx"], Candidate>;
|
|
184
|
+
html: RecipeLikeMode<Base["html"], Candidate>;
|
|
185
|
+
htmlObj: RecipeLikeMode<Base["htmlObj"], Candidate>;
|
|
186
|
+
} : never;
|
|
153
187
|
type MergeExtendedVariants<T> = T extends readonly [infer First, ...infer Rest] ? ExtractVariants<First> & MergeExtendedVariants<Rest> : {};
|
|
154
|
-
type ExtractVariants<T> = T extends
|
|
188
|
+
type ExtractVariants<T> = T extends ModalRecipe<infer V, any> ? V : {};
|
|
155
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;
|
|
156
190
|
type MergeVariantMaps<Child, Parent> = Omit<Parent, keyof Child> & Child & { [K in keyof Child & keyof Parent]: MergeVariantDefinition<Child[K], Parent[K]>; };
|
|
157
|
-
type MergeVariants<V, E extends
|
|
191
|
+
type MergeVariants<V, E extends AnyRecipe[]> = MergeVariantMaps<NoInfer<V>, MergeExtendedVariants<E>>;
|
|
158
192
|
type StringToBoolean<T> = T extends "true" | "false" ? boolean : T;
|
|
159
193
|
type VariantValue = ClassValue | StyleClassValue;
|
|
160
194
|
type NonNullKeys<T> = { [K in keyof T]: T[K] extends null ? never : K; }[keyof T];
|
|
@@ -209,12 +243,12 @@ type Refine<V> = (context: RefineContext<V>) => VariantValue;
|
|
|
209
243
|
type Variant$1 = ClassValue | Record<string, VariantValue> | ((value: any) => VariantValue);
|
|
210
244
|
type Variants = Record<string, Variant$1>;
|
|
211
245
|
type NullablePartial<T> = T extends Record<string, any> ? { [K in keyof T]?: T[K] | null; } : T | null;
|
|
212
|
-
type ExtendableVariants<V extends Variants, E extends
|
|
246
|
+
type ExtendableVariants<V extends Variants, E extends AnyRecipe[]> = V & { [K in keyof MergeExtendedVariants<E>]?: NullablePartial<MergeExtendedVariants<E>[K]> | Variant$1; };
|
|
213
247
|
//#endregion
|
|
214
248
|
//#region src/index.d.ts
|
|
215
249
|
/**
|
|
216
|
-
* Extracts the variant props inferred for a Clava
|
|
217
|
-
*
|
|
250
|
+
* Extracts the variant props inferred for a Clava recipe. Use it to add a
|
|
251
|
+
* recipe's variant props to framework component props.
|
|
218
252
|
*
|
|
219
253
|
* @example
|
|
220
254
|
* ```ts
|
|
@@ -238,11 +272,37 @@ type ExtendableVariants<V extends Variants, E extends AnyComponent[]> = V & { [K
|
|
|
238
272
|
* };
|
|
239
273
|
* ```
|
|
240
274
|
*/
|
|
241
|
-
export type VariantProps<T extends Pick<
|
|
275
|
+
export type VariantProps<T extends Pick<AnyRecipe, "getVariants">> = ReturnType<T["getVariants"]>;
|
|
276
|
+
/**
|
|
277
|
+
* Combines variant props with a recipe prop named `recipe` by default. The
|
|
278
|
+
* prop is optional when the selected recipe is assignable to the base type,
|
|
279
|
+
* and required when the selected recipe adds variants. Pass a third type
|
|
280
|
+
* argument to use another prop name.
|
|
281
|
+
*
|
|
282
|
+
* When inferring `R`, passing `undefined` or forwarding an optional recipe
|
|
283
|
+
* can fail the recipe constraint. Resolve the optional value before passing
|
|
284
|
+
* it, for example with `recipe: override ?? disclosure`.
|
|
285
|
+
*
|
|
286
|
+
* @example
|
|
287
|
+
* ```ts
|
|
288
|
+
* import { type RecipeLike, type VariantPropsWithRecipe, cv } from "clava";
|
|
289
|
+
*
|
|
290
|
+
* const disclosure = cv({ variants: { $open: "open" } });
|
|
291
|
+
*
|
|
292
|
+
* type DisclosureProps<
|
|
293
|
+
* R extends RecipeLike<typeof disclosure, R> = typeof disclosure,
|
|
294
|
+
* > = VariantPropsWithRecipe<typeof disclosure, R>;
|
|
295
|
+
*
|
|
296
|
+
* type StyledDisclosureProps<
|
|
297
|
+
* R extends RecipeLike<typeof disclosure, R> = typeof disclosure,
|
|
298
|
+
* > = VariantPropsWithRecipe<typeof disclosure, R, "styles">;
|
|
299
|
+
* ```
|
|
300
|
+
*/
|
|
301
|
+
export type VariantPropsWithRecipe<Base extends Recipe<any, any, any>, R extends RecipeLike<Base, R>, Name extends string = "recipe"> = VariantProps<R> & ([R] extends [Base] ? { [Key in Name]?: R; } : { [Key in Name]: R; });
|
|
242
302
|
type VariantKey<T> = T extends boolean ? "true" | "false" : Extract<T, string>;
|
|
243
303
|
/**
|
|
244
304
|
* Constrains a variant map to the same value keys as a variant on another
|
|
245
|
-
*
|
|
305
|
+
* recipe. Boolean variants are represented with `"true"` and `"false"`
|
|
246
306
|
* object keys.
|
|
247
307
|
*
|
|
248
308
|
* @example
|
|
@@ -266,10 +326,10 @@ type VariantKey<T> = T extends boolean ? "true" | "false" : Extract<T, string>;
|
|
|
266
326
|
* });
|
|
267
327
|
* ```
|
|
268
328
|
*/
|
|
269
|
-
export type Variant<T extends Pick<
|
|
329
|
+
export type Variant<T extends Pick<AnyRecipe, "getVariants">, K extends keyof VariantProps<T>> = Record<VariantKey<NonNullable<VariantProps<T>[K]>>, ClassValue | StyleClassValue>;
|
|
270
330
|
/**
|
|
271
331
|
* The configuration object accepted by `cv()`. It defines base class/style
|
|
272
|
-
* output, variants, default variants,
|
|
332
|
+
* output, variants, default variants, recipe extensions, and refinement
|
|
273
333
|
* logic.
|
|
274
334
|
*
|
|
275
335
|
* @example
|
|
@@ -293,7 +353,7 @@ export type Variant<T extends Pick<AnyComponent, "getVariants">, K extends keyof
|
|
|
293
353
|
* const alert = cv(config);
|
|
294
354
|
* ```
|
|
295
355
|
*/
|
|
296
|
-
export interface CVConfig<V extends Variants = {}, E extends
|
|
356
|
+
export interface CVConfig<V extends Variants = {}, E extends AnyRecipe[] = []> {
|
|
297
357
|
extend?: E;
|
|
298
358
|
class?: ClassValue;
|
|
299
359
|
style?: StyleValue;
|
|
@@ -306,8 +366,8 @@ interface CreateParams {
|
|
|
306
366
|
}
|
|
307
367
|
/**
|
|
308
368
|
* Splits props into multiple groups based on key sources. Each source gets its
|
|
309
|
-
* own result object containing all its matching keys. The first
|
|
310
|
-
* source claims styling props (class/className/style). Subsequent
|
|
369
|
+
* own result object containing all its matching keys. The first recipe
|
|
370
|
+
* source claims styling props (class/className/style). Subsequent recipes
|
|
311
371
|
* only receive variant props. Arrays receive their listed keys but don't claim
|
|
312
372
|
* styling props. The last element is always the "rest" containing keys not
|
|
313
373
|
* claimed by any source.
|
|
@@ -317,10 +377,10 @@ export declare const splitProps: SplitPropsFunction;
|
|
|
317
377
|
* Creates the cv and cx functions.
|
|
318
378
|
*/
|
|
319
379
|
export declare function create(params?: CreateParams): {
|
|
320
|
-
cv: <V extends Variants = {}, const E extends
|
|
380
|
+
cv: <V extends Variants = {}, const E extends AnyRecipe[] = []>(config?: CVConfig<V, E>) => Recipe<V, E>;
|
|
321
381
|
cx: (...classes: ClassValue$1[]) => string;
|
|
322
382
|
};
|
|
323
|
-
export declare const cv: <V extends Variants = {}, const E extends
|
|
383
|
+
export declare const cv: <V extends Variants = {}, const E extends AnyRecipe[] = []>(config?: CVConfig<V, E>) => Recipe<V, E>;
|
|
324
384
|
/**
|
|
325
385
|
* Joins class values without applying a class transform.
|
|
326
386
|
*
|
|
@@ -328,5 +388,5 @@ export declare const cv: <V extends Variants = {}, const E extends AnyComponent[
|
|
|
328
388
|
*/
|
|
329
389
|
export declare function cx(...classes: ClassValue$1[]): string;
|
|
330
390
|
//#endregion
|
|
331
|
-
export type {
|
|
391
|
+
export type { ClassValue, HTMLObjProps, HTMLProps, JSXProps, Recipe, RecipeLike, StyleClassProps, StyleClassValue, StyleValue };
|
|
332
392
|
//# sourceMappingURL=index.d.ts.map
|