clava 0.5.0 → 0.6.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 +56 -3
- package/README.md +68 -24
- package/dist/index.d.ts +215 -23
- package/dist/index.js +385 -598
- package/dist/index.js.map +1 -1
- package/package.json +17 -2
- package/src/index.ts +364 -642
- package/src/refine-warning.ts +4 -11
- package/src/types.ts +129 -20
- package/src/utils.ts +123 -99
- package/rolldown.config.ts +0 -15
- package/tests/_utils.ts +0 -178
- package/tests/build.test.ts +0 -93
- package/tests/class-style.test.ts +0 -341
- package/tests/component-api.test.ts +0 -548
- package/tests/extend.test.ts +0 -421
- package/tests/function-variants.test.ts +0 -378
- package/tests/language-service.test.ts +0 -181
- package/tests/prototype-pollution.test.ts +0 -46
- package/tests/react.test.ts +0 -40
- package/tests/refine-warning.test.ts +0 -28
- package/tests/refine.test.ts +0 -1542
- package/tests/solid.test.ts +0 -64
- package/tests/split-props.test.ts +0 -662
- package/tests/variants-inference.test.ts +0 -333
- package/tests/variants.test.ts +0 -408
- package/tsconfig.json +0 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,58 @@
|
|
|
1
1
|
# clava
|
|
2
2
|
|
|
3
|
+
## 0.6.1
|
|
4
|
+
|
|
5
|
+
- Reduced the production bundle size and improved tree-shaking for standalone imports.
|
|
6
|
+
- Fixed [`getVariants`](https://clava.style/docs/reference/getVariants) to ignore input keys that are not declared variants.
|
|
7
|
+
- Limited the published package to runtime builds, source-condition files, and package documentation.
|
|
8
|
+
- Fixed Microsoft-prefixed CSS properties to round-trip between camelCase and hyphenated style formats.
|
|
9
|
+
- Fixed [`cv`](https://clava.style/docs/reference/cv) to preserve inherited variant types when extending `.jsx`, `.html`, or `.htmlObj` mode components.
|
|
10
|
+
- Fixed deep component extension chains to preserve stable refinement output without exhausting the convergence limit.
|
|
11
|
+
- Fixed [`splitProps`](https://clava.style/docs/reference/split-props) to ignore inherited property values.
|
|
12
|
+
- Fixed numeric style normalization to preserve unitless CSS and custom property values.
|
|
13
|
+
|
|
14
|
+
## 0.6.0
|
|
15
|
+
|
|
16
|
+
### Computed default variant parameters
|
|
17
|
+
|
|
18
|
+
**BREAKING** if you're using computed `defaultVariants` functions in [`cv`](https://clava.style/docs/reference/cv).
|
|
19
|
+
|
|
20
|
+
Computed `defaultVariants` functions now receive `defaultValue` and `variants` as separate parameters instead of a context object.
|
|
21
|
+
|
|
22
|
+
Before:
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
const button = cv({
|
|
26
|
+
variants: {
|
|
27
|
+
size: { sm: "sm", lg: "lg" },
|
|
28
|
+
intent: { neutral: "neutral", brand: "brand" },
|
|
29
|
+
},
|
|
30
|
+
defaultVariants: {
|
|
31
|
+
intent: ({ defaultValue, variants }) =>
|
|
32
|
+
variants.size === "lg" ? "neutral" : defaultValue,
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
After:
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
const button = cv({
|
|
41
|
+
variants: {
|
|
42
|
+
size: { sm: "sm", lg: "lg" },
|
|
43
|
+
intent: { neutral: "neutral", brand: "brand" },
|
|
44
|
+
},
|
|
45
|
+
defaultVariants: {
|
|
46
|
+
intent: (defaultValue, variants) =>
|
|
47
|
+
variants.size === "lg" ? "neutral" : defaultValue,
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Other updates
|
|
53
|
+
|
|
54
|
+
- Documented Clava's public TypeScript helper declarations with inline examples.
|
|
55
|
+
|
|
3
56
|
## 0.5.0
|
|
4
57
|
|
|
5
58
|
### Computed default variants
|
|
@@ -18,7 +71,7 @@ const button = cv({
|
|
|
18
71
|
size: { sm: "sm", lg: "lg" },
|
|
19
72
|
intent: { neutral: "neutral", brand: "brand" },
|
|
20
73
|
},
|
|
21
|
-
refine
|
|
74
|
+
refine({ variants, setDefaultVariants }) {
|
|
22
75
|
if (variants.size === "lg") {
|
|
23
76
|
setDefaultVariants({ intent: "neutral" });
|
|
24
77
|
}
|
|
@@ -111,7 +164,7 @@ Before:
|
|
|
111
164
|
```ts
|
|
112
165
|
const button = cv({
|
|
113
166
|
variants: { size: { sm: "sm", lg: "lg" } },
|
|
114
|
-
computed
|
|
167
|
+
computed({ variants, addClass }) {
|
|
115
168
|
if (variants.size === "lg") {
|
|
116
169
|
addClass("is-large");
|
|
117
170
|
}
|
|
@@ -124,7 +177,7 @@ After:
|
|
|
124
177
|
```ts
|
|
125
178
|
const button = cv({
|
|
126
179
|
variants: { size: { sm: "sm", lg: "lg" } },
|
|
127
|
-
refine
|
|
180
|
+
refine({ variants, addClass }) {
|
|
128
181
|
if (variants.size === "lg") {
|
|
129
182
|
addClass("is-large");
|
|
130
183
|
}
|
package/README.md
CHANGED
|
@@ -24,6 +24,7 @@ import type { Variant, VariantProps } from "clava";
|
|
|
24
24
|
- [Solid](#solid)
|
|
25
25
|
- [`create()` And `cx()`](#create-and-cx)
|
|
26
26
|
- [Type Helpers](#type-helpers)
|
|
27
|
+
- [Comparison](#comparison)
|
|
27
28
|
- [API Summary](#api-summary)
|
|
28
29
|
|
|
29
30
|
## Install
|
|
@@ -141,13 +142,14 @@ box().class;
|
|
|
141
142
|
// "box rounded"
|
|
142
143
|
```
|
|
143
144
|
|
|
144
|
-
Config styles use camelCase CSS property names
|
|
145
|
+
Config styles use camelCase CSS property names. CSS custom properties accept string or number values.
|
|
145
146
|
|
|
146
147
|
```ts
|
|
147
148
|
const card = cv({
|
|
148
149
|
style: {
|
|
149
150
|
paddingBlock: "8px",
|
|
150
151
|
"--card-accent": "oklch(62% 0.2 250)",
|
|
152
|
+
"--card-scale": 1.05,
|
|
151
153
|
},
|
|
152
154
|
});
|
|
153
155
|
```
|
|
@@ -230,26 +232,34 @@ Variant values can be class values, arrays, `{ class, style }` objects, or [func
|
|
|
230
232
|
|
|
231
233
|
## Function Variants
|
|
232
234
|
|
|
233
|
-
Add a function as a variant value when the prop should generate class/style output dynamically. The function's parameter type defines the prop type.
|
|
235
|
+
Add a function as a variant value when the prop should generate class/style output dynamically. The function's parameter type defines the prop type. Method syntax works well for multi-line callbacks; compact one-line callbacks can stay as arrow-function property values.
|
|
236
|
+
|
|
237
|
+
When function variants emit Tailwind utilities, keep utility names static so Tailwind can detect them at build time. Pass dynamic CSS values through variables instead of building utilities like `` `grid-cols-${value}` ``.
|
|
234
238
|
|
|
235
239
|
```ts
|
|
236
240
|
const grid = cv({
|
|
237
241
|
class: "grid",
|
|
238
242
|
variants: {
|
|
239
|
-
columns
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
243
|
+
columns(value: number) {
|
|
244
|
+
return {
|
|
245
|
+
class: "grid-cols-(--grid-columns)",
|
|
246
|
+
style: { "--grid-columns": `repeat(${value}, minmax(0, 1fr))` },
|
|
247
|
+
};
|
|
248
|
+
},
|
|
249
|
+
color(value: string | null) {
|
|
250
|
+
if (!value) return "text-current";
|
|
251
|
+
return {
|
|
252
|
+
class: "text-(--text-color)",
|
|
253
|
+
style: { "--text-color": value },
|
|
254
|
+
};
|
|
245
255
|
},
|
|
246
256
|
},
|
|
247
257
|
});
|
|
248
258
|
|
|
249
259
|
grid({ columns: 3, color: null });
|
|
250
260
|
// {
|
|
251
|
-
// class: "grid grid-cols-
|
|
252
|
-
// style: { "--grid-columns": "3" },
|
|
261
|
+
// class: "grid grid-cols-(--grid-columns) text-current",
|
|
262
|
+
// style: { "--grid-columns": "repeat(3, minmax(0, 1fr))" },
|
|
253
263
|
// }
|
|
254
264
|
```
|
|
255
265
|
|
|
@@ -316,7 +326,7 @@ You can extend any component mode, including `baseButton.jsx`, `baseButton.html`
|
|
|
316
326
|
|
|
317
327
|
## Refine
|
|
318
328
|
|
|
319
|
-
Use computed `defaultVariants` for dependent defaults. A function entry receives the current default value for that key
|
|
329
|
+
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.
|
|
320
330
|
|
|
321
331
|
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.
|
|
322
332
|
|
|
@@ -331,10 +341,10 @@ const toolbarButton = cv({
|
|
|
331
341
|
loading: "toolbar-button-loading",
|
|
332
342
|
},
|
|
333
343
|
defaultVariants: {
|
|
334
|
-
intent: (
|
|
344
|
+
intent: (defaultValue, variants) =>
|
|
335
345
|
variants.size === "lg" ? "neutral" : defaultValue,
|
|
336
346
|
},
|
|
337
|
-
refine
|
|
347
|
+
refine({ variants, setVariants, addClass, addStyle }) {
|
|
338
348
|
if (variants.loading) {
|
|
339
349
|
setVariants({ pressed: false });
|
|
340
350
|
}
|
|
@@ -384,17 +394,18 @@ const button = cv({
|
|
|
384
394
|
lg: "button-lg",
|
|
385
395
|
},
|
|
386
396
|
},
|
|
387
|
-
})
|
|
397
|
+
});
|
|
388
398
|
|
|
389
|
-
|
|
399
|
+
interface ButtonProps
|
|
400
|
+
extends ComponentProps<"button">, VariantProps<typeof button> {}
|
|
390
401
|
|
|
391
402
|
function Button(props: ButtonProps) {
|
|
392
403
|
const [variantProps, buttonProps] = splitProps(props, button);
|
|
393
|
-
return <button {...buttonProps} {...button(variantProps)} />;
|
|
404
|
+
return <button {...buttonProps} {...button.jsx(variantProps)} />;
|
|
394
405
|
}
|
|
395
406
|
```
|
|
396
407
|
|
|
397
|
-
The first component source claims variant props plus styling props
|
|
408
|
+
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.
|
|
398
409
|
|
|
399
410
|
```ts
|
|
400
411
|
const [buttonProps, fieldProps, rest] = splitProps(props, button, field);
|
|
@@ -431,14 +442,14 @@ const button = cv({
|
|
|
431
442
|
md: "button-md",
|
|
432
443
|
},
|
|
433
444
|
},
|
|
434
|
-
})
|
|
445
|
+
});
|
|
435
446
|
|
|
436
447
|
interface ButtonProps
|
|
437
448
|
extends ComponentProps<"button">, VariantProps<typeof button> {}
|
|
438
449
|
|
|
439
450
|
function Button(props: ButtonProps) {
|
|
440
451
|
const [variantProps, buttonProps] = splitProps(props, button);
|
|
441
|
-
return <button {...buttonProps} {...button(variantProps)} />;
|
|
452
|
+
return <button {...buttonProps} {...button.jsx(variantProps)} />;
|
|
442
453
|
}
|
|
443
454
|
```
|
|
444
455
|
|
|
@@ -459,19 +470,20 @@ const button = cv({
|
|
|
459
470
|
md: "button-md",
|
|
460
471
|
},
|
|
461
472
|
},
|
|
462
|
-
})
|
|
473
|
+
});
|
|
463
474
|
|
|
464
|
-
|
|
475
|
+
interface ButtonProps
|
|
476
|
+
extends ComponentProps<"button">, VariantProps<typeof button> {}
|
|
465
477
|
|
|
466
478
|
function Button(props: ButtonProps) {
|
|
467
479
|
const [variantProps, buttonProps] = splitProps(props, button);
|
|
468
|
-
return <button {...buttonProps} {...button(variantProps)} />;
|
|
480
|
+
return <button {...buttonProps} {...button.htmlObj(variantProps)} />;
|
|
469
481
|
}
|
|
470
482
|
```
|
|
471
483
|
|
|
472
484
|
## `create()` And `cx()`
|
|
473
485
|
|
|
474
|
-
The package-level `cv` and `cx`
|
|
486
|
+
The package-level `cv` and `cx` behave like the helpers returned by `create()` with no class transform. Use `create({ transformClass })` when every generated class string should pass through a transform, such as a prefixer or CSS-module lookup.
|
|
475
487
|
|
|
476
488
|
```ts
|
|
477
489
|
import { create } from "clava";
|
|
@@ -504,7 +516,8 @@ Use `VariantProps<typeof component>` to add a Clava component's variant props to
|
|
|
504
516
|
import type { ComponentProps } from "react";
|
|
505
517
|
import type { VariantProps } from "clava";
|
|
506
518
|
|
|
507
|
-
|
|
519
|
+
interface ButtonProps
|
|
520
|
+
extends ComponentProps<"button">, VariantProps<typeof button> {}
|
|
508
521
|
```
|
|
509
522
|
|
|
510
523
|
Use `Variant<typeof component, "key">` to constrain a new variant map to the same values as another component's variant.
|
|
@@ -534,6 +547,37 @@ const icon = cv({
|
|
|
534
547
|
|
|
535
548
|
The package also exports `ClassValue`, `StyleValue`, `StyleClassProps`, `StyleClassValue`, `JSXProps`, `HTMLProps`, `HTMLObjProps`, `CVComponent`, and `CVConfig`.
|
|
536
549
|
|
|
550
|
+
## Comparison
|
|
551
|
+
|
|
552
|
+
This table compares Clava with the packages used by the repository's alternative benchmark: `class-variance-authority@0.7.1`, `cva@1.0.0-beta.4`, `tailwind-variants/lite@3.2.2`, `tailwind-variants@3.2.2`, and `tailwind-merge@3.6.0` through Clava's `transformClass`.
|
|
553
|
+
|
|
554
|
+
| Feature | Clava | CVA v0 | CVA v1 beta | TV Lite | TV |
|
|
555
|
+
| ---------------------------------------------------- | -------------- | ------------------ | ------------------ | ------------------ | ------------------ |
|
|
556
|
+
| Typed variants and default variants | Yes | Yes | Yes | Yes | Yes |
|
|
557
|
+
| Boolean shorthand variants | Yes | No | No | No | No |
|
|
558
|
+
| Component extension or composition | `extend` | No helper | `compose()` | `extend` | `extend` |
|
|
559
|
+
| Cross-variant conditions | `refine()` | `compoundVariants` | `compoundVariants` | `compoundVariants` | `compoundVariants` |
|
|
560
|
+
| Function variant values | Yes | No | No | No | No |
|
|
561
|
+
| Computed default variants | Yes | No | No | No | No |
|
|
562
|
+
| Class and style prop output | Yes | Classes only | Classes only | Classes only | Classes only |
|
|
563
|
+
| JSX, HTML string, and hyphenated-object output modes | Yes | No | No | No | No |
|
|
564
|
+
| Built-in prop splitting for framework props | `splitProps()` | No | No | No | No |
|
|
565
|
+
| Dedicated slots API | No | No | No | Yes | Yes |
|
|
566
|
+
| Built-in Tailwind conflict merging | No | No | No | No | Yes |
|
|
567
|
+
|
|
568
|
+
The `pnpm perf-alternatives` benchmark resolves a composed Tailwind-style button with inherited variants, defaults, and cross-variant conditions. On Node v24.14.1, Vitest reported these results, where higher ops/sec is better:
|
|
569
|
+
|
|
570
|
+
| Package | Ops/sec | Relative to Clava |
|
|
571
|
+
| ------------------------------------ | --------: | ----------------: |
|
|
572
|
+
| `clava@0.6.0` | 1,093,632 | 1.00x |
|
|
573
|
+
| `clava@0.6.0 + tailwind-merge@3.6.0` | 535,392 | 2.04x slower |
|
|
574
|
+
| `class-variance-authority@0.7.1` | 445,527 | 2.45x slower |
|
|
575
|
+
| `tailwind-variants/lite@3.2.2` | 377,985 | 2.89x slower |
|
|
576
|
+
| `tailwind-variants@3.2.2` | 286,653 | 3.82x slower |
|
|
577
|
+
| `cva@1.0.0-beta.4` | 218,829 | 5.00x slower |
|
|
578
|
+
|
|
579
|
+
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.
|
|
580
|
+
|
|
537
581
|
## API Summary
|
|
538
582
|
|
|
539
583
|
`cv(config?)` creates a typed Clava component. Supported config keys are `extend`, `class`, `style`, `variants`, `defaultVariants`, and `refine`.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,23 +1,88 @@
|
|
|
1
1
|
import { ClassValue as ClassValue$1 } from "clsx";
|
|
2
2
|
import * as CSS from "csstype";
|
|
3
|
-
|
|
4
3
|
//#region src/types.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* A class value accepted by Clava. It supports strings, numbers, booleans,
|
|
6
|
+
* nullish values, and nested arrays.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import type { ClassValue } from "clava";
|
|
11
|
+
*
|
|
12
|
+
* const className: ClassValue = [
|
|
13
|
+
* "button",
|
|
14
|
+
* false && "button-hidden",
|
|
15
|
+
* ["button-primary"],
|
|
16
|
+
* ];
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
5
19
|
type ClassValue = string | number | boolean | null | undefined | void | ClassValue[];
|
|
6
|
-
|
|
7
|
-
|
|
20
|
+
interface JSXCSSProperties extends CSS.Properties<string | number> {
|
|
21
|
+
[key: `--${string}`]: string | number;
|
|
22
|
+
}
|
|
23
|
+
interface HTMLCSSProperties extends CSS.PropertiesHyphen<string | number> {
|
|
24
|
+
[key: `--${string}`]: string | number;
|
|
25
|
+
}
|
|
8
26
|
type StyleProperty = JSXCSSProperties | HTMLCSSProperties | string;
|
|
27
|
+
/**
|
|
28
|
+
* The prop object returned by a component's `.jsx()` mode.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* import { type JSXProps, cv } from "clava";
|
|
33
|
+
*
|
|
34
|
+
* const button = cv({ class: "button" });
|
|
35
|
+
* const props: JSXProps = button.jsx();
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
9
38
|
interface JSXProps {
|
|
10
39
|
className: string;
|
|
11
40
|
style: JSXCSSProperties;
|
|
12
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* The prop object returned by a component's `.html()` mode. The `style` value
|
|
44
|
+
* is serialized as an HTML style string.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* import { type HTMLProps, cv } from "clava";
|
|
49
|
+
*
|
|
50
|
+
* const button = cv({ style: { color: "red" } });
|
|
51
|
+
* const props: HTMLProps = button.html();
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
13
54
|
interface HTMLProps {
|
|
14
55
|
class: string;
|
|
15
56
|
style: string;
|
|
16
57
|
}
|
|
58
|
+
/**
|
|
59
|
+
* The prop object returned by a component's `.htmlObj()` mode. The `style`
|
|
60
|
+
* value uses hyphenated CSS property names.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* import { type HTMLObjProps, cv } from "clava";
|
|
65
|
+
*
|
|
66
|
+
* const button = cv({ style: { fontSize: "16px" } });
|
|
67
|
+
* const props: HTMLObjProps = button.htmlObj();
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
17
70
|
interface HTMLObjProps {
|
|
18
71
|
class: string;
|
|
19
72
|
style: HTMLCSSProperties;
|
|
20
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* The default prop object returned by a Clava component. It uses `class`
|
|
76
|
+
* rather than `className` and keeps styles as a normalized object.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```ts
|
|
80
|
+
* import { type StyleClassProps, cv } from "clava";
|
|
81
|
+
*
|
|
82
|
+
* const button = cv({ class: "button" });
|
|
83
|
+
* const props: StyleClassProps = button();
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
21
86
|
interface StyleClassProps {
|
|
22
87
|
class: string;
|
|
23
88
|
style: StyleValue;
|
|
@@ -25,7 +90,7 @@ interface StyleClassProps {
|
|
|
25
90
|
type ComponentResult = JSXProps | HTMLProps | HTMLObjProps | StyleClassProps;
|
|
26
91
|
type AllComponentResultKeys = keyof JSXProps | keyof HTMLProps | keyof HTMLObjProps;
|
|
27
92
|
type ComponentResultValue<K extends AllComponentResultKeys> = K extends "style" ? StyleProperty : K extends "className" ? string : K extends "class" ? string : never;
|
|
28
|
-
type NullableComponentResult = { [K in AllComponentResultKeys]?: ComponentResultValue<K> | null };
|
|
93
|
+
type NullableComponentResult = { [K in AllComponentResultKeys]?: ComponentResultValue<K> | null; };
|
|
29
94
|
type ComponentProps<V = {}> = VariantValues<V> & NullableComponentResult;
|
|
30
95
|
type GetVariants<V> = (variants?: VariantValues<V>) => VariantValues<V>;
|
|
31
96
|
type ComponentPropKey<R extends ComponentResult> = keyof R | (R extends StyleClassProps ? "className" : never);
|
|
@@ -61,6 +126,25 @@ interface ModalComponent<V, R extends ComponentResult> {
|
|
|
61
126
|
variantKeys: (keyof V)[];
|
|
62
127
|
propKeys: (keyof V | ComponentPropKey<R>)[];
|
|
63
128
|
}
|
|
129
|
+
/**
|
|
130
|
+
* A callable Clava component returned by `cv()`. It includes the default
|
|
131
|
+
* output mode plus `.jsx()`, `.html()`, and `.htmlObj()` mode helpers.
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```ts
|
|
135
|
+
* import { type CVComponent, cv } from "clava";
|
|
136
|
+
*
|
|
137
|
+
* const button: CVComponent<{
|
|
138
|
+
* size: { sm: string; lg: string };
|
|
139
|
+
* }> = cv({
|
|
140
|
+
* variants: {
|
|
141
|
+
* size: { sm: "button-sm", lg: "button-lg" },
|
|
142
|
+
* },
|
|
143
|
+
* });
|
|
144
|
+
*
|
|
145
|
+
* button.jsx({ size: "lg" });
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
64
148
|
interface CVComponent<V extends Variants = {}, E extends AnyComponent[] = [], R extends ComponentResult = StyleClassProps> extends ModalComponent<MergeVariants<V, E>, R> {
|
|
65
149
|
jsx: ModalComponent<MergeVariants<V, E>, JSXProps>;
|
|
66
150
|
html: ModalComponent<MergeVariants<V, E>, HTMLProps>;
|
|
@@ -68,26 +152,52 @@ interface CVComponent<V extends Variants = {}, E extends AnyComponent[] = [], R
|
|
|
68
152
|
}
|
|
69
153
|
type AnyComponent = CVComponent<any, any, any> | ModalComponent<any, any>;
|
|
70
154
|
type MergeExtendedVariants<T> = T extends readonly [infer First, ...infer Rest] ? ExtractVariants<First> & MergeExtendedVariants<Rest> : {};
|
|
71
|
-
type ExtractVariants<T> = T extends
|
|
155
|
+
type ExtractVariants<T> = T extends ModalComponent<infer V, any> ? V : {};
|
|
72
156
|
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;
|
|
73
|
-
type MergeVariantMaps<Child, Parent> = Omit<Parent, keyof Child> & Child & { [K in keyof Child & keyof Parent]: MergeVariantDefinition<Child[K], Parent[K]
|
|
157
|
+
type MergeVariantMaps<Child, Parent> = Omit<Parent, keyof Child> & Child & { [K in keyof Child & keyof Parent]: MergeVariantDefinition<Child[K], Parent[K]>; };
|
|
74
158
|
type MergeVariants<V, E extends AnyComponent[]> = MergeVariantMaps<NoInfer<V>, MergeExtendedVariants<E>>;
|
|
75
159
|
type StringToBoolean<T> = T extends "true" | "false" ? boolean : T;
|
|
76
160
|
type VariantValue = ClassValue | StyleClassValue;
|
|
77
|
-
type NonNullKeys<T> = { [K in keyof T]: T[K] extends null ? never : K }[keyof T];
|
|
161
|
+
type NonNullKeys<T> = { [K in keyof T]: T[K] extends null ? never : K; }[keyof T];
|
|
78
162
|
type ExtractVariantValue<T> = T extends null ? never : T extends ((value: infer V) => any) ? V : T extends readonly unknown[] ? boolean : T extends Record<string, any> ? StringToBoolean<NonNullKeys<T>> : T extends ClassValue ? boolean : never;
|
|
79
|
-
type VariantValues<V> = { [K in keyof V]?: ExtractVariantValue<V[K]> | undefined };
|
|
80
|
-
|
|
81
|
-
defaultValue: ExtractVariantValue<V[K]> | undefined;
|
|
82
|
-
variants: Readonly<VariantValues<V>>;
|
|
83
|
-
}
|
|
84
|
-
type ComputedDefaultVariant<V, K extends keyof V> = (context: DefaultVariantContext<V, K>) => ExtractVariantValue<V[K]> | undefined;
|
|
163
|
+
type VariantValues<V> = { [K in keyof V]?: ExtractVariantValue<V[K]> | undefined; };
|
|
164
|
+
type ComputedDefaultVariant<V, K extends keyof V> = (defaultValue: ExtractVariantValue<V[K]> | undefined, variants: Readonly<VariantValues<V>>) => ExtractVariantValue<V[K]> | undefined;
|
|
85
165
|
type NonFunctionVariantValue<T> = Exclude<T, (...args: any[]) => any>;
|
|
86
166
|
type DefaultVariantValue<V, K extends keyof V> = [NonFunctionVariantValue<ExtractVariantValue<V[K]>>] extends [never] ? ComputedDefaultVariant<V, K> : NonFunctionVariantValue<ExtractVariantValue<V[K]>> | ComputedDefaultVariant<V, K>;
|
|
87
|
-
type DefaultVariants<V> = { [K in keyof V]?: DefaultVariantValue<V, K> | undefined };
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
167
|
+
type DefaultVariants<V> = { [K in keyof V]?: DefaultVariantValue<V, K> | undefined; };
|
|
168
|
+
/**
|
|
169
|
+
* A normalized style object accepted by Clava config, variant, and refine
|
|
170
|
+
* style entries. CSS custom properties are supported with string and number
|
|
171
|
+
* values.
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```ts
|
|
175
|
+
* import type { StyleValue } from "clava";
|
|
176
|
+
*
|
|
177
|
+
* const style: StyleValue = {
|
|
178
|
+
* color: "red",
|
|
179
|
+
* "--button-accent": "oklch(62% 0.2 250)",
|
|
180
|
+
* "--button-scale": 1.1,
|
|
181
|
+
* };
|
|
182
|
+
* ```
|
|
183
|
+
*/
|
|
184
|
+
interface StyleValue extends CSS.Properties {
|
|
185
|
+
[key: `--${string}`]: string | number;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* A value that contributes both class and style output from a base config,
|
|
189
|
+
* variant value, function variant, or refine callback.
|
|
190
|
+
*
|
|
191
|
+
* @example
|
|
192
|
+
* ```ts
|
|
193
|
+
* import type { StyleClassValue } from "clava";
|
|
194
|
+
*
|
|
195
|
+
* const tone: StyleClassValue = {
|
|
196
|
+
* class: "button-primary",
|
|
197
|
+
* style: { color: "white" },
|
|
198
|
+
* };
|
|
199
|
+
* ```
|
|
200
|
+
*/
|
|
91
201
|
interface StyleClassValue {
|
|
92
202
|
style?: StyleValue;
|
|
93
203
|
class?: ClassValue;
|
|
@@ -101,13 +211,91 @@ interface RefineContext<V> {
|
|
|
101
211
|
type Refine<V> = (context: RefineContext<V>) => VariantValue;
|
|
102
212
|
type Variant$1 = ClassValue | Record<string, VariantValue> | ((value: any) => VariantValue);
|
|
103
213
|
type Variants = Record<string, Variant$1>;
|
|
104
|
-
type NullablePartial<T> = T extends Record<string, any> ? { [K in keyof T]?: T[K] | null } : T | null;
|
|
105
|
-
type ExtendableVariants<V extends Variants, E extends AnyComponent[]> = V & { [K in keyof MergeExtendedVariants<E>]?: NullablePartial<MergeExtendedVariants<E>[K]> | Variant$1 };
|
|
214
|
+
type NullablePartial<T> = T extends Record<string, any> ? { [K in keyof T]?: T[K] | null; } : T | null;
|
|
215
|
+
type ExtendableVariants<V extends Variants, E extends AnyComponent[]> = V & { [K in keyof MergeExtendedVariants<E>]?: NullablePartial<MergeExtendedVariants<E>[K]> | Variant$1; };
|
|
106
216
|
//#endregion
|
|
107
217
|
//#region src/index.d.ts
|
|
218
|
+
/**
|
|
219
|
+
* Extracts the variant props inferred for a Clava component. Use it to add a
|
|
220
|
+
* component's variant props to framework component props.
|
|
221
|
+
*
|
|
222
|
+
* @example
|
|
223
|
+
* ```ts
|
|
224
|
+
* import { type VariantProps, cv } from "clava";
|
|
225
|
+
* import type { ComponentProps } from "react";
|
|
226
|
+
*
|
|
227
|
+
* const button = cv({
|
|
228
|
+
* variants: {
|
|
229
|
+
* size: { sm: "button-sm", lg: "button-lg" },
|
|
230
|
+
* disabled: { true: "button-disabled", false: "" },
|
|
231
|
+
* },
|
|
232
|
+
* });
|
|
233
|
+
*
|
|
234
|
+
* interface ButtonProps
|
|
235
|
+
* extends ComponentProps<"button">,
|
|
236
|
+
* VariantProps<typeof button> {}
|
|
237
|
+
*
|
|
238
|
+
* const props: ButtonProps = {
|
|
239
|
+
* size: "lg",
|
|
240
|
+
* disabled: true,
|
|
241
|
+
* };
|
|
242
|
+
* ```
|
|
243
|
+
*/
|
|
108
244
|
type VariantProps<T extends Pick<AnyComponent, "getVariants">> = ReturnType<T["getVariants"]>;
|
|
109
245
|
type VariantKey<T> = T extends boolean ? "true" | "false" : Extract<T, string>;
|
|
246
|
+
/**
|
|
247
|
+
* Constrains a variant map to the same value keys as a variant on another
|
|
248
|
+
* component. Boolean variants are represented with `"true"` and `"false"`
|
|
249
|
+
* object keys.
|
|
250
|
+
*
|
|
251
|
+
* @example
|
|
252
|
+
* ```ts
|
|
253
|
+
* import { type Variant, cv } from "clava";
|
|
254
|
+
*
|
|
255
|
+
* const button = cv({
|
|
256
|
+
* variants: {
|
|
257
|
+
* size: { sm: "button-sm", lg: "button-lg" },
|
|
258
|
+
* },
|
|
259
|
+
* });
|
|
260
|
+
*
|
|
261
|
+
* const icon = cv({
|
|
262
|
+
* extend: [button],
|
|
263
|
+
* variants: {
|
|
264
|
+
* size: {
|
|
265
|
+
* sm: "icon-sm",
|
|
266
|
+
* lg: "icon-lg",
|
|
267
|
+
* } satisfies Variant<typeof button, "size">,
|
|
268
|
+
* },
|
|
269
|
+
* });
|
|
270
|
+
* ```
|
|
271
|
+
*/
|
|
110
272
|
type Variant<T extends Pick<AnyComponent, "getVariants">, K extends keyof VariantProps<T>> = Record<VariantKey<NonNullable<VariantProps<T>[K]>>, ClassValue | StyleClassValue>;
|
|
273
|
+
/**
|
|
274
|
+
* The configuration object accepted by `cv()`. It defines base class/style
|
|
275
|
+
* output, variants, default variants, component extensions, and refinement
|
|
276
|
+
* logic.
|
|
277
|
+
*
|
|
278
|
+
* @example
|
|
279
|
+
* ```ts
|
|
280
|
+
* import { type CVConfig, cv } from "clava";
|
|
281
|
+
*
|
|
282
|
+
* const config: CVConfig<{
|
|
283
|
+
* tone: { info: string; danger: string };
|
|
284
|
+
* }> = {
|
|
285
|
+
* variants: {
|
|
286
|
+
* tone: {
|
|
287
|
+
* info: "alert-info",
|
|
288
|
+
* danger: "alert-danger",
|
|
289
|
+
* },
|
|
290
|
+
* },
|
|
291
|
+
* defaultVariants: {
|
|
292
|
+
* tone: "info",
|
|
293
|
+
* },
|
|
294
|
+
* };
|
|
295
|
+
*
|
|
296
|
+
* const alert = cv(config);
|
|
297
|
+
* ```
|
|
298
|
+
*/
|
|
111
299
|
interface CVConfig<V extends Variants = {}, E extends AnyComponent[] = []> {
|
|
112
300
|
extend?: E;
|
|
113
301
|
class?: ClassValue;
|
|
@@ -131,13 +319,17 @@ declare const splitProps: SplitPropsFunction;
|
|
|
131
319
|
/**
|
|
132
320
|
* Creates the cv and cx functions.
|
|
133
321
|
*/
|
|
134
|
-
declare function create({
|
|
135
|
-
transformClass
|
|
136
|
-
}?: CreateParams): {
|
|
322
|
+
declare function create({ transformClass }?: CreateParams): {
|
|
137
323
|
cv: <V extends Variants = {}, const E extends AnyComponent[] = []>(config?: CVConfig<V, E>) => CVComponent<V, E>;
|
|
138
324
|
cx: (...classes: ClassValue$1[]) => string;
|
|
139
325
|
};
|
|
140
|
-
declare const cv: <V extends Variants = {}, const E extends AnyComponent[] = []>(config?: CVConfig<V, E>) => CVComponent<V, E
|
|
326
|
+
declare const cv: <V extends Variants = {}, const E extends AnyComponent[] = []>(config?: CVConfig<V, E>) => CVComponent<V, E>;
|
|
327
|
+
/**
|
|
328
|
+
* Joins class values without applying a class transform.
|
|
329
|
+
*
|
|
330
|
+
* This behaves like the `cx` function returned by `create()` with no options.
|
|
331
|
+
*/
|
|
332
|
+
declare function cx(...classes: ClassValue$1[]): string;
|
|
141
333
|
//#endregion
|
|
142
334
|
export { type CVComponent, CVConfig, type ClassValue, type HTMLObjProps, type HTMLProps, type JSXProps, type StyleClassProps, type StyleClassValue, type StyleValue, Variant, VariantProps, create, cv, cx, splitProps };
|
|
143
335
|
//# sourceMappingURL=index.d.ts.map
|