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