clava 0.7.1 → 0.7.3
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 +38 -0
- package/README.md +89 -9
- package/dist/index.d.ts +84 -11
- package/dist/index.js +227 -56
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
- package/src/debug.ts +64 -0
- package/src/index.ts +475 -58
- package/src/types.ts +110 -9
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,43 @@
|
|
|
1
1
|
# clava
|
|
2
2
|
|
|
3
|
+
## 0.7.3
|
|
4
|
+
|
|
5
|
+
### Debug attribute in development
|
|
6
|
+
|
|
7
|
+
In development, recipe results now include a `data-clava-debug` attribute. It lists the resolved variants, so you can inspect them in the browser's developer tools:
|
|
8
|
+
|
|
9
|
+
```html
|
|
10
|
+
<button data-clava-debug="size='lg' intent='primary'"></button>
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
When one recipe's output feeds another, for example through [`splitProps`](https://clava.style/docs/reference/split-props) in a wrapper component, the attribute lists the variants of every recipe. Those of the recipe that renders the element come first.
|
|
14
|
+
|
|
15
|
+
The attribute is added only when `NODE_ENV` is `"development"`, so test snapshots don't change. Use the new `debug` option of [`create`](https://clava.style/docs/reference/create) to change this. The attribute is never added in production.
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
const { cv } = create({ debug: true });
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Clava now reads `process.env.NODE_ENV` when it's imported, not only when a recipe uses `refine` or computed defaults. Bundlers replace this expression. If you load Clava without a bundler, define `process.env.NODE_ENV` first.
|
|
22
|
+
|
|
23
|
+
## 0.7.2
|
|
24
|
+
|
|
25
|
+
### Computed defaults read settled values in extension chains
|
|
26
|
+
|
|
27
|
+
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.
|
|
28
|
+
|
|
29
|
+
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.
|
|
30
|
+
|
|
31
|
+
### Other updates
|
|
32
|
+
|
|
33
|
+
- 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.
|
|
34
|
+
- 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.
|
|
35
|
+
- 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).
|
|
36
|
+
- 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).
|
|
37
|
+
- Fixed a type error when calling [`cv`](https://clava.style/docs/reference/cv) inline inside `extend`, such as `cv({ extend: [cv({ variants })] })`.
|
|
38
|
+
- 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.
|
|
39
|
+
- 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.
|
|
40
|
+
|
|
3
41
|
## 0.7.1
|
|
4
42
|
|
|
5
43
|
### Union recipes accept only shared variants
|
package/README.md
CHANGED
|
@@ -23,6 +23,7 @@ import type { Variant, VariantProps } from "clava";
|
|
|
23
23
|
- [React](#react)
|
|
24
24
|
- [Solid](#solid)
|
|
25
25
|
- [`create()` And `cx()`](#create-and-cx)
|
|
26
|
+
- [Debugging](#debugging)
|
|
26
27
|
- [Type Helpers](#type-helpers)
|
|
27
28
|
- [Comparison](#comparison)
|
|
28
29
|
- [API Summary](#api-summary)
|
|
@@ -326,6 +327,24 @@ const plainButton = cv({
|
|
|
326
327
|
});
|
|
327
328
|
```
|
|
328
329
|
|
|
330
|
+
Extends run in order, so a recipe listed before this one in `extend` can still add a class for the removed value.
|
|
331
|
+
|
|
332
|
+
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:
|
|
333
|
+
|
|
334
|
+
```ts
|
|
335
|
+
const loudIconButton = cv({
|
|
336
|
+
extend: [iconButton],
|
|
337
|
+
variants: {
|
|
338
|
+
intent: {
|
|
339
|
+
brand: "loud-brand",
|
|
340
|
+
},
|
|
341
|
+
},
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
loudIconButton({ intent: "brand" }).class;
|
|
345
|
+
// "button icon-button button-lg loud-brand"
|
|
346
|
+
```
|
|
347
|
+
|
|
329
348
|
You can extend any recipe mode, including `baseButton.jsx`, `baseButton.html`, and `baseButton.htmlObj`.
|
|
330
349
|
|
|
331
350
|
### Constraining Recipe Props
|
|
@@ -333,13 +352,13 @@ You can extend any recipe mode, including `baseButton.jsx`, `baseButton.html`, a
|
|
|
333
352
|
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
353
|
|
|
335
354
|
```tsx
|
|
336
|
-
import type { ComponentProps } from "react";
|
|
337
355
|
import {
|
|
338
356
|
type RecipeLike,
|
|
339
357
|
type VariantPropsWithRecipe,
|
|
340
358
|
cv,
|
|
341
359
|
splitProps,
|
|
342
360
|
} from "clava";
|
|
361
|
+
import type { ComponentProps } from "react";
|
|
343
362
|
|
|
344
363
|
const disclosure = cv({ variants: { $open: "open" } });
|
|
345
364
|
|
|
@@ -460,15 +479,17 @@ const input = cv({
|
|
|
460
479
|
|
|
461
480
|
`setVariants()` overrides explicit props. `addClass()` and `addStyle()` append output without changing resolved variant values. `getVariants()` includes values changed by computed `defaultVariants` and `setVariants()`.
|
|
462
481
|
|
|
463
|
-
When a computed default or `refine` callback changes variants, Clava re-runs the refine chain so later reads see the latest values.
|
|
482
|
+
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.
|
|
483
|
+
|
|
484
|
+
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
485
|
|
|
465
486
|
## Splitting Props
|
|
466
487
|
|
|
467
488
|
Use `splitProps()` to separate variant/style props from DOM or framework props without manually maintaining prop-name lists.
|
|
468
489
|
|
|
469
490
|
```tsx
|
|
470
|
-
import type { ComponentProps } from "react";
|
|
471
491
|
import { type VariantProps, cv, splitProps } from "clava";
|
|
492
|
+
import type { ComponentProps } from "react";
|
|
472
493
|
|
|
473
494
|
const button = cv({
|
|
474
495
|
class: "button",
|
|
@@ -514,8 +535,8 @@ const [dataProps, variantProps, otherProps] = splitProps(
|
|
|
514
535
|
Use `.jsx` for React components because it returns `className` and a camelCase style object.
|
|
515
536
|
|
|
516
537
|
```tsx
|
|
517
|
-
import type { ComponentProps } from "react";
|
|
518
538
|
import { type VariantProps, cv, splitProps } from "clava";
|
|
539
|
+
import type { ComponentProps } from "react";
|
|
519
540
|
|
|
520
541
|
const button = cv({
|
|
521
542
|
class: "button",
|
|
@@ -542,8 +563,8 @@ function Button(props: ButtonProps) {
|
|
|
542
563
|
Use `.htmlObj` for Solid components when you want `class` and hyphenated style object output.
|
|
543
564
|
|
|
544
565
|
```tsx
|
|
545
|
-
import type { ComponentProps } from "solid-js";
|
|
546
566
|
import { type VariantProps, cv, splitProps } from "clava";
|
|
567
|
+
import type { ComponentProps } from "solid-js";
|
|
547
568
|
|
|
548
569
|
const button = cv({
|
|
549
570
|
class: "button",
|
|
@@ -592,6 +613,61 @@ title().class;
|
|
|
592
613
|
|
|
593
614
|
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.
|
|
594
615
|
|
|
616
|
+
## Debugging
|
|
617
|
+
|
|
618
|
+
In development, recipe results include a `data-clava-debug` attribute. It shows the resolved variants, so you can inspect them in the browser's developer tools.
|
|
619
|
+
|
|
620
|
+
```tsx
|
|
621
|
+
const button = cv({
|
|
622
|
+
variants: {
|
|
623
|
+
size: { sm: "button-sm", lg: "button-lg" },
|
|
624
|
+
intent: { primary: "button-primary" },
|
|
625
|
+
},
|
|
626
|
+
defaultVariants: { size: "sm", intent: "primary" },
|
|
627
|
+
});
|
|
628
|
+
|
|
629
|
+
<button {...button.jsx({ size: "lg" })} />;
|
|
630
|
+
```
|
|
631
|
+
|
|
632
|
+
```html
|
|
633
|
+
<button
|
|
634
|
+
class="button-lg button-primary"
|
|
635
|
+
data-clava-debug="size='lg' intent='primary'"
|
|
636
|
+
></button>
|
|
637
|
+
```
|
|
638
|
+
|
|
639
|
+
The value lists the variants that [`getVariants()`](#output-modes) returns as `key=value` pairs, in variant order. These values include defaults and [`refine`](#refine) updates. Strings use single quotes. The attribute doesn't show where a recipe was created, because a source location can differ between server-rendered HTML and the browser, which would cause hydration mismatches.
|
|
640
|
+
|
|
641
|
+
When a recipe receives a `data-clava-debug` prop, it adds the incoming value after its own. [`splitProps()`](#splitting-props) sends this prop to the first recipe, together with `class`, `className`, and `style`, and also keeps it in the rest props. In a wrapper component, the element then lists the variants of the recipe that renders it first, followed by those of the outer recipes:
|
|
642
|
+
|
|
643
|
+
```tsx
|
|
644
|
+
function Button(props: ButtonProps) {
|
|
645
|
+
const [variantProps, rest] = splitProps(props, button);
|
|
646
|
+
return <Clickable {...rest} {...button.jsx(variantProps)} />;
|
|
647
|
+
}
|
|
648
|
+
```
|
|
649
|
+
|
|
650
|
+
```html
|
|
651
|
+
<button
|
|
652
|
+
class="clickable button-lg button-primary"
|
|
653
|
+
data-clava-debug="disabled=false size='lg' intent='primary'"
|
|
654
|
+
></button>
|
|
655
|
+
```
|
|
656
|
+
|
|
657
|
+
The attribute is added only when `process.env.NODE_ENV` is `"development"`, so test snapshots don't change. Use the `debug` option of [`create()`](#create-and-cx) to change this. The attribute is never added in production, and bundlers remove the code that builds it from production builds. Don't use it to select or style elements.
|
|
658
|
+
|
|
659
|
+
```ts
|
|
660
|
+
import { create } from "clava";
|
|
661
|
+
|
|
662
|
+
// Add it in tests too.
|
|
663
|
+
const { cv } = create({ debug: true });
|
|
664
|
+
|
|
665
|
+
// Never add it.
|
|
666
|
+
const { cv: quietCv } = create({ debug: false });
|
|
667
|
+
```
|
|
668
|
+
|
|
669
|
+
Clava reads `process.env.NODE_ENV` when it creates a factory, including the package-level `cv` on import. Bundlers replace this expression. If you load Clava without a bundler, define `process.env.NODE_ENV` first.
|
|
670
|
+
|
|
595
671
|
## Type Helpers
|
|
596
672
|
|
|
597
673
|
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).
|
|
@@ -599,8 +675,8 @@ Use `RecipeLike<typeof recipe, R>` to constrain a generic component's recipe pro
|
|
|
599
675
|
Use `VariantProps<typeof recipe>` to add a Clava recipe's variant props to framework component props.
|
|
600
676
|
|
|
601
677
|
```ts
|
|
602
|
-
import type { ComponentProps } from "react";
|
|
603
678
|
import type { VariantProps } from "clava";
|
|
679
|
+
import type { ComponentProps } from "react";
|
|
604
680
|
|
|
605
681
|
interface ButtonProps
|
|
606
682
|
extends ComponentProps<"button">, VariantProps<typeof button> {}
|
|
@@ -641,7 +717,7 @@ const button: Recipe<{ size: { sm: string; lg: string } }> = cv({
|
|
|
641
717
|
});
|
|
642
718
|
```
|
|
643
719
|
|
|
644
|
-
The package also exports `ClassValue`, `StyleValue`, `StyleClassProps`, `StyleClassValue`, `JSXProps`, `HTMLProps`, `HTMLObjProps`, and `CVConfig`.
|
|
720
|
+
The package also exports `ClassValue`, `StyleValue`, `StyleClassProps`, `StyleClassValue`, `JSXProps`, `HTMLProps`, `HTMLObjProps`, `JSXCSSProperties`, `HTMLCSSProperties`, and `CVConfig`.
|
|
645
721
|
|
|
646
722
|
## Comparison
|
|
647
723
|
|
|
@@ -715,10 +791,14 @@ Both groups resolve one fixed prop set on every iteration, so every package with
|
|
|
715
791
|
|
|
716
792
|
`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
793
|
|
|
794
|
+
`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.
|
|
795
|
+
|
|
796
|
+
`RecipeProps<V>` is the type of the props that a recipe accepts, where `V` is the resolved variant map.
|
|
797
|
+
|
|
718
798
|
`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
799
|
|
|
720
|
-
`splitProps(props, source1, ...sources)` returns one object per source plus a final rest object.
|
|
800
|
+
`splitProps(props, source1, ...sources)` returns one object per source plus a final rest object. `SplitPropsFunction` is its type.
|
|
721
801
|
|
|
722
802
|
`cx(...classes)` joins class values with `clsx` and applies the factory's `transformClass`.
|
|
723
803
|
|
|
724
|
-
`create(options?)` returns isolated `{ cv, cx }` helpers. The
|
|
804
|
+
`create(options?)` returns isolated `{ cv, cx }` helpers. The options are `transformClass?: (className: string) => string` and `debug?: boolean`. See [Debugging](#debugging).
|
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
|
|
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.
|
|
@@ -368,6 +437,12 @@ export interface CVConfig<V extends Variants = {}, E extends AnyRecipe[] = []> {
|
|
|
368
437
|
}
|
|
369
438
|
interface CreateParams {
|
|
370
439
|
transformClass?: (className: string) => string;
|
|
440
|
+
/**
|
|
441
|
+
* Whether recipes add a `data-clava-debug` attribute that lists their
|
|
442
|
+
* resolved variants. Defaults to `true` when `NODE_ENV` is
|
|
443
|
+
* `"development"`. The attribute is never added in production.
|
|
444
|
+
*/
|
|
445
|
+
debug?: boolean;
|
|
371
446
|
}
|
|
372
447
|
/**
|
|
373
448
|
* Splits props into multiple groups based on key sources. Each source gets its
|
|
@@ -382,16 +457,14 @@ export declare const splitProps: SplitPropsFunction;
|
|
|
382
457
|
* Creates the cv and cx functions.
|
|
383
458
|
*/
|
|
384
459
|
export declare function create(params?: CreateParams): {
|
|
385
|
-
cv: <V extends Variants = {}, const E extends AnyRecipe[] = []>(config?: CVConfig<V, E>) =>
|
|
386
|
-
cx:
|
|
460
|
+
cv: <V extends Variants = {}, const E extends AnyRecipe[] = []>(config?: CVConfig<V, E>) => InferredRecipe<V, E>;
|
|
461
|
+
cx: typeof cx;
|
|
387
462
|
};
|
|
388
|
-
export declare const cv: <V extends Variants = {}, const E extends AnyRecipe[] = []>(config?: CVConfig<V, E>) =>
|
|
463
|
+
export declare const cv: <V extends Variants = {}, const E extends AnyRecipe[] = []>(config?: CVConfig<V, E>) => InferredRecipe<V, E>;
|
|
389
464
|
/**
|
|
390
465
|
* Joins class values without applying a class transform.
|
|
391
466
|
*
|
|
392
467
|
* This behaves like the `cx` function returned by `create()` with no options.
|
|
393
468
|
*/
|
|
394
469
|
export declare function cx(...classes: ClassValue$1[]): string;
|
|
395
|
-
|
|
396
|
-
export type { ClassValue, HTMLObjProps, HTMLProps, JSXProps, Recipe, RecipeLike, StyleClassProps, StyleClassValue, StyleValue };
|
|
397
|
-
//# sourceMappingURL=index.d.ts.map
|
|
470
|
+
export type { ClassValue, HTMLCSSProperties, HTMLObjProps, HTMLProps, JSXCSSProperties, JSXProps, ModalRecipe, Recipe, RecipeLike, RecipeProps, SplitPropsFunction, StyleClassProps, StyleClassValue, StyleValue };
|