clava 0.6.2 → 0.6.4

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/src/index.ts CHANGED
@@ -29,6 +29,7 @@ import type {
29
29
  Variants,
30
30
  } from "./types.ts";
31
31
  import {
32
+ getOwn,
32
33
  hasOwn,
33
34
  htmlObjStyleToStyleValue,
34
35
  htmlStyleToStyleValue,
@@ -57,8 +58,7 @@ type ComputeOnceFn = (
57
58
  skipValues: Record<string, Set<string>> | null,
58
59
  classesOut: ClsxClassValue[],
59
60
  styleOut: StyleValue,
60
- protectedVariants?: Record<string, unknown> | null,
61
- protectedVariantKeys?: Set<string> | null,
61
+ protection?: RefineProtection | null,
62
62
  defaultResolved?: Record<string, unknown>,
63
63
  renderOnly?: boolean,
64
64
  ) => Record<string, unknown>;
@@ -73,8 +73,7 @@ type ResolveRefineOnceFn = (
73
73
  resolved: Record<string, unknown>,
74
74
  userVariantProps: Record<string, unknown>,
75
75
  filterOwnVariants?: boolean,
76
- protectedVariants?: Record<string, unknown> | null,
77
- protectedVariantKeys?: Set<string> | null,
76
+ protection?: RefineProtection | null,
78
77
  defaultResolved?: Record<string, unknown>,
79
78
  ) => Record<string, unknown>;
80
79
 
@@ -85,6 +84,9 @@ type ComputedDefaultVariantFn = (
85
84
 
86
85
  // Internal metadata stored on components but hidden from public types.
87
86
  interface ComponentMeta {
87
+ identity?: object;
88
+ extends?: ComponentMeta[];
89
+ rebuild?: (extensions: ComponentMeta[]) => ComponentMeta;
88
90
  baseClass: string;
89
91
  staticDefaults: Record<string, unknown>;
90
92
  // Performs a single compute pass for extending components, returning the
@@ -116,36 +118,83 @@ interface ComponentWithMeta {
116
118
 
117
119
  const EMPTY_DEFAULTS: Record<string, unknown> = {};
118
120
 
121
+ // Prune shared recipes before compiling their parent's compute path. Keeping
122
+ // the first path preserves its overrides and factory transform boundaries.
123
+ function deduplicateExtensions(
124
+ extensions: ComponentMeta[],
125
+ seen = new Set<object>(),
126
+ ): ComponentMeta[] {
127
+ if (extensions.length === 0) {
128
+ return extensions;
129
+ }
130
+ const result: ComponentMeta[] = [];
131
+ let changed = false;
132
+ for (const meta of extensions) {
133
+ const identity = getOwn(meta, "identity") ?? meta;
134
+ if (seen.has(identity)) {
135
+ changed = true;
136
+ continue;
137
+ }
138
+ seen.add(identity);
139
+ // Older Clava copies expose the compute metadata but not their graph.
140
+ // Keep their contribution intact instead of traversing hidden ancestors.
141
+ const inheritedExtensions = getOwn(meta, "extends");
142
+ const rebuild = getOwn(meta, "rebuild");
143
+ if (!inheritedExtensions || !rebuild) {
144
+ result.push(meta);
145
+ continue;
146
+ }
147
+ const children = deduplicateExtensions(inheritedExtensions, seen);
148
+ if (children === inheritedExtensions) {
149
+ result.push(meta);
150
+ } else {
151
+ result.push(rebuild(children));
152
+ changed = true;
153
+ }
154
+ }
155
+ return changed ? result : extensions;
156
+ }
157
+
119
158
  const MAX_REFINE_RUNS = 50;
120
159
 
121
160
  function areVariantsEqual(
122
161
  a: Record<string, unknown>,
123
162
  b: Record<string, unknown>,
124
163
  ): boolean {
164
+ // The first loop proves every own key of `a` is an own key of `b` with the
165
+ // same value, so counting both sides is enough to prove the key sets match.
166
+ // Counting keeps the total number of lookups the same as before the own-key
167
+ // check was added to the first loop.
168
+ let ownKeyDifference = 0;
125
169
  for (const key in a) {
126
170
  if (!hasOwn(a, key)) continue;
171
+ if (!hasOwn(b, key)) return false;
127
172
  if (!Object.is(a[key], b[key])) return false;
173
+ ownKeyDifference++;
128
174
  }
129
175
  for (const key in b) {
130
176
  if (!hasOwn(b, key)) continue;
131
- if (!hasOwn(a, key)) return false;
177
+ ownKeyDifference--;
132
178
  }
133
- return true;
179
+ return ownKeyDifference === 0;
134
180
  }
135
181
 
136
- function getExtUserVariantProps(
137
- userVariantProps: Record<string, unknown>,
138
- protectedVariants: Record<string, unknown> | null,
139
- ): Record<string, unknown> {
140
- return Object.assign({}, userVariantProps, protectedVariants);
182
+ // Variants that a `refine` callback assigned through `setVariants`. Two stages
183
+ // read them: computed defaults skip a key that was assigned this way, and
184
+ // `mergeProtectedIntoBase` folds them into the base each pass resolves from.
185
+ // The record is created on the first `setVariants` call, because most callbacks
186
+ // only read variants. The holder is shared so that a callback anywhere in the
187
+ // chain can report its assignment back to the resolution that owns the pass.
188
+ interface RefineProtection {
189
+ variants: Record<string, unknown> | null;
141
190
  }
142
191
 
143
192
  function mergeProtectedIntoBase(
144
193
  baseResolved: Record<string, unknown>,
145
- protectedVariants: Record<string, unknown>,
146
- protectedVariantKeys: Set<string>,
194
+ protection: RefineProtection | null,
147
195
  ): Record<string, unknown> {
148
- if (protectedVariantKeys.size === 0) return baseResolved;
196
+ const protectedVariants = protection?.variants;
197
+ if (!protectedVariants) return baseResolved;
149
198
  return Object.assign({}, baseResolved, protectedVariants);
150
199
  }
151
200
 
@@ -268,6 +317,10 @@ interface CreateParams {
268
317
  transformClass?: (className: string) => string;
269
318
  }
270
319
 
320
+ function identityClass(className: string) {
321
+ return className;
322
+ }
323
+
271
324
  function isRecordObject(value: unknown): value is Record<string, unknown> {
272
325
  if (typeof value !== "object") return false;
273
326
  if (value == null) return false;
@@ -302,13 +355,19 @@ function extractClassAndStylePrebuilt(value: unknown): PrebuiltValue {
302
355
  if (!isRecordObject(value)) {
303
356
  return { class: value as ClassValue, style: null };
304
357
  }
305
- if (!("style" in value || "class" in value)) {
358
+ // A value that owns neither key contributes nothing, and a value that owns
359
+ // one of them must not pick up the other from a polluted `Object.prototype`.
360
+ // Checked inline rather than through `getOwn`: this runs for every variant
361
+ // value at creation time, where the call is worth about 3% of `cv()`.
362
+ const styleClassValue = value as StyleClassValue;
363
+ const classValue = hasOwn(value, "class") ? styleClassValue.class : undefined;
364
+ const styleValue = hasOwn(value, "style") ? styleClassValue.style : undefined;
365
+ if (classValue === undefined && styleValue === undefined) {
306
366
  return { class: null, style: null };
307
367
  }
308
- const styleClassValue = value as StyleClassValue;
309
- const style = normalizeStyle(styleClassValue.style);
368
+ const style = normalizeStyle(styleValue);
310
369
  return {
311
- class: styleClassValue.class ?? null,
370
+ class: classValue ?? null,
312
371
  style: Object.keys(style).length > 0 ? style : null,
313
372
  };
314
373
  }
@@ -448,8 +507,10 @@ export const splitProps: SplitPropsFunction = ((
448
507
  * fallback when the variant is a single class value (treated as `{ true: ... }`).
449
508
  */
450
509
  interface PrebuiltVariant {
451
- // For object variants: map of value -> prebuilt class/style
452
- values: Record<string, PrebuiltValue> | null;
510
+ // For object variants: map of value -> prebuilt class/style. A Map keeps the
511
+ // lookup keyed by the caller's variant value out of reach of a polluted
512
+ // `Object.prototype`.
513
+ values: Map<string, PrebuiltValue> | null;
453
514
  // For shorthand variants: the value to use when selectedValue is true
454
515
  shorthand: PrebuiltValue | null;
455
516
  // Set of value keys that are disabled (value === null in the original variant
@@ -465,7 +526,7 @@ function buildPrebuiltVariant(variantDef: unknown): PrebuiltVariant {
465
526
  disabledValues: null,
466
527
  };
467
528
  }
468
- const values: Record<string, PrebuiltValue> = {};
529
+ const values = new Map<string, PrebuiltValue>();
469
530
  let disabledValues: Set<string> | null = null;
470
531
  for (const key in variantDef) {
471
532
  if (!hasOwn(variantDef, key)) continue;
@@ -477,7 +538,7 @@ function buildPrebuiltVariant(variantDef: unknown): PrebuiltVariant {
477
538
  disabledValues.add(key);
478
539
  continue;
479
540
  }
480
- values[key] = extractClassAndStylePrebuilt(value);
541
+ values.set(key, extractClassAndStylePrebuilt(value));
481
542
  }
482
543
  return {
483
544
  values,
@@ -489,37 +550,34 @@ function buildPrebuiltVariant(variantDef: unknown): PrebuiltVariant {
489
550
  /**
490
551
  * Creates the cv and cx functions.
491
552
  */
492
- export function create({
493
- transformClass = (className) => className,
494
- }: CreateParams = {}) {
553
+ export function create(params: CreateParams = {}) {
554
+ // Destructuring would read an inherited `transformClass`, which a polluted
555
+ // `Object.prototype` could use to rewrite every class string this factory
556
+ // produces, including those of the package-level `cv`.
557
+ const transformClass = getOwn(params, "transformClass") ?? identityClass;
558
+
495
559
  const cx = (...classes: ClsxClassValue[]) => transformClass(clsx(classes));
496
560
 
497
- const cv = <V extends Variants = {}, const E extends AnyComponent[] = []>(
561
+ const buildComponent = <
562
+ V extends Variants = {},
563
+ const E extends AnyComponent[] = [],
564
+ >(
498
565
  config: CVConfig<V, E> = {},
499
- ): CVComponent<V, E> => {
566
+ ): { component: CVComponent<V, E>; meta: ComponentMeta } => {
500
567
  type MergedVariants = MergeVariants<V, E>;
501
568
 
502
- // ----- Pre-computed at creation time -----
503
- const extend = config.extend;
504
- const hasExtend = !!extend && extend.length > 0;
505
- const variants = config.variants;
506
- const refine = config.refine;
507
- const baseStyle = config.style;
569
+ // Compile caller-owned configuration once. Rebuilding a pruned branch
570
+ // reuses these values, including references that rendering already retains.
571
+ const extend = getOwn(config, "extend");
572
+ const variants = getOwn(config, "variants");
573
+ const refine = getOwn(config, "refine");
574
+ const baseStyle = getOwn(config, "style");
508
575
  const hasBaseStyle = !!baseStyle;
509
-
576
+ const baseClassValue = getOwn(config, "class");
510
577
  const variantKeySet = new Set<string>();
511
- const staticDefaults: Record<string, unknown> = {};
512
-
513
- // Pre-build extended component info, so the render path doesn't need to
514
- // read component metadata. Extends from a different `create()`
515
- // factory (different `transformClass` identity) need their contribution
516
- // transformed by their own `transformClass` before being joined into our
517
- // class string.
518
- const extMetas: ComponentMeta[] = [];
519
- const extBaseClassesArr: string[] = [];
520
- const extIsolated: boolean[] = [];
521
- let hasIsolatedExt = false;
522
- if (hasExtend) {
578
+ const inheritedComputedDefaultKeys = new Set<string>();
579
+ const originalExtMetas: ComponentMeta[] = [];
580
+ if (extend) {
523
581
  for (const ext of extend) {
524
582
  const extKeys = ext.variantKeys as readonly string[];
525
583
  for (let i = 0; i < extKeys.length; i++) {
@@ -527,30 +585,26 @@ export function create({
527
585
  if (key === undefined) continue;
528
586
  variantKeySet.add(key);
529
587
  }
530
-
531
588
  const meta = (ext as AnyComponent & ComponentWithMeta)[META_KEY];
532
589
  if (!meta) continue;
533
- extMetas.push(meta);
534
- Object.assign(staticDefaults, meta.staticDefaults);
535
-
536
- const isolated = meta.transformClass !== transformClass;
537
- extIsolated.push(isolated);
538
- if (isolated) {
539
- hasIsolatedExt = true;
540
- extBaseClassesArr.push(meta.transformClass(meta.baseClass));
541
- } else {
542
- extBaseClassesArr.push(meta.baseClass);
590
+ originalExtMetas.push(meta);
591
+ // A later branch can read a computed default contributed by the first
592
+ // path, even when that shared ancestor is pruned from this branch.
593
+ for (const key of meta.computedDefaultKeys) {
594
+ inheritedComputedDefaultKeys.add(key);
543
595
  }
544
596
  }
545
597
  }
546
- const extCount = extMetas.length;
547
-
548
- // Build all own variant metadata in one pass. Disabled value sets are
549
- // shared with their prebuilt variants instead of scanning and allocating
550
- // them twice.
598
+ const initialExtensions =
599
+ originalExtMetas.length > 1
600
+ ? deduplicateExtensions(originalExtMetas)
601
+ : originalExtMetas;
602
+
603
+ // Keep own default operations separate from inherited values. An explicit
604
+ // undefined must still clear a default when the retained extensions change.
605
+ const ownStaticDefaults: Record<string, unknown> = {};
551
606
  const disabledVariantKeys = new Set<string>();
552
- const disabledVariantValues: Record<string, Set<string>> = {};
553
- let hasDisabledVariantValues = false;
607
+ let disabledValuesBuilder: Record<string, Set<string>> | null = null;
554
608
  const variantEntryNames: string[] = [];
555
609
  const variantEntryDefs: PrebuiltVariant[] = [];
556
610
  const functionVariantNames: string[] = [];
@@ -574,15 +628,15 @@ export function create({
574
628
  variantEntryNames.push(name);
575
629
  variantEntryDefs.push(prebuiltVariant);
576
630
  if (prebuiltVariant.disabledValues) {
577
- disabledVariantValues[name] = prebuiltVariant.disabledValues;
578
- hasDisabledVariantValues = true;
631
+ disabledValuesBuilder ??= {};
632
+ disabledValuesBuilder[name] = prebuiltVariant.disabledValues;
579
633
  }
634
+ // Capture this decision before pruning can remove an inherited default.
580
635
  if (
581
- prebuiltVariant.values &&
582
- hasOwn(prebuiltVariant.values, "false") &&
583
- staticDefaults[name] === undefined
636
+ prebuiltVariant.values?.has("false") &&
637
+ !initialExtensions.some((meta) => hasOwn(meta.staticDefaults, name))
584
638
  ) {
585
- staticDefaults[name] = false;
639
+ ownStaticDefaults[name] = false;
586
640
  }
587
641
  }
588
642
  }
@@ -591,24 +645,16 @@ export function create({
591
645
  const variantEntryCount = variantEntryNames.length;
592
646
  const functionVariantCount = functionVariantNames.length;
593
647
  const hasDisabledVariantKeys = disabledVariantKeys.size > 0;
594
- const hasAnyDisabled = hasDisabledVariantKeys || hasDisabledVariantValues;
595
-
648
+ const disabledVariantValues = disabledValuesBuilder;
649
+ const hasAnyDisabled =
650
+ hasDisabledVariantKeys || disabledVariantValues !== null;
596
651
  const inputPropsKeys = ["class", "className", "style", ...variantKeys];
597
652
 
598
653
  const computedDefaultNames: string[] = [];
599
654
  const computedDefaultFns: ComputedDefaultVariantFn[] = [];
600
- const defaultVariants = config.defaultVariants as
655
+ const defaultVariants = getOwn(config, "defaultVariants") as
601
656
  | Record<string, unknown>
602
657
  | undefined;
603
-
604
- // Pre-compute static defaults. Includes:
605
- // - extended components' static defaults
606
- // - implicit boolean defaults (variants with a `false` key default to false)
607
- // - this config's literal defaultVariants (overriding the above)
608
- //
609
- // Function entries in defaultVariants are computed defaults. They run in
610
- // the refine loop so they can react to setVariants updates.
611
- // Then filtered through disabled-variants.
612
658
  if (defaultVariants) {
613
659
  for (const name in defaultVariants) {
614
660
  if (!hasOwn(defaultVariants, name)) continue;
@@ -616,1109 +662,1153 @@ export function create({
616
662
  if (typeof value === "function") {
617
663
  computedDefaultNames.push(name);
618
664
  computedDefaultFns.push(value as ComputedDefaultVariantFn);
619
- continue;
620
- }
621
- if (value === undefined) {
622
- delete staticDefaults[name];
623
- continue;
665
+ } else {
666
+ ownStaticDefaults[name] = value;
624
667
  }
625
- staticDefaults[name] = value;
626
668
  }
627
669
  }
628
670
  const computedDefaultCount = computedDefaultNames.length;
629
- if (hasAnyDisabled) {
630
- // Filter disabled variants in-place
631
- for (const key in staticDefaults) {
632
- if (!hasOwn(staticDefaults, key)) continue;
633
- if (disabledVariantKeys.has(key)) {
634
- delete staticDefaults[key];
635
- continue;
636
- }
637
- if (hasDisabledVariantValues) {
638
- const value = staticDefaults[key];
639
- const valueKey = getVariantValueKey(value);
640
- if (valueKey != null && disabledVariantValues[key]?.has(valueKey)) {
641
- delete staticDefaults[key];
642
- }
643
- }
644
- }
645
- }
646
-
647
- const inheritedComputedDefaultKeys = new Set<string>();
648
- for (let i = 0; i < extCount; i++) {
649
- const keys = extMetas[i].computedDefaultKeys;
650
- for (const key of keys) {
651
- inheritedComputedDefaultKeys.add(key);
652
- }
653
- }
654
-
655
- // Filter to only extends with computed default or refine work in their
656
- // chain. Those are the components that can change resolved variants across
657
- // fixed-point iterations.
658
- const extMetasWithRefine: ComponentMeta[] = [];
659
- for (let i = 0; i < extCount; i++) {
660
- const meta = extMetas[i];
661
- if (meta.resolveRefine) {
662
- extMetasWithRefine.push(meta);
663
- }
664
- }
665
- const extMetasWithRefineCount = extMetasWithRefine.length;
666
-
667
- // Call-site frame captured at the `cv()` call site so refine-limit warnings
668
- // can point developers at the component definition. Skipped entirely for
669
- // components that can never enter the refine loop, and stripped in
670
- // production via the NODE_ENV guard inside `captureCreationFrame`. The
671
- // frame is captured at creation time but the underlying `.stack` string is
672
- // formatted lazily on first access, so component creation stays cheap
673
- // unless the warning actually fires.
671
+ const baseClass = clsx(baseClassValue as ClsxClassValue);
672
+ const identity = {};
674
673
  const canTriggerRefineWarning =
675
- !!refine || computedDefaultCount > 0 || extMetasWithRefineCount > 0;
674
+ !!refine ||
675
+ computedDefaultCount > 0 ||
676
+ initialExtensions.some((meta) => meta.resolveRefine);
676
677
  const creationFrame =
677
678
  canTriggerRefineWarning && process.env.NODE_ENV !== "production"
678
679
  ? captureCreationFrame(cv)
679
680
  : undefined;
680
681
 
681
- // Function variant keys inherited from extends, filtered through this
682
- // component's own variants: a static (object/shorthand) variant in this
683
- // component replaces an inherited function variant for the same key.
684
- // The closure is exposed on `ComponentMeta` so any further extending
685
- // component can detect "ancestor's effective variant for K is a function"
686
- // and skip it when overriding K with a non-function.
687
- const functionVariantKeys = new Set<string>();
688
- for (let i = 0; i < extCount; i++) {
689
- const fnKeys = extMetas[i].functionVariantKeys;
690
- for (const k of fnKeys) {
691
- if (disabledVariantKeys.has(k)) continue;
692
- functionVariantKeys.add(k);
693
- }
694
- }
695
- for (let i = 0; i < functionVariantCount; i++) {
696
- functionVariantKeys.add(functionVariantNames[i]);
697
- }
698
- for (let i = 0; i < variantEntryCount; i++) {
699
- // A static variant in this component replaces an inherited function
700
- // variant for the same key; from this component onward, the key is no
701
- // longer a function variant.
702
- functionVariantKeys.delete(variantEntryNames[i]);
703
- }
704
-
705
- const computedDefaultKeys = new Set(inheritedComputedDefaultKeys);
706
- for (let i = 0; i < computedDefaultCount; i++) {
707
- computedDefaultKeys.add(computedDefaultNames[i]);
708
- }
709
-
710
- // Pre-compute static skip key/value sets to pass to extends. These never
711
- // change across calls — when caller passes no skip sets, we reuse the same
712
- // object and avoid Set allocation.
713
- let staticExtSkipKeys: Set<string> | null = null;
714
- if (hasDisabledVariantKeys || functionVariantCount > 0) {
715
- staticExtSkipKeys = new Set<string>();
716
- for (const k of disabledVariantKeys) {
717
- staticExtSkipKeys.add(k);
718
- }
719
- for (let i = 0; i < functionVariantCount; i++) {
720
- staticExtSkipKeys.add(functionVariantNames[i]);
682
+ // Only extension-dependent state is rebuilt. Own tables, callbacks,
683
+ // inheritance visibility, and the creation frame stay shared and unchanged.
684
+ const build = (
685
+ extMetas: ComponentMeta[],
686
+ ): {
687
+ component: CVComponent<V, E>;
688
+ meta: ComponentMeta;
689
+ } => {
690
+ const hasExtend = extMetas.length > 0;
691
+ const extCount = extMetas.length;
692
+ const staticDefaults: Record<string, unknown> = {};
693
+ const extBaseClassesArr: string[] = [];
694
+ const extIsolated: boolean[] = [];
695
+ let hasIsolatedExt = false;
696
+ for (const meta of extMetas) {
697
+ Object.assign(staticDefaults, meta.staticDefaults);
698
+ const isolated = meta.transformClass !== transformClass;
699
+ extIsolated.push(isolated);
700
+ if (isolated) {
701
+ hasIsolatedExt = true;
702
+ extBaseClassesArr.push(meta.transformClass(meta.baseClass));
703
+ } else {
704
+ extBaseClassesArr.push(meta.baseClass);
705
+ }
721
706
  }
722
- }
723
-
724
- // A static variant in this component replaces an inherited function
725
- // variant, so the ancestor must skip that key.
726
- if (variantEntryCount > 0 && extCount > 0) {
727
- for (let i = 0; i < variantEntryCount; i++) {
728
- const name = variantEntryNames[i];
729
- for (let j = 0; j < extCount; j++) {
730
- if (extMetas[j].functionVariantKeys.has(name)) {
731
- staticExtSkipKeys ??= new Set<string>();
732
- staticExtSkipKeys.add(name);
733
- break;
734
- }
707
+ for (const name in ownStaticDefaults) {
708
+ if (!hasOwn(ownStaticDefaults, name)) continue;
709
+ const value = ownStaticDefaults[name];
710
+ if (value === undefined) {
711
+ delete staticDefaults[name];
712
+ } else {
713
+ staticDefaults[name] = value;
735
714
  }
736
715
  }
737
- }
738
- // Skip values are passed directly to extends. We can reuse the same object
739
- // when no caller-provided values need merging.
740
- const staticExtSkipValues: Record<string, Set<string>> | null =
741
- hasDisabledVariantValues ? disabledVariantValues : null;
742
-
743
- // Callers use this only when disabled keys or values exist.
744
- function filterDisabledInto(
745
- input: Record<string, unknown>,
746
- out: Record<string, unknown>,
747
- ): void {
748
- for (const key in input) {
749
- if (!hasOwn(input, key)) continue;
750
- if (disabledVariantKeys.has(key)) continue;
751
- const value = input[key];
752
- if (hasDisabledVariantValues) {
753
- const valueKey = getVariantValueKey(value);
754
- if (valueKey != null && disabledVariantValues[key]?.has(valueKey)) {
716
+ if (hasAnyDisabled) {
717
+ for (const key in staticDefaults) {
718
+ if (!hasOwn(staticDefaults, key)) continue;
719
+ if (disabledVariantKeys.has(key)) {
720
+ delete staticDefaults[key];
755
721
  continue;
756
722
  }
723
+ if (disabledVariantValues) {
724
+ const valueKey = getVariantValueKey(staticDefaults[key]);
725
+ if (
726
+ valueKey != null &&
727
+ getOwn(disabledVariantValues, key)?.has(valueKey)
728
+ ) {
729
+ delete staticDefaults[key];
730
+ }
731
+ }
757
732
  }
758
- out[key] = value;
759
733
  }
760
- }
761
734
 
762
- const isOwnDisabledValue = (key: string, value: unknown): boolean => {
763
- if (disabledVariantKeys.has(key)) {
764
- return true;
735
+ // Filter to only extends with computed default or refine work in their
736
+ // chain. Those are the components that can change resolved variants across
737
+ // fixed-point iterations.
738
+ const extMetasWithRefine: ComponentMeta[] = [];
739
+ for (let i = 0; i < extCount; i++) {
740
+ const meta = extMetas[i];
741
+ if (meta.resolveRefine) {
742
+ extMetasWithRefine.push(meta);
743
+ }
765
744
  }
766
- if (hasDisabledVariantValues) {
767
- const valueKey = getVariantValueKey(value);
768
- if (valueKey != null && disabledVariantValues[key]?.has(valueKey)) {
769
- return true;
745
+ const extMetasWithRefineCount = extMetasWithRefine.length;
746
+
747
+ // Only a `refine` callback protects variants, through `setVariants`. This
748
+ // over-approximates: `extMetasWithRefineCount` also counts extends that
749
+ // only have computed defaults. Tightening it would mean tracking `refine`
750
+ // separately in `ComponentMeta` to save one allocation. The record inside
751
+ // the holder is created only when a callback assigns something.
752
+ const canProtectVariants = !!refine || extMetasWithRefineCount > 0;
753
+
754
+ // `userVariantProps` tells computed defaults which variants the user set
755
+ // explicitly, and `runComputedDefaults` is its only reader anywhere in the
756
+ // chain: a `refine` callback never receives it. `computedDefaultKeys` is
757
+ // transitive, so an empty inherited set proves no extend runs computed
758
+ // defaults either, and nothing reads the record. The record is then the
759
+ // caller's own props object, which no stage may write to.
760
+ const needsUserVariantProps =
761
+ computedDefaultCount > 0 || inheritedComputedDefaultKeys.size > 0;
762
+
763
+ // Function variant keys inherited from extends, filtered through this
764
+ // component's own variants: a static (object/shorthand) variant in this
765
+ // component replaces an inherited function variant for the same key.
766
+ // The closure is exposed on `ComponentMeta` so any further extending
767
+ // component can detect "ancestor's effective variant for K is a function"
768
+ // and skip it when overriding K with a non-function.
769
+ const functionVariantKeys = new Set<string>();
770
+ for (let i = 0; i < extCount; i++) {
771
+ const fnKeys = extMetas[i].functionVariantKeys;
772
+ for (const k of fnKeys) {
773
+ if (disabledVariantKeys.has(k)) continue;
774
+ functionVariantKeys.add(k);
770
775
  }
771
776
  }
772
- return false;
773
- };
774
-
775
- const filterOwnDisabledVariants = (
776
- input: Record<string, unknown>,
777
- fallback: Record<string, unknown>,
778
- ): Record<string, unknown> => {
779
- if (!hasAnyDisabled) {
780
- return input;
777
+ for (let i = 0; i < functionVariantCount; i++) {
778
+ functionVariantKeys.add(functionVariantNames[i]);
781
779
  }
782
-
783
- let hasOwnDisabledValue = false;
784
- for (const key in input) {
785
- if (!hasOwn(input, key)) continue;
786
- const value = input[key];
787
- if (isOwnDisabledValue(key, value)) {
788
- hasOwnDisabledValue = true;
789
- break;
790
- }
780
+ for (let i = 0; i < variantEntryCount; i++) {
781
+ // A static variant in this component replaces an inherited function
782
+ // variant for the same key; from this component onward, the key is no
783
+ // longer a function variant.
784
+ functionVariantKeys.delete(variantEntryNames[i]);
791
785
  }
792
- if (!hasOwnDisabledValue) {
793
- return input;
786
+
787
+ const computedDefaultKeys = new Set(inheritedComputedDefaultKeys);
788
+ for (let i = 0; i < computedDefaultCount; i++) {
789
+ computedDefaultKeys.add(computedDefaultNames[i]);
794
790
  }
795
791
 
796
- const filtered: Record<string, unknown> = {};
797
- for (const key in input) {
798
- if (!hasOwn(input, key)) continue;
799
- const value = input[key];
800
- if (!isOwnDisabledValue(key, value)) {
801
- filtered[key] = value;
802
- continue;
792
+ // Pre-compute static skip key/value sets to pass to extends. These never
793
+ // change across calls — when caller passes no skip sets, we reuse the same
794
+ // object and avoid Set allocation.
795
+ let staticExtSkipKeys: Set<string> | null = null;
796
+ if (hasDisabledVariantKeys || functionVariantCount > 0) {
797
+ staticExtSkipKeys = new Set<string>();
798
+ for (const k of disabledVariantKeys) {
799
+ staticExtSkipKeys.add(k);
803
800
  }
804
- const fallbackValue = fallback[key];
805
- if (
806
- fallbackValue !== undefined &&
807
- !isOwnDisabledValue(key, fallbackValue)
808
- ) {
809
- filtered[key] = fallbackValue;
801
+ for (let i = 0; i < functionVariantCount; i++) {
802
+ staticExtSkipKeys.add(functionVariantNames[i]);
810
803
  }
811
804
  }
812
805
 
813
- return filtered;
814
- };
815
-
816
- // Hot path: resolve variants by merging static defaults + extends'
817
- // static defaults + user-provided props.
818
- function resolveVariantsHot(
819
- propsVariants: Record<string, unknown>,
820
- ): Record<string, unknown> {
821
- // Start with static defaults
822
- const defaults = Object.assign({}, staticDefaults);
823
-
824
- // Apply propsVariants on top (filter undefined).
825
- for (let i = 0; i < variantKeysLength; i++) {
826
- const k = variantKeys[i];
827
- if (!hasOwn(propsVariants, k)) continue;
828
- const v = propsVariants[k];
829
- if (v === undefined) continue;
830
- defaults[k] = v;
806
+ // A static variant in this component replaces an inherited function
807
+ // variant, so the ancestor must skip that key.
808
+ if (variantEntryCount > 0 && extCount > 0) {
809
+ for (let i = 0; i < variantEntryCount; i++) {
810
+ const name = variantEntryNames[i];
811
+ for (let j = 0; j < extCount; j++) {
812
+ if (extMetas[j].functionVariantKeys.has(name)) {
813
+ staticExtSkipKeys ??= new Set<string>();
814
+ staticExtSkipKeys.add(name);
815
+ break;
816
+ }
817
+ }
818
+ }
831
819
  }
832
-
833
- if (!hasAnyDisabled) {
834
- return defaults;
820
+ // Skip values are passed directly to extends. We can reuse the same object
821
+ // when no caller-provided values need merging.
822
+ const staticExtSkipValues = disabledVariantValues;
823
+
824
+ // Callers use this only when disabled keys or values exist.
825
+ function filterDisabledInto(
826
+ input: Record<string, unknown>,
827
+ out: Record<string, unknown>,
828
+ ): void {
829
+ for (const key in input) {
830
+ if (!hasOwn(input, key)) continue;
831
+ if (disabledVariantKeys.has(key)) continue;
832
+ const value = input[key];
833
+ if (disabledVariantValues) {
834
+ const valueKey = getVariantValueKey(value);
835
+ if (
836
+ valueKey != null &&
837
+ getOwn(disabledVariantValues, key)?.has(valueKey)
838
+ ) {
839
+ continue;
840
+ }
841
+ }
842
+ out[key] = value;
843
+ }
835
844
  }
836
845
 
837
- // Filter disabled
838
- const result: Record<string, unknown> = {};
839
- filterDisabledInto(defaults, result);
840
- return result;
841
- }
842
-
843
- const runComputedDefaults = (
844
- resolved: Record<string, unknown>,
845
- defaultResolved: Record<string, unknown>,
846
- userVariantProps: Record<string, unknown>,
847
- filterOwnVariants: boolean,
848
- protectedVariantKeys: Set<string> | null | undefined,
849
- ): Record<string, unknown> => {
850
- let ownVariants = filterOwnVariants ? null : resolved;
851
- const getOwnVariants = (): Record<string, unknown> => {
852
- if (ownVariants) {
853
- return ownVariants;
846
+ const isOwnDisabledValue = (key: string, value: unknown): boolean => {
847
+ if (disabledVariantKeys.has(key)) {
848
+ return true;
854
849
  }
855
- const filteredVariants: Record<string, unknown> = {};
856
- for (let i = 0; i < variantKeysLength; i++) {
857
- const key = variantKeys[i];
858
- if (hasOwn(resolved, key)) {
859
- filteredVariants[key] = resolved[key];
850
+ if (disabledVariantValues) {
851
+ const valueKey = getVariantValueKey(value);
852
+ if (
853
+ valueKey != null &&
854
+ getOwn(disabledVariantValues, key)?.has(valueKey)
855
+ ) {
856
+ return true;
860
857
  }
861
858
  }
862
- ownVariants = filteredVariants;
863
- return filteredVariants;
859
+ return false;
864
860
  };
865
861
 
866
- let updatedVariants: Record<string, unknown> | null = null;
867
- const ensureUpdated = (): Record<string, unknown> => {
868
- if (updatedVariants) {
869
- return updatedVariants;
862
+ const filterOwnDisabledVariants = (
863
+ input: Record<string, unknown>,
864
+ fallback: Record<string, unknown>,
865
+ ): Record<string, unknown> => {
866
+ if (!hasAnyDisabled) {
867
+ return input;
870
868
  }
871
- const updated = Object.assign({}, resolved);
872
- updatedVariants = updated;
873
- return updated;
874
- };
875
869
 
876
- for (let i = 0; i < computedDefaultCount; i++) {
877
- const key = computedDefaultNames[i];
878
- if (hasOwn(userVariantProps, key)) {
879
- if (userVariantProps[key] !== undefined) continue;
870
+ let hasOwnDisabledValue = false;
871
+ for (const key in input) {
872
+ if (!hasOwn(input, key)) continue;
873
+ const value = input[key];
874
+ if (isOwnDisabledValue(key, value)) {
875
+ hasOwnDisabledValue = true;
876
+ break;
877
+ }
878
+ }
879
+ if (!hasOwnDisabledValue) {
880
+ return input;
880
881
  }
881
- if (protectedVariantKeys?.has(key)) continue;
882
882
 
883
- const variantSnapshot = getOwnVariants();
884
- const defaultValue = inheritedComputedDefaultKeys.has(key)
885
- ? variantSnapshot[key]
886
- : defaultResolved[key];
887
- const value = computedDefaultFns[i](defaultValue, variantSnapshot);
888
- if (hasAnyDisabled) {
889
- if (disabledVariantKeys.has(key)) continue;
890
- const valueKey = getVariantValueKey(value);
891
- if (valueKey != null && disabledVariantValues[key]?.has(valueKey)) {
883
+ const filtered: Record<string, unknown> = {};
884
+ for (const key in input) {
885
+ if (!hasOwn(input, key)) continue;
886
+ const value = input[key];
887
+ if (!isOwnDisabledValue(key, value)) {
888
+ filtered[key] = value;
892
889
  continue;
893
890
  }
891
+ const fallbackValue = getOwn(fallback, key);
892
+ if (
893
+ fallbackValue !== undefined &&
894
+ !isOwnDisabledValue(key, fallbackValue)
895
+ ) {
896
+ filtered[key] = fallbackValue;
897
+ }
894
898
  }
895
899
 
896
- if (value === undefined) {
897
- if (!hasOwn(variantSnapshot, key)) continue;
898
- delete ensureUpdated()[key];
899
- continue;
900
+ return filtered;
901
+ };
902
+
903
+ // Hot path: resolve variants by merging static defaults + extends'
904
+ // static defaults + user-provided props.
905
+ function resolveVariantsHot(
906
+ propsVariants: Record<string, unknown>,
907
+ ): Record<string, unknown> {
908
+ // Start with static defaults
909
+ const defaults = Object.assign({}, staticDefaults);
910
+
911
+ // Apply propsVariants on top (filter undefined).
912
+ for (let i = 0; i < variantKeysLength; i++) {
913
+ const k = variantKeys[i];
914
+ if (!hasOwn(propsVariants, k)) continue;
915
+ const v = propsVariants[k];
916
+ if (v === undefined) continue;
917
+ defaults[k] = v;
900
918
  }
901
- if (Object.is(variantSnapshot[key], value)) continue;
902
- ensureUpdated()[key] = value;
903
- }
904
919
 
905
- return updatedVariants ?? resolved;
906
- };
920
+ if (!hasAnyDisabled) {
921
+ return defaults;
922
+ }
907
923
 
908
- const runRefineContext = (
909
- resolved: Record<string, unknown>,
910
- filterOwnVariants: boolean,
911
- collectOutput: boolean,
912
- applyVariantUpdates: boolean,
913
- protectedVariants: Record<string, unknown> | null | undefined,
914
- protectedVariantKeys: Set<string> | null | undefined,
915
- ): {
916
- workingResolved: Record<string, unknown>;
917
- classes: ClassValue[] | null;
918
- style: StyleValue | null;
919
- } => {
920
- let workingResolved = resolved;
921
- let cClasses: ClassValue[] | null = null;
922
- let cStyle: StyleValue | null = null;
923
-
924
- if (refine) {
925
- let ownVariants = resolved;
926
- if (filterOwnVariants) {
927
- // When this component is being extended, `resolved` is the parent's
928
- // workingResolved (a superset of our variant keys). Filter to our own
929
- // keys for `ctx.variants` so the user's `refine` callback sees the
930
- // shape declared by `VariantValues<V>` and not foreign parent keys.
924
+ // Filter disabled
925
+ const result: Record<string, unknown> = {};
926
+ filterDisabledInto(defaults, result);
927
+ return result;
928
+ }
929
+
930
+ const runComputedDefaults = (
931
+ resolved: Record<string, unknown>,
932
+ defaultResolved: Record<string, unknown>,
933
+ userVariantProps: Record<string, unknown>,
934
+ filterOwnVariants: boolean,
935
+ protection: RefineProtection | null | undefined,
936
+ ): Record<string, unknown> => {
937
+ const protectedVariants = protection?.variants;
938
+ let ownVariants = filterOwnVariants ? null : resolved;
939
+ const getOwnVariants = (): Record<string, unknown> => {
940
+ if (ownVariants) {
941
+ return ownVariants;
942
+ }
931
943
  const filteredVariants: Record<string, unknown> = {};
932
944
  for (let i = 0; i < variantKeysLength; i++) {
933
- const k = variantKeys[i];
934
- if (hasOwn(resolved, k)) {
935
- filteredVariants[k] = resolved[k];
945
+ const key = variantKeys[i];
946
+ if (hasOwn(resolved, key)) {
947
+ filteredVariants[key] = resolved[key];
936
948
  }
937
949
  }
938
950
  ownVariants = filteredVariants;
939
- }
940
- // Lazy-init updatedVariants — many refine callbacks only inspect
941
- // `variants`, so the copy is unnecessary in the common case.
951
+ return filteredVariants;
952
+ };
953
+
942
954
  let updatedVariants: Record<string, unknown> | null = null;
943
- const localCClasses: ClassValue[] | null = collectOutput ? [] : null;
944
- let localCStyle: StyleValue | null = null;
945
955
  const ensureUpdated = (): Record<string, unknown> => {
946
956
  if (updatedVariants) {
947
957
  return updatedVariants;
948
958
  }
949
- const u = Object.assign({}, ownVariants);
950
- updatedVariants = u;
951
- return u;
959
+ const updated = Object.assign({}, resolved);
960
+ updatedVariants = updated;
961
+ return updated;
952
962
  };
953
- const protectVariant = (key: string, value: unknown) => {
954
- if (protectedVariants) {
955
- protectedVariants[key] = value;
956
- protectedVariantKeys?.add(key);
963
+
964
+ for (let i = 0; i < computedDefaultCount; i++) {
965
+ const key = computedDefaultNames[i];
966
+ if (hasOwn(userVariantProps, key)) {
967
+ if (userVariantProps[key] !== undefined) continue;
957
968
  }
958
- };
959
- const ctx = {
960
- variants: ownVariants as VariantValues<Record<string, unknown>>,
961
- setVariants: (
962
- newVariants: VariantValues<Record<string, unknown>>,
963
- ) => {
964
- if (!applyVariantUpdates) {
965
- return;
969
+ if (protectedVariants && hasOwn(protectedVariants, key)) continue;
970
+
971
+ const variantSnapshot = getOwnVariants();
972
+ const defaultValue = inheritedComputedDefaultKeys.has(key)
973
+ ? getOwn(variantSnapshot, key)
974
+ : getOwn(defaultResolved, key);
975
+ const value = computedDefaultFns[i](defaultValue, variantSnapshot);
976
+ if (hasAnyDisabled) {
977
+ if (disabledVariantKeys.has(key)) continue;
978
+ if (disabledVariantValues) {
979
+ const valueKey = getVariantValueKey(value);
980
+ if (
981
+ valueKey != null &&
982
+ getOwn(disabledVariantValues, key)?.has(valueKey)
983
+ ) {
984
+ continue;
985
+ }
986
+ }
987
+ }
988
+
989
+ if (value === undefined) {
990
+ if (!hasOwn(variantSnapshot, key)) continue;
991
+ delete ensureUpdated()[key];
992
+ continue;
993
+ }
994
+ if (Object.is(getOwn(variantSnapshot, key), value)) continue;
995
+ ensureUpdated()[key] = value;
996
+ }
997
+
998
+ return updatedVariants ?? resolved;
999
+ };
1000
+
1001
+ const runRefineContext = (
1002
+ resolved: Record<string, unknown>,
1003
+ filterOwnVariants: boolean,
1004
+ collectOutput: boolean,
1005
+ applyVariantUpdates: boolean,
1006
+ protection: RefineProtection | null | undefined,
1007
+ ): {
1008
+ workingResolved: Record<string, unknown>;
1009
+ classes: ClassValue[] | null;
1010
+ style: StyleValue | null;
1011
+ } => {
1012
+ let workingResolved = resolved;
1013
+ let cClasses: ClassValue[] | null = null;
1014
+ let cStyle: StyleValue | null = null;
1015
+
1016
+ if (refine) {
1017
+ let ownVariants = resolved;
1018
+ if (filterOwnVariants) {
1019
+ // When this component is being extended, `resolved` is the parent's
1020
+ // workingResolved (a superset of our variant keys). Filter to our own
1021
+ // keys for `ctx.variants` so the user's `refine` callback sees the
1022
+ // shape declared by `VariantValues<V>` and not foreign parent keys.
1023
+ const filteredVariants: Record<string, unknown> = {};
1024
+ for (let i = 0; i < variantKeysLength; i++) {
1025
+ const k = variantKeys[i];
1026
+ if (hasOwn(resolved, k)) {
1027
+ filteredVariants[k] = resolved[k];
1028
+ }
966
1029
  }
967
- if (!hasAnyDisabled) {
1030
+ ownVariants = filteredVariants;
1031
+ }
1032
+ // `assignedVariants`, `localCClasses` and `localCStyle` are created on
1033
+ // first use, so a callback that only inspects `variants` allocates
1034
+ // none of them. The `setVariants` bookkeeping is inlined for the same
1035
+ // reason: helper closures would be allocated on every call, including
1036
+ // the calls that never assign anything.
1037
+ // `assignedVariants` holds only the changed keys, because copying all
1038
+ // of `ownVariants` into it would repeat values `workingResolved` has.
1039
+ let assignedVariants: Record<string, unknown> | null = null;
1040
+ let localCClasses: ClassValue[] | null = null;
1041
+ let localCStyle: StyleValue | null = null;
1042
+ const ctx = {
1043
+ variants: ownVariants as VariantValues<Record<string, unknown>>,
1044
+ setVariants: (
1045
+ newVariants: VariantValues<Record<string, unknown>>,
1046
+ ) => {
1047
+ if (!applyVariantUpdates) {
1048
+ return;
1049
+ }
968
1050
  for (const key in newVariants) {
969
1051
  if (!hasOwn(newVariants, key)) continue;
1052
+ // `disabledVariantKeys` is empty unless this component disables a
1053
+ // variant, so the lookup replaces a `hasAnyDisabled` branch. The
1054
+ // key is checked before the value is read, because reading it can
1055
+ // run a caller-defined accessor.
1056
+ if (disabledVariantKeys.has(key)) continue;
970
1057
  const value = (newVariants as Record<string, unknown>)[key];
971
- protectVariant(key, value);
972
- if (Object.is((updatedVariants ?? ownVariants)[key], value)) {
973
- continue;
1058
+ if (disabledVariantValues) {
1059
+ const valueKey = getVariantValueKey(value);
1060
+ if (
1061
+ valueKey != null &&
1062
+ getOwn(disabledVariantValues, key)?.has(valueKey)
1063
+ ) {
1064
+ continue;
1065
+ }
974
1066
  }
975
- ensureUpdated()[key] = value;
976
- }
977
- return;
978
- }
979
- for (const key in newVariants) {
980
- if (!hasOwn(newVariants, key)) continue;
981
- if (disabledVariantKeys.has(key)) continue;
982
- const value = (newVariants as Record<string, unknown>)[key];
983
- if (hasDisabledVariantValues) {
984
- const valueKey = getVariantValueKey(value);
985
- if (
986
- valueKey != null &&
987
- disabledVariantValues[key]?.has(valueKey)
988
- ) {
1067
+ if (protection) {
1068
+ const protectedVariants = (protection.variants ??= {});
1069
+ protectedVariants[key] = value;
1070
+ }
1071
+ // The record holds only assigned keys, so both halves matter: a
1072
+ // key it owns can hold a clear that `??` on the value would
1073
+ // discard, and a key it does not own must fall back to
1074
+ // `ownVariants`, or a clear compares against `undefined`.
1075
+ const current =
1076
+ assignedVariants && hasOwn(assignedVariants, key)
1077
+ ? assignedVariants[key]
1078
+ : getOwn(ownVariants, key);
1079
+ if (Object.is(current, value)) {
989
1080
  continue;
990
1081
  }
1082
+ assignedVariants ??= {};
1083
+ assignedVariants[key] = value;
991
1084
  }
992
- protectVariant(key, value);
993
- if (Object.is((updatedVariants ?? ownVariants)[key], value)) {
994
- continue;
995
- }
996
- ensureUpdated()[key] = value;
1085
+ },
1086
+ addClass: (className: ClassValue) => {
1087
+ if (!collectOutput) return;
1088
+ localCClasses ??= [];
1089
+ localCClasses.push(className);
1090
+ },
1091
+ addStyle: (newStyle: StyleValue) => {
1092
+ if (!collectOutput) return;
1093
+ localCStyle ??= {};
1094
+ Object.assign(localCStyle, newStyle);
1095
+ },
1096
+ };
1097
+ const result = refine(ctx);
1098
+ if (collectOutput && result != null) {
1099
+ const r = extractClassAndStylePrebuilt(result);
1100
+ if (r.class != null) {
1101
+ localCClasses ??= [];
1102
+ localCClasses.push(r.class);
997
1103
  }
998
- },
999
- addClass: (className: ClassValue) => {
1000
- localCClasses?.push(className);
1001
- },
1002
- addStyle: (newStyle: StyleValue) => {
1003
- if (!collectOutput) return;
1004
- if (!localCStyle) {
1005
- localCStyle = {};
1104
+ if (r.style) {
1105
+ localCStyle ??= {};
1106
+ Object.assign(localCStyle, r.style);
1006
1107
  }
1007
- Object.assign(localCStyle, newStyle);
1008
- },
1009
- };
1010
- const result = refine(ctx);
1011
- if (collectOutput && result != null) {
1012
- const r = extractClassAndStylePrebuilt(result);
1013
- if (r.class != null) {
1014
- localCClasses?.push(r.class);
1015
1108
  }
1016
- if (r.style) {
1017
- if (!localCStyle) {
1018
- localCStyle = {};
1019
- }
1020
- Object.assign(localCStyle, r.style);
1021
- }
1022
- }
1023
- cClasses = localCClasses;
1024
- cStyle = localCStyle;
1025
- if (updatedVariants) {
1026
- const nextResolved = Object.assign({}, workingResolved);
1027
- if (hasAnyDisabled) {
1028
- filterDisabledInto(updatedVariants, nextResolved);
1029
- } else {
1030
- Object.assign(nextResolved, updatedVariants);
1109
+ cClasses = localCClasses;
1110
+ cStyle = localCStyle;
1111
+ if (assignedVariants) {
1112
+ // No disabled filtering here: `setVariants` drops a disabled key or
1113
+ // value before it records one, so every assigned key already passed
1114
+ // the same two checks.
1115
+ workingResolved = Object.assign(
1116
+ {},
1117
+ workingResolved,
1118
+ assignedVariants,
1119
+ );
1031
1120
  }
1032
- workingResolved = nextResolved;
1033
1121
  }
1034
- }
1035
1122
 
1036
- return {
1037
- workingResolved,
1038
- classes: cClasses,
1039
- style: cStyle,
1123
+ return {
1124
+ workingResolved,
1125
+ classes: cClasses,
1126
+ style: cStyle,
1127
+ };
1040
1128
  };
1041
- };
1042
1129
 
1043
- // Core compute path. Called both for top-level rendering (via
1044
- // `computeResult`) and recursively when this component is used as an
1045
- // `extend` target by another component. Pushes variant classes (excluding
1046
- // base class) into `classesOut` and merges styles into `styleOut`.
1047
- const computeOnce: ComputeOnceFn = (
1048
- resolved,
1049
- userVariantProps,
1050
- skipKeys,
1051
- skipValues,
1052
- classesOut,
1053
- styleOut,
1054
- protectedVariants,
1055
- protectedVariantKeys,
1056
- defaultResolved = resolved,
1057
- renderOnly = false,
1058
- ) => {
1059
- let workingResolved = resolved;
1060
- let cClasses: ClassValue[] | null = null;
1061
- let cStyle: StyleValue | null = null;
1062
-
1063
- // Run extends' contributions first (their full classes + styles) so our
1064
- // own base style and variants apply on top, matching the original
1065
- // ext1 → ext2 → … → current ordering.
1066
- if (hasExtend) {
1067
- // Build skip sets to pass to extends. Reuse precomputed values when no
1068
- // caller-provided sets need merging.
1069
- let extSkipKeys: Set<string> | null;
1070
- if (skipKeys === null) {
1071
- extSkipKeys = staticExtSkipKeys;
1072
- } else if (staticExtSkipKeys === null) {
1073
- extSkipKeys = skipKeys;
1074
- } else {
1075
- extSkipKeys = new Set(skipKeys);
1076
- for (const k of staticExtSkipKeys) {
1077
- extSkipKeys.add(k);
1130
+ // Core compute path. Called both for top-level rendering (via
1131
+ // `computeResult`) and recursively when this component is used as an
1132
+ // `extend` target by another component. Pushes variant classes (excluding
1133
+ // base class) into `classesOut` and merges styles into `styleOut`.
1134
+ const computeOnce: ComputeOnceFn = (
1135
+ resolved,
1136
+ userVariantProps,
1137
+ skipKeys,
1138
+ skipValues,
1139
+ classesOut,
1140
+ styleOut,
1141
+ protection,
1142
+ defaultResolved = resolved,
1143
+ renderOnly = false,
1144
+ ) => {
1145
+ let workingResolved = resolved;
1146
+ let cClasses: ClassValue[] | null = null;
1147
+ let cStyle: StyleValue | null = null;
1148
+
1149
+ // Run extends' contributions first (their full classes + styles) so our
1150
+ // own base style and variants apply on top, matching the original
1151
+ // ext1 → ext2 → … → current ordering.
1152
+ if (hasExtend) {
1153
+ // Build skip sets to pass to extends. Reuse precomputed values when no
1154
+ // caller-provided sets need merging.
1155
+ let extSkipKeys: Set<string> | null;
1156
+ if (skipKeys === null) {
1157
+ extSkipKeys = staticExtSkipKeys;
1158
+ } else if (staticExtSkipKeys === null) {
1159
+ extSkipKeys = skipKeys;
1160
+ } else {
1161
+ extSkipKeys = new Set(skipKeys);
1162
+ for (const k of staticExtSkipKeys) {
1163
+ extSkipKeys.add(k);
1164
+ }
1078
1165
  }
1079
- }
1080
1166
 
1081
- let extSkipVals: Record<string, Set<string>> | null;
1082
- if (skipValues === null) {
1083
- extSkipVals = staticExtSkipValues;
1084
- } else if (staticExtSkipValues === null) {
1085
- extSkipVals = skipValues;
1086
- } else {
1087
- extSkipVals = {};
1088
- for (const k in skipValues) {
1089
- extSkipVals[k] = skipValues[k];
1090
- }
1091
- for (const k in staticExtSkipValues) {
1092
- const existing = extSkipVals[k];
1093
- if (existing) {
1094
- const merged = new Set<string>(existing);
1095
- for (const v of staticExtSkipValues[k]) {
1096
- merged.add(v);
1167
+ let extSkipVals: Record<string, Set<string>> | null;
1168
+ if (skipValues === null) {
1169
+ extSkipVals = staticExtSkipValues;
1170
+ } else if (staticExtSkipValues === null) {
1171
+ extSkipVals = skipValues;
1172
+ } else {
1173
+ extSkipVals = {};
1174
+ for (const k in skipValues) {
1175
+ if (!hasOwn(skipValues, k)) continue;
1176
+ extSkipVals[k] = skipValues[k];
1177
+ }
1178
+ for (const k in staticExtSkipValues) {
1179
+ if (!hasOwn(staticExtSkipValues, k)) continue;
1180
+ const values = staticExtSkipValues[k];
1181
+ const existing = getOwn(extSkipVals, k);
1182
+ if (existing) {
1183
+ const merged = new Set<string>(existing);
1184
+ for (const v of values) {
1185
+ merged.add(v);
1186
+ }
1187
+ extSkipVals[k] = merged;
1188
+ } else {
1189
+ extSkipVals[k] = values;
1097
1190
  }
1098
- extSkipVals[k] = merged;
1099
- } else {
1100
- extSkipVals[k] = staticExtSkipValues[k];
1101
1191
  }
1102
1192
  }
1103
- }
1104
1193
 
1105
- const extUserVariantProps =
1106
- extMetasWithRefineCount > 0
1107
- ? getExtUserVariantProps(
1194
+ for (let i = 0; i < extCount; i++) {
1195
+ if (hasIsolatedExt && extIsolated[i]) {
1196
+ // Isolated extend (different factory): gather its variant classes
1197
+ // into a scratch array, then push the joined string after applying
1198
+ // its own transformClass. Our outer transform applies on top.
1199
+ const extClasses: ClsxClassValue[] = [];
1200
+ workingResolved = extMetas[i].compute(
1201
+ workingResolved,
1108
1202
  userVariantProps,
1109
- protectedVariants ?? null,
1110
- )
1111
- : userVariantProps;
1112
- for (let i = 0; i < extCount; i++) {
1113
- if (hasIsolatedExt && extIsolated[i]) {
1114
- // Isolated extend (different factory): gather its variant classes
1115
- // into a scratch array, then push the joined string after applying
1116
- // its own transformClass. Our outer transform applies on top.
1117
- const extClasses: ClsxClassValue[] = [];
1118
- workingResolved = extMetas[i].compute(
1119
- workingResolved,
1120
- extUserVariantProps,
1121
- extSkipKeys,
1122
- extSkipVals,
1123
- extClasses,
1124
- styleOut,
1125
- protectedVariants,
1126
- protectedVariantKeys,
1127
- defaultResolved,
1128
- renderOnly,
1129
- );
1130
- if (extClasses.length > 0) {
1131
- const joined = clsx(extClasses);
1132
- if (joined.length > 0) {
1133
- classesOut.push(extMetas[i].transformClass(joined));
1203
+ extSkipKeys,
1204
+ extSkipVals,
1205
+ extClasses,
1206
+ styleOut,
1207
+ protection,
1208
+ defaultResolved,
1209
+ renderOnly,
1210
+ );
1211
+ if (extClasses.length > 0) {
1212
+ const joined = clsx(extClasses);
1213
+ if (joined.length > 0) {
1214
+ classesOut.push(extMetas[i].transformClass(joined));
1215
+ }
1134
1216
  }
1217
+ } else {
1218
+ workingResolved = extMetas[i].compute(
1219
+ workingResolved,
1220
+ userVariantProps,
1221
+ extSkipKeys,
1222
+ extSkipVals,
1223
+ classesOut,
1224
+ styleOut,
1225
+ protection,
1226
+ defaultResolved,
1227
+ renderOnly,
1228
+ );
1135
1229
  }
1136
- } else {
1137
- workingResolved = extMetas[i].compute(
1230
+ workingResolved = filterOwnDisabledVariants(
1138
1231
  workingResolved,
1139
- extUserVariantProps,
1140
- extSkipKeys,
1141
- extSkipVals,
1142
- classesOut,
1143
- styleOut,
1144
- protectedVariants,
1145
- protectedVariantKeys,
1146
1232
  defaultResolved,
1147
- renderOnly,
1148
1233
  );
1149
1234
  }
1150
- workingResolved = filterOwnDisabledVariants(
1235
+ }
1236
+
1237
+ // Run own computed defaults after extended components so defaults resolve
1238
+ // from base to child. They still run before this component's `refine`.
1239
+ if (!renderOnly && computedDefaultCount > 0) {
1240
+ workingResolved = runComputedDefaults(
1151
1241
  workingResolved,
1152
1242
  defaultResolved,
1243
+ userVariantProps,
1244
+ true,
1245
+ protection,
1153
1246
  );
1154
- // Only sync protected variants when a child refine resolver can
1155
- // observe them. Otherwise extUserVariantProps may alias caller props.
1156
- if (protectedVariants && extMetasWithRefineCount > 0) {
1157
- Object.assign(extUserVariantProps, protectedVariants);
1158
- }
1159
1247
  }
1160
- }
1161
1248
 
1162
- // Run own computed defaults after extended components so defaults resolve
1163
- // from base to child. They still run before this component's `refine`.
1164
- if (!renderOnly && computedDefaultCount > 0) {
1165
- workingResolved = runComputedDefaults(
1166
- workingResolved,
1167
- defaultResolved,
1168
- userVariantProps,
1169
- true,
1170
- protectedVariantKeys,
1171
- );
1172
- }
1249
+ // Run own `refine` (if any). May modify resolved variants and emit
1250
+ // classes and styles that are applied after this component's variants.
1251
+ if (refine) {
1252
+ const refineResult = runRefineContext(
1253
+ workingResolved,
1254
+ true,
1255
+ true,
1256
+ !renderOnly,
1257
+ protection,
1258
+ );
1259
+ workingResolved = refineResult.workingResolved;
1260
+ cClasses = refineResult.classes;
1261
+ cStyle = refineResult.style;
1262
+ }
1173
1263
 
1174
- // Run own `refine` (if any). May modify resolved variants and emit
1175
- // classes and styles that are applied after this component's variants.
1176
- if (refine) {
1177
- const refineResult = runRefineContext(
1178
- workingResolved,
1179
- true,
1180
- true,
1181
- !renderOnly,
1182
- protectedVariants,
1183
- protectedVariantKeys,
1184
- );
1185
- workingResolved = refineResult.workingResolved;
1186
- cClasses = refineResult.classes;
1187
- cStyle = refineResult.style;
1188
- }
1264
+ // Apply own base style (after extends' styles, matching original order).
1265
+ if (hasBaseStyle) {
1266
+ Object.assign(styleOut, baseStyle);
1267
+ }
1189
1268
 
1190
- // Apply own base style (after extends' styles, matching original order).
1191
- if (hasBaseStyle) {
1192
- Object.assign(styleOut, baseStyle);
1193
- }
1269
+ // Apply own variants. Skip keys/values come from caller (e.g., parent
1270
+ // wants its own function variant to override this variant).
1271
+ // `variantEntryNames` already excludes disabled keys (those with `null`
1272
+ // value in config) and function variants, so we don't re-check
1273
+ // `disabledVariantKeys` here.
1274
+ const ownSkipKeys = skipKeys;
1275
+ const ownSkipValues = skipValues;
1276
+ for (let i = 0; i < variantEntryCount; i++) {
1277
+ const variantName = variantEntryNames[i];
1278
+ if (ownSkipKeys && ownSkipKeys.has(variantName)) continue;
1279
+ // Read first: an inherited value that could change the output is
1280
+ // never `undefined`, so the own-property check only runs for the
1281
+ // variants that carry a value.
1282
+ const selectedValue = workingResolved[variantName];
1283
+ if (selectedValue === undefined) continue;
1284
+ if (!hasOwn(workingResolved, variantName)) continue;
1285
+ const selectedKey = getVariantValueKey(selectedValue);
1286
+ const variant = variantEntryDefs[i];
1287
+ if (
1288
+ variant.disabledValues &&
1289
+ selectedKey != null &&
1290
+ variant.disabledValues.has(selectedKey)
1291
+ ) {
1292
+ continue;
1293
+ }
1294
+ if (
1295
+ ownSkipValues &&
1296
+ selectedKey != null &&
1297
+ getOwn(ownSkipValues, variantName)?.has(selectedKey)
1298
+ ) {
1299
+ continue;
1300
+ }
1194
1301
 
1195
- // Apply own variants. Skip keys/values come from caller (e.g., parent
1196
- // wants its own function variant to override this variant).
1197
- // `variantEntryNames` already excludes disabled keys (those with `null`
1198
- // value in config) and function variants, so we don't re-check
1199
- // `disabledVariantKeys` here.
1200
- const ownSkipKeys = skipKeys;
1201
- const ownSkipValues = skipValues;
1202
- for (let i = 0; i < variantEntryCount; i++) {
1203
- const variantName = variantEntryNames[i];
1204
- if (ownSkipKeys && ownSkipKeys.has(variantName)) continue;
1205
- const selectedValue = workingResolved[variantName];
1206
- if (selectedValue === undefined) continue;
1207
- const selectedKey = getVariantValueKey(selectedValue);
1208
- const variant = variantEntryDefs[i];
1209
- if (
1210
- variant.disabledValues &&
1211
- selectedKey != null &&
1212
- variant.disabledValues.has(selectedKey)
1213
- ) {
1214
- continue;
1215
- }
1216
- if (
1217
- ownSkipValues &&
1218
- selectedKey != null &&
1219
- ownSkipValues[variantName]?.has(selectedKey)
1220
- ) {
1221
- continue;
1302
+ let value: PrebuiltValue | null | undefined;
1303
+ if (variant.values) {
1304
+ if (selectedKey == null) continue;
1305
+ value = variant.values.get(selectedKey);
1306
+ } else if (selectedValue === true) {
1307
+ value = variant.shorthand;
1308
+ }
1309
+ if (!value) continue;
1310
+ if (value.class != null) {
1311
+ classesOut.push(value.class);
1312
+ }
1313
+ if (value.style) {
1314
+ Object.assign(styleOut, value.style);
1315
+ }
1222
1316
  }
1223
1317
 
1224
- let value: PrebuiltValue | null | undefined;
1225
- if (variant.values) {
1226
- if (selectedKey == null) continue;
1227
- value = variant.values[selectedKey];
1228
- } else if (selectedValue === true) {
1229
- value = variant.shorthand;
1230
- }
1231
- if (!value) continue;
1232
- if (value.class != null) {
1233
- classesOut.push(value.class);
1234
- }
1235
- if (value.style) {
1236
- Object.assign(styleOut, value.style);
1318
+ // Apply function variants — entries in `variants` whose value is a
1319
+ // function. They run after static variants and override any same-key
1320
+ // inherited variant via `staticExtSkipKeys`.
1321
+ for (let i = 0; i < functionVariantCount; i++) {
1322
+ const variantName = functionVariantNames[i];
1323
+ if (ownSkipKeys && ownSkipKeys.has(variantName)) continue;
1324
+ const selectedValue = workingResolved[variantName];
1325
+ if (selectedValue === undefined) continue;
1326
+ if (!hasOwn(workingResolved, variantName)) continue;
1327
+ const selectedKey = getVariantValueKey(selectedValue);
1328
+ if (
1329
+ ownSkipValues &&
1330
+ selectedKey != null &&
1331
+ getOwn(ownSkipValues, variantName)?.has(selectedKey)
1332
+ ) {
1333
+ continue;
1334
+ }
1335
+ const fn = functionVariantFns[i];
1336
+ const computedResult = fn(selectedValue);
1337
+ if (computedResult == null) continue;
1338
+ const r = extractClassAndStylePrebuilt(computedResult);
1339
+ if (r.class != null) {
1340
+ classesOut.push(r.class);
1341
+ }
1342
+ if (r.style) {
1343
+ Object.assign(styleOut, r.style);
1344
+ }
1237
1345
  }
1238
- }
1239
1346
 
1240
- // Apply function variants — entries in `variants` whose value is a
1241
- // function. They run after static variants and override any same-key
1242
- // inherited variant via `staticExtSkipKeys`.
1243
- for (let i = 0; i < functionVariantCount; i++) {
1244
- const variantName = functionVariantNames[i];
1245
- if (ownSkipKeys && ownSkipKeys.has(variantName)) continue;
1246
- const selectedValue = workingResolved[variantName];
1247
- if (selectedValue === undefined) continue;
1248
- const selectedKey = getVariantValueKey(selectedValue);
1249
- if (
1250
- ownSkipValues &&
1251
- selectedKey != null &&
1252
- ownSkipValues[variantName]?.has(selectedKey)
1253
- ) {
1254
- continue;
1255
- }
1256
- const fn = functionVariantFns[i];
1257
- const computedResult = fn(selectedValue);
1258
- if (computedResult == null) continue;
1259
- const r = extractClassAndStylePrebuilt(computedResult);
1260
- if (r.class != null) {
1261
- classesOut.push(r.class);
1347
+ // Apply `refine` results — must come after own variants (static and
1348
+ // function).
1349
+ if (cClasses) {
1350
+ for (let i = 0; i < cClasses.length; i++) {
1351
+ classesOut.push(cClasses[i] as ClsxClassValue);
1352
+ }
1262
1353
  }
1263
- if (r.style) {
1264
- Object.assign(styleOut, r.style);
1354
+ if (cStyle) {
1355
+ Object.assign(styleOut, cStyle);
1265
1356
  }
1266
- }
1267
-
1268
- // Apply `refine` results — must come after own variants (static and
1269
- // function).
1270
- if (cClasses) {
1271
- for (let i = 0; i < cClasses.length; i++) {
1272
- classesOut.push(cClasses[i] as ClsxClassValue);
1273
- }
1274
- }
1275
- if (cStyle) {
1276
- Object.assign(styleOut, cStyle);
1277
- }
1278
1357
 
1279
- return workingResolved;
1280
- };
1358
+ return workingResolved;
1359
+ };
1281
1360
 
1282
- const compute: ComputeFn =
1283
- !refine && computedDefaultCount === 0 && extMetasWithRefineCount === 0
1284
- ? computeOnce
1285
- : (
1286
- resolved,
1287
- userVariantProps,
1288
- skipKeys,
1289
- skipValues,
1290
- classesOut,
1291
- styleOut,
1292
- ) => {
1293
- let remaining = MAX_REFINE_RUNS;
1294
- const protectedVariants: Record<string, unknown> = {};
1295
- const protectedVariantKeys = new Set<string>();
1296
- const incomingDefaultResolved = resolved;
1297
- let workingResolved = resolved;
1298
- // Latest variant changes from non-converging iterations inside the
1299
- // tracking window. Lazy-init keeps convergent loops allocation-free.
1300
- let unstableChanges: Map<string, VariantChange> | null = null;
1301
- let lastClasses: ClsxClassValue[] = [];
1302
- let lastStyle: StyleValue = {};
1303
- let isFirstRun = true;
1304
-
1305
- while (remaining > 0) {
1306
- remaining -= 1;
1307
- const useDirectOutput = isFirstRun;
1308
- const classCount = classesOut.length;
1309
- const nextClasses: ClsxClassValue[] = useDirectOutput
1310
- ? classesOut
1311
- : [];
1312
- const nextStyle: StyleValue = useDirectOutput ? styleOut : {};
1313
- const defaultResolved = mergeProtectedIntoBase(
1314
- incomingDefaultResolved,
1315
- protectedVariants,
1316
- protectedVariantKeys,
1317
- );
1318
- const nextResolved = computeOnce(
1319
- workingResolved,
1320
- userVariantProps,
1321
- skipKeys,
1322
- skipValues,
1323
- nextClasses,
1324
- nextStyle,
1325
- protectedVariants,
1326
- protectedVariantKeys,
1327
- defaultResolved,
1328
- );
1361
+ const compute: ComputeFn =
1362
+ !refine && computedDefaultCount === 0 && extMetasWithRefineCount === 0
1363
+ ? computeOnce
1364
+ : (
1365
+ resolved,
1366
+ userVariantProps,
1367
+ skipKeys,
1368
+ skipValues,
1369
+ classesOut,
1370
+ styleOut,
1371
+ ) => {
1372
+ let remaining = MAX_REFINE_RUNS;
1373
+ const protection: RefineProtection | null = canProtectVariants
1374
+ ? { variants: null }
1375
+ : null;
1376
+ const incomingDefaultResolved = resolved;
1377
+ let workingResolved = resolved;
1378
+ // Latest variant changes from non-converging iterations inside the
1379
+ // tracking window. Lazy-init keeps convergent loops allocation-free.
1380
+ let unstableChanges: Map<string, VariantChange> | null = null;
1381
+ let lastClasses: ClsxClassValue[] = [];
1382
+ let lastStyle: StyleValue = {};
1383
+ let isFirstRun = true;
1384
+
1385
+ while (remaining > 0) {
1386
+ remaining -= 1;
1387
+ const useDirectOutput = isFirstRun;
1388
+ const classCount = classesOut.length;
1389
+ const nextClasses: ClsxClassValue[] = useDirectOutput
1390
+ ? classesOut
1391
+ : [];
1392
+ const nextStyle: StyleValue = useDirectOutput ? styleOut : {};
1393
+ const defaultResolved = mergeProtectedIntoBase(
1394
+ incomingDefaultResolved,
1395
+ protection,
1396
+ );
1397
+ const nextResolved = computeOnce(
1398
+ workingResolved,
1399
+ userVariantProps,
1400
+ skipKeys,
1401
+ skipValues,
1402
+ nextClasses,
1403
+ nextStyle,
1404
+ protection,
1405
+ defaultResolved,
1406
+ );
1329
1407
 
1330
- if (
1331
- nextResolved === workingResolved ||
1332
- areVariantsEqual(workingResolved, nextResolved)
1333
- ) {
1334
- if (nextResolved !== workingResolved) {
1335
- if (useDirectOutput) {
1336
- classesOut.length = classCount;
1337
- for (const key in styleOut) {
1338
- if (hasOwn(styleOut, key)) {
1339
- Reflect.deleteProperty(styleOut, key);
1408
+ if (
1409
+ nextResolved === workingResolved ||
1410
+ areVariantsEqual(workingResolved, nextResolved)
1411
+ ) {
1412
+ if (nextResolved !== workingResolved) {
1413
+ if (useDirectOutput) {
1414
+ classesOut.length = classCount;
1415
+ for (const key in styleOut) {
1416
+ if (hasOwn(styleOut, key)) {
1417
+ Reflect.deleteProperty(styleOut, key);
1418
+ }
1340
1419
  }
1341
1420
  }
1421
+ computeOnce(
1422
+ nextResolved,
1423
+ userVariantProps,
1424
+ skipKeys,
1425
+ skipValues,
1426
+ classesOut,
1427
+ styleOut,
1428
+ protection,
1429
+ defaultResolved,
1430
+ true,
1431
+ );
1432
+ } else if (!useDirectOutput) {
1433
+ for (let i = 0; i < nextClasses.length; i++) {
1434
+ classesOut.push(nextClasses[i]);
1435
+ }
1436
+ Object.assign(styleOut, nextStyle);
1342
1437
  }
1343
- computeOnce(
1438
+ return nextResolved;
1439
+ }
1440
+
1441
+ // `remaining` is checked first: `process.env.NODE_ENV` is an
1442
+ // environment lookup in Node, not a property load, and only the
1443
+ // last `REFINE_UNSTABLE_TRACKING_WINDOW` passes need it. Bundlers
1444
+ // still inline it and drop the block.
1445
+ if (
1446
+ remaining < REFINE_UNSTABLE_TRACKING_WINDOW &&
1447
+ process.env.NODE_ENV !== "production"
1448
+ ) {
1449
+ if (!unstableChanges) {
1450
+ unstableChanges = new Map<string, VariantChange>();
1451
+ }
1452
+ accumulateUnstableVariantChanges(
1453
+ unstableChanges,
1454
+ workingResolved,
1344
1455
  nextResolved,
1345
- userVariantProps,
1346
- skipKeys,
1347
- skipValues,
1348
- classesOut,
1349
- styleOut,
1350
- protectedVariants,
1351
- protectedVariantKeys,
1352
- defaultResolved,
1353
- true,
1354
1456
  );
1355
- } else if (!useDirectOutput) {
1356
- for (let i = 0; i < nextClasses.length; i++) {
1357
- classesOut.push(nextClasses[i]);
1457
+ }
1458
+
1459
+ if (useDirectOutput) {
1460
+ classesOut.length = classCount;
1461
+ for (const key in styleOut) {
1462
+ if (hasOwn(styleOut, key)) {
1463
+ Reflect.deleteProperty(styleOut, key);
1464
+ }
1358
1465
  }
1359
- Object.assign(styleOut, nextStyle);
1466
+ } else {
1467
+ lastClasses = nextClasses;
1468
+ lastStyle = nextStyle;
1360
1469
  }
1361
- return nextResolved;
1470
+
1471
+ workingResolved = nextResolved;
1472
+ isFirstRun = false;
1362
1473
  }
1363
1474
 
1364
- if (
1365
- process.env.NODE_ENV !== "production" &&
1366
- remaining < REFINE_UNSTABLE_TRACKING_WINDOW
1367
- ) {
1368
- if (!unstableChanges) {
1369
- unstableChanges = new Map<string, VariantChange>();
1370
- }
1371
- accumulateUnstableVariantChanges(
1475
+ if (process.env.NODE_ENV !== "production") {
1476
+ warnRefineLimit({
1477
+ creationFrame,
1372
1478
  unstableChanges,
1373
- workingResolved,
1374
- nextResolved,
1375
- );
1479
+ });
1376
1480
  }
1377
1481
 
1378
- if (useDirectOutput) {
1379
- classesOut.length = classCount;
1380
- for (const key in styleOut) {
1381
- if (hasOwn(styleOut, key)) {
1382
- Reflect.deleteProperty(styleOut, key);
1383
- }
1384
- }
1385
- } else {
1386
- lastClasses = nextClasses;
1387
- lastStyle = nextStyle;
1482
+ for (let i = 0; i < lastClasses.length; i++) {
1483
+ classesOut.push(lastClasses[i]);
1388
1484
  }
1485
+ Object.assign(styleOut, lastStyle);
1486
+ return workingResolved;
1487
+ };
1488
+
1489
+ const resolveRefineOnce: ResolveRefineOnceFn = (
1490
+ resolved,
1491
+ userVariantProps,
1492
+ filterOwnVariants = true,
1493
+ protection,
1494
+ defaultResolved = resolved,
1495
+ ) => {
1496
+ let workingResolved = resolved;
1389
1497
 
1390
- workingResolved = nextResolved;
1391
- isFirstRun = false;
1392
- }
1393
-
1394
- if (process.env.NODE_ENV !== "production") {
1395
- warnRefineLimit({
1396
- creationFrame,
1397
- unstableChanges,
1398
- });
1399
- }
1400
-
1401
- for (let i = 0; i < lastClasses.length; i++) {
1402
- classesOut.push(lastClasses[i]);
1403
- }
1404
- Object.assign(styleOut, lastStyle);
1405
- return workingResolved;
1406
- };
1407
-
1408
- const resolveRefineOnce: ResolveRefineOnceFn = (
1409
- resolved,
1410
- userVariantProps,
1411
- filterOwnVariants = true,
1412
- protectedVariants,
1413
- protectedVariantKeys,
1414
- defaultResolved = resolved,
1415
- ) => {
1416
- let workingResolved = resolved;
1417
-
1418
- if (extMetasWithRefineCount > 0) {
1419
- const extUserVariantProps = getExtUserVariantProps(
1420
- userVariantProps,
1421
- protectedVariants ?? null,
1422
- );
1423
1498
  for (let i = 0; i < extMetasWithRefineCount; i++) {
1424
1499
  const meta = extMetasWithRefine[i];
1425
1500
  const resolveRefine = meta.resolveRefine;
1426
1501
  if (!resolveRefine) continue;
1427
1502
  workingResolved = resolveRefine(
1428
1503
  workingResolved,
1429
- extUserVariantProps,
1504
+ userVariantProps,
1430
1505
  true,
1431
- protectedVariants,
1432
- protectedVariantKeys,
1506
+ protection,
1433
1507
  defaultResolved,
1434
1508
  );
1435
1509
  workingResolved = filterOwnDisabledVariants(
1436
1510
  workingResolved,
1437
1511
  defaultResolved,
1438
1512
  );
1439
- if (protectedVariants) {
1440
- Object.assign(extUserVariantProps, protectedVariants);
1441
- }
1442
1513
  }
1443
- }
1444
1514
 
1445
- if (computedDefaultCount > 0) {
1446
- workingResolved = runComputedDefaults(
1447
- workingResolved,
1448
- defaultResolved,
1449
- userVariantProps,
1450
- filterOwnVariants,
1451
- protectedVariantKeys,
1452
- );
1453
- }
1454
- if (refine) {
1455
- const refineResult = runRefineContext(
1456
- workingResolved,
1457
- filterOwnVariants,
1458
- false,
1459
- true,
1460
- protectedVariants,
1461
- protectedVariantKeys,
1462
- );
1463
- workingResolved = refineResult.workingResolved;
1464
- }
1515
+ if (computedDefaultCount > 0) {
1516
+ workingResolved = runComputedDefaults(
1517
+ workingResolved,
1518
+ defaultResolved,
1519
+ userVariantProps,
1520
+ filterOwnVariants,
1521
+ protection,
1522
+ );
1523
+ }
1524
+ if (refine) {
1525
+ const refineResult = runRefineContext(
1526
+ workingResolved,
1527
+ filterOwnVariants,
1528
+ false,
1529
+ true,
1530
+ protection,
1531
+ );
1532
+ workingResolved = refineResult.workingResolved;
1533
+ }
1465
1534
 
1466
- return workingResolved;
1467
- };
1535
+ return workingResolved;
1536
+ };
1468
1537
 
1469
- const resolveRefine: ResolveRefineFn | null =
1470
- refine || computedDefaultCount > 0 || extMetasWithRefineCount > 0
1471
- ? (resolved, userVariantProps, filterOwnVariants = true) => {
1472
- let remaining = MAX_REFINE_RUNS;
1473
- const protectedVariants: Record<string, unknown> = {};
1474
- const protectedVariantKeys = new Set<string>();
1475
- const incomingDefaultResolved = resolved;
1476
- let workingResolved = resolved;
1477
- // Latest variant changes from non-converging iterations inside the
1478
- // tracking window. See the compute loop above for the shared
1479
- // rationale.
1480
- let unstableChanges: Map<string, VariantChange> | null = null;
1481
-
1482
- while (remaining > 0) {
1483
- remaining -= 1;
1484
- const defaultResolved = mergeProtectedIntoBase(
1485
- incomingDefaultResolved,
1486
- protectedVariants,
1487
- protectedVariantKeys,
1488
- );
1489
- const nextResolved = resolveRefineOnce(
1490
- workingResolved,
1491
- userVariantProps,
1492
- filterOwnVariants,
1493
- protectedVariants,
1494
- protectedVariantKeys,
1495
- defaultResolved,
1496
- );
1538
+ const resolveRefine: ResolveRefineFn | null =
1539
+ refine || computedDefaultCount > 0 || extMetasWithRefineCount > 0
1540
+ ? (resolved, userVariantProps, filterOwnVariants = true) => {
1541
+ let remaining = MAX_REFINE_RUNS;
1542
+ const protection: RefineProtection | null = canProtectVariants
1543
+ ? { variants: null }
1544
+ : null;
1545
+ const incomingDefaultResolved = resolved;
1546
+ let workingResolved = resolved;
1547
+ // Latest variant changes from non-converging iterations inside the
1548
+ // tracking window. See the compute loop above for the shared
1549
+ // rationale.
1550
+ let unstableChanges: Map<string, VariantChange> | null = null;
1551
+
1552
+ while (remaining > 0) {
1553
+ remaining -= 1;
1554
+ const defaultResolved = mergeProtectedIntoBase(
1555
+ incomingDefaultResolved,
1556
+ protection,
1557
+ );
1558
+ const nextResolved = resolveRefineOnce(
1559
+ workingResolved,
1560
+ userVariantProps,
1561
+ filterOwnVariants,
1562
+ protection,
1563
+ defaultResolved,
1564
+ );
1497
1565
 
1498
- if (
1499
- nextResolved === workingResolved ||
1500
- areVariantsEqual(workingResolved, nextResolved)
1501
- ) {
1502
- return nextResolved;
1503
- }
1566
+ if (
1567
+ nextResolved === workingResolved ||
1568
+ areVariantsEqual(workingResolved, nextResolved)
1569
+ ) {
1570
+ return nextResolved;
1571
+ }
1504
1572
 
1505
- if (
1506
- process.env.NODE_ENV !== "production" &&
1507
- remaining < REFINE_UNSTABLE_TRACKING_WINDOW
1508
- ) {
1509
- if (!unstableChanges) {
1510
- unstableChanges = new Map<string, VariantChange>();
1573
+ // `remaining` is checked first for the reason given in the
1574
+ // compute loop above.
1575
+ if (
1576
+ remaining < REFINE_UNSTABLE_TRACKING_WINDOW &&
1577
+ process.env.NODE_ENV !== "production"
1578
+ ) {
1579
+ if (!unstableChanges) {
1580
+ unstableChanges = new Map<string, VariantChange>();
1581
+ }
1582
+ accumulateUnstableVariantChanges(
1583
+ unstableChanges,
1584
+ workingResolved,
1585
+ nextResolved,
1586
+ );
1511
1587
  }
1512
- accumulateUnstableVariantChanges(
1588
+ workingResolved = nextResolved;
1589
+ }
1590
+
1591
+ if (process.env.NODE_ENV !== "production") {
1592
+ warnRefineLimit({
1593
+ creationFrame,
1513
1594
  unstableChanges,
1514
- workingResolved,
1515
- nextResolved,
1516
- );
1595
+ });
1517
1596
  }
1518
- workingResolved = nextResolved;
1519
- }
1520
1597
 
1521
- if (process.env.NODE_ENV !== "production") {
1522
- warnRefineLimit({
1523
- creationFrame,
1524
- unstableChanges,
1525
- });
1598
+ return workingResolved;
1526
1599
  }
1600
+ : null;
1527
1601
 
1528
- return workingResolved;
1529
- }
1530
- : null;
1531
-
1532
- // Top-level: resolves variants from user props, calls compute, then
1533
- // assembles className and style with user-provided class/style overrides.
1534
- const computeResult = (
1535
- props: ComponentProps<MergedVariants> = EMPTY_DEFAULTS as ComponentProps<MergedVariants>,
1536
- ): { className: string; style: StyleValue } => {
1537
- const propsRecord = props as Record<string, unknown>;
1602
+ // Top-level: resolves variants from user props, calls compute, then
1603
+ // assembles className and style with user-provided class/style overrides.
1604
+ const computeResult = (
1605
+ props: ComponentProps<MergedVariants> = EMPTY_DEFAULTS as ComponentProps<MergedVariants>,
1606
+ ): { className: string; style: StyleValue } => {
1607
+ const propsRecord = props as Record<string, unknown>;
1538
1608
 
1539
- let resolved: Record<string, unknown> = {};
1540
- Object.assign(resolved, staticDefaults);
1609
+ let resolved: Record<string, unknown> = {};
1610
+ Object.assign(resolved, staticDefaults);
1541
1611
 
1542
- let userVariantProps: Record<string, unknown>;
1543
- if (refine || computedDefaultCount > 0 || extMetasWithRefineCount > 0) {
1544
- const variantProps: Record<string, unknown> = {};
1545
- for (let i = 0; i < variantKeysLength; i++) {
1546
- const key = variantKeys[i];
1547
- if (hasOwn(propsRecord, key)) {
1548
- variantProps[key] = propsRecord[key];
1612
+ let userVariantProps: Record<string, unknown>;
1613
+ if (needsUserVariantProps) {
1614
+ const variantProps: Record<string, unknown> = {};
1615
+ for (let i = 0; i < variantKeysLength; i++) {
1616
+ const key = variantKeys[i];
1617
+ if (!hasOwn(propsRecord, key)) continue;
1618
+ const value = propsRecord[key];
1619
+ variantProps[key] = value;
1620
+ if (value === undefined) continue;
1621
+ resolved[key] = value;
1549
1622
  }
1623
+ userVariantProps = variantProps;
1624
+ } else {
1625
+ // Fast path: walk variantKeys directly against propsRecord.
1626
+ // Own-property checks ensure a polluted Object.prototype can't add
1627
+ // values the user didn't pass.
1628
+ for (let i = 0; i < variantKeysLength; i++) {
1629
+ const key = variantKeys[i];
1630
+ if (!hasOwn(propsRecord, key)) continue;
1631
+ const v = propsRecord[key];
1632
+ if (v === undefined) continue;
1633
+ resolved[key] = v;
1634
+ }
1635
+ userVariantProps = propsRecord;
1550
1636
  }
1551
- for (const k in variantProps) {
1552
- if (!hasOwn(variantProps, k)) continue;
1553
- const v = variantProps[k];
1554
- if (v === undefined) continue;
1555
- resolved[k] = v;
1556
- }
1557
- userVariantProps = variantProps;
1558
- } else {
1559
- // Fast path: walk variantKeys directly against propsRecord.
1560
- // Own-property checks ensure a polluted Object.prototype can't add
1561
- // values the user didn't pass.
1562
- for (let i = 0; i < variantKeysLength; i++) {
1563
- const key = variantKeys[i];
1564
- if (!hasOwn(propsRecord, key)) continue;
1565
- const v = propsRecord[key];
1566
- if (v === undefined) continue;
1567
- resolved[key] = v;
1568
- }
1569
- userVariantProps = propsRecord;
1570
- }
1571
-
1572
- if (hasAnyDisabled) {
1573
- const filtered: Record<string, unknown> = {};
1574
- filterDisabledInto(resolved, filtered);
1575
- resolved = filtered;
1576
- }
1577
1637
 
1578
- // Build allClasses directly. computedBaseClass already has all extend
1579
- // bases joined with config.class — `compute` only adds variant classes
1580
- // on top.
1581
- const allClasses: ClsxClassValue[] = [computedBaseClass];
1582
- const style: StyleValue = {};
1583
- compute(resolved, userVariantProps, null, null, allClasses, style);
1638
+ if (hasAnyDisabled) {
1639
+ const filtered: Record<string, unknown> = {};
1640
+ filterDisabledInto(resolved, filtered);
1641
+ resolved = filtered;
1642
+ }
1584
1643
 
1585
- // Apply user-provided class / className.
1586
- if ("class" in propsRecord) {
1587
- allClasses.push(propsRecord.class as ClsxClassValue);
1588
- }
1589
- if ("className" in propsRecord) {
1590
- allClasses.push(propsRecord.className as ClsxClassValue);
1591
- }
1644
+ // Build allClasses directly. computedBaseClass already has all extend
1645
+ // bases joined with config.class — `compute` only adds variant classes
1646
+ // on top.
1647
+ const allClasses: ClsxClassValue[] = [computedBaseClass];
1648
+ const style: StyleValue = {};
1649
+ compute(resolved, userVariantProps, null, null, allClasses, style);
1650
+
1651
+ // Apply user-provided class / className. Own-property checks keep these
1652
+ // consistent with the variant props read above: only what the caller
1653
+ // passed is applied, never a key inherited from Object.prototype.
1654
+ if (hasOwn(propsRecord, "class")) {
1655
+ allClasses.push(propsRecord.class as ClsxClassValue);
1656
+ }
1657
+ if (hasOwn(propsRecord, "className")) {
1658
+ allClasses.push(propsRecord.className as ClsxClassValue);
1659
+ }
1592
1660
 
1593
- // Apply user-provided style.
1594
- const psv = propsRecord.style;
1595
- if (psv != null) {
1596
- if (typeof psv === "string") {
1597
- if (psv.length > 0) {
1598
- htmlStyleToStyleValue(psv, style);
1661
+ // Apply user-provided style, read before the own-property check for the
1662
+ // same reason as the variant loops above.
1663
+ const psv = propsRecord.style;
1664
+ if (psv != null && hasOwn(propsRecord, "style")) {
1665
+ if (typeof psv === "string") {
1666
+ if (psv.length > 0) {
1667
+ htmlStyleToStyleValue(psv, style);
1668
+ }
1669
+ } else if (typeof psv === "object") {
1670
+ htmlObjStyleToStyleValue(psv, style);
1599
1671
  }
1600
- } else if (typeof psv === "object") {
1601
- htmlObjStyleToStyleValue(psv, style);
1602
1672
  }
1603
- }
1604
1673
 
1605
- return {
1606
- className: transformClass(clsx(allClasses)),
1607
- style,
1674
+ return {
1675
+ className: transformClass(clsx(allClasses)),
1676
+ style,
1677
+ };
1608
1678
  };
1609
- };
1610
1679
 
1611
- const getVariants = (variants?: VariantValues<MergedVariants>) => {
1612
- const variantsRecord = variants ?? EMPTY_DEFAULTS;
1613
- let variantProps = variantsRecord;
1614
- // Extended refinement copies user props, so filter before unknown
1615
- // accessors or prototype keys can be observed by Object.assign.
1616
- if (variants && extMetasWithRefineCount > 0) {
1617
- variantProps = {};
1618
- for (let i = 0; i < variantKeysLength; i++) {
1619
- const key = variantKeys[i];
1620
- if (hasOwn(variantsRecord, key)) {
1621
- variantProps[key] = variantsRecord[key];
1680
+ const getVariants = (variants?: VariantValues<MergedVariants>) => {
1681
+ const variantsRecord = variants ?? EMPTY_DEFAULTS;
1682
+ let variantProps = variantsRecord;
1683
+ // Copy to the declared variant keys, so the computed defaults that read
1684
+ // this record cannot see an undeclared key as an explicitly passed
1685
+ // variant, and cannot re-run a caller accessor once per refine pass. The
1686
+ // gate is narrower than that purpose: it misses a chain whose only
1687
+ // computed default is this component's own.
1688
+ // See https://github.com/ariakit/clava/issues/494
1689
+ if (variants && extMetasWithRefineCount > 0) {
1690
+ variantProps = {};
1691
+ for (let i = 0; i < variantKeysLength; i++) {
1692
+ const key = variantKeys[i];
1693
+ if (hasOwn(variantsRecord, key)) {
1694
+ variantProps[key] = variantsRecord[key];
1695
+ }
1622
1696
  }
1623
1697
  }
1624
- }
1625
- let resolvedVariants = resolveVariantsHot(variantProps);
1626
- if (resolveRefine) {
1627
- resolvedVariants = resolveRefine(resolvedVariants, variantProps, false);
1628
- }
1629
- return resolvedVariants as VariantValues<MergedVariants>;
1630
- };
1698
+ let resolvedVariants = resolveVariantsHot(variantProps);
1699
+ if (resolveRefine) {
1700
+ resolvedVariants = resolveRefine(
1701
+ resolvedVariants,
1702
+ variantProps,
1703
+ false,
1704
+ );
1705
+ }
1706
+ return resolvedVariants as VariantValues<MergedVariants>;
1707
+ };
1631
1708
 
1632
- // Compute base class (without variants) — includes extended base classes.
1633
- // Plain `clsx` (no `transformClass`): `meta.baseClass` flows back into
1634
- // parent extends as `clsx` input and then through the single
1635
- // `transformClass(clsx(allClasses))` at render time, so applying it here
1636
- // would compound (double for own-render, triple+ for extend chains) and
1637
- // misbehave for non-idempotent transforms.
1638
- const computedBaseClass = hasExtend
1639
- ? clsx(extBaseClassesArr, config.class as ClsxClassValue)
1640
- : clsx(config.class as ClsxClassValue);
1641
-
1642
- // Shared closures across the default and modal components.
1643
- const classFn = (props: ComponentProps<MergedVariants> = {}) => {
1644
- return computeResult(props).className;
1645
- };
1646
- const meta: ComponentMeta = {
1647
- baseClass: computedBaseClass,
1648
- staticDefaults,
1649
- compute: computeOnce,
1650
- resolveRefine: resolveRefine ? resolveRefineOnce : null,
1651
- transformClass,
1652
- functionVariantKeys,
1653
- computedDefaultKeys,
1654
- };
1709
+ // Compute base class (without variants) — includes extended base classes.
1710
+ // Plain `clsx` (no `transformClass`): `meta.baseClass` flows back into
1711
+ // parent extends as `clsx` input and then through the single
1712
+ // `transformClass(clsx(allClasses))` at render time, so applying it here
1713
+ // would compound (double for own-render, triple+ for extend chains) and
1714
+ // misbehave for non-idempotent transforms.
1715
+ const computedBaseClass = hasExtend
1716
+ ? clsx(extBaseClassesArr, baseClass)
1717
+ : baseClass;
1718
+
1719
+ // Shared closures across the default and modal components.
1720
+ const classFn = (props: ComponentProps<MergedVariants> = {}) => {
1721
+ return computeResult(props).className;
1722
+ };
1723
+ const meta: ComponentMeta = {
1724
+ identity,
1725
+ extends: extMetas,
1726
+ rebuild: (extensions) => build(extensions).meta,
1727
+ baseClass: computedBaseClass,
1728
+ staticDefaults,
1729
+ compute: computeOnce,
1730
+ resolveRefine: resolveRefine ? resolveRefineOnce : null,
1731
+ transformClass,
1732
+ functionVariantKeys,
1733
+ computedDefaultKeys,
1734
+ };
1655
1735
 
1656
- const initComponent = <
1657
- R extends ComponentResult,
1658
- T extends ModalComponent<MergedVariants, R>,
1659
- >(
1660
- c: T,
1661
- propKeys: string[],
1662
- style: T["style"],
1663
- ): T => {
1664
- c.class = classFn;
1665
- c.style = style;
1666
- c.getVariants = getVariants;
1667
- c.variantKeys = variantKeys;
1668
- c.propKeys = propKeys;
1669
- (c as AnyComponent & ComponentWithMeta)[META_KEY] = meta;
1670
- return c;
1671
- };
1736
+ const initComponent = <
1737
+ R extends ComponentResult,
1738
+ T extends ModalComponent<MergedVariants, R>,
1739
+ >(
1740
+ c: T,
1741
+ propKeys: string[],
1742
+ style: T["style"],
1743
+ ): T => {
1744
+ c.class = classFn;
1745
+ c.style = style;
1746
+ c.getVariants = getVariants;
1747
+ c.variantKeys = variantKeys;
1748
+ c.propKeys = propKeys;
1749
+ (c as AnyComponent & ComponentWithMeta)[META_KEY] = meta;
1750
+ return c;
1751
+ };
1672
1752
 
1673
- // Default component
1674
- const defaultComponent = ((props: ComponentProps<MergedVariants> = {}) => {
1675
- const { className, style } = computeResult(props);
1676
- return { class: className, style };
1677
- }) as CVComponent<V, E>;
1678
- initComponent(defaultComponent, inputPropsKeys, (props = {}) => {
1679
- return computeResult(props).style;
1680
- });
1681
-
1682
- // JSX component
1683
- const jsxComponent = ((props: ComponentProps<MergedVariants> = {}) => {
1684
- const { className, style } = computeResult(props);
1685
- return { className, style };
1686
- }) as ModalComponent<MergedVariants, JSXProps>;
1687
- initComponent(
1688
- jsxComponent,
1689
- ["className", "style", ...variantKeys],
1690
- (props = {}) => computeResult(props).style,
1691
- );
1692
-
1693
- // HTML component
1694
- const htmlComponent = ((props: ComponentProps<MergedVariants> = {}) => {
1695
- const { className, style } = computeResult(props);
1696
- return { class: className, style: styleValueToHTMLStyle(style) };
1697
- }) as ModalComponent<MergedVariants, HTMLProps>;
1698
- initComponent(
1699
- htmlComponent,
1700
- ["class", "style", ...variantKeys],
1701
- (props = {}) => styleValueToHTMLStyle(computeResult(props).style),
1702
- );
1703
-
1704
- // HTMLObj component
1705
- const htmlObjComponent = ((props: ComponentProps<MergedVariants> = {}) => {
1706
- const { className, style } = computeResult(props);
1707
- return { class: className, style: styleValueToHTMLObjStyle(style) };
1708
- }) as ModalComponent<MergedVariants, HTMLObjProps>;
1709
- initComponent(
1710
- htmlObjComponent,
1711
- ["class", "style", ...variantKeys],
1712
- (props = {}) => styleValueToHTMLObjStyle(computeResult(props).style),
1713
- );
1714
-
1715
- defaultComponent.jsx = jsxComponent;
1716
- defaultComponent.html = htmlComponent;
1717
- defaultComponent.htmlObj = htmlObjComponent;
1718
-
1719
- return defaultComponent;
1753
+ // Default component
1754
+ const defaultComponent = ((
1755
+ props: ComponentProps<MergedVariants> = {},
1756
+ ) => {
1757
+ const { className, style } = computeResult(props);
1758
+ return { class: className, style };
1759
+ }) as CVComponent<V, E>;
1760
+ initComponent(defaultComponent, inputPropsKeys, (props = {}) => {
1761
+ return computeResult(props).style;
1762
+ });
1763
+
1764
+ // JSX component
1765
+ const jsxComponent = ((props: ComponentProps<MergedVariants> = {}) => {
1766
+ const { className, style } = computeResult(props);
1767
+ return { className, style };
1768
+ }) as ModalComponent<MergedVariants, JSXProps>;
1769
+ initComponent(
1770
+ jsxComponent,
1771
+ ["className", "style", ...variantKeys],
1772
+ (props = {}) => computeResult(props).style,
1773
+ );
1774
+
1775
+ // HTML component
1776
+ const htmlComponent = ((props: ComponentProps<MergedVariants> = {}) => {
1777
+ const { className, style } = computeResult(props);
1778
+ return { class: className, style: styleValueToHTMLStyle(style) };
1779
+ }) as ModalComponent<MergedVariants, HTMLProps>;
1780
+ initComponent(
1781
+ htmlComponent,
1782
+ ["class", "style", ...variantKeys],
1783
+ (props = {}) => styleValueToHTMLStyle(computeResult(props).style),
1784
+ );
1785
+
1786
+ // HTMLObj component
1787
+ const htmlObjComponent = ((
1788
+ props: ComponentProps<MergedVariants> = {},
1789
+ ) => {
1790
+ const { className, style } = computeResult(props);
1791
+ return { class: className, style: styleValueToHTMLObjStyle(style) };
1792
+ }) as ModalComponent<MergedVariants, HTMLObjProps>;
1793
+ initComponent(
1794
+ htmlObjComponent,
1795
+ ["class", "style", ...variantKeys],
1796
+ (props = {}) => styleValueToHTMLObjStyle(computeResult(props).style),
1797
+ );
1798
+
1799
+ defaultComponent.jsx = jsxComponent;
1800
+ defaultComponent.html = htmlComponent;
1801
+ defaultComponent.htmlObj = htmlObjComponent;
1802
+
1803
+ return { component: defaultComponent, meta };
1804
+ };
1805
+ return build(initialExtensions);
1720
1806
  };
1721
1807
 
1808
+ const cv = <V extends Variants = {}, const E extends AnyComponent[] = []>(
1809
+ config: CVConfig<V, E> = {},
1810
+ ): CVComponent<V, E> => buildComponent(config).component;
1811
+
1722
1812
  return { cv, cx };
1723
1813
  }
1724
1814