clava 0.6.0 → 0.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/README.md +24 -16
- package/dist/index.d.ts +31 -25
- package/dist/index.js +384 -594
- package/dist/index.js.map +1 -1
- package/package.json +18 -3
- package/src/index.ts +281 -634
- package/src/refine-warning.ts +4 -11
- package/src/types.ts +27 -16
- 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 -192
- 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 -1498
- package/tests/solid.test.ts +0 -64
- package/tests/split-props.test.ts +0 -662
- package/tests/variants-inference.test.ts +0 -401
- package/tests/variants.test.ts +0 -408
- package/tsconfig.json +0 -7
package/src/refine-warning.ts
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
remaining: number;
|
|
3
|
-
warned?: boolean;
|
|
4
|
-
}
|
|
1
|
+
import { hasOwn } from "./utils.ts";
|
|
5
2
|
|
|
6
3
|
export interface CreationFrame {
|
|
7
4
|
stack?: string;
|
|
@@ -107,14 +104,14 @@ export function accumulateUnstableVariantChanges(
|
|
|
107
104
|
next: Record<string, unknown>,
|
|
108
105
|
): void {
|
|
109
106
|
for (const key in next) {
|
|
110
|
-
if (!
|
|
107
|
+
if (!hasOwn(next, key)) continue;
|
|
111
108
|
if (!Object.is(prev[key], next[key])) {
|
|
112
109
|
setVariantChange(into, key, prev[key], next[key]);
|
|
113
110
|
}
|
|
114
111
|
}
|
|
115
112
|
for (const key in prev) {
|
|
116
|
-
if (!
|
|
117
|
-
if (
|
|
113
|
+
if (!hasOwn(prev, key)) continue;
|
|
114
|
+
if (hasOwn(next, key)) continue;
|
|
118
115
|
setVariantChange(into, key, prev[key], undefined);
|
|
119
116
|
}
|
|
120
117
|
}
|
|
@@ -128,21 +125,17 @@ function formatVariantChanges(changes: Map<string, VariantChange>): string {
|
|
|
128
125
|
}
|
|
129
126
|
|
|
130
127
|
interface WarnRefineLimitParams {
|
|
131
|
-
runState: RefineRunState;
|
|
132
128
|
creationFrame: CreationFrame | undefined;
|
|
133
129
|
unstableChanges: Map<string, VariantChange> | null;
|
|
134
130
|
}
|
|
135
131
|
|
|
136
132
|
export function warnRefineLimit({
|
|
137
|
-
runState,
|
|
138
133
|
creationFrame,
|
|
139
134
|
unstableChanges,
|
|
140
135
|
}: WarnRefineLimitParams): void {
|
|
141
136
|
// Bundlers are expected to replace this branch with a production literal,
|
|
142
137
|
// allowing warning-only code below to be removed from consumer bundles.
|
|
143
138
|
if (process.env.NODE_ENV === "production") return;
|
|
144
|
-
if (runState.warned) return;
|
|
145
|
-
runState.warned = true;
|
|
146
139
|
let message =
|
|
147
140
|
"Clava: Maximum refine iterations exceeded. This can happen when a " +
|
|
148
141
|
"computed default variant or refine callback changes one of the " +
|
package/src/types.ts
CHANGED
|
@@ -24,13 +24,28 @@ export type ClassValue =
|
|
|
24
24
|
| void
|
|
25
25
|
| ClassValue[];
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
interface CustomProperties {
|
|
28
|
+
[key: `--${string}`]: string | number;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface JSXCSSProperties
|
|
32
|
+
extends CSS.Properties<string | number>, CustomProperties {}
|
|
28
33
|
|
|
29
|
-
export
|
|
34
|
+
export interface HTMLCSSProperties
|
|
35
|
+
extends CSS.PropertiesHyphen<string | number>, CustomProperties {}
|
|
30
36
|
|
|
31
37
|
export type CSSProperties = CSS.Properties;
|
|
32
38
|
|
|
33
|
-
|
|
39
|
+
// Indexed members keep custom properties known to fresh style literals, one
|
|
40
|
+
// per input mode. Index-free members keep framework style types assignable:
|
|
41
|
+
// an all-indexed union rejects React's `CSSProperties`, surfacing as TS2590.
|
|
42
|
+
// https://github.com/ariakit/clava/issues/483#issuecomment-5447514407
|
|
43
|
+
export type StyleProperty =
|
|
44
|
+
| JSXCSSProperties
|
|
45
|
+
| HTMLCSSProperties
|
|
46
|
+
| CSS.Properties<string | number>
|
|
47
|
+
| CSS.PropertiesHyphen<string | number>
|
|
48
|
+
| string;
|
|
34
49
|
|
|
35
50
|
/**
|
|
36
51
|
* The prop object returned by a component's `.jsx()` mode.
|
|
@@ -276,15 +291,11 @@ type MergeExtendedVariants<T> = T extends readonly [infer First, ...infer Rest]
|
|
|
276
291
|
? ExtractVariants<First> & MergeExtendedVariants<Rest>
|
|
277
292
|
: {};
|
|
278
293
|
|
|
279
|
-
// Returns a component's effective variants
|
|
280
|
-
//
|
|
281
|
-
//
|
|
282
|
-
//
|
|
283
|
-
|
|
284
|
-
type ExtractVariants<T> =
|
|
285
|
-
T extends CVComponent<infer V, infer E, any>
|
|
286
|
-
? MergeVariantMaps<V, MergeExtendedVariants<E>>
|
|
287
|
-
: {};
|
|
294
|
+
// Returns a component's effective variants so an intermediate component's
|
|
295
|
+
// static variant hides a grandparent's function variant from descendants.
|
|
296
|
+
// CVComponent and its mode helpers instantiate ModalComponent with the already
|
|
297
|
+
// merged MergeVariants<V, E>, so inferring V yields that effective chain.
|
|
298
|
+
type ExtractVariants<T> = T extends ModalComponent<infer V, any> ? V : {};
|
|
288
299
|
|
|
289
300
|
// A function value in `variants` (a function variant) replaces any inherited
|
|
290
301
|
// variant for the same key. An object value merges value-by-value with an
|
|
@@ -359,7 +370,8 @@ export type DefaultVariants<V> = {
|
|
|
359
370
|
|
|
360
371
|
/**
|
|
361
372
|
* A normalized style object accepted by Clava config, variant, and refine
|
|
362
|
-
* style entries. CSS custom properties are supported with string
|
|
373
|
+
* style entries. CSS custom properties are supported with string and number
|
|
374
|
+
* values.
|
|
363
375
|
*
|
|
364
376
|
* @example
|
|
365
377
|
* ```ts
|
|
@@ -368,12 +380,11 @@ export type DefaultVariants<V> = {
|
|
|
368
380
|
* const style: StyleValue = {
|
|
369
381
|
* color: "red",
|
|
370
382
|
* "--button-accent": "oklch(62% 0.2 250)",
|
|
383
|
+
* "--button-scale": 1.1,
|
|
371
384
|
* };
|
|
372
385
|
* ```
|
|
373
386
|
*/
|
|
374
|
-
export
|
|
375
|
-
[key: `--${string}`]: string;
|
|
376
|
-
};
|
|
387
|
+
export interface StyleValue extends CSS.Properties, CustomProperties {}
|
|
377
388
|
|
|
378
389
|
/**
|
|
379
390
|
* A value that contributes both class and style output from a base config,
|
package/src/utils.ts
CHANGED
|
@@ -1,40 +1,81 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import type {
|
|
3
|
-
HTMLCSSProperties,
|
|
4
|
-
JSXCSSProperties,
|
|
5
|
-
StyleValue,
|
|
6
|
-
} from "./types.ts";
|
|
1
|
+
import type { HTMLCSSProperties, StyleValue } from "./types.ts";
|
|
7
2
|
|
|
8
|
-
export const
|
|
9
|
-
export type Mode = (typeof MODES)[number];
|
|
3
|
+
export const hasOwn = Object.hasOwn;
|
|
10
4
|
|
|
11
|
-
//
|
|
12
|
-
|
|
5
|
+
// Keep this explicit so normalization does not depend on browser globals.
|
|
6
|
+
// Vendor-prefixed variants are derived below instead of duplicating the list.
|
|
7
|
+
const unitlessNumberProperties = new Set([
|
|
8
|
+
"animationIterationCount",
|
|
9
|
+
"aspectRatio",
|
|
10
|
+
"borderImage",
|
|
11
|
+
"borderImageOutset",
|
|
12
|
+
"borderImageSlice",
|
|
13
|
+
"borderImageWidth",
|
|
14
|
+
"boxFlex",
|
|
15
|
+
"boxFlexGroup",
|
|
16
|
+
"boxOrdinalGroup",
|
|
17
|
+
"columnCount",
|
|
18
|
+
"columns",
|
|
19
|
+
"fillOpacity",
|
|
20
|
+
"flex",
|
|
21
|
+
"flexGrow",
|
|
22
|
+
"flexNegative",
|
|
23
|
+
"flexOrder",
|
|
24
|
+
"flexPositive",
|
|
25
|
+
"flexShrink",
|
|
26
|
+
"floodOpacity",
|
|
27
|
+
"fontSizeAdjust",
|
|
28
|
+
"fontWeight",
|
|
29
|
+
"gridArea",
|
|
30
|
+
"gridColumn",
|
|
31
|
+
"gridColumnEnd",
|
|
32
|
+
"gridColumnSpan",
|
|
33
|
+
"gridColumnStart",
|
|
34
|
+
"gridRow",
|
|
35
|
+
"gridRowEnd",
|
|
36
|
+
"gridRowSpan",
|
|
37
|
+
"gridRowStart",
|
|
38
|
+
"hyphenateLimitChars",
|
|
39
|
+
"initialLetter",
|
|
40
|
+
"lineClamp",
|
|
41
|
+
"lineHeight",
|
|
42
|
+
"maskBorder",
|
|
43
|
+
"maskBorderOutset",
|
|
44
|
+
"maskBorderSlice",
|
|
45
|
+
"maskBorderWidth",
|
|
46
|
+
"mathDepth",
|
|
47
|
+
"maxLines",
|
|
48
|
+
"opacity",
|
|
49
|
+
"order",
|
|
50
|
+
"orphans",
|
|
51
|
+
"scale",
|
|
52
|
+
"shapeImageThreshold",
|
|
53
|
+
"stopOpacity",
|
|
54
|
+
"strokeDasharray",
|
|
55
|
+
"strokeDashoffset",
|
|
56
|
+
"strokeMiterlimit",
|
|
57
|
+
"strokeOpacity",
|
|
58
|
+
"strokeWidth",
|
|
59
|
+
"tabSize",
|
|
60
|
+
"widows",
|
|
61
|
+
"zIndex",
|
|
62
|
+
"zoom",
|
|
63
|
+
]);
|
|
13
64
|
|
|
14
|
-
function
|
|
15
|
-
|
|
16
|
-
return code >= 97 && code <= 122;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Returns the appropriate class property name based on the mode.
|
|
21
|
-
* @example
|
|
22
|
-
* getClassPropertyName("jsx") // "className"
|
|
23
|
-
* getClassPropertyName("html") // "class"
|
|
24
|
-
*/
|
|
25
|
-
export function getClassPropertyName(mode: Mode) {
|
|
26
|
-
return mode === "jsx" ? "className" : "class";
|
|
65
|
+
function isCustomProperty(property: string) {
|
|
66
|
+
return property.charCodeAt(0) === 45 && property.charCodeAt(1) === 45;
|
|
27
67
|
}
|
|
28
68
|
|
|
29
69
|
/**
|
|
30
70
|
* Converts a hyphenated CSS property name to camelCase.
|
|
31
71
|
* @example
|
|
32
72
|
* hyphenToCamel("background-color") // "backgroundColor"
|
|
73
|
+
* hyphenToCamel("-ms-transition") // "msTransition" (lowercase "ms" prefix)
|
|
33
74
|
* hyphenToCamel("--custom-var") // "--custom-var" (CSS variables are preserved)
|
|
34
75
|
*/
|
|
35
76
|
export function hyphenToCamel(str: string) {
|
|
36
77
|
// CSS custom properties (variables) should not be converted
|
|
37
|
-
if (
|
|
78
|
+
if (isCustomProperty(str)) {
|
|
38
79
|
return str;
|
|
39
80
|
}
|
|
40
81
|
// Fast path: no hyphen -> return as-is
|
|
@@ -56,8 +97,13 @@ export function hyphenToCamel(str: string) {
|
|
|
56
97
|
}
|
|
57
98
|
|
|
58
99
|
const code = str.charCodeAt(nextIndex);
|
|
59
|
-
|
|
60
|
-
|
|
100
|
+
const isAsciiLetter =
|
|
101
|
+
(code >= 65 && code <= 90) || (code >= 97 && code <= 122);
|
|
102
|
+
if (isAsciiLetter) {
|
|
103
|
+
const isMicrosoftPrefix = hyphenIndex === 0 && str.startsWith("ms-", 1);
|
|
104
|
+
result += isMicrosoftPrefix
|
|
105
|
+
? str[nextIndex]
|
|
106
|
+
: str[nextIndex].toUpperCase();
|
|
61
107
|
lastIndex = nextIndex + 1;
|
|
62
108
|
} else {
|
|
63
109
|
result += "-";
|
|
@@ -74,11 +120,12 @@ export function hyphenToCamel(str: string) {
|
|
|
74
120
|
* Converts a camelCase CSS property name to hyphenated form.
|
|
75
121
|
* @example
|
|
76
122
|
* camelToHyphen("backgroundColor") // "background-color"
|
|
123
|
+
* camelToHyphen("msTransition") // "-ms-transition" (lowercase "ms" prefix)
|
|
77
124
|
* camelToHyphen("--customVar") // "--customVar" (CSS variables are preserved)
|
|
78
125
|
*/
|
|
79
126
|
export function camelToHyphen(str: string) {
|
|
80
127
|
// CSS custom properties (variables) should not be converted
|
|
81
|
-
if (
|
|
128
|
+
if (isCustomProperty(str)) {
|
|
82
129
|
return str;
|
|
83
130
|
}
|
|
84
131
|
|
|
@@ -87,6 +134,10 @@ export function camelToHyphen(str: string) {
|
|
|
87
134
|
for (let i = 0; i < str.length; i++) {
|
|
88
135
|
const code = str.charCodeAt(i);
|
|
89
136
|
if (code < 65 || code > 90) continue;
|
|
137
|
+
const isMicrosoftPrefix = i === 2 && str.startsWith("ms");
|
|
138
|
+
if (isMicrosoftPrefix) {
|
|
139
|
+
result += "-";
|
|
140
|
+
}
|
|
90
141
|
result += str.slice(lastIndex, i);
|
|
91
142
|
result += "-";
|
|
92
143
|
result += str[i].toLowerCase();
|
|
@@ -99,16 +150,38 @@ export function camelToHyphen(str: string) {
|
|
|
99
150
|
return result + str.slice(lastIndex);
|
|
100
151
|
}
|
|
101
152
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
153
|
+
function hasVendorPrefix(property: string, prefix: string) {
|
|
154
|
+
const nextCode = property.charCodeAt(prefix.length);
|
|
155
|
+
if (nextCode < 65 || nextCode > 90) return false;
|
|
156
|
+
return property.startsWith(prefix);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// "ms" is used in JSX, while camelizing hyphenated properties produces "Ms".
|
|
160
|
+
const vendorPrefixes = ["Webkit", "Moz", "ms", "Ms", "O"];
|
|
161
|
+
|
|
162
|
+
function isUnitlessNumberProperty(property: string) {
|
|
163
|
+
if (unitlessNumberProperties.has(property)) return true;
|
|
164
|
+
|
|
165
|
+
for (const prefix of vendorPrefixes) {
|
|
166
|
+
if (!hasVendorPrefix(property, prefix)) continue;
|
|
167
|
+
const firstCode = property.charCodeAt(prefix.length);
|
|
168
|
+
const unprefixedProperty =
|
|
169
|
+
String.fromCharCode(firstCode + 32) + property.slice(prefix.length + 1);
|
|
170
|
+
return unitlessNumberProperties.has(unprefixedProperty);
|
|
171
|
+
}
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function normalizeStyleValue(property: string, value: string | number) {
|
|
109
176
|
if (typeof value === "string") {
|
|
110
177
|
return value;
|
|
111
178
|
}
|
|
179
|
+
if (isCustomProperty(property)) {
|
|
180
|
+
return value;
|
|
181
|
+
}
|
|
182
|
+
if (isUnitlessNumberProperty(property)) {
|
|
183
|
+
return value;
|
|
184
|
+
}
|
|
112
185
|
return `${value}px`;
|
|
113
186
|
}
|
|
114
187
|
|
|
@@ -118,10 +191,11 @@ export function parseLengthValue(value: string | number) {
|
|
|
118
191
|
* htmlStyleToStyleValue("background-color: red; font-size: 16px;");
|
|
119
192
|
* // { backgroundColor: "red", fontSize: "16px" }
|
|
120
193
|
*/
|
|
121
|
-
export function htmlStyleToStyleValue(
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
194
|
+
export function htmlStyleToStyleValue(
|
|
195
|
+
styleString: string,
|
|
196
|
+
result: StyleValue = {},
|
|
197
|
+
) {
|
|
198
|
+
if (!styleString) return result;
|
|
125
199
|
const len = styleString.length;
|
|
126
200
|
let i = 0;
|
|
127
201
|
while (i < len) {
|
|
@@ -202,34 +276,18 @@ export function htmlStyleToStyleValue(styleString: string) {
|
|
|
202
276
|
* htmlObjStyleToStyleValue({ "background-color": "red", "font-size": "16px" });
|
|
203
277
|
* // { backgroundColor: "red", fontSize: "16px" }
|
|
204
278
|
*/
|
|
205
|
-
export function htmlObjStyleToStyleValue(
|
|
206
|
-
|
|
279
|
+
export function htmlObjStyleToStyleValue(
|
|
280
|
+
style: object,
|
|
281
|
+
result: StyleValue = {},
|
|
282
|
+
): StyleValue {
|
|
207
283
|
for (const key in style) {
|
|
208
|
-
if (!hasOwn
|
|
284
|
+
if (!hasOwn(style, key)) continue;
|
|
209
285
|
const value = (style as Record<string, unknown>)[key];
|
|
210
286
|
if (value == null) continue;
|
|
287
|
+
const property = hyphenToCamel(key);
|
|
211
288
|
// CSS property names and values are dynamic - cast required for index access
|
|
212
|
-
(result as Record<string, string>)[
|
|
213
|
-
|
|
214
|
-
);
|
|
215
|
-
}
|
|
216
|
-
return result;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* Converts a camelCase style object to a StyleValue object.
|
|
221
|
-
* @example
|
|
222
|
-
* jsxStyleToStyleValue({ backgroundColor: "red", fontSize: 16 });
|
|
223
|
-
* // { backgroundColor: "red", fontSize: "16px" }
|
|
224
|
-
*/
|
|
225
|
-
export function jsxStyleToStyleValue(style: JSXCSSProperties) {
|
|
226
|
-
const result: StyleValue = {};
|
|
227
|
-
for (const key in style) {
|
|
228
|
-
if (!hasOwn.call(style, key)) continue;
|
|
229
|
-
const value = (style as Record<string, unknown>)[key];
|
|
230
|
-
if (value == null) continue;
|
|
231
|
-
// CSS property names and values are dynamic - cast required for index access
|
|
232
|
-
(result as Record<string, string>)[key] = parseLengthValue(
|
|
289
|
+
(result as Record<string, string | number>)[property] = normalizeStyleValue(
|
|
290
|
+
property,
|
|
233
291
|
value as string | number,
|
|
234
292
|
);
|
|
235
293
|
}
|
|
@@ -245,7 +303,7 @@ export function jsxStyleToStyleValue(style: JSXCSSProperties) {
|
|
|
245
303
|
export function styleValueToHTMLStyle(style: StyleValue): string {
|
|
246
304
|
let result = "";
|
|
247
305
|
for (const key in style) {
|
|
248
|
-
if (!hasOwn
|
|
306
|
+
if (!hasOwn(style, key)) continue;
|
|
249
307
|
const value = (style as Record<string, unknown>)[key];
|
|
250
308
|
if (value == null) continue;
|
|
251
309
|
if (result) {
|
|
@@ -266,46 +324,12 @@ export function styleValueToHTMLStyle(style: StyleValue): string {
|
|
|
266
324
|
* // { "background-color": "red", "font-size": "16px" }
|
|
267
325
|
*/
|
|
268
326
|
export function styleValueToHTMLObjStyle(style: StyleValue) {
|
|
269
|
-
const result:
|
|
327
|
+
const result: HTMLCSSProperties = {};
|
|
270
328
|
for (const key in style) {
|
|
271
|
-
if (!hasOwn
|
|
329
|
+
if (!hasOwn(style, key)) continue;
|
|
272
330
|
const value = (style as Record<string, unknown>)[key];
|
|
273
331
|
if (value == null) continue;
|
|
274
332
|
(result as Record<string, unknown>)[camelToHyphen(key)] = value;
|
|
275
333
|
}
|
|
276
334
|
return result;
|
|
277
335
|
}
|
|
278
|
-
|
|
279
|
-
/**
|
|
280
|
-
* Converts a StyleValue object to a camelCase style object.
|
|
281
|
-
* @example
|
|
282
|
-
* styleValueToJSXStyle({ backgroundColor: "red", fontSize: "16px" });
|
|
283
|
-
* // { backgroundColor: "red", fontSize: "16px" }
|
|
284
|
-
*/
|
|
285
|
-
export function styleValueToJSXStyle(style: StyleValue) {
|
|
286
|
-
return style as JSXCSSProperties;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
/**
|
|
290
|
-
* Type guard to check if a style object has hyphenated keys.
|
|
291
|
-
* @example
|
|
292
|
-
* isHTMLObjStyle({ "background-color": "red" }); // true
|
|
293
|
-
* isHTMLObjStyle({ backgroundColor: "red" }); // false
|
|
294
|
-
*/
|
|
295
|
-
export function isHTMLObjStyle(
|
|
296
|
-
style: CSS.Properties<any> | CSS.PropertiesHyphen<any>,
|
|
297
|
-
): style is CSS.PropertiesHyphen {
|
|
298
|
-
for (const key in style) {
|
|
299
|
-
if (!hasOwn.call(style, key)) continue;
|
|
300
|
-
// Quick exclusion of CSS custom properties (--foo)
|
|
301
|
-
if (
|
|
302
|
-
key.length >= 2 &&
|
|
303
|
-
key.charCodeAt(0) === 45 &&
|
|
304
|
-
key.charCodeAt(1) === 45
|
|
305
|
-
) {
|
|
306
|
-
continue;
|
|
307
|
-
}
|
|
308
|
-
if (key.indexOf("-") !== -1) return true;
|
|
309
|
-
}
|
|
310
|
-
return false;
|
|
311
|
-
}
|
package/rolldown.config.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { defineConfig } from "rolldown";
|
|
2
|
-
import { dts } from "rolldown-plugin-dts";
|
|
3
|
-
import packageJson from "./package.json" with { type: "json" };
|
|
4
|
-
|
|
5
|
-
export default defineConfig({
|
|
6
|
-
input: "src/index.ts",
|
|
7
|
-
external: Object.keys(packageJson.dependencies ?? {}),
|
|
8
|
-
platform: "neutral",
|
|
9
|
-
output: {
|
|
10
|
-
cleanDir: true,
|
|
11
|
-
format: "es",
|
|
12
|
-
sourcemap: true,
|
|
13
|
-
},
|
|
14
|
-
plugins: [dts()],
|
|
15
|
-
});
|
package/tests/_utils.ts
DELETED
|
@@ -1,178 +0,0 @@
|
|
|
1
|
-
import { expect } from "vitest";
|
|
2
|
-
import { type VariantProps, create, cv as cvBase } from "../src/index.ts";
|
|
3
|
-
import type {
|
|
4
|
-
AnyComponent,
|
|
5
|
-
CVComponent,
|
|
6
|
-
ComponentResult,
|
|
7
|
-
HTMLObjProps,
|
|
8
|
-
HTMLProps,
|
|
9
|
-
JSXProps,
|
|
10
|
-
StyleClassProps,
|
|
11
|
-
StyleProperty,
|
|
12
|
-
Variants,
|
|
13
|
-
} from "../src/types.ts";
|
|
14
|
-
import {
|
|
15
|
-
htmlObjStyleToStyleValue,
|
|
16
|
-
htmlStyleToStyleValue,
|
|
17
|
-
isHTMLObjStyle,
|
|
18
|
-
jsxStyleToStyleValue,
|
|
19
|
-
} from "../src/utils.ts";
|
|
20
|
-
|
|
21
|
-
const MODES = ["jsx", "html", "htmlObj"] as const;
|
|
22
|
-
type Mode = (typeof MODES)[number] | null;
|
|
23
|
-
|
|
24
|
-
export type HTMLProperties<T extends AnyComponent> = VariantProps<T> & {
|
|
25
|
-
id?: string;
|
|
26
|
-
class?: string;
|
|
27
|
-
className?: string;
|
|
28
|
-
style?: StyleProperty;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
type ConfigParams = NonNullable<Parameters<typeof create>[0]>;
|
|
32
|
-
|
|
33
|
-
interface Config {
|
|
34
|
-
mode: Mode;
|
|
35
|
-
transformClass?: ConfigParams["transformClass"];
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const transformClass = {
|
|
39
|
-
uppercase: (className: string) => className.toUpperCase(),
|
|
40
|
-
} satisfies Record<string, Config["transformClass"]>;
|
|
41
|
-
|
|
42
|
-
export const CONFIGS = {
|
|
43
|
-
default: { mode: null },
|
|
44
|
-
jsx: { mode: "jsx" },
|
|
45
|
-
html: { mode: "html" },
|
|
46
|
-
htmlObj: { mode: "htmlObj" },
|
|
47
|
-
uppercase: {
|
|
48
|
-
mode: null,
|
|
49
|
-
transformClass: transformClass.uppercase,
|
|
50
|
-
},
|
|
51
|
-
} satisfies Record<string, Config>;
|
|
52
|
-
|
|
53
|
-
export function getConfigMode<T extends Config>(config: T): T["mode"] {
|
|
54
|
-
if (!("mode" in config)) return null;
|
|
55
|
-
return config.mode;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export function getConfigTransformClass(config: Config) {
|
|
59
|
-
if (!("transformClass" in config) || !config.transformClass) {
|
|
60
|
-
return (className: string) => className;
|
|
61
|
-
}
|
|
62
|
-
return config.transformClass;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export function getConfigDescription(config: Config) {
|
|
66
|
-
for (const [name, currentConfig] of Object.entries(CONFIGS)) {
|
|
67
|
-
if (currentConfig !== config) continue;
|
|
68
|
-
return name;
|
|
69
|
-
}
|
|
70
|
-
return "custom";
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export function createCVFromConfig(
|
|
74
|
-
config: Config,
|
|
75
|
-
): ReturnType<typeof create>["cv"] {
|
|
76
|
-
const transformClass = getConfigTransformClass(config);
|
|
77
|
-
const hasTransform = "transformClass" in config && config.transformClass;
|
|
78
|
-
if (!hasTransform) {
|
|
79
|
-
return cvBase;
|
|
80
|
-
}
|
|
81
|
-
const { cv } = create({ transformClass });
|
|
82
|
-
return cv;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export function getModeComponent<
|
|
86
|
-
M extends Mode,
|
|
87
|
-
V extends Variants = {},
|
|
88
|
-
const E extends AnyComponent[] = [],
|
|
89
|
-
>(mode: M, component: CVComponent<V, E>) {
|
|
90
|
-
if (!mode) {
|
|
91
|
-
return component;
|
|
92
|
-
}
|
|
93
|
-
return component[mode];
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
function getClass(props: ComponentResult) {
|
|
97
|
-
if ("class" in props) {
|
|
98
|
-
return props.class;
|
|
99
|
-
}
|
|
100
|
-
return props.className;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
export function getClassPropertyName(config: Config) {
|
|
104
|
-
const mode = config.mode;
|
|
105
|
-
if (mode === "jsx" || mode === null) return "className";
|
|
106
|
-
return "class";
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
export function getExpectedPropsKeys(config: Config, ...variantKeys: string[]) {
|
|
110
|
-
if (config.mode === null) {
|
|
111
|
-
return ["class", "className", "style", ...variantKeys];
|
|
112
|
-
}
|
|
113
|
-
return [getClassPropertyName(config), "style", ...variantKeys];
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export function assertDefaultProps(
|
|
117
|
-
props: ComponentResult,
|
|
118
|
-
): asserts props is StyleClassProps {
|
|
119
|
-
if (!("class" in props)) {
|
|
120
|
-
expect.fail("Expected default props to have class");
|
|
121
|
-
}
|
|
122
|
-
if (!("style" in props) || typeof props.style !== "object") {
|
|
123
|
-
expect.fail("Expected default props to have style");
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
export function assertJSXProps(
|
|
128
|
-
props: ComponentResult,
|
|
129
|
-
): asserts props is JSXProps {
|
|
130
|
-
if (!("className" in props)) {
|
|
131
|
-
expect.fail("Expected jsx props to have className");
|
|
132
|
-
}
|
|
133
|
-
if (!("style" in props) || typeof props.style !== "object") {
|
|
134
|
-
expect.fail("Expected jsx props to have style");
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
export function assertHTMLProps(
|
|
139
|
-
props: ComponentResult,
|
|
140
|
-
): asserts props is HTMLProps {
|
|
141
|
-
if (!("class" in props)) {
|
|
142
|
-
expect.fail("Expected html props to have class");
|
|
143
|
-
}
|
|
144
|
-
if (!("style" in props) || typeof props.style !== "string") {
|
|
145
|
-
expect.fail("Expected html props to have style");
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
export function assertHTMLObjProps(
|
|
150
|
-
props: ComponentResult,
|
|
151
|
-
): asserts props is HTMLObjProps {
|
|
152
|
-
if (!("class" in props)) {
|
|
153
|
-
expect.fail("Expected htmlObj props to have class");
|
|
154
|
-
}
|
|
155
|
-
if (!("style" in props) || typeof props.style !== "object") {
|
|
156
|
-
expect.fail("Expected htmlObj props to have style");
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
export function getStyle(props: Pick<ComponentResult, "style">) {
|
|
161
|
-
if (typeof props.style === "string") {
|
|
162
|
-
return htmlStyleToStyleValue(props.style);
|
|
163
|
-
}
|
|
164
|
-
if (typeof props.style === "object") {
|
|
165
|
-
if (isHTMLObjStyle(props.style)) {
|
|
166
|
-
return htmlObjStyleToStyleValue(props.style);
|
|
167
|
-
}
|
|
168
|
-
return jsxStyleToStyleValue(props.style);
|
|
169
|
-
}
|
|
170
|
-
return {};
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
export function getStyleClass(props: ComponentResult): Record<string, unknown> {
|
|
174
|
-
return {
|
|
175
|
-
...getStyle(props),
|
|
176
|
-
class: getClass(props),
|
|
177
|
-
};
|
|
178
|
-
}
|