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/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
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
import type * as CSS from "csstype";
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* A class value accepted by Clava. It supports strings, numbers, booleans,
|
|
5
|
+
* nullish values, and nested arrays.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import type { ClassValue } from "clava";
|
|
10
|
+
*
|
|
11
|
+
* const className: ClassValue = [
|
|
12
|
+
* "button",
|
|
13
|
+
* false && "button-hidden",
|
|
14
|
+
* ["button-primary"],
|
|
15
|
+
* ];
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
3
18
|
export type ClassValue =
|
|
4
19
|
| string
|
|
5
20
|
| number
|
|
@@ -9,29 +24,82 @@ export type ClassValue =
|
|
|
9
24
|
| void
|
|
10
25
|
| ClassValue[];
|
|
11
26
|
|
|
12
|
-
export
|
|
27
|
+
export interface JSXCSSProperties extends CSS.Properties<string | number> {
|
|
28
|
+
[key: `--${string}`]: string | number;
|
|
29
|
+
}
|
|
13
30
|
|
|
14
|
-
export
|
|
31
|
+
export interface HTMLCSSProperties extends CSS.PropertiesHyphen<
|
|
32
|
+
string | number
|
|
33
|
+
> {
|
|
34
|
+
[key: `--${string}`]: string | number;
|
|
35
|
+
}
|
|
15
36
|
|
|
16
37
|
export type CSSProperties = CSS.Properties;
|
|
17
38
|
|
|
18
39
|
export type StyleProperty = JSXCSSProperties | HTMLCSSProperties | string;
|
|
19
40
|
|
|
41
|
+
/**
|
|
42
|
+
* The prop object returned by a component's `.jsx()` mode.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* import { type JSXProps, cv } from "clava";
|
|
47
|
+
*
|
|
48
|
+
* const button = cv({ class: "button" });
|
|
49
|
+
* const props: JSXProps = button.jsx();
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
20
52
|
export interface JSXProps {
|
|
21
53
|
className: string;
|
|
22
54
|
style: JSXCSSProperties;
|
|
23
55
|
}
|
|
24
56
|
|
|
57
|
+
/**
|
|
58
|
+
* The prop object returned by a component's `.html()` mode. The `style` value
|
|
59
|
+
* is serialized as an HTML style string.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* import { type HTMLProps, cv } from "clava";
|
|
64
|
+
*
|
|
65
|
+
* const button = cv({ style: { color: "red" } });
|
|
66
|
+
* const props: HTMLProps = button.html();
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
25
69
|
export interface HTMLProps {
|
|
26
70
|
class: string;
|
|
27
71
|
style: string;
|
|
28
72
|
}
|
|
29
73
|
|
|
74
|
+
/**
|
|
75
|
+
* The prop object returned by a component's `.htmlObj()` mode. The `style`
|
|
76
|
+
* value uses hyphenated CSS property names.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```ts
|
|
80
|
+
* import { type HTMLObjProps, cv } from "clava";
|
|
81
|
+
*
|
|
82
|
+
* const button = cv({ style: { fontSize: "16px" } });
|
|
83
|
+
* const props: HTMLObjProps = button.htmlObj();
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
30
86
|
export interface HTMLObjProps {
|
|
31
87
|
class: string;
|
|
32
88
|
style: HTMLCSSProperties;
|
|
33
89
|
}
|
|
34
90
|
|
|
91
|
+
/**
|
|
92
|
+
* The default prop object returned by a Clava component. It uses `class`
|
|
93
|
+
* rather than `className` and keeps styles as a normalized object.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```ts
|
|
97
|
+
* import { type StyleClassProps, cv } from "clava";
|
|
98
|
+
*
|
|
99
|
+
* const button = cv({ class: "button" });
|
|
100
|
+
* const props: StyleClassProps = button();
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
35
103
|
export interface StyleClassProps {
|
|
36
104
|
class: string;
|
|
37
105
|
style: StyleValue;
|
|
@@ -177,6 +245,25 @@ export interface ModalComponent<V, R extends ComponentResult> {
|
|
|
177
245
|
propKeys: (keyof V | ComponentPropKey<R>)[];
|
|
178
246
|
}
|
|
179
247
|
|
|
248
|
+
/**
|
|
249
|
+
* A callable Clava component returned by `cv()`. It includes the default
|
|
250
|
+
* output mode plus `.jsx()`, `.html()`, and `.htmlObj()` mode helpers.
|
|
251
|
+
*
|
|
252
|
+
* @example
|
|
253
|
+
* ```ts
|
|
254
|
+
* import { type CVComponent, cv } from "clava";
|
|
255
|
+
*
|
|
256
|
+
* const button: CVComponent<{
|
|
257
|
+
* size: { sm: string; lg: string };
|
|
258
|
+
* }> = cv({
|
|
259
|
+
* variants: {
|
|
260
|
+
* size: { sm: "button-sm", lg: "button-lg" },
|
|
261
|
+
* },
|
|
262
|
+
* });
|
|
263
|
+
*
|
|
264
|
+
* button.jsx({ size: "lg" });
|
|
265
|
+
* ```
|
|
266
|
+
*/
|
|
180
267
|
export interface CVComponent<
|
|
181
268
|
V extends Variants = {},
|
|
182
269
|
E extends AnyComponent[] = [],
|
|
@@ -195,15 +282,11 @@ type MergeExtendedVariants<T> = T extends readonly [infer First, ...infer Rest]
|
|
|
195
282
|
? ExtractVariants<First> & MergeExtendedVariants<Rest>
|
|
196
283
|
: {};
|
|
197
284
|
|
|
198
|
-
// Returns a component's effective variants
|
|
199
|
-
//
|
|
200
|
-
//
|
|
201
|
-
//
|
|
202
|
-
|
|
203
|
-
type ExtractVariants<T> =
|
|
204
|
-
T extends CVComponent<infer V, infer E, any>
|
|
205
|
-
? MergeVariantMaps<V, MergeExtendedVariants<E>>
|
|
206
|
-
: {};
|
|
285
|
+
// Returns a component's effective variants so an intermediate component's
|
|
286
|
+
// static variant hides a grandparent's function variant from descendants.
|
|
287
|
+
// CVComponent and its mode helpers instantiate ModalComponent with the already
|
|
288
|
+
// merged MergeVariants<V, E>, so inferring V yields that effective chain.
|
|
289
|
+
type ExtractVariants<T> = T extends ModalComponent<infer V, any> ? V : {};
|
|
207
290
|
|
|
208
291
|
// A function value in `variants` (a function variant) replaces any inherited
|
|
209
292
|
// variant for the same key. An object value merges value-by-value with an
|
|
@@ -257,13 +340,9 @@ export type VariantValues<V> = {
|
|
|
257
340
|
[K in keyof V]?: ExtractVariantValue<V[K]> | undefined;
|
|
258
341
|
};
|
|
259
342
|
|
|
260
|
-
interface DefaultVariantContext<V, K extends keyof V> {
|
|
261
|
-
defaultValue: ExtractVariantValue<V[K]> | undefined;
|
|
262
|
-
variants: Readonly<VariantValues<V>>;
|
|
263
|
-
}
|
|
264
|
-
|
|
265
343
|
type ComputedDefaultVariant<V, K extends keyof V> = (
|
|
266
|
-
|
|
344
|
+
defaultValue: ExtractVariantValue<V[K]> | undefined,
|
|
345
|
+
variants: Readonly<VariantValues<V>>,
|
|
267
346
|
) => ExtractVariantValue<V[K]> | undefined;
|
|
268
347
|
|
|
269
348
|
type NonFunctionVariantValue<T> = Exclude<T, (...args: any[]) => any>;
|
|
@@ -280,10 +359,40 @@ export type DefaultVariants<V> = {
|
|
|
280
359
|
[K in keyof V]?: DefaultVariantValue<V, K> | undefined;
|
|
281
360
|
};
|
|
282
361
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
362
|
+
/**
|
|
363
|
+
* A normalized style object accepted by Clava config, variant, and refine
|
|
364
|
+
* style entries. CSS custom properties are supported with string and number
|
|
365
|
+
* values.
|
|
366
|
+
*
|
|
367
|
+
* @example
|
|
368
|
+
* ```ts
|
|
369
|
+
* import type { StyleValue } from "clava";
|
|
370
|
+
*
|
|
371
|
+
* const style: StyleValue = {
|
|
372
|
+
* color: "red",
|
|
373
|
+
* "--button-accent": "oklch(62% 0.2 250)",
|
|
374
|
+
* "--button-scale": 1.1,
|
|
375
|
+
* };
|
|
376
|
+
* ```
|
|
377
|
+
*/
|
|
378
|
+
export interface StyleValue extends CSS.Properties {
|
|
379
|
+
[key: `--${string}`]: string | number;
|
|
380
|
+
}
|
|
286
381
|
|
|
382
|
+
/**
|
|
383
|
+
* A value that contributes both class and style output from a base config,
|
|
384
|
+
* variant value, function variant, or refine callback.
|
|
385
|
+
*
|
|
386
|
+
* @example
|
|
387
|
+
* ```ts
|
|
388
|
+
* import type { StyleClassValue } from "clava";
|
|
389
|
+
*
|
|
390
|
+
* const tone: StyleClassValue = {
|
|
391
|
+
* class: "button-primary",
|
|
392
|
+
* style: { color: "white" },
|
|
393
|
+
* };
|
|
394
|
+
* ```
|
|
395
|
+
*/
|
|
287
396
|
export interface StyleClassValue {
|
|
288
397
|
style?: StyleValue;
|
|
289
398
|
class?: ClassValue;
|
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
|
-
});
|