clava 0.6.1 → 0.6.3

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
 
@@ -122,30 +121,40 @@ function areVariantsEqual(
122
121
  a: Record<string, unknown>,
123
122
  b: Record<string, unknown>,
124
123
  ): boolean {
124
+ // The first loop proves every own key of `a` is an own key of `b` with the
125
+ // same value, so counting both sides is enough to prove the key sets match.
126
+ // Counting keeps the total number of lookups the same as before the own-key
127
+ // check was added to the first loop.
128
+ let ownKeyDifference = 0;
125
129
  for (const key in a) {
126
130
  if (!hasOwn(a, key)) continue;
131
+ if (!hasOwn(b, key)) return false;
127
132
  if (!Object.is(a[key], b[key])) return false;
133
+ ownKeyDifference++;
128
134
  }
129
135
  for (const key in b) {
130
136
  if (!hasOwn(b, key)) continue;
131
- if (!hasOwn(a, key)) return false;
137
+ ownKeyDifference--;
132
138
  }
133
- return true;
139
+ return ownKeyDifference === 0;
134
140
  }
135
141
 
136
- function getExtUserVariantProps(
137
- userVariantProps: Record<string, unknown>,
138
- protectedVariants: Record<string, unknown> | null,
139
- ): Record<string, unknown> {
140
- return Object.assign({}, userVariantProps, protectedVariants);
142
+ // Variants that a `refine` callback assigned through `setVariants`. Two stages
143
+ // read them: computed defaults skip a key that was assigned this way, and
144
+ // `mergeProtectedIntoBase` folds them into the base each pass resolves from.
145
+ // The record is created on the first `setVariants` call, because most callbacks
146
+ // only read variants. The holder is shared so that a callback anywhere in the
147
+ // chain can report its assignment back to the resolution that owns the pass.
148
+ interface RefineProtection {
149
+ variants: Record<string, unknown> | null;
141
150
  }
142
151
 
143
152
  function mergeProtectedIntoBase(
144
153
  baseResolved: Record<string, unknown>,
145
- protectedVariants: Record<string, unknown>,
146
- protectedVariantKeys: Set<string>,
154
+ protection: RefineProtection | null,
147
155
  ): Record<string, unknown> {
148
- if (protectedVariantKeys.size === 0) return baseResolved;
156
+ const protectedVariants = protection?.variants;
157
+ if (!protectedVariants) return baseResolved;
149
158
  return Object.assign({}, baseResolved, protectedVariants);
150
159
  }
151
160
 
@@ -268,6 +277,10 @@ interface CreateParams {
268
277
  transformClass?: (className: string) => string;
269
278
  }
270
279
 
280
+ function identityClass(className: string) {
281
+ return className;
282
+ }
283
+
271
284
  function isRecordObject(value: unknown): value is Record<string, unknown> {
272
285
  if (typeof value !== "object") return false;
273
286
  if (value == null) return false;
@@ -302,13 +315,19 @@ function extractClassAndStylePrebuilt(value: unknown): PrebuiltValue {
302
315
  if (!isRecordObject(value)) {
303
316
  return { class: value as ClassValue, style: null };
304
317
  }
305
- if (!("style" in value || "class" in value)) {
318
+ // A value that owns neither key contributes nothing, and a value that owns
319
+ // one of them must not pick up the other from a polluted `Object.prototype`.
320
+ // Checked inline rather than through `getOwn`: this runs for every variant
321
+ // value at creation time, where the call is worth about 3% of `cv()`.
322
+ const styleClassValue = value as StyleClassValue;
323
+ const classValue = hasOwn(value, "class") ? styleClassValue.class : undefined;
324
+ const styleValue = hasOwn(value, "style") ? styleClassValue.style : undefined;
325
+ if (classValue === undefined && styleValue === undefined) {
306
326
  return { class: null, style: null };
307
327
  }
308
- const styleClassValue = value as StyleClassValue;
309
- const style = normalizeStyle(styleClassValue.style);
328
+ const style = normalizeStyle(styleValue);
310
329
  return {
311
- class: styleClassValue.class ?? null,
330
+ class: classValue ?? null,
312
331
  style: Object.keys(style).length > 0 ? style : null,
313
332
  };
314
333
  }
@@ -448,8 +467,10 @@ export const splitProps: SplitPropsFunction = ((
448
467
  * fallback when the variant is a single class value (treated as `{ true: ... }`).
449
468
  */
450
469
  interface PrebuiltVariant {
451
- // For object variants: map of value -> prebuilt class/style
452
- values: Record<string, PrebuiltValue> | null;
470
+ // For object variants: map of value -> prebuilt class/style. A Map keeps the
471
+ // lookup keyed by the caller's variant value out of reach of a polluted
472
+ // `Object.prototype`.
473
+ values: Map<string, PrebuiltValue> | null;
453
474
  // For shorthand variants: the value to use when selectedValue is true
454
475
  shorthand: PrebuiltValue | null;
455
476
  // Set of value keys that are disabled (value === null in the original variant
@@ -465,7 +486,7 @@ function buildPrebuiltVariant(variantDef: unknown): PrebuiltVariant {
465
486
  disabledValues: null,
466
487
  };
467
488
  }
468
- const values: Record<string, PrebuiltValue> = {};
489
+ const values = new Map<string, PrebuiltValue>();
469
490
  let disabledValues: Set<string> | null = null;
470
491
  for (const key in variantDef) {
471
492
  if (!hasOwn(variantDef, key)) continue;
@@ -477,7 +498,7 @@ function buildPrebuiltVariant(variantDef: unknown): PrebuiltVariant {
477
498
  disabledValues.add(key);
478
499
  continue;
479
500
  }
480
- values[key] = extractClassAndStylePrebuilt(value);
501
+ values.set(key, extractClassAndStylePrebuilt(value));
481
502
  }
482
503
  return {
483
504
  values,
@@ -489,9 +510,12 @@ function buildPrebuiltVariant(variantDef: unknown): PrebuiltVariant {
489
510
  /**
490
511
  * Creates the cv and cx functions.
491
512
  */
492
- export function create({
493
- transformClass = (className) => className,
494
- }: CreateParams = {}) {
513
+ export function create(params: CreateParams = {}) {
514
+ // Destructuring would read an inherited `transformClass`, which a polluted
515
+ // `Object.prototype` could use to rewrite every class string this factory
516
+ // produces, including those of the package-level `cv`.
517
+ const transformClass = getOwn(params, "transformClass") ?? identityClass;
518
+
495
519
  const cx = (...classes: ClsxClassValue[]) => transformClass(clsx(classes));
496
520
 
497
521
  const cv = <V extends Variants = {}, const E extends AnyComponent[] = []>(
@@ -500,12 +524,16 @@ export function create({
500
524
  type MergedVariants = MergeVariants<V, E>;
501
525
 
502
526
  // ----- Pre-computed at creation time -----
503
- const extend = config.extend;
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.
530
+ const extend = getOwn(config, "extend");
504
531
  const hasExtend = !!extend && extend.length > 0;
505
- const variants = config.variants;
506
- const refine = config.refine;
507
- const baseStyle = config.style;
532
+ const variants = getOwn(config, "variants");
533
+ const refine = getOwn(config, "refine");
534
+ const baseStyle = getOwn(config, "style");
508
535
  const hasBaseStyle = !!baseStyle;
536
+ const baseClass = getOwn(config, "class");
509
537
 
510
538
  const variantKeySet = new Set<string>();
511
539
  const staticDefaults: Record<string, unknown> = {};
@@ -549,8 +577,7 @@ export function create({
549
577
  // shared with their prebuilt variants instead of scanning and allocating
550
578
  // them twice.
551
579
  const disabledVariantKeys = new Set<string>();
552
- const disabledVariantValues: Record<string, Set<string>> = {};
553
- let hasDisabledVariantValues = false;
580
+ let disabledValuesBuilder: Record<string, Set<string>> | null = null;
554
581
  const variantEntryNames: string[] = [];
555
582
  const variantEntryDefs: PrebuiltVariant[] = [];
556
583
  const functionVariantNames: string[] = [];
@@ -574,13 +601,12 @@ export function create({
574
601
  variantEntryNames.push(name);
575
602
  variantEntryDefs.push(prebuiltVariant);
576
603
  if (prebuiltVariant.disabledValues) {
577
- disabledVariantValues[name] = prebuiltVariant.disabledValues;
578
- hasDisabledVariantValues = true;
604
+ disabledValuesBuilder ??= {};
605
+ disabledValuesBuilder[name] = prebuiltVariant.disabledValues;
579
606
  }
580
607
  if (
581
- prebuiltVariant.values &&
582
- hasOwn(prebuiltVariant.values, "false") &&
583
- staticDefaults[name] === undefined
608
+ prebuiltVariant.values?.has("false") &&
609
+ !hasOwn(staticDefaults, name)
584
610
  ) {
585
611
  staticDefaults[name] = false;
586
612
  }
@@ -591,13 +617,18 @@ export function create({
591
617
  const variantEntryCount = variantEntryNames.length;
592
618
  const functionVariantCount = functionVariantNames.length;
593
619
  const hasDisabledVariantKeys = disabledVariantKeys.size > 0;
594
- const hasAnyDisabled = hasDisabledVariantKeys || hasDisabledVariantValues;
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
+ const disabledVariantValues = disabledValuesBuilder;
624
+ const hasAnyDisabled =
625
+ hasDisabledVariantKeys || disabledVariantValues !== null;
595
626
 
596
627
  const inputPropsKeys = ["class", "className", "style", ...variantKeys];
597
628
 
598
629
  const computedDefaultNames: string[] = [];
599
630
  const computedDefaultFns: ComputedDefaultVariantFn[] = [];
600
- const defaultVariants = config.defaultVariants as
631
+ const defaultVariants = getOwn(config, "defaultVariants") as
601
632
  | Record<string, unknown>
602
633
  | undefined;
603
634
 
@@ -634,10 +665,13 @@ export function create({
634
665
  delete staticDefaults[key];
635
666
  continue;
636
667
  }
637
- if (hasDisabledVariantValues) {
668
+ if (disabledVariantValues) {
638
669
  const value = staticDefaults[key];
639
670
  const valueKey = getVariantValueKey(value);
640
- if (valueKey != null && disabledVariantValues[key]?.has(valueKey)) {
671
+ if (
672
+ valueKey != null &&
673
+ getOwn(disabledVariantValues, key)?.has(valueKey)
674
+ ) {
641
675
  delete staticDefaults[key];
642
676
  }
643
677
  }
@@ -664,6 +698,22 @@ export function create({
664
698
  }
665
699
  const extMetasWithRefineCount = extMetasWithRefine.length;
666
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
+
667
717
  // Call-site frame captured at the `cv()` call site so refine-limit warnings
668
718
  // can point developers at the component definition. Skipped entirely for
669
719
  // components that can never enter the refine loop, and stripped in
@@ -737,8 +787,7 @@ export function create({
737
787
  }
738
788
  // Skip values are passed directly to extends. We can reuse the same object
739
789
  // when no caller-provided values need merging.
740
- const staticExtSkipValues: Record<string, Set<string>> | null =
741
- hasDisabledVariantValues ? disabledVariantValues : null;
790
+ const staticExtSkipValues = disabledVariantValues;
742
791
 
743
792
  // Callers use this only when disabled keys or values exist.
744
793
  function filterDisabledInto(
@@ -749,9 +798,12 @@ export function create({
749
798
  if (!hasOwn(input, key)) continue;
750
799
  if (disabledVariantKeys.has(key)) continue;
751
800
  const value = input[key];
752
- if (hasDisabledVariantValues) {
801
+ if (disabledVariantValues) {
753
802
  const valueKey = getVariantValueKey(value);
754
- if (valueKey != null && disabledVariantValues[key]?.has(valueKey)) {
803
+ if (
804
+ valueKey != null &&
805
+ getOwn(disabledVariantValues, key)?.has(valueKey)
806
+ ) {
755
807
  continue;
756
808
  }
757
809
  }
@@ -763,9 +815,12 @@ export function create({
763
815
  if (disabledVariantKeys.has(key)) {
764
816
  return true;
765
817
  }
766
- if (hasDisabledVariantValues) {
818
+ if (disabledVariantValues) {
767
819
  const valueKey = getVariantValueKey(value);
768
- if (valueKey != null && disabledVariantValues[key]?.has(valueKey)) {
820
+ if (
821
+ valueKey != null &&
822
+ getOwn(disabledVariantValues, key)?.has(valueKey)
823
+ ) {
769
824
  return true;
770
825
  }
771
826
  }
@@ -801,7 +856,7 @@ export function create({
801
856
  filtered[key] = value;
802
857
  continue;
803
858
  }
804
- const fallbackValue = fallback[key];
859
+ const fallbackValue = getOwn(fallback, key);
805
860
  if (
806
861
  fallbackValue !== undefined &&
807
862
  !isOwnDisabledValue(key, fallbackValue)
@@ -845,8 +900,9 @@ export function create({
845
900
  defaultResolved: Record<string, unknown>,
846
901
  userVariantProps: Record<string, unknown>,
847
902
  filterOwnVariants: boolean,
848
- protectedVariantKeys: Set<string> | null | undefined,
903
+ protection: RefineProtection | null | undefined,
849
904
  ): Record<string, unknown> => {
905
+ const protectedVariants = protection?.variants;
850
906
  let ownVariants = filterOwnVariants ? null : resolved;
851
907
  const getOwnVariants = (): Record<string, unknown> => {
852
908
  if (ownVariants) {
@@ -878,18 +934,23 @@ export function create({
878
934
  if (hasOwn(userVariantProps, key)) {
879
935
  if (userVariantProps[key] !== undefined) continue;
880
936
  }
881
- if (protectedVariantKeys?.has(key)) continue;
937
+ if (protectedVariants && hasOwn(protectedVariants, key)) continue;
882
938
 
883
939
  const variantSnapshot = getOwnVariants();
884
940
  const defaultValue = inheritedComputedDefaultKeys.has(key)
885
- ? variantSnapshot[key]
886
- : defaultResolved[key];
941
+ ? getOwn(variantSnapshot, key)
942
+ : getOwn(defaultResolved, key);
887
943
  const value = computedDefaultFns[i](defaultValue, variantSnapshot);
888
944
  if (hasAnyDisabled) {
889
945
  if (disabledVariantKeys.has(key)) continue;
890
- const valueKey = getVariantValueKey(value);
891
- if (valueKey != null && disabledVariantValues[key]?.has(valueKey)) {
892
- continue;
946
+ if (disabledVariantValues) {
947
+ const valueKey = getVariantValueKey(value);
948
+ if (
949
+ valueKey != null &&
950
+ getOwn(disabledVariantValues, key)?.has(valueKey)
951
+ ) {
952
+ continue;
953
+ }
893
954
  }
894
955
  }
895
956
 
@@ -898,7 +959,7 @@ export function create({
898
959
  delete ensureUpdated()[key];
899
960
  continue;
900
961
  }
901
- if (Object.is(variantSnapshot[key], value)) continue;
962
+ if (Object.is(getOwn(variantSnapshot, key), value)) continue;
902
963
  ensureUpdated()[key] = value;
903
964
  }
904
965
 
@@ -910,8 +971,7 @@ export function create({
910
971
  filterOwnVariants: boolean,
911
972
  collectOutput: boolean,
912
973
  applyVariantUpdates: boolean,
913
- protectedVariants: Record<string, unknown> | null | undefined,
914
- protectedVariantKeys: Set<string> | null | undefined,
974
+ protection: RefineProtection | null | undefined,
915
975
  ): {
916
976
  workingResolved: Record<string, unknown>;
917
977
  classes: ClassValue[] | null;
@@ -937,25 +997,16 @@ export function create({
937
997
  }
938
998
  ownVariants = filteredVariants;
939
999
  }
940
- // Lazy-init updatedVariants — many refine callbacks only inspect
941
- // `variants`, so the copy is unnecessary in the common case.
942
- let updatedVariants: Record<string, unknown> | null = null;
943
- const localCClasses: ClassValue[] | null = collectOutput ? [] : null;
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;
944
1009
  let localCStyle: StyleValue | null = null;
945
- const ensureUpdated = (): Record<string, unknown> => {
946
- if (updatedVariants) {
947
- return updatedVariants;
948
- }
949
- const u = Object.assign({}, ownVariants);
950
- updatedVariants = u;
951
- return u;
952
- };
953
- const protectVariant = (key: string, value: unknown) => {
954
- if (protectedVariants) {
955
- protectedVariants[key] = value;
956
- protectedVariantKeys?.add(key);
957
- }
958
- };
959
1010
  const ctx = {
960
1011
  variants: ownVariants as VariantValues<Record<string, unknown>>,
961
1012
  setVariants: (
@@ -964,46 +1015,50 @@ export function create({
964
1015
  if (!applyVariantUpdates) {
965
1016
  return;
966
1017
  }
967
- if (!hasAnyDisabled) {
968
- for (const key in newVariants) {
969
- if (!hasOwn(newVariants, key)) continue;
970
- const value = (newVariants as Record<string, unknown>)[key];
971
- protectVariant(key, value);
972
- if (Object.is((updatedVariants ?? ownVariants)[key], value)) {
973
- continue;
974
- }
975
- ensureUpdated()[key] = value;
976
- }
977
- return;
978
- }
979
1018
  for (const key in newVariants) {
980
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.
981
1024
  if (disabledVariantKeys.has(key)) continue;
982
1025
  const value = (newVariants as Record<string, unknown>)[key];
983
- if (hasDisabledVariantValues) {
1026
+ if (disabledVariantValues) {
984
1027
  const valueKey = getVariantValueKey(value);
985
1028
  if (
986
1029
  valueKey != null &&
987
- disabledVariantValues[key]?.has(valueKey)
1030
+ getOwn(disabledVariantValues, key)?.has(valueKey)
988
1031
  ) {
989
1032
  continue;
990
1033
  }
991
1034
  }
992
- protectVariant(key, value);
993
- if (Object.is((updatedVariants ?? ownVariants)[key], value)) {
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)) {
994
1048
  continue;
995
1049
  }
996
- ensureUpdated()[key] = value;
1050
+ assignedVariants ??= {};
1051
+ assignedVariants[key] = value;
997
1052
  }
998
1053
  },
999
1054
  addClass: (className: ClassValue) => {
1000
- localCClasses?.push(className);
1055
+ if (!collectOutput) return;
1056
+ localCClasses ??= [];
1057
+ localCClasses.push(className);
1001
1058
  },
1002
1059
  addStyle: (newStyle: StyleValue) => {
1003
1060
  if (!collectOutput) return;
1004
- if (!localCStyle) {
1005
- localCStyle = {};
1006
- }
1061
+ localCStyle ??= {};
1007
1062
  Object.assign(localCStyle, newStyle);
1008
1063
  },
1009
1064
  };
@@ -1011,25 +1066,25 @@ export function create({
1011
1066
  if (collectOutput && result != null) {
1012
1067
  const r = extractClassAndStylePrebuilt(result);
1013
1068
  if (r.class != null) {
1014
- localCClasses?.push(r.class);
1069
+ localCClasses ??= [];
1070
+ localCClasses.push(r.class);
1015
1071
  }
1016
1072
  if (r.style) {
1017
- if (!localCStyle) {
1018
- localCStyle = {};
1019
- }
1073
+ localCStyle ??= {};
1020
1074
  Object.assign(localCStyle, r.style);
1021
1075
  }
1022
1076
  }
1023
1077
  cClasses = localCClasses;
1024
1078
  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);
1031
- }
1032
- workingResolved = nextResolved;
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
+ );
1033
1088
  }
1034
1089
  }
1035
1090
 
@@ -1051,8 +1106,7 @@ export function create({
1051
1106
  skipValues,
1052
1107
  classesOut,
1053
1108
  styleOut,
1054
- protectedVariants,
1055
- protectedVariantKeys,
1109
+ protection,
1056
1110
  defaultResolved = resolved,
1057
1111
  renderOnly = false,
1058
1112
  ) => {
@@ -1086,29 +1140,25 @@ export function create({
1086
1140
  } else {
1087
1141
  extSkipVals = {};
1088
1142
  for (const k in skipValues) {
1143
+ if (!hasOwn(skipValues, k)) continue;
1089
1144
  extSkipVals[k] = skipValues[k];
1090
1145
  }
1091
1146
  for (const k in staticExtSkipValues) {
1092
- const existing = extSkipVals[k];
1147
+ if (!hasOwn(staticExtSkipValues, k)) continue;
1148
+ const values = staticExtSkipValues[k];
1149
+ const existing = getOwn(extSkipVals, k);
1093
1150
  if (existing) {
1094
1151
  const merged = new Set<string>(existing);
1095
- for (const v of staticExtSkipValues[k]) {
1152
+ for (const v of values) {
1096
1153
  merged.add(v);
1097
1154
  }
1098
1155
  extSkipVals[k] = merged;
1099
1156
  } else {
1100
- extSkipVals[k] = staticExtSkipValues[k];
1157
+ extSkipVals[k] = values;
1101
1158
  }
1102
1159
  }
1103
1160
  }
1104
1161
 
1105
- const extUserVariantProps =
1106
- extMetasWithRefineCount > 0
1107
- ? getExtUserVariantProps(
1108
- userVariantProps,
1109
- protectedVariants ?? null,
1110
- )
1111
- : userVariantProps;
1112
1162
  for (let i = 0; i < extCount; i++) {
1113
1163
  if (hasIsolatedExt && extIsolated[i]) {
1114
1164
  // Isolated extend (different factory): gather its variant classes
@@ -1117,13 +1167,12 @@ export function create({
1117
1167
  const extClasses: ClsxClassValue[] = [];
1118
1168
  workingResolved = extMetas[i].compute(
1119
1169
  workingResolved,
1120
- extUserVariantProps,
1170
+ userVariantProps,
1121
1171
  extSkipKeys,
1122
1172
  extSkipVals,
1123
1173
  extClasses,
1124
1174
  styleOut,
1125
- protectedVariants,
1126
- protectedVariantKeys,
1175
+ protection,
1127
1176
  defaultResolved,
1128
1177
  renderOnly,
1129
1178
  );
@@ -1136,13 +1185,12 @@ export function create({
1136
1185
  } else {
1137
1186
  workingResolved = extMetas[i].compute(
1138
1187
  workingResolved,
1139
- extUserVariantProps,
1188
+ userVariantProps,
1140
1189
  extSkipKeys,
1141
1190
  extSkipVals,
1142
1191
  classesOut,
1143
1192
  styleOut,
1144
- protectedVariants,
1145
- protectedVariantKeys,
1193
+ protection,
1146
1194
  defaultResolved,
1147
1195
  renderOnly,
1148
1196
  );
@@ -1151,11 +1199,6 @@ export function create({
1151
1199
  workingResolved,
1152
1200
  defaultResolved,
1153
1201
  );
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
1202
  }
1160
1203
  }
1161
1204
 
@@ -1167,7 +1210,7 @@ export function create({
1167
1210
  defaultResolved,
1168
1211
  userVariantProps,
1169
1212
  true,
1170
- protectedVariantKeys,
1213
+ protection,
1171
1214
  );
1172
1215
  }
1173
1216
 
@@ -1179,8 +1222,7 @@ export function create({
1179
1222
  true,
1180
1223
  true,
1181
1224
  !renderOnly,
1182
- protectedVariants,
1183
- protectedVariantKeys,
1225
+ protection,
1184
1226
  );
1185
1227
  workingResolved = refineResult.workingResolved;
1186
1228
  cClasses = refineResult.classes;
@@ -1202,8 +1244,12 @@ export function create({
1202
1244
  for (let i = 0; i < variantEntryCount; i++) {
1203
1245
  const variantName = variantEntryNames[i];
1204
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.
1205
1250
  const selectedValue = workingResolved[variantName];
1206
1251
  if (selectedValue === undefined) continue;
1252
+ if (!hasOwn(workingResolved, variantName)) continue;
1207
1253
  const selectedKey = getVariantValueKey(selectedValue);
1208
1254
  const variant = variantEntryDefs[i];
1209
1255
  if (
@@ -1216,7 +1262,7 @@ export function create({
1216
1262
  if (
1217
1263
  ownSkipValues &&
1218
1264
  selectedKey != null &&
1219
- ownSkipValues[variantName]?.has(selectedKey)
1265
+ getOwn(ownSkipValues, variantName)?.has(selectedKey)
1220
1266
  ) {
1221
1267
  continue;
1222
1268
  }
@@ -1224,7 +1270,7 @@ export function create({
1224
1270
  let value: PrebuiltValue | null | undefined;
1225
1271
  if (variant.values) {
1226
1272
  if (selectedKey == null) continue;
1227
- value = variant.values[selectedKey];
1273
+ value = variant.values.get(selectedKey);
1228
1274
  } else if (selectedValue === true) {
1229
1275
  value = variant.shorthand;
1230
1276
  }
@@ -1245,11 +1291,12 @@ export function create({
1245
1291
  if (ownSkipKeys && ownSkipKeys.has(variantName)) continue;
1246
1292
  const selectedValue = workingResolved[variantName];
1247
1293
  if (selectedValue === undefined) continue;
1294
+ if (!hasOwn(workingResolved, variantName)) continue;
1248
1295
  const selectedKey = getVariantValueKey(selectedValue);
1249
1296
  if (
1250
1297
  ownSkipValues &&
1251
1298
  selectedKey != null &&
1252
- ownSkipValues[variantName]?.has(selectedKey)
1299
+ getOwn(ownSkipValues, variantName)?.has(selectedKey)
1253
1300
  ) {
1254
1301
  continue;
1255
1302
  }
@@ -1291,8 +1338,9 @@ export function create({
1291
1338
  styleOut,
1292
1339
  ) => {
1293
1340
  let remaining = MAX_REFINE_RUNS;
1294
- const protectedVariants: Record<string, unknown> = {};
1295
- const protectedVariantKeys = new Set<string>();
1341
+ const protection: RefineProtection | null = canProtectVariants
1342
+ ? { variants: null }
1343
+ : null;
1296
1344
  const incomingDefaultResolved = resolved;
1297
1345
  let workingResolved = resolved;
1298
1346
  // Latest variant changes from non-converging iterations inside the
@@ -1312,8 +1360,7 @@ export function create({
1312
1360
  const nextStyle: StyleValue = useDirectOutput ? styleOut : {};
1313
1361
  const defaultResolved = mergeProtectedIntoBase(
1314
1362
  incomingDefaultResolved,
1315
- protectedVariants,
1316
- protectedVariantKeys,
1363
+ protection,
1317
1364
  );
1318
1365
  const nextResolved = computeOnce(
1319
1366
  workingResolved,
@@ -1322,8 +1369,7 @@ export function create({
1322
1369
  skipValues,
1323
1370
  nextClasses,
1324
1371
  nextStyle,
1325
- protectedVariants,
1326
- protectedVariantKeys,
1372
+ protection,
1327
1373
  defaultResolved,
1328
1374
  );
1329
1375
 
@@ -1347,8 +1393,7 @@ export function create({
1347
1393
  skipValues,
1348
1394
  classesOut,
1349
1395
  styleOut,
1350
- protectedVariants,
1351
- protectedVariantKeys,
1396
+ protection,
1352
1397
  defaultResolved,
1353
1398
  true,
1354
1399
  );
@@ -1361,9 +1406,13 @@ export function create({
1361
1406
  return nextResolved;
1362
1407
  }
1363
1408
 
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.
1364
1413
  if (
1365
- process.env.NODE_ENV !== "production" &&
1366
- remaining < REFINE_UNSTABLE_TRACKING_WINDOW
1414
+ remaining < REFINE_UNSTABLE_TRACKING_WINDOW &&
1415
+ process.env.NODE_ENV !== "production"
1367
1416
  ) {
1368
1417
  if (!unstableChanges) {
1369
1418
  unstableChanges = new Map<string, VariantChange>();
@@ -1409,37 +1458,26 @@ export function create({
1409
1458
  resolved,
1410
1459
  userVariantProps,
1411
1460
  filterOwnVariants = true,
1412
- protectedVariants,
1413
- protectedVariantKeys,
1461
+ protection,
1414
1462
  defaultResolved = resolved,
1415
1463
  ) => {
1416
1464
  let workingResolved = resolved;
1417
1465
 
1418
- if (extMetasWithRefineCount > 0) {
1419
- const extUserVariantProps = getExtUserVariantProps(
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,
1420
1472
  userVariantProps,
1421
- protectedVariants ?? null,
1473
+ true,
1474
+ protection,
1475
+ defaultResolved,
1476
+ );
1477
+ workingResolved = filterOwnDisabledVariants(
1478
+ workingResolved,
1479
+ defaultResolved,
1422
1480
  );
1423
- for (let i = 0; i < extMetasWithRefineCount; i++) {
1424
- const meta = extMetasWithRefine[i];
1425
- const resolveRefine = meta.resolveRefine;
1426
- if (!resolveRefine) continue;
1427
- workingResolved = resolveRefine(
1428
- workingResolved,
1429
- extUserVariantProps,
1430
- true,
1431
- protectedVariants,
1432
- protectedVariantKeys,
1433
- defaultResolved,
1434
- );
1435
- workingResolved = filterOwnDisabledVariants(
1436
- workingResolved,
1437
- defaultResolved,
1438
- );
1439
- if (protectedVariants) {
1440
- Object.assign(extUserVariantProps, protectedVariants);
1441
- }
1442
- }
1443
1481
  }
1444
1482
 
1445
1483
  if (computedDefaultCount > 0) {
@@ -1448,7 +1486,7 @@ export function create({
1448
1486
  defaultResolved,
1449
1487
  userVariantProps,
1450
1488
  filterOwnVariants,
1451
- protectedVariantKeys,
1489
+ protection,
1452
1490
  );
1453
1491
  }
1454
1492
  if (refine) {
@@ -1457,8 +1495,7 @@ export function create({
1457
1495
  filterOwnVariants,
1458
1496
  false,
1459
1497
  true,
1460
- protectedVariants,
1461
- protectedVariantKeys,
1498
+ protection,
1462
1499
  );
1463
1500
  workingResolved = refineResult.workingResolved;
1464
1501
  }
@@ -1470,8 +1507,9 @@ export function create({
1470
1507
  refine || computedDefaultCount > 0 || extMetasWithRefineCount > 0
1471
1508
  ? (resolved, userVariantProps, filterOwnVariants = true) => {
1472
1509
  let remaining = MAX_REFINE_RUNS;
1473
- const protectedVariants: Record<string, unknown> = {};
1474
- const protectedVariantKeys = new Set<string>();
1510
+ const protection: RefineProtection | null = canProtectVariants
1511
+ ? { variants: null }
1512
+ : null;
1475
1513
  const incomingDefaultResolved = resolved;
1476
1514
  let workingResolved = resolved;
1477
1515
  // Latest variant changes from non-converging iterations inside the
@@ -1483,15 +1521,13 @@ export function create({
1483
1521
  remaining -= 1;
1484
1522
  const defaultResolved = mergeProtectedIntoBase(
1485
1523
  incomingDefaultResolved,
1486
- protectedVariants,
1487
- protectedVariantKeys,
1524
+ protection,
1488
1525
  );
1489
1526
  const nextResolved = resolveRefineOnce(
1490
1527
  workingResolved,
1491
1528
  userVariantProps,
1492
1529
  filterOwnVariants,
1493
- protectedVariants,
1494
- protectedVariantKeys,
1530
+ protection,
1495
1531
  defaultResolved,
1496
1532
  );
1497
1533
 
@@ -1502,9 +1538,11 @@ export function create({
1502
1538
  return nextResolved;
1503
1539
  }
1504
1540
 
1541
+ // `remaining` is checked first for the reason given in the
1542
+ // compute loop above.
1505
1543
  if (
1506
- process.env.NODE_ENV !== "production" &&
1507
- remaining < REFINE_UNSTABLE_TRACKING_WINDOW
1544
+ remaining < REFINE_UNSTABLE_TRACKING_WINDOW &&
1545
+ process.env.NODE_ENV !== "production"
1508
1546
  ) {
1509
1547
  if (!unstableChanges) {
1510
1548
  unstableChanges = new Map<string, VariantChange>();
@@ -1540,19 +1578,15 @@ export function create({
1540
1578
  Object.assign(resolved, staticDefaults);
1541
1579
 
1542
1580
  let userVariantProps: Record<string, unknown>;
1543
- if (refine || computedDefaultCount > 0 || extMetasWithRefineCount > 0) {
1581
+ if (needsUserVariantProps) {
1544
1582
  const variantProps: Record<string, unknown> = {};
1545
1583
  for (let i = 0; i < variantKeysLength; i++) {
1546
1584
  const key = variantKeys[i];
1547
- if (hasOwn(propsRecord, key)) {
1548
- variantProps[key] = propsRecord[key];
1549
- }
1550
- }
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;
1585
+ if (!hasOwn(propsRecord, key)) continue;
1586
+ const value = propsRecord[key];
1587
+ variantProps[key] = value;
1588
+ if (value === undefined) continue;
1589
+ resolved[key] = value;
1556
1590
  }
1557
1591
  userVariantProps = variantProps;
1558
1592
  } else {
@@ -1582,17 +1616,20 @@ export function create({
1582
1616
  const style: StyleValue = {};
1583
1617
  compute(resolved, userVariantProps, null, null, allClasses, style);
1584
1618
 
1585
- // Apply user-provided class / className.
1586
- if ("class" in propsRecord) {
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")) {
1587
1623
  allClasses.push(propsRecord.class as ClsxClassValue);
1588
1624
  }
1589
- if ("className" in propsRecord) {
1625
+ if (hasOwn(propsRecord, "className")) {
1590
1626
  allClasses.push(propsRecord.className as ClsxClassValue);
1591
1627
  }
1592
1628
 
1593
- // Apply user-provided style.
1629
+ // Apply user-provided style, read before the own-property check for the
1630
+ // same reason as the variant loops above.
1594
1631
  const psv = propsRecord.style;
1595
- if (psv != null) {
1632
+ if (psv != null && hasOwn(propsRecord, "style")) {
1596
1633
  if (typeof psv === "string") {
1597
1634
  if (psv.length > 0) {
1598
1635
  htmlStyleToStyleValue(psv, style);
@@ -1611,8 +1648,12 @@ export function create({
1611
1648
  const getVariants = (variants?: VariantValues<MergedVariants>) => {
1612
1649
  const variantsRecord = variants ?? EMPTY_DEFAULTS;
1613
1650
  let variantProps = variantsRecord;
1614
- // Extended refinement copies user props, so filter before unknown
1615
- // accessors or prototype keys can be observed by Object.assign.
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
1616
1657
  if (variants && extMetasWithRefineCount > 0) {
1617
1658
  variantProps = {};
1618
1659
  for (let i = 0; i < variantKeysLength; i++) {
@@ -1636,8 +1677,8 @@ export function create({
1636
1677
  // would compound (double for own-render, triple+ for extend chains) and
1637
1678
  // misbehave for non-idempotent transforms.
1638
1679
  const computedBaseClass = hasExtend
1639
- ? clsx(extBaseClassesArr, config.class as ClsxClassValue)
1640
- : clsx(config.class as ClsxClassValue);
1680
+ ? clsx(extBaseClassesArr, baseClass as ClsxClassValue)
1681
+ : clsx(baseClass as ClsxClassValue);
1641
1682
 
1642
1683
  // Shared closures across the default and modal components.
1643
1684
  const classFn = (props: ComponentProps<MergedVariants> = {}) => {