clava 0.2.4 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +24 -0
- package/README.md +552 -0
- package/dist/index.d.ts +5 -5
- package/dist/index.js +310 -141
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/rolldown.config.ts +1 -0
- package/src/index.ts +600 -205
- package/src/types.ts +8 -5
- package/tests/build.test.ts +18 -0
- package/tests/component-api.test.ts +276 -7
- package/tests/computed.test.ts +424 -1
- package/tests/solid.test.ts +30 -0
- package/tests/split-props.test.ts +73 -1
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import clsx from "clsx";
|
|
2
2
|
//#region src/utils.ts
|
|
3
|
-
const hasOwn
|
|
3
|
+
const hasOwn = Object.prototype.hasOwnProperty;
|
|
4
4
|
function isAsciiLetter(code) {
|
|
5
5
|
if (code >= 65 && code <= 90) return true;
|
|
6
6
|
return code >= 97 && code <= 122;
|
|
@@ -137,7 +137,7 @@ function htmlStyleToStyleValue(styleString) {
|
|
|
137
137
|
function htmlObjStyleToStyleValue(style) {
|
|
138
138
|
const result = {};
|
|
139
139
|
for (const key in style) {
|
|
140
|
-
if (!hasOwn
|
|
140
|
+
if (!hasOwn.call(style, key)) continue;
|
|
141
141
|
const value = style[key];
|
|
142
142
|
if (value == null) continue;
|
|
143
143
|
result[hyphenToCamel(key)] = parseLengthValue(value);
|
|
@@ -153,7 +153,7 @@ function htmlObjStyleToStyleValue(style) {
|
|
|
153
153
|
function jsxStyleToStyleValue(style) {
|
|
154
154
|
const result = {};
|
|
155
155
|
for (const key in style) {
|
|
156
|
-
if (!hasOwn
|
|
156
|
+
if (!hasOwn.call(style, key)) continue;
|
|
157
157
|
const value = style[key];
|
|
158
158
|
if (value == null) continue;
|
|
159
159
|
result[key] = parseLengthValue(value);
|
|
@@ -169,7 +169,7 @@ function jsxStyleToStyleValue(style) {
|
|
|
169
169
|
function styleValueToHTMLStyle(style) {
|
|
170
170
|
let result = "";
|
|
171
171
|
for (const key in style) {
|
|
172
|
-
if (!hasOwn
|
|
172
|
+
if (!hasOwn.call(style, key)) continue;
|
|
173
173
|
const value = style[key];
|
|
174
174
|
if (value == null) continue;
|
|
175
175
|
if (result) result += "; ";
|
|
@@ -189,7 +189,7 @@ function styleValueToHTMLStyle(style) {
|
|
|
189
189
|
function styleValueToHTMLObjStyle(style) {
|
|
190
190
|
const result = {};
|
|
191
191
|
for (const key in style) {
|
|
192
|
-
if (!hasOwn
|
|
192
|
+
if (!hasOwn.call(style, key)) continue;
|
|
193
193
|
const value = style[key];
|
|
194
194
|
if (value == null) continue;
|
|
195
195
|
result[camelToHyphen(key)] = value;
|
|
@@ -213,7 +213,7 @@ function styleValueToJSXStyle(style) {
|
|
|
213
213
|
*/
|
|
214
214
|
function isHTMLObjStyle(style) {
|
|
215
215
|
for (const key in style) {
|
|
216
|
-
if (!hasOwn
|
|
216
|
+
if (!hasOwn.call(style, key)) continue;
|
|
217
217
|
if (key.length >= 2 && key.charCodeAt(0) === 45 && key.charCodeAt(1) === 45) continue;
|
|
218
218
|
if (key.indexOf("-") !== -1) return true;
|
|
219
219
|
}
|
|
@@ -222,8 +222,51 @@ function isHTMLObjStyle(style) {
|
|
|
222
222
|
//#endregion
|
|
223
223
|
//#region src/index.ts
|
|
224
224
|
const META_KEY = "__meta";
|
|
225
|
-
const hasOwn = Object.prototype.hasOwnProperty;
|
|
226
225
|
const EMPTY_DEFAULTS = Object.freeze({});
|
|
226
|
+
const MAX_COMPUTED_RUNS = 50;
|
|
227
|
+
function areVariantsEqual(a, b) {
|
|
228
|
+
for (const key in a) {
|
|
229
|
+
if (!Object.hasOwn(a, key)) continue;
|
|
230
|
+
if (!Object.is(a[key], b[key])) return false;
|
|
231
|
+
}
|
|
232
|
+
for (const key in b) {
|
|
233
|
+
if (!Object.hasOwn(b, key)) continue;
|
|
234
|
+
if (!Object.hasOwn(a, key)) return false;
|
|
235
|
+
}
|
|
236
|
+
return true;
|
|
237
|
+
}
|
|
238
|
+
function warnComputedLimit(runState) {
|
|
239
|
+
if (runState.warned) return;
|
|
240
|
+
runState.warned = true;
|
|
241
|
+
if (process.env.NODE_ENV !== "production") console.warn("Clava: Maximum computed update iterations exceeded. This can happen when a computed callback calls setVariants or setDefaultVariants, but one of the variants changes on every run.");
|
|
242
|
+
}
|
|
243
|
+
function getExtUserVariantProps(userVariantProps, protectedVariants, changedVariants) {
|
|
244
|
+
const extUserVariantProps = {};
|
|
245
|
+
Object.assign(extUserVariantProps, userVariantProps);
|
|
246
|
+
if (protectedVariants) Object.assign(extUserVariantProps, protectedVariants);
|
|
247
|
+
if (changedVariants) Object.assign(extUserVariantProps, changedVariants);
|
|
248
|
+
return extUserVariantProps;
|
|
249
|
+
}
|
|
250
|
+
function mergeVariants(target, source, skipKeys) {
|
|
251
|
+
let changed = false;
|
|
252
|
+
if (!skipKeys || skipKeys.size === 0) {
|
|
253
|
+
for (const key in source) {
|
|
254
|
+
if (!Object.hasOwn(source, key)) continue;
|
|
255
|
+
const value = source[key];
|
|
256
|
+
if (!Object.is(target[key], value)) changed = true;
|
|
257
|
+
target[key] = value;
|
|
258
|
+
}
|
|
259
|
+
return changed;
|
|
260
|
+
}
|
|
261
|
+
for (const key in source) {
|
|
262
|
+
if (!Object.hasOwn(source, key)) continue;
|
|
263
|
+
if (skipKeys.has(key)) continue;
|
|
264
|
+
const value = source[key];
|
|
265
|
+
if (!Object.is(target[key], value)) changed = true;
|
|
266
|
+
target[key] = value;
|
|
267
|
+
}
|
|
268
|
+
return changed;
|
|
269
|
+
}
|
|
227
270
|
function getComponentMeta(component) {
|
|
228
271
|
return component[META_KEY];
|
|
229
272
|
}
|
|
@@ -284,7 +327,7 @@ function collectVariantKeys(config) {
|
|
|
284
327
|
for (let i = 0; i < extKeys.length; i++) keys.add(extKeys[i]);
|
|
285
328
|
}
|
|
286
329
|
if (config.variants) for (const key in config.variants) {
|
|
287
|
-
if (!hasOwn
|
|
330
|
+
if (!Object.hasOwn(config.variants, key)) continue;
|
|
288
331
|
if (config.variants[key] === null) {
|
|
289
332
|
keys.delete(key);
|
|
290
333
|
continue;
|
|
@@ -292,7 +335,7 @@ function collectVariantKeys(config) {
|
|
|
292
335
|
keys.add(key);
|
|
293
336
|
}
|
|
294
337
|
if (config.computedVariants) for (const key in config.computedVariants) {
|
|
295
|
-
if (!hasOwn
|
|
338
|
+
if (!Object.hasOwn(config.computedVariants, key)) continue;
|
|
296
339
|
keys.add(key);
|
|
297
340
|
}
|
|
298
341
|
return Array.from(keys);
|
|
@@ -316,7 +359,7 @@ function collectDisabledVariantKeys(config) {
|
|
|
316
359
|
const keys = /* @__PURE__ */ new Set();
|
|
317
360
|
if (!config.variants) return keys;
|
|
318
361
|
for (const key in config.variants) {
|
|
319
|
-
if (!hasOwn
|
|
362
|
+
if (!Object.hasOwn(config.variants, key)) continue;
|
|
320
363
|
if (config.variants[key] === null) keys.add(key);
|
|
321
364
|
}
|
|
322
365
|
return keys;
|
|
@@ -325,12 +368,12 @@ function collectDisabledVariantValues(config) {
|
|
|
325
368
|
const values = {};
|
|
326
369
|
if (!config.variants) return values;
|
|
327
370
|
for (const key in config.variants) {
|
|
328
|
-
if (!hasOwn
|
|
371
|
+
if (!Object.hasOwn(config.variants, key)) continue;
|
|
329
372
|
const variant = config.variants[key];
|
|
330
373
|
if (!isRecordObject(variant)) continue;
|
|
331
374
|
let bucket;
|
|
332
375
|
for (const variantValue in variant) {
|
|
333
|
-
if (!hasOwn
|
|
376
|
+
if (!Object.hasOwn(variant, variantValue)) continue;
|
|
334
377
|
if (variant[variantValue] !== null) continue;
|
|
335
378
|
if (!bucket) {
|
|
336
379
|
bucket = /* @__PURE__ */ new Set();
|
|
@@ -342,23 +385,24 @@ function collectDisabledVariantValues(config) {
|
|
|
342
385
|
return values;
|
|
343
386
|
}
|
|
344
387
|
const EMPTY_SOURCE = {
|
|
345
|
-
|
|
388
|
+
propKeys: [],
|
|
346
389
|
variantKeys: [],
|
|
347
390
|
isComponent: false
|
|
348
391
|
};
|
|
349
392
|
function normalizeKeySource(source) {
|
|
350
393
|
if (Array.isArray(source)) return {
|
|
351
|
-
|
|
394
|
+
propKeys: source,
|
|
352
395
|
variantKeys: source,
|
|
353
396
|
isComponent: false
|
|
354
397
|
};
|
|
355
398
|
if (!source) return EMPTY_SOURCE;
|
|
356
399
|
if (typeof source !== "object" && typeof source !== "function") return EMPTY_SOURCE;
|
|
357
|
-
if (!("keys" in source)) return EMPTY_SOURCE;
|
|
358
|
-
if (!("variantKeys" in source)) return EMPTY_SOURCE;
|
|
359
400
|
const typed = source;
|
|
401
|
+
if (typeof typed.getVariants !== "function") return EMPTY_SOURCE;
|
|
402
|
+
if (!Array.isArray(typed.propKeys)) return EMPTY_SOURCE;
|
|
403
|
+
if (!Array.isArray(typed.variantKeys)) return EMPTY_SOURCE;
|
|
360
404
|
return {
|
|
361
|
-
|
|
405
|
+
propKeys: typed.propKeys,
|
|
362
406
|
variantKeys: typed.variantKeys,
|
|
363
407
|
isComponent: true
|
|
364
408
|
};
|
|
@@ -384,7 +428,7 @@ function splitPropsImpl(selfKeys, selfIsComponent, props, sources) {
|
|
|
384
428
|
for (let s = 0; s < sourcesLength; s++) {
|
|
385
429
|
const source = normalizeKeySource(sources[s]);
|
|
386
430
|
const sourceResult = {};
|
|
387
|
-
const effectiveKeys = source.isComponent && stylingClaimed ? source.variantKeys : source.
|
|
431
|
+
const effectiveKeys = source.isComponent && stylingClaimed ? source.variantKeys : source.propKeys;
|
|
388
432
|
const effectiveKeysLength = effectiveKeys.length;
|
|
389
433
|
for (let i = 0; i < effectiveKeysLength; i++) {
|
|
390
434
|
const key = effectiveKeys[i];
|
|
@@ -422,7 +466,7 @@ function splitPropsImpl(selfKeys, selfIsComponent, props, sources) {
|
|
|
422
466
|
*/
|
|
423
467
|
const splitProps = ((props, source1, ...sources) => {
|
|
424
468
|
const normalizedSource1 = normalizeKeySource(source1);
|
|
425
|
-
return splitPropsImpl(normalizedSource1.
|
|
469
|
+
return splitPropsImpl(normalizedSource1.propKeys, normalizedSource1.isComponent, props, sources);
|
|
426
470
|
});
|
|
427
471
|
function buildPrebuiltVariant(variantDef) {
|
|
428
472
|
if (!isRecordObject(variantDef)) return {
|
|
@@ -433,7 +477,7 @@ function buildPrebuiltVariant(variantDef) {
|
|
|
433
477
|
const values = {};
|
|
434
478
|
let disabledValues = null;
|
|
435
479
|
for (const key in variantDef) {
|
|
436
|
-
if (!hasOwn
|
|
480
|
+
if (!Object.hasOwn(variantDef, key)) continue;
|
|
437
481
|
const value = variantDef[key];
|
|
438
482
|
if (value === null) {
|
|
439
483
|
if (!disabledValues) disabledValues = /* @__PURE__ */ new Set();
|
|
@@ -477,7 +521,7 @@ function create({ transformClass = (className) => className } = {}) {
|
|
|
477
521
|
const variantEntryNames = [];
|
|
478
522
|
const variantEntryDefs = [];
|
|
479
523
|
if (variants) for (const name in variants) {
|
|
480
|
-
if (!hasOwn
|
|
524
|
+
if (!Object.hasOwn(variants, name)) continue;
|
|
481
525
|
const variant = variants[name];
|
|
482
526
|
if (variant === null) continue;
|
|
483
527
|
variantEntryNames.push(name);
|
|
@@ -487,7 +531,7 @@ function create({ transformClass = (className) => className } = {}) {
|
|
|
487
531
|
const computedVariantNames = [];
|
|
488
532
|
const computedVariantFns = [];
|
|
489
533
|
if (computedVariantsCfg) for (const name in computedVariantsCfg) {
|
|
490
|
-
if (!hasOwn
|
|
534
|
+
if (!Object.hasOwn(computedVariantsCfg, name)) continue;
|
|
491
535
|
computedVariantNames.push(name);
|
|
492
536
|
computedVariantFns.push(computedVariantsCfg[name]);
|
|
493
537
|
}
|
|
@@ -498,14 +542,14 @@ function create({ transformClass = (className) => className } = {}) {
|
|
|
498
542
|
if (meta) Object.assign(staticDefaults, meta.staticDefaults);
|
|
499
543
|
}
|
|
500
544
|
if (variants) for (const name in variants) {
|
|
501
|
-
if (!hasOwn
|
|
545
|
+
if (!Object.hasOwn(variants, name)) continue;
|
|
502
546
|
const variantDef = variants[name];
|
|
503
547
|
if (!isRecordObject(variantDef)) continue;
|
|
504
|
-
if (hasOwn
|
|
548
|
+
if (Object.hasOwn(variantDef, "false") && staticDefaults[name] === void 0) staticDefaults[name] = false;
|
|
505
549
|
}
|
|
506
550
|
if (config.defaultVariants) Object.assign(staticDefaults, config.defaultVariants);
|
|
507
551
|
if (hasAnyDisabled) for (const key in staticDefaults) {
|
|
508
|
-
if (!hasOwn
|
|
552
|
+
if (!Object.hasOwn(staticDefaults, key)) continue;
|
|
509
553
|
if (disabledVariantKeys.has(key)) {
|
|
510
554
|
delete staticDefaults[key];
|
|
511
555
|
continue;
|
|
@@ -520,7 +564,7 @@ function create({ transformClass = (className) => className } = {}) {
|
|
|
520
564
|
const extBaseClassesArr = [];
|
|
521
565
|
const extIsolated = [];
|
|
522
566
|
let hasIsolatedExt = false;
|
|
523
|
-
if (
|
|
567
|
+
if (hasExtend) for (const ext of extend) {
|
|
524
568
|
const meta = getComponentMeta(ext);
|
|
525
569
|
if (!meta) continue;
|
|
526
570
|
extMetas.push(meta);
|
|
@@ -532,9 +576,13 @@ function create({ transformClass = (className) => className } = {}) {
|
|
|
532
576
|
} else extBaseClassesArr.push(meta.baseClass);
|
|
533
577
|
}
|
|
534
578
|
const extCount = extMetas.length;
|
|
535
|
-
const
|
|
536
|
-
for (let i = 0; i < extCount; i++)
|
|
537
|
-
|
|
579
|
+
const extMetasWithComputed = [];
|
|
580
|
+
for (let i = 0; i < extCount; i++) {
|
|
581
|
+
const meta = extMetas[i];
|
|
582
|
+
if (meta.resolveDefaults) extMetasWithComputed.push(meta);
|
|
583
|
+
}
|
|
584
|
+
const extMetasWithComputedCount = extMetasWithComputed.length;
|
|
585
|
+
const shouldCollectChangedVariants = extMetasWithComputedCount > 0;
|
|
538
586
|
let staticExtSkipKeys = null;
|
|
539
587
|
if (hasDisabledVariantKeys || computedVariantCount > 0) {
|
|
540
588
|
staticExtSkipKeys = /* @__PURE__ */ new Set();
|
|
@@ -544,11 +592,11 @@ function create({ transformClass = (className) => className } = {}) {
|
|
|
544
592
|
const staticExtSkipValues = hasDisabledVariantValues ? disabledVariantValues : null;
|
|
545
593
|
function filterDisabledInto(input, out) {
|
|
546
594
|
if (!hasAnyDisabled) {
|
|
547
|
-
for (const key in input) if (hasOwn
|
|
595
|
+
for (const key in input) if (Object.hasOwn(input, key)) out[key] = input[key];
|
|
548
596
|
return;
|
|
549
597
|
}
|
|
550
598
|
for (const key in input) {
|
|
551
|
-
if (!hasOwn
|
|
599
|
+
if (!Object.hasOwn(input, key)) continue;
|
|
552
600
|
if (disabledVariantKeys.has(key)) continue;
|
|
553
601
|
const value = input[key];
|
|
554
602
|
if (hasDisabledVariantValues) {
|
|
@@ -558,26 +606,26 @@ function create({ transformClass = (className) => className } = {}) {
|
|
|
558
606
|
out[key] = value;
|
|
559
607
|
}
|
|
560
608
|
}
|
|
561
|
-
const resolveDefaultsFn = computed ||
|
|
609
|
+
const resolveDefaultsFn = computed || extMetasWithComputedCount > 0 ? (childDefaults, userProps = EMPTY_DEFAULTS) => {
|
|
562
610
|
const resolvedVariants = {};
|
|
563
611
|
Object.assign(resolvedVariants, staticDefaults);
|
|
564
612
|
for (const key in childDefaults) {
|
|
565
|
-
if (!hasOwn
|
|
613
|
+
if (!Object.hasOwn(childDefaults, key)) continue;
|
|
566
614
|
const v = childDefaults[key];
|
|
567
615
|
if (v === void 0) continue;
|
|
568
616
|
resolvedVariants[key] = v;
|
|
569
617
|
}
|
|
570
618
|
for (const key in userProps) {
|
|
571
|
-
if (!hasOwn
|
|
619
|
+
if (!Object.hasOwn(userProps, key)) continue;
|
|
572
620
|
const v = userProps[key];
|
|
573
621
|
if (v === void 0) continue;
|
|
574
622
|
resolvedVariants[key] = v;
|
|
575
623
|
}
|
|
576
624
|
const computedDefaults = {};
|
|
577
|
-
for (let i = 0; i <
|
|
578
|
-
const extDefaults =
|
|
625
|
+
for (let i = 0; i < extMetasWithComputedCount; i++) {
|
|
626
|
+
const extDefaults = extMetasWithComputed[i].resolveDefaults(childDefaults, userProps);
|
|
579
627
|
for (const k in extDefaults) {
|
|
580
|
-
if (!hasOwn
|
|
628
|
+
if (!Object.hasOwn(extDefaults, k)) continue;
|
|
581
629
|
computedDefaults[k] = extDefaults[k];
|
|
582
630
|
}
|
|
583
631
|
}
|
|
@@ -585,14 +633,14 @@ function create({ transformClass = (className) => className } = {}) {
|
|
|
585
633
|
const ownVariants = {};
|
|
586
634
|
for (let i = 0; i < variantKeysLength; i++) {
|
|
587
635
|
const k = variantKeys[i];
|
|
588
|
-
if (hasOwn
|
|
636
|
+
if (Object.hasOwn(resolvedVariants, k)) ownVariants[k] = resolvedVariants[k];
|
|
589
637
|
}
|
|
590
638
|
computed({
|
|
591
639
|
variants: ownVariants,
|
|
592
640
|
setVariants: noop,
|
|
593
641
|
setDefaultVariants: (newDefaults) => {
|
|
594
642
|
for (const key in newDefaults) {
|
|
595
|
-
if (!hasOwn
|
|
643
|
+
if (!Object.hasOwn(newDefaults, key)) continue;
|
|
596
644
|
const value = newDefaults[key];
|
|
597
645
|
if (userProps[key] !== void 0) continue;
|
|
598
646
|
if (isVariantDisabled(config, key)) continue;
|
|
@@ -609,15 +657,15 @@ function create({ transformClass = (className) => className } = {}) {
|
|
|
609
657
|
function resolveVariantsHot(propsVariants) {
|
|
610
658
|
const defaults = {};
|
|
611
659
|
Object.assign(defaults, staticDefaults);
|
|
612
|
-
for (let i = 0; i <
|
|
613
|
-
const extComputed =
|
|
660
|
+
for (let i = 0; i < extMetasWithComputedCount; i++) {
|
|
661
|
+
const extComputed = extMetasWithComputed[i].resolveDefaults(defaults, propsVariants);
|
|
614
662
|
for (const k in extComputed) {
|
|
615
|
-
if (!hasOwn
|
|
663
|
+
if (!Object.hasOwn(extComputed, k)) continue;
|
|
616
664
|
defaults[k] = extComputed[k];
|
|
617
665
|
}
|
|
618
666
|
}
|
|
619
667
|
for (const k in propsVariants) {
|
|
620
|
-
if (!hasOwn
|
|
668
|
+
if (!Object.hasOwn(propsVariants, k)) continue;
|
|
621
669
|
const v = propsVariants[k];
|
|
622
670
|
if (v === void 0) continue;
|
|
623
671
|
defaults[k] = v;
|
|
@@ -627,18 +675,23 @@ function create({ transformClass = (className) => className } = {}) {
|
|
|
627
675
|
filterDisabledInto(defaults, result);
|
|
628
676
|
return result;
|
|
629
677
|
}
|
|
630
|
-
const
|
|
678
|
+
const runComputedContext = (resolved, userVariantProps, filterOwnVariants, collectOutput, protectedVariants, pendingProtectedVariants, protectedVariantKeys) => {
|
|
631
679
|
let workingResolved = resolved;
|
|
632
680
|
let cClasses = null;
|
|
633
681
|
let cStyle = null;
|
|
682
|
+
let changedVariants = null;
|
|
634
683
|
if (computed) {
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
const
|
|
638
|
-
|
|
684
|
+
let ownVariants = resolved;
|
|
685
|
+
if (filterOwnVariants) {
|
|
686
|
+
const filteredVariants = {};
|
|
687
|
+
for (let i = 0; i < variantKeysLength; i++) {
|
|
688
|
+
const k = variantKeys[i];
|
|
689
|
+
if (Object.hasOwn(resolved, k)) filteredVariants[k] = resolved[k];
|
|
690
|
+
}
|
|
691
|
+
ownVariants = filteredVariants;
|
|
639
692
|
}
|
|
640
693
|
let updatedVariants = null;
|
|
641
|
-
const localCClasses = [];
|
|
694
|
+
const localCClasses = collectOutput ? [] : null;
|
|
642
695
|
let localCStyle = null;
|
|
643
696
|
const ensureUpdated = () => {
|
|
644
697
|
if (updatedVariants) return updatedVariants;
|
|
@@ -647,48 +700,74 @@ function create({ transformClass = (className) => className } = {}) {
|
|
|
647
700
|
updatedVariants = u;
|
|
648
701
|
return u;
|
|
649
702
|
};
|
|
703
|
+
const setChangedVariant = (key, value, protect = false) => {
|
|
704
|
+
if (shouldCollectChangedVariants) {
|
|
705
|
+
if (!changedVariants) changedVariants = {};
|
|
706
|
+
changedVariants[key] = value;
|
|
707
|
+
}
|
|
708
|
+
if (protect && protectedVariants) {
|
|
709
|
+
protectedVariants[key] = value;
|
|
710
|
+
protectedVariantKeys?.add(key);
|
|
711
|
+
}
|
|
712
|
+
};
|
|
713
|
+
const getCurrentVariantValue = (key) => {
|
|
714
|
+
return updatedVariants ? updatedVariants[key] : ownVariants[key];
|
|
715
|
+
};
|
|
650
716
|
const result = computed({
|
|
651
717
|
variants: ownVariants,
|
|
652
718
|
setVariants: (newVariants) => {
|
|
653
719
|
if (!hasAnyDisabled) {
|
|
654
|
-
|
|
720
|
+
for (const key in newVariants) {
|
|
721
|
+
if (!Object.hasOwn(newVariants, key)) continue;
|
|
722
|
+
const value = newVariants[key];
|
|
723
|
+
setChangedVariant(key, value, true);
|
|
724
|
+
if (getCurrentVariantValue(key) === value) continue;
|
|
725
|
+
ensureUpdated()[key] = value;
|
|
726
|
+
}
|
|
655
727
|
return;
|
|
656
728
|
}
|
|
657
729
|
for (const key in newVariants) {
|
|
658
|
-
if (!hasOwn
|
|
730
|
+
if (!Object.hasOwn(newVariants, key)) continue;
|
|
659
731
|
if (disabledVariantKeys.has(key)) continue;
|
|
660
732
|
const value = newVariants[key];
|
|
661
733
|
if (hasDisabledVariantValues) {
|
|
662
734
|
const valueKey = getVariantValueKey(value);
|
|
663
735
|
if (valueKey != null && disabledVariantValues[key]?.has(valueKey)) continue;
|
|
664
736
|
}
|
|
737
|
+
setChangedVariant(key, value, true);
|
|
738
|
+
if (getCurrentVariantValue(key) === value) continue;
|
|
665
739
|
ensureUpdated()[key] = value;
|
|
666
740
|
}
|
|
667
741
|
},
|
|
668
742
|
setDefaultVariants: (newDefaults) => {
|
|
669
743
|
for (const key in newDefaults) {
|
|
670
|
-
if (!hasOwn
|
|
744
|
+
if (!Object.hasOwn(newDefaults, key)) continue;
|
|
671
745
|
if (userVariantProps[key] !== void 0) continue;
|
|
746
|
+
if (protectedVariantKeys?.has(key)) continue;
|
|
672
747
|
const value = newDefaults[key];
|
|
673
748
|
if (hasAnyDisabled) {
|
|
674
749
|
if (disabledVariantKeys.has(key)) continue;
|
|
675
750
|
const valueKey = getVariantValueKey(value);
|
|
676
751
|
if (valueKey != null && disabledVariantValues[key]?.has(valueKey)) continue;
|
|
677
752
|
}
|
|
753
|
+
setChangedVariant(key, value);
|
|
754
|
+
if (pendingProtectedVariants) pendingProtectedVariants[key] = value;
|
|
755
|
+
if (getCurrentVariantValue(key) === value) continue;
|
|
678
756
|
ensureUpdated()[key] = value;
|
|
679
757
|
}
|
|
680
758
|
},
|
|
681
759
|
addClass: (className) => {
|
|
682
|
-
localCClasses
|
|
760
|
+
localCClasses?.push(className);
|
|
683
761
|
},
|
|
684
762
|
addStyle: (newStyle) => {
|
|
763
|
+
if (!collectOutput) return;
|
|
685
764
|
if (!localCStyle) localCStyle = {};
|
|
686
765
|
Object.assign(localCStyle, newStyle);
|
|
687
766
|
}
|
|
688
767
|
});
|
|
689
|
-
if (result != null) {
|
|
768
|
+
if (collectOutput && result != null) {
|
|
690
769
|
const r = extractClassAndStylePrebuilt(result);
|
|
691
|
-
if (r.class != null) localCClasses
|
|
770
|
+
if (r.class != null) localCClasses?.push(r.class);
|
|
692
771
|
if (r.style) {
|
|
693
772
|
if (!localCStyle) localCStyle = {};
|
|
694
773
|
Object.assign(localCStyle, r.style);
|
|
@@ -696,42 +775,72 @@ function create({ transformClass = (className) => className } = {}) {
|
|
|
696
775
|
}
|
|
697
776
|
cClasses = localCClasses;
|
|
698
777
|
cStyle = localCStyle;
|
|
699
|
-
if (updatedVariants)
|
|
700
|
-
const
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
778
|
+
if (updatedVariants) {
|
|
779
|
+
const nextResolved = {};
|
|
780
|
+
Object.assign(nextResolved, workingResolved);
|
|
781
|
+
if (hasAnyDisabled) {
|
|
782
|
+
const filteredUpdated = {};
|
|
783
|
+
filterDisabledInto(updatedVariants, filteredUpdated);
|
|
784
|
+
Object.assign(nextResolved, filteredUpdated);
|
|
785
|
+
} else Object.assign(nextResolved, updatedVariants);
|
|
786
|
+
workingResolved = nextResolved;
|
|
787
|
+
}
|
|
704
788
|
}
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
789
|
+
return {
|
|
790
|
+
workingResolved,
|
|
791
|
+
changedVariants,
|
|
792
|
+
classes: cClasses,
|
|
793
|
+
style: cStyle
|
|
794
|
+
};
|
|
795
|
+
};
|
|
796
|
+
const computeOnce = (resolved, userVariantProps, skipKeys, skipValues, classesOut, styleOut, runState, protectedVariants, pendingProtectedVariants, protectedVariantKeys) => {
|
|
797
|
+
let workingResolved = resolved;
|
|
798
|
+
let cClasses = null;
|
|
799
|
+
let cStyle = null;
|
|
800
|
+
let changedVariants = null;
|
|
801
|
+
if (computed) {
|
|
802
|
+
const computedResult = runComputedContext(resolved, userVariantProps, true, true, protectedVariants, pendingProtectedVariants, protectedVariantKeys);
|
|
803
|
+
workingResolved = computedResult.workingResolved;
|
|
804
|
+
cClasses = computedResult.classes;
|
|
805
|
+
cStyle = computedResult.style;
|
|
806
|
+
changedVariants = computedResult.changedVariants;
|
|
711
807
|
}
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
const existing = extSkipVals[k];
|
|
720
|
-
if (existing) {
|
|
721
|
-
const merged = new Set(existing);
|
|
722
|
-
for (const v of staticExtSkipValues[k]) merged.add(v);
|
|
723
|
-
extSkipVals[k] = merged;
|
|
724
|
-
} else extSkipVals[k] = staticExtSkipValues[k];
|
|
808
|
+
if (hasExtend) {
|
|
809
|
+
let extSkipKeys;
|
|
810
|
+
if (skipKeys === null) extSkipKeys = staticExtSkipKeys;
|
|
811
|
+
else if (staticExtSkipKeys === null) extSkipKeys = skipKeys;
|
|
812
|
+
else {
|
|
813
|
+
extSkipKeys = new Set(skipKeys);
|
|
814
|
+
for (const k of staticExtSkipKeys) extSkipKeys.add(k);
|
|
725
815
|
}
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
const
|
|
732
|
-
|
|
816
|
+
let extSkipVals;
|
|
817
|
+
if (skipValues === null) extSkipVals = staticExtSkipValues;
|
|
818
|
+
else if (staticExtSkipValues === null) extSkipVals = skipValues;
|
|
819
|
+
else {
|
|
820
|
+
extSkipVals = {};
|
|
821
|
+
for (const k in skipValues) extSkipVals[k] = skipValues[k];
|
|
822
|
+
for (const k in staticExtSkipValues) {
|
|
823
|
+
const existing = extSkipVals[k];
|
|
824
|
+
if (existing) {
|
|
825
|
+
const merged = new Set(existing);
|
|
826
|
+
for (const v of staticExtSkipValues[k]) merged.add(v);
|
|
827
|
+
extSkipVals[k] = merged;
|
|
828
|
+
} else extSkipVals[k] = staticExtSkipValues[k];
|
|
829
|
+
}
|
|
733
830
|
}
|
|
734
|
-
|
|
831
|
+
const extUserVariantProps = extMetasWithComputedCount > 0 ? getExtUserVariantProps(userVariantProps, protectedVariants ?? null, changedVariants) : userVariantProps;
|
|
832
|
+
for (let i = 0; i < extCount; i++) {
|
|
833
|
+
if (hasIsolatedExt && extIsolated[i]) {
|
|
834
|
+
const extClasses = [];
|
|
835
|
+
workingResolved = extMetas[i].compute(workingResolved, extUserVariantProps, extSkipKeys, extSkipVals, extClasses, styleOut, runState, protectedVariants, pendingProtectedVariants, protectedVariantKeys);
|
|
836
|
+
if (extClasses.length > 0) {
|
|
837
|
+
const joined = clsx(extClasses);
|
|
838
|
+
if (joined.length > 0) classesOut.push(extMetas[i].transformClass(joined));
|
|
839
|
+
}
|
|
840
|
+
} else workingResolved = extMetas[i].compute(workingResolved, extUserVariantProps, extSkipKeys, extSkipVals, classesOut, styleOut, runState, protectedVariants, pendingProtectedVariants, protectedVariantKeys);
|
|
841
|
+
if (protectedVariants && extMetasWithComputedCount > 0) Object.assign(extUserVariantProps, protectedVariants);
|
|
842
|
+
}
|
|
843
|
+
}
|
|
735
844
|
if (hasBaseStyle) Object.assign(styleOut, baseStyle);
|
|
736
845
|
const ownSkipKeys = skipKeys;
|
|
737
846
|
const ownSkipValues = skipValues;
|
|
@@ -772,27 +881,129 @@ function create({ transformClass = (className) => className } = {}) {
|
|
|
772
881
|
}
|
|
773
882
|
if (cClasses) for (let i = 0; i < cClasses.length; i++) classesOut.push(cClasses[i]);
|
|
774
883
|
if (cStyle) Object.assign(styleOut, cStyle);
|
|
884
|
+
return workingResolved;
|
|
775
885
|
};
|
|
886
|
+
const compute = !computed && extMetasWithComputedCount === 0 ? (resolved, userVariantProps, skipKeys, skipValues, classesOut, styleOut, runState, protectedVariants, pendingProtectedVariants, protectedVariantKeys) => {
|
|
887
|
+
return computeOnce(resolved, userVariantProps, skipKeys, skipValues, classesOut, styleOut, runState, protectedVariants, pendingProtectedVariants, protectedVariantKeys);
|
|
888
|
+
} : (resolved, userVariantProps, skipKeys, skipValues, classesOut, styleOut, runState, protectedVariants, pendingProtectedVariants, protectedVariantKeys) => {
|
|
889
|
+
runState ??= {
|
|
890
|
+
remaining: MAX_COMPUTED_RUNS,
|
|
891
|
+
warned: false
|
|
892
|
+
};
|
|
893
|
+
protectedVariants ??= {};
|
|
894
|
+
protectedVariantKeys ??= /* @__PURE__ */ new Set();
|
|
895
|
+
let workingResolved = resolved;
|
|
896
|
+
let lastClasses = [];
|
|
897
|
+
let lastStyle = {};
|
|
898
|
+
let isFirstRun = true;
|
|
899
|
+
while (runState.remaining > 0) {
|
|
900
|
+
runState.remaining -= 1;
|
|
901
|
+
let useDirectOutput = isFirstRun;
|
|
902
|
+
if (useDirectOutput) {
|
|
903
|
+
for (const key in styleOut) if (Object.hasOwn(styleOut, key)) {
|
|
904
|
+
useDirectOutput = false;
|
|
905
|
+
break;
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
const classCount = classesOut.length;
|
|
909
|
+
const nextPendingProtectedVariants = {};
|
|
910
|
+
const nextClasses = useDirectOutput ? classesOut : [];
|
|
911
|
+
const nextStyle = useDirectOutput ? styleOut : {};
|
|
912
|
+
const nextResolved = computeOnce(workingResolved, userVariantProps, skipKeys, skipValues, nextClasses, nextStyle, runState, protectedVariants, nextPendingProtectedVariants, protectedVariantKeys);
|
|
913
|
+
let protectedChanged;
|
|
914
|
+
if (pendingProtectedVariants) protectedChanged = mergeVariants(pendingProtectedVariants, nextPendingProtectedVariants, protectedVariantKeys);
|
|
915
|
+
else protectedChanged = mergeVariants(protectedVariants, nextPendingProtectedVariants, protectedVariantKeys);
|
|
916
|
+
if (!protectedChanged && (nextResolved === workingResolved || areVariantsEqual(workingResolved, nextResolved))) {
|
|
917
|
+
if (!useDirectOutput) {
|
|
918
|
+
for (let i = 0; i < nextClasses.length; i++) classesOut.push(nextClasses[i]);
|
|
919
|
+
Object.assign(styleOut, nextStyle);
|
|
920
|
+
}
|
|
921
|
+
return nextResolved;
|
|
922
|
+
}
|
|
923
|
+
if (useDirectOutput && runState.remaining === 0) {
|
|
924
|
+
warnComputedLimit(runState);
|
|
925
|
+
return nextResolved;
|
|
926
|
+
}
|
|
927
|
+
if (useDirectOutput) {
|
|
928
|
+
classesOut.length = classCount;
|
|
929
|
+
for (const key in styleOut) if (Object.hasOwn(styleOut, key)) Reflect.deleteProperty(styleOut, key);
|
|
930
|
+
} else {
|
|
931
|
+
lastClasses = nextClasses;
|
|
932
|
+
lastStyle = nextStyle;
|
|
933
|
+
}
|
|
934
|
+
workingResolved = nextResolved;
|
|
935
|
+
isFirstRun = false;
|
|
936
|
+
}
|
|
937
|
+
warnComputedLimit(runState);
|
|
938
|
+
for (let i = 0; i < lastClasses.length; i++) classesOut.push(lastClasses[i]);
|
|
939
|
+
Object.assign(styleOut, lastStyle);
|
|
940
|
+
return workingResolved;
|
|
941
|
+
};
|
|
942
|
+
const resolveComputedOnce = (resolved, userVariantProps, filterOwnVariants = true, runState, protectedVariants, pendingProtectedVariants, protectedVariantKeys) => {
|
|
943
|
+
let workingResolved = resolved;
|
|
944
|
+
let changedVariants = null;
|
|
945
|
+
if (computed) {
|
|
946
|
+
const computedResult = runComputedContext(resolved, userVariantProps, filterOwnVariants, false, protectedVariants, pendingProtectedVariants, protectedVariantKeys);
|
|
947
|
+
workingResolved = computedResult.workingResolved;
|
|
948
|
+
changedVariants = computedResult.changedVariants;
|
|
949
|
+
}
|
|
950
|
+
if (extMetasWithComputedCount > 0) {
|
|
951
|
+
const extUserVariantProps = getExtUserVariantProps(userVariantProps, protectedVariants ?? null, changedVariants);
|
|
952
|
+
for (let i = 0; i < extMetasWithComputedCount; i++) {
|
|
953
|
+
const resolveComputed = extMetasWithComputed[i].resolveComputed;
|
|
954
|
+
if (!resolveComputed) continue;
|
|
955
|
+
workingResolved = resolveComputed(workingResolved, extUserVariantProps, true, runState, protectedVariants, pendingProtectedVariants, protectedVariantKeys);
|
|
956
|
+
if (protectedVariants) Object.assign(extUserVariantProps, protectedVariants);
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
return workingResolved;
|
|
960
|
+
};
|
|
961
|
+
const resolveComputed = computed || extMetasWithComputedCount > 0 ? (resolved, userVariantProps, filterOwnVariants = true, runState, protectedVariants, pendingProtectedVariants, protectedVariantKeys) => {
|
|
962
|
+
runState ??= {
|
|
963
|
+
remaining: MAX_COMPUTED_RUNS,
|
|
964
|
+
warned: false
|
|
965
|
+
};
|
|
966
|
+
protectedVariants ??= {};
|
|
967
|
+
protectedVariantKeys ??= /* @__PURE__ */ new Set();
|
|
968
|
+
let workingResolved = resolved;
|
|
969
|
+
let reachedLimit = true;
|
|
970
|
+
while (runState.remaining > 0) {
|
|
971
|
+
runState.remaining -= 1;
|
|
972
|
+
const nextPendingProtectedVariants = {};
|
|
973
|
+
const nextResolved = resolveComputedOnce(workingResolved, userVariantProps, filterOwnVariants, runState, protectedVariants, nextPendingProtectedVariants, protectedVariantKeys);
|
|
974
|
+
let protectedChanged;
|
|
975
|
+
if (pendingProtectedVariants) protectedChanged = mergeVariants(pendingProtectedVariants, nextPendingProtectedVariants, protectedVariantKeys);
|
|
976
|
+
else protectedChanged = mergeVariants(protectedVariants, nextPendingProtectedVariants, protectedVariantKeys);
|
|
977
|
+
if (!protectedChanged && (nextResolved === workingResolved || areVariantsEqual(workingResolved, nextResolved))) {
|
|
978
|
+
workingResolved = nextResolved;
|
|
979
|
+
reachedLimit = false;
|
|
980
|
+
break;
|
|
981
|
+
}
|
|
982
|
+
workingResolved = nextResolved;
|
|
983
|
+
}
|
|
984
|
+
if (reachedLimit) warnComputedLimit(runState);
|
|
985
|
+
return workingResolved;
|
|
986
|
+
} : null;
|
|
776
987
|
const computeResult = (props = EMPTY_DEFAULTS) => {
|
|
777
988
|
const propsRecord = props;
|
|
778
989
|
let resolved = {};
|
|
779
990
|
Object.assign(resolved, staticDefaults);
|
|
780
991
|
let userVariantProps;
|
|
781
|
-
if (
|
|
992
|
+
if (extMetasWithComputedCount > 0) {
|
|
782
993
|
const variantProps = {};
|
|
783
994
|
for (let i = 0; i < variantKeysLength; i++) {
|
|
784
995
|
const key = variantKeys[i];
|
|
785
|
-
if (hasOwn
|
|
996
|
+
if (Object.hasOwn(propsRecord, key)) variantProps[key] = propsRecord[key];
|
|
786
997
|
}
|
|
787
|
-
for (let i = 0; i <
|
|
788
|
-
const extComputed =
|
|
998
|
+
for (let i = 0; i < extMetasWithComputedCount; i++) {
|
|
999
|
+
const extComputed = extMetasWithComputed[i].resolveDefaults(resolved, variantProps);
|
|
789
1000
|
for (const k in extComputed) {
|
|
790
|
-
if (!hasOwn
|
|
1001
|
+
if (!Object.hasOwn(extComputed, k)) continue;
|
|
791
1002
|
resolved[k] = extComputed[k];
|
|
792
1003
|
}
|
|
793
1004
|
}
|
|
794
1005
|
for (const k in variantProps) {
|
|
795
|
-
if (!hasOwn
|
|
1006
|
+
if (!Object.hasOwn(variantProps, k)) continue;
|
|
796
1007
|
const v = variantProps[k];
|
|
797
1008
|
if (v === void 0) continue;
|
|
798
1009
|
resolved[k] = v;
|
|
@@ -801,7 +1012,7 @@ function create({ transformClass = (className) => className } = {}) {
|
|
|
801
1012
|
} else {
|
|
802
1013
|
for (let i = 0; i < variantKeysLength; i++) {
|
|
803
1014
|
const key = variantKeys[i];
|
|
804
|
-
if (!hasOwn
|
|
1015
|
+
if (!Object.hasOwn(propsRecord, key)) continue;
|
|
805
1016
|
const v = propsRecord[key];
|
|
806
1017
|
if (v === void 0) continue;
|
|
807
1018
|
resolved[key] = v;
|
|
@@ -839,52 +1050,10 @@ function create({ transformClass = (className) => className } = {}) {
|
|
|
839
1050
|
const getVariants = (variants) => {
|
|
840
1051
|
const variantProps = variants ?? EMPTY_DEFAULTS;
|
|
841
1052
|
let resolvedVariants = resolveVariantsHot(variantProps);
|
|
842
|
-
if (
|
|
843
|
-
const updatedVariants = {};
|
|
844
|
-
Object.assign(updatedVariants, resolvedVariants);
|
|
845
|
-
computed({
|
|
846
|
-
variants: resolvedVariants,
|
|
847
|
-
setVariants: (newVariants) => {
|
|
848
|
-
if (!hasAnyDisabled) {
|
|
849
|
-
Object.assign(updatedVariants, newVariants);
|
|
850
|
-
return;
|
|
851
|
-
}
|
|
852
|
-
for (const key in newVariants) {
|
|
853
|
-
if (!hasOwn.call(newVariants, key)) continue;
|
|
854
|
-
if (disabledVariantKeys.has(key)) continue;
|
|
855
|
-
const value = newVariants[key];
|
|
856
|
-
if (hasDisabledVariantValues) {
|
|
857
|
-
const valueKey = getVariantValueKey(value);
|
|
858
|
-
if (valueKey != null && disabledVariantValues[key]?.has(valueKey)) continue;
|
|
859
|
-
}
|
|
860
|
-
updatedVariants[key] = value;
|
|
861
|
-
}
|
|
862
|
-
},
|
|
863
|
-
setDefaultVariants: (newDefaults) => {
|
|
864
|
-
for (const key in newDefaults) {
|
|
865
|
-
if (!hasOwn.call(newDefaults, key)) continue;
|
|
866
|
-
if (variantProps[key] !== void 0) continue;
|
|
867
|
-
const value = newDefaults[key];
|
|
868
|
-
if (hasAnyDisabled) {
|
|
869
|
-
if (disabledVariantKeys.has(key)) continue;
|
|
870
|
-
const valueKey = getVariantValueKey(value);
|
|
871
|
-
if (valueKey != null && disabledVariantValues[key]?.has(valueKey)) continue;
|
|
872
|
-
}
|
|
873
|
-
updatedVariants[key] = value;
|
|
874
|
-
}
|
|
875
|
-
},
|
|
876
|
-
addClass: noop,
|
|
877
|
-
addStyle: noop
|
|
878
|
-
});
|
|
879
|
-
if (hasAnyDisabled) {
|
|
880
|
-
const filteredUpdated = {};
|
|
881
|
-
filterDisabledInto(updatedVariants, filteredUpdated);
|
|
882
|
-
resolvedVariants = filteredUpdated;
|
|
883
|
-
} else resolvedVariants = updatedVariants;
|
|
884
|
-
}
|
|
1053
|
+
if (resolveComputed) resolvedVariants = resolveComputed(resolvedVariants, variantProps, false);
|
|
885
1054
|
return resolvedVariants;
|
|
886
1055
|
};
|
|
887
|
-
const computedBaseClass = clsx(...extBaseClassesArr, config.class);
|
|
1056
|
+
const computedBaseClass = hasExtend ? clsx(...extBaseClassesArr, config.class) : clsx(config.class);
|
|
888
1057
|
const classFn = (props = {}) => {
|
|
889
1058
|
return computeResult(props).className;
|
|
890
1059
|
};
|
|
@@ -893,15 +1062,15 @@ function create({ transformClass = (className) => className } = {}) {
|
|
|
893
1062
|
staticDefaults,
|
|
894
1063
|
resolveDefaults: resolveDefaultsFn,
|
|
895
1064
|
compute,
|
|
1065
|
+
resolveComputed,
|
|
896
1066
|
transformClass
|
|
897
1067
|
};
|
|
898
|
-
const initComponent = (c,
|
|
1068
|
+
const initComponent = (c, propKeys, style) => {
|
|
899
1069
|
c.class = classFn;
|
|
900
1070
|
c.style = style;
|
|
901
1071
|
c.getVariants = getVariants;
|
|
902
|
-
c.keys = keys;
|
|
903
1072
|
c.variantKeys = variantKeys;
|
|
904
|
-
c.propKeys =
|
|
1073
|
+
c.propKeys = propKeys;
|
|
905
1074
|
setComponentMeta(c, meta);
|
|
906
1075
|
return c;
|
|
907
1076
|
};
|