clava 0.7.1 → 0.7.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
@@ -1,4 +1,9 @@
1
1
  import clsx, { type ClassValue as ClsxClassValue } from "clsx";
2
+ import {
3
+ DEBUG_ATTRIBUTE,
4
+ formatDebugAttribute,
5
+ isDebugEnabled,
6
+ } from "./debug.ts";
2
7
  import {
3
8
  REFINE_UNSTABLE_TRACKING_WINDOW,
4
9
  type VariantChange,
@@ -11,8 +16,11 @@ import type {
11
16
  ClassValue,
12
17
  DefaultVariants,
13
18
  ExtendableVariants,
19
+ HTMLCSSProperties,
14
20
  HTMLObjProps,
15
21
  HTMLProps,
22
+ InferredRecipe,
23
+ JSXCSSProperties,
16
24
  JSXProps,
17
25
  KeySourceRecipe,
18
26
  MergeVariants,
@@ -62,6 +70,7 @@ type ComputeOnceFn = (
62
70
  protection?: RefineProtection | null,
63
71
  defaultResolved?: Record<string, unknown>,
64
72
  renderOnly?: boolean,
73
+ pass?: RefinePass | null,
65
74
  ) => Record<string, unknown>;
66
75
 
67
76
  type ResolveRefineFn = (
@@ -76,6 +85,8 @@ type ResolveRefineOnceFn = (
76
85
  filterOwnVariants?: boolean,
77
86
  protection?: RefineProtection | null,
78
87
  defaultResolved?: Record<string, unknown>,
88
+ pass?: RefinePass | null,
89
+ renderOnly?: boolean,
79
90
  ) => Record<string, unknown>;
80
91
 
81
92
  type ComputedDefaultVariantFn = (
@@ -93,7 +104,13 @@ interface RecipeMeta {
93
104
  // Performs a single compute pass for extending recipes, returning the
94
105
  // resolved variants while pushing classes and styles into the output values.
95
106
  compute: ComputeOnceFn;
107
+ // Set only when this recipe's chain has computed defaults or `refine`.
96
108
  resolveRefine: ResolveRefineOnceFn | null;
109
+ // Mirrors the variant changes of `compute` without rendering. A recipe with
110
+ // no callbacks in its chain can still have it, because it can filter disabled
111
+ // values that a later extend's callbacks would otherwise read. Missing when
112
+ // the chain can't change variants, and in older Clava copies.
113
+ resolveVariants?: ResolveRefineOnceFn;
97
114
  // Reference identity is used to detect mixed-factory `extend`. When a
98
115
  // recipe is extended by a parent from a different `create()` call, the
99
116
  // parent applies this transform to the extend's contribution before joining,
@@ -109,6 +126,16 @@ interface RecipeMeta {
109
126
  // Child recipes use this to preserve inherited computed defaults through
110
127
  // `defaultValue` without preserving their own prior computed result.
111
128
  computedDefaultKeys: Set<string>;
129
+ // Variant keys whose computed defaults run in this recipe's pruned chain.
130
+ // Unlike `computedDefaultKeys`, a pruned shared ancestor doesn't add keys.
131
+ // Older Clava copies don't expose it.
132
+ activeComputedDefaultKeys?: Set<string>;
133
+ // Variant keys that this recipe's chain disables entirely or partly. Older
134
+ // Clava copies don't expose it or `isDisabledValue`.
135
+ disabledKeys?: Set<string>;
136
+ // Whether this recipe's chain removes the value for the key from the variants
137
+ // it returns. A recipe that defines a value again stops its removal.
138
+ isDisabledValue?: (key: string, value: unknown) => boolean;
112
139
  }
113
140
 
114
141
  const META_KEY = "__meta";
@@ -199,6 +226,72 @@ function mergeProtectedIntoBase(
199
226
  return Object.assign({}, baseResolved, protectedVariants);
200
227
  }
201
228
 
229
+ // A pass after the first one of a resolution loop. A computed default that
230
+ // writes a key that a later computed default computes again, such as a base that
231
+ // resets it to its static default, writes it to `chainValues` instead of the
232
+ // working variants. Only the later computed defaults for that key read it, so
233
+ // other callbacks keep the value that the previous pass settled on.
234
+ // See https://github.com/ariakit/clava/issues/613
235
+ interface RefinePass {
236
+ // One frame for each recipe on the current extend path.
237
+ frames: PendingFrame[];
238
+ // Values for keys that a later computed default still computes. The working
239
+ // variants hold `undefined` the same way as a missing key, so it marks a
240
+ // cleared key here too.
241
+ chainValues: Map<string, unknown> | null;
242
+ }
243
+
244
+ interface PendingFrame {
245
+ // Keys that computed defaults compute after the current extend.
246
+ keys: ReadonlySet<string> | null;
247
+ // Whether the recipe removes a value after each extend. A computed default
248
+ // below it writes such a value to the working variants, as before, so the
249
+ // removal applies.
250
+ isDisabled: ((key: string, value: unknown) => boolean) | null;
251
+ }
252
+
253
+ function isPendingKey(frames: PendingFrame[], key: string): boolean {
254
+ for (let i = 0; i < frames.length; i++) {
255
+ if (frames[i]?.keys?.has(key)) return true;
256
+ }
257
+ return false;
258
+ }
259
+
260
+ function isDisabledOnPath(
261
+ frames: PendingFrame[],
262
+ key: string,
263
+ value: unknown,
264
+ ): boolean {
265
+ for (let i = 0; i < frames.length; i++) {
266
+ if (frames[i]?.isDisabled?.(key, value)) return true;
267
+ }
268
+ return false;
269
+ }
270
+
271
+ // Chains that mix in recipes from an older Clava copy aren't supported. Their
272
+ // metadata lacks these keys, which then count as none instead of throwing.
273
+ const NO_KEYS: ReadonlySet<string> = new Set();
274
+
275
+ function getActiveComputedDefaultKeys(meta: RecipeMeta): ReadonlySet<string> {
276
+ return getOwn(meta, "activeComputedDefaultKeys") ?? NO_KEYS;
277
+ }
278
+
279
+ // Returns a copy of `variants` where `key` holds `value`, or is missing when
280
+ // `value` is `undefined`.
281
+ function withVariant(
282
+ variants: Record<string, unknown>,
283
+ key: string,
284
+ value: unknown,
285
+ ): Record<string, unknown> {
286
+ const copy = Object.assign({}, variants);
287
+ if (value === undefined) {
288
+ delete copy[key];
289
+ } else {
290
+ copy[key] = value;
291
+ }
292
+ return copy;
293
+ }
294
+
202
295
  export type {
203
296
  ClassValue,
204
297
  StyleValue,
@@ -207,8 +300,13 @@ export type {
207
300
  JSXProps,
208
301
  HTMLProps,
209
302
  HTMLObjProps,
303
+ JSXCSSProperties,
304
+ HTMLCSSProperties,
305
+ ModalRecipe,
210
306
  Recipe,
211
307
  RecipeLike,
308
+ RecipeProps,
309
+ SplitPropsFunction,
212
310
  };
213
311
 
214
312
  /**
@@ -364,6 +462,12 @@ export interface CVConfig<V extends Variants = {}, E extends AnyRecipe[] = []> {
364
462
 
365
463
  interface CreateParams {
366
464
  transformClass?: (className: string) => string;
465
+ /**
466
+ * Whether recipes add a `data-clava-debug` attribute that lists their
467
+ * resolved variants. Defaults to `true` when `NODE_ENV` is
468
+ * `"development"`. The attribute is never added in production.
469
+ */
470
+ debug?: boolean;
367
471
  }
368
472
 
369
473
  function identityClass(className: string) {
@@ -462,6 +566,10 @@ function splitPropsImpl(
462
566
  const sourcesLength = sources.length;
463
567
  const results: Record<string, unknown>[] = [];
464
568
  let stylingClaimed = selfIsRecipe;
569
+ // The recipe that claims styling props also receives an incoming debug
570
+ // attribute, so it can append the value to its own. The rest props keep the
571
+ // value too, so it still reaches the element through `.class()`.
572
+ let stylingResult: Record<string, unknown> | null = null;
465
573
 
466
574
  const selfResult: Record<string, unknown> = {};
467
575
  const selfKeysLength = selfKeys.length;
@@ -472,6 +580,9 @@ function splitPropsImpl(
472
580
  }
473
581
  }
474
582
  results.push(selfResult);
583
+ if (selfIsRecipe) {
584
+ stylingResult = selfResult;
585
+ }
475
586
 
476
587
  // Track effective key arrays for the rest computation — for typical inputs
477
588
  // a linear scan beats building a Set up-front.
@@ -503,6 +614,7 @@ function splitPropsImpl(
503
614
 
504
615
  if (sourceIsRecipe && !stylingClaimed) {
505
616
  stylingClaimed = true;
617
+ stylingResult = sourceResult;
506
618
  }
507
619
  }
508
620
 
@@ -513,6 +625,12 @@ function splitPropsImpl(
513
625
  outer: for (let i = 0; i < propKeysLength; i++) {
514
626
  const key = propKeys[i];
515
627
  if (key === undefined) continue;
628
+ // Checked in this loop, which already visits every own enumerable key, so
629
+ // that calls without the attribute don't pay for a separate `hasOwn`
630
+ // lookup.
631
+ if (key === DEBUG_ATTRIBUTE && stylingResult) {
632
+ stylingResult[key] = props[key];
633
+ }
516
634
  for (let g = 0; g < groupCount; g++) {
517
635
  const arr = effectiveKeyArrays[g];
518
636
  if (arr === undefined) continue;
@@ -604,8 +722,17 @@ export function create(params: CreateParams = {}) {
604
722
  // `Object.prototype` could use to rewrite every class string this factory
605
723
  // produces, including those of the package-level `cv`.
606
724
  const transformClass = getOwn(params, "transformClass") ?? identityClass;
607
-
608
- const cx = (...classes: ClsxClassValue[]) => transformClass(clsx(classes));
725
+ // The production check stays here, so bundlers can fold `debug` to `false`
726
+ // and remove the debug code from production bundles.
727
+ const debug =
728
+ process.env.NODE_ENV !== "production" &&
729
+ isDebugEnabled(getOwn(params, "debug"));
730
+
731
+ // The root `cx` type lets consumers emit `typeof cx` for an exported result.
732
+ // Otherwise, emit must name clsx's `ClassValue`, which pnpm installs hide.
733
+ const transformedCx: typeof cx = (...classes) => {
734
+ return transformClass(clsx(classes));
735
+ };
609
736
 
610
737
  const buildRecipe = <
611
738
  V extends Variants = {},
@@ -658,6 +785,8 @@ export function create(params: CreateParams = {}) {
658
785
  const variantEntryDefs: PrebuiltVariant[] = [];
659
786
  const functionVariantNames: string[] = [];
660
787
  const functionVariantFns: Array<(value: unknown) => unknown> = [];
788
+ // Own variant definitions by name. `null` stands for a function variant.
789
+ const ownVariantDefs = new Map<string, PrebuiltVariant | null>();
661
790
  if (variants) {
662
791
  for (const name in variants) {
663
792
  if (!hasOwn(variants, name)) continue;
@@ -671,9 +800,11 @@ export function create(params: CreateParams = {}) {
671
800
  if (typeof variant === "function") {
672
801
  functionVariantNames.push(name);
673
802
  functionVariantFns.push(variant as (value: unknown) => unknown);
803
+ ownVariantDefs.set(name, null);
674
804
  continue;
675
805
  }
676
806
  const prebuiltVariant = buildPrebuiltVariant(variant);
807
+ ownVariantDefs.set(name, prebuiltVariant);
677
808
  variantEntryNames.push(name);
678
809
  variantEntryDefs.push(prebuiltVariant);
679
810
  if (prebuiltVariant.disabledValues) {
@@ -689,6 +820,17 @@ export function create(params: CreateParams = {}) {
689
820
  }
690
821
  }
691
822
  }
823
+ const definesOwnValue = (key: string, value: unknown): boolean => {
824
+ const variant = ownVariantDefs.get(key);
825
+ if (variant === undefined) return false;
826
+ if (variant === null) return true;
827
+ if (!variant.values) return typeof value === "boolean";
828
+ // `false` is a valid value for a variant that only defines `true`.
829
+ if (value === false && variant.values.has("true")) return true;
830
+ const valueKey = getVariantValueKey(value);
831
+ return valueKey != null && variant.values.has(valueKey);
832
+ };
833
+
692
834
  const variantKeys = Array.from(variantKeySet);
693
835
  const variantKeysLength = variantKeys.length;
694
836
  const variantEntryCount = variantEntryNames.length;
@@ -781,17 +923,26 @@ export function create(params: CreateParams = {}) {
781
923
  }
782
924
  }
783
925
 
784
- // Filter to only extends with computed default or refine work in their
785
- // chain. Those are the recipes that can change resolved variants across
926
+ // Count only extends with computed default or refine work in their chain.
927
+ // Those are the recipes that can change resolved variants across
786
928
  // fixed-point iterations.
787
- const extMetasWithRefine: RecipeMeta[] = [];
929
+ let extMetasWithRefineCount = 0;
788
930
  for (let i = 0; i < extCount; i++) {
789
- const meta = extMetas[i];
790
- if (meta.resolveRefine) {
791
- extMetasWithRefine.push(meta);
931
+ if (extMetas[i].resolveRefine) {
932
+ extMetasWithRefineCount += 1;
792
933
  }
793
934
  }
794
- const extMetasWithRefineCount = extMetasWithRefine.length;
935
+
936
+ // The extends that can change variants for `getVariants`. Built once so
937
+ // that each pass skips the others without reading their metadata.
938
+ const extResolvers = extMetas.map(
939
+ (meta) => getOwn(meta, "resolveVariants") ?? meta.resolveRefine,
940
+ );
941
+ const canResolveVariants =
942
+ (hasExtend && hasAnyDisabled) ||
943
+ !!refine ||
944
+ computedDefaultCount > 0 ||
945
+ extResolvers.some(Boolean);
795
946
 
796
947
  // Only a `refine` callback protects variants, through `setVariants`. This
797
948
  // over-approximates: `extMetasWithRefineCount` also counts extends that
@@ -908,6 +1059,97 @@ export function create(params: CreateParams = {}) {
908
1059
  return false;
909
1060
  };
910
1061
 
1062
+ const isDisabledValue = (key: string, value: unknown): boolean => {
1063
+ if (isOwnDisabledValue(key, value)) return true;
1064
+ if (definesOwnValue(key, value)) return false;
1065
+ return extMetas.some((meta) =>
1066
+ getOwn(meta, "isDisabledValue")?.(key, value),
1067
+ );
1068
+ };
1069
+
1070
+ const disabledKeys = new Set(disabledVariantKeys);
1071
+ for (const key in disabledVariantValues) {
1072
+ if (!hasOwn(disabledVariantValues, key)) continue;
1073
+ disabledKeys.add(key);
1074
+ }
1075
+ // Own keys that an extend may disable, so an extend that doesn't disable
1076
+ // anything this recipe defines adds no work to each compute pass.
1077
+ const restorableKeys: string[] = [];
1078
+ for (const meta of extMetas) {
1079
+ const extDisabledKeys = getOwn(meta, "disabledKeys");
1080
+ if (!extDisabledKeys) continue;
1081
+ for (const key of extDisabledKeys) {
1082
+ disabledKeys.add(key);
1083
+ if (ownVariantDefs.has(key) && !restorableKeys.includes(key)) {
1084
+ restorableKeys.push(key);
1085
+ }
1086
+ }
1087
+ }
1088
+
1089
+ // An extend removes the values that its chain disables, but this recipe
1090
+ // defines some of them again. Its value comes back after the extend runs,
1091
+ // so the extend's own callbacks still read the variants that it declares.
1092
+ const restoreOwnValues = (
1093
+ before: Record<string, unknown>,
1094
+ after: Record<string, unknown>,
1095
+ extMeta: RecipeMeta,
1096
+ fallback: Record<string, unknown>,
1097
+ protection: RefineProtection | null | undefined,
1098
+ ): Record<string, unknown> => {
1099
+ let restored = after;
1100
+ for (const key of restorableKeys) {
1101
+ const value = getOwn(before, key);
1102
+ if (value === undefined) continue;
1103
+ if (!definesOwnValue(key, value)) continue;
1104
+ if (!getOwn(extMeta, "isDisabledValue")?.(key, value)) continue;
1105
+ const current = getOwn(after, key);
1106
+ // A disabling recipe without extends removes nothing.
1107
+ if (Object.is(current, value)) continue;
1108
+ // A different protected value comes from a callback, even a clear.
1109
+ const protectedVariants = protection?.variants;
1110
+ if (
1111
+ protectedVariants &&
1112
+ hasOwn(protectedVariants, key) &&
1113
+ !Object.is(protectedVariants[key], value)
1114
+ ) {
1115
+ continue;
1116
+ }
1117
+ // The extend replaces a disabled value with the fallback. Any other
1118
+ // value comes from its callbacks.
1119
+ if (
1120
+ current !== undefined &&
1121
+ !Object.is(current, getOwn(fallback, key))
1122
+ ) {
1123
+ continue;
1124
+ }
1125
+ if (restored === after) {
1126
+ restored = Object.assign({}, after);
1127
+ }
1128
+ restored[key] = value;
1129
+ }
1130
+ return restored;
1131
+ };
1132
+
1133
+ // The frame for each extend lists the keys that computed defaults
1134
+ // compute after it runs: those of later extends and this recipe's own.
1135
+ const extPendingFrames: Array<PendingFrame | null> = [];
1136
+ const laterComputedDefaultKeys = new Set(computedDefaultNames);
1137
+ for (let i = extCount - 1; i >= 0; i--) {
1138
+ const keys =
1139
+ laterComputedDefaultKeys.size > 0
1140
+ ? new Set(laterComputedDefaultKeys)
1141
+ : null;
1142
+ extPendingFrames.unshift(
1143
+ keys || hasAnyDisabled
1144
+ ? { keys, isDisabled: hasAnyDisabled ? isOwnDisabledValue : null }
1145
+ : null,
1146
+ );
1147
+ for (const key of getActiveComputedDefaultKeys(extMetas[i])) {
1148
+ laterComputedDefaultKeys.add(key);
1149
+ }
1150
+ }
1151
+ const activeComputedDefaultKeys = laterComputedDefaultKeys;
1152
+
911
1153
  const filterOwnDisabledVariants = (
912
1154
  input: Record<string, unknown>,
913
1155
  fallback: Record<string, unknown>,
@@ -949,6 +1191,26 @@ export function create(params: CreateParams = {}) {
949
1191
  return filtered;
950
1192
  };
951
1193
 
1194
+ // Applies `filterOwnDisabledVariants` to the values that computed defaults
1195
+ // keep out of the working variants, so later ones read the same value.
1196
+ const filterOwnDisabledChainValues = (
1197
+ chainValues: Map<string, unknown>,
1198
+ fallback: Record<string, unknown>,
1199
+ ): void => {
1200
+ if (!hasAnyDisabled) return;
1201
+ for (const [key, value] of chainValues) {
1202
+ if (!isOwnDisabledValue(key, value)) continue;
1203
+ const fallbackValue = getOwn(fallback, key);
1204
+ chainValues.set(
1205
+ key,
1206
+ fallbackValue !== undefined &&
1207
+ !isOwnDisabledValue(key, fallbackValue)
1208
+ ? fallbackValue
1209
+ : undefined,
1210
+ );
1211
+ }
1212
+ };
1213
+
952
1214
  // Hot path: resolve variants by merging static defaults + extends'
953
1215
  // static defaults + user-provided props.
954
1216
  function resolveVariantsHot(
@@ -982,6 +1244,7 @@ export function create(params: CreateParams = {}) {
982
1244
  userVariantProps: Record<string, unknown>,
983
1245
  filterOwnVariants: boolean,
984
1246
  protection: RefineProtection | null | undefined,
1247
+ pass: RefinePass | null | undefined,
985
1248
  ): Record<string, unknown> => {
986
1249
  const protectedVariants = protection?.variants;
987
1250
  let ownVariants = filterOwnVariants ? null : resolved;
@@ -1018,29 +1281,54 @@ export function create(params: CreateParams = {}) {
1018
1281
  if (protectedVariants && hasOwn(protectedVariants, key)) continue;
1019
1282
 
1020
1283
  const variantSnapshot = getOwnVariants();
1284
+ const currentValue = getOwn(variantSnapshot, key);
1285
+ // An earlier computed default may have kept its value for this key
1286
+ // out of the working variants. This one still reads that value as the
1287
+ // one it replaces, through `defaultValue` and through its own key.
1288
+ const chainValues = pass?.chainValues;
1289
+ const latestValue =
1290
+ chainValues && chainValues.has(key)
1291
+ ? chainValues.get(key)
1292
+ : currentValue;
1021
1293
  const defaultValue = inheritedComputedDefaultKeys.has(key)
1022
- ? getOwn(variantSnapshot, key)
1294
+ ? latestValue
1023
1295
  : getOwn(defaultResolved, key);
1024
- const value = computedDefaultFns[i](defaultValue, variantSnapshot);
1025
- if (hasAnyDisabled) {
1026
- if (disabledVariantKeys.has(key)) continue;
1027
- if (disabledVariantValues) {
1028
- const valueKey = getVariantValueKey(value);
1029
- if (
1030
- valueKey != null &&
1031
- getOwn(disabledVariantValues, key)?.has(valueKey)
1032
- ) {
1033
- continue;
1034
- }
1296
+ let value = computedDefaultFns[i](
1297
+ defaultValue,
1298
+ Object.is(latestValue, currentValue)
1299
+ ? variantSnapshot
1300
+ : withVariant(variantSnapshot, key, latestValue),
1301
+ );
1302
+ if (hasAnyDisabled && isOwnDisabledValue(key, value)) {
1303
+ // The latest value stays, as if this computed default didn't run.
1304
+ // When no later one computes the key, it becomes the final value.
1305
+ if (Object.is(latestValue, currentValue)) continue;
1306
+ if (pass && isPendingKey(pass.frames, key)) continue;
1307
+ value = latestValue;
1308
+ } else if (pass) {
1309
+ if (Object.is(value, currentValue)) {
1310
+ chainValues?.delete(key);
1311
+ continue;
1312
+ }
1313
+ if (
1314
+ isPendingKey(pass.frames, key) &&
1315
+ !isDisabledOnPath(pass.frames, key, value)
1316
+ ) {
1317
+ pass.chainValues ??= new Map();
1318
+ pass.chainValues.set(key, value);
1319
+ continue;
1035
1320
  }
1036
1321
  }
1322
+ // The working variants receive this value, so a value that an
1323
+ // earlier computed default kept out of them no longer applies.
1324
+ chainValues?.delete(key);
1037
1325
 
1038
1326
  if (value === undefined) {
1039
1327
  if (!hasOwn(variantSnapshot, key)) continue;
1040
1328
  delete ensureUpdated()[key];
1041
1329
  continue;
1042
1330
  }
1043
- if (Object.is(getOwn(variantSnapshot, key), value)) continue;
1331
+ if (Object.is(currentValue, value)) continue;
1044
1332
  ensureUpdated()[key] = value;
1045
1333
  }
1046
1334
 
@@ -1190,6 +1478,7 @@ export function create(params: CreateParams = {}) {
1190
1478
  protection,
1191
1479
  defaultResolved = resolved,
1192
1480
  renderOnly = false,
1481
+ pass,
1193
1482
  ) => {
1194
1483
  let workingResolved = resolved;
1195
1484
  let cClasses: ClassValue[] | null = null;
@@ -1241,6 +1530,11 @@ export function create(params: CreateParams = {}) {
1241
1530
  }
1242
1531
 
1243
1532
  for (let i = 0; i < extCount; i++) {
1533
+ const pendingFrame = pass ? extPendingFrames[i] : null;
1534
+ if (pass && pendingFrame) {
1535
+ pass.frames.push(pendingFrame);
1536
+ }
1537
+ const beforeExtend = workingResolved;
1244
1538
  if (hasIsolatedExt && extIsolated[i]) {
1245
1539
  // Isolated extend (different factory): gather its variant classes
1246
1540
  // into a scratch array, then push the joined string after applying
@@ -1256,6 +1550,7 @@ export function create(params: CreateParams = {}) {
1256
1550
  protection,
1257
1551
  defaultResolved,
1258
1552
  renderOnly,
1553
+ pass,
1259
1554
  );
1260
1555
  if (extClasses.length > 0) {
1261
1556
  const joined = clsx(extClasses);
@@ -1274,12 +1569,28 @@ export function create(params: CreateParams = {}) {
1274
1569
  protection,
1275
1570
  defaultResolved,
1276
1571
  renderOnly,
1572
+ pass,
1573
+ );
1574
+ }
1575
+ if (pass && pendingFrame) {
1576
+ pass.frames.pop();
1577
+ }
1578
+ if (restorableKeys.length > 0) {
1579
+ workingResolved = restoreOwnValues(
1580
+ beforeExtend,
1581
+ workingResolved,
1582
+ extMetas[i],
1583
+ defaultResolved,
1584
+ protection,
1277
1585
  );
1278
1586
  }
1279
1587
  workingResolved = filterOwnDisabledVariants(
1280
1588
  workingResolved,
1281
1589
  defaultResolved,
1282
1590
  );
1591
+ if (pass?.chainValues) {
1592
+ filterOwnDisabledChainValues(pass.chainValues, defaultResolved);
1593
+ }
1283
1594
  }
1284
1595
  }
1285
1596
 
@@ -1292,6 +1603,7 @@ export function create(params: CreateParams = {}) {
1292
1603
  userVariantProps,
1293
1604
  true,
1294
1605
  protection,
1606
+ pass,
1295
1607
  );
1296
1608
  }
1297
1609
 
@@ -1430,6 +1742,7 @@ export function create(params: CreateParams = {}) {
1430
1742
  let lastClasses: ClsxClassValue[] = [];
1431
1743
  let lastStyle: StyleValue = {};
1432
1744
  let isFirstRun = true;
1745
+ let pass: RefinePass | null = null;
1433
1746
 
1434
1747
  while (remaining > 0) {
1435
1748
  remaining -= 1;
@@ -1443,6 +1756,10 @@ export function create(params: CreateParams = {}) {
1443
1756
  incomingDefaultResolved,
1444
1757
  protection,
1445
1758
  );
1759
+ if (!isFirstRun) {
1760
+ pass ??= { frames: [], chainValues: null };
1761
+ pass.chainValues?.clear();
1762
+ }
1446
1763
  const nextResolved = computeOnce(
1447
1764
  workingResolved,
1448
1765
  userVariantProps,
@@ -1452,21 +1769,19 @@ export function create(params: CreateParams = {}) {
1452
1769
  nextStyle,
1453
1770
  protection,
1454
1771
  defaultResolved,
1772
+ false,
1773
+ pass,
1455
1774
  );
1456
1775
 
1776
+ // No pass has settled before the first one, so it reads the
1777
+ // latest values. It settles only when it changed nothing,
1778
+ // because a later computed default may replace a value it read.
1457
1779
  if (
1458
1780
  nextResolved === workingResolved ||
1459
- areVariantsEqual(workingResolved, nextResolved)
1781
+ (!isFirstRun &&
1782
+ areVariantsEqual(workingResolved, nextResolved))
1460
1783
  ) {
1461
1784
  if (nextResolved !== workingResolved) {
1462
- if (useDirectOutput) {
1463
- classesOut.length = classCount;
1464
- for (const key in styleOut) {
1465
- if (hasOwn(styleOut, key)) {
1466
- Reflect.deleteProperty(styleOut, key);
1467
- }
1468
- }
1469
- }
1470
1785
  computeOnce(
1471
1786
  nextResolved,
1472
1787
  userVariantProps,
@@ -1541,26 +1856,54 @@ export function create(params: CreateParams = {}) {
1541
1856
  filterOwnVariants = true,
1542
1857
  protection,
1543
1858
  defaultResolved = resolved,
1859
+ pass,
1860
+ renderOnly = false,
1544
1861
  ) => {
1545
1862
  let workingResolved = resolved;
1546
1863
 
1547
- for (let i = 0; i < extMetasWithRefineCount; i++) {
1548
- const meta = extMetasWithRefine[i];
1549
- const resolveRefine = meta.resolveRefine;
1550
- if (!resolveRefine) continue;
1551
- workingResolved = resolveRefine(
1552
- workingResolved,
1553
- userVariantProps,
1554
- true,
1555
- protection,
1556
- defaultResolved,
1557
- );
1864
+ // Visit every extend, as `computeOnce` does, so that disabled values are
1865
+ // removed at the same points. A later extend's callbacks can read them.
1866
+ for (let i = 0; i < extCount; i++) {
1867
+ const resolveExtend = extResolvers[i];
1868
+ const beforeExtend = workingResolved;
1869
+ if (resolveExtend) {
1870
+ const pendingFrame = pass ? extPendingFrames[i] : null;
1871
+ if (pass && pendingFrame) {
1872
+ pass.frames.push(pendingFrame);
1873
+ }
1874
+ workingResolved = resolveExtend(
1875
+ workingResolved,
1876
+ userVariantProps,
1877
+ true,
1878
+ protection,
1879
+ defaultResolved,
1880
+ pass,
1881
+ renderOnly,
1882
+ );
1883
+ if (pass && pendingFrame) {
1884
+ pass.frames.pop();
1885
+ }
1886
+ if (restorableKeys.length > 0) {
1887
+ workingResolved = restoreOwnValues(
1888
+ beforeExtend,
1889
+ workingResolved,
1890
+ extMetas[i],
1891
+ defaultResolved,
1892
+ protection,
1893
+ );
1894
+ }
1895
+ }
1558
1896
  workingResolved = filterOwnDisabledVariants(
1559
1897
  workingResolved,
1560
1898
  defaultResolved,
1561
1899
  );
1900
+ if (pass?.chainValues) {
1901
+ filterOwnDisabledChainValues(pass.chainValues, defaultResolved);
1902
+ }
1562
1903
  }
1563
1904
 
1905
+ if (renderOnly) return workingResolved;
1906
+
1564
1907
  if (computedDefaultCount > 0) {
1565
1908
  workingResolved = runComputedDefaults(
1566
1909
  workingResolved,
@@ -1568,6 +1911,7 @@ export function create(params: CreateParams = {}) {
1568
1911
  userVariantProps,
1569
1912
  filterOwnVariants,
1570
1913
  protection,
1914
+ pass,
1571
1915
  );
1572
1916
  }
1573
1917
  if (refine) {
@@ -1597,6 +1941,7 @@ export function create(params: CreateParams = {}) {
1597
1941
  // tracking window. See the compute loop above for the shared
1598
1942
  // rationale.
1599
1943
  let unstableChanges: Map<string, VariantChange> | null = null;
1944
+ let pass: RefinePass | null = null;
1600
1945
 
1601
1946
  while (remaining > 0) {
1602
1947
  remaining -= 1;
@@ -1610,14 +1955,28 @@ export function create(params: CreateParams = {}) {
1610
1955
  filterOwnVariants,
1611
1956
  protection,
1612
1957
  defaultResolved,
1958
+ pass,
1613
1959
  );
1614
1960
 
1615
- if (
1616
- nextResolved === workingResolved ||
1617
- areVariantsEqual(workingResolved, nextResolved)
1618
- ) {
1961
+ // The first pass settles only when it changed nothing, for the
1962
+ // reason given in the compute loop above.
1963
+ if (nextResolved === workingResolved) {
1619
1964
  return nextResolved;
1620
1965
  }
1966
+ if (pass && areVariantsEqual(workingResolved, nextResolved)) {
1967
+ // Match the final pass of the compute loop above, which
1968
+ // removes a disabled value that a callback set after the
1969
+ // extend that disables it.
1970
+ return resolveRefineOnce(
1971
+ nextResolved,
1972
+ userVariantProps,
1973
+ filterOwnVariants,
1974
+ protection,
1975
+ defaultResolved,
1976
+ null,
1977
+ true,
1978
+ );
1979
+ }
1621
1980
 
1622
1981
  // `remaining` is checked first for the reason given in the
1623
1982
  // compute loop above.
@@ -1635,6 +1994,8 @@ export function create(params: CreateParams = {}) {
1635
1994
  );
1636
1995
  }
1637
1996
  workingResolved = nextResolved;
1997
+ pass ??= { frames: [], chainValues: null };
1998
+ pass.chainValues?.clear();
1638
1999
  }
1639
2000
 
1640
2001
  if (process.env.NODE_ENV !== "production") {
@@ -1652,7 +2013,11 @@ export function create(params: CreateParams = {}) {
1652
2013
  // assembles className and style with user-provided class/style overrides.
1653
2014
  const computeResult = (
1654
2015
  props: RecipeProps<MergedVariants> = EMPTY_DEFAULTS as RecipeProps<MergedVariants>,
1655
- ): { className: string; style: StyleValue } => {
2016
+ ): {
2017
+ className: string;
2018
+ style: StyleValue;
2019
+ variants: Record<string, unknown>;
2020
+ } => {
1656
2021
  const propsRecord = props as Record<string, unknown>;
1657
2022
 
1658
2023
  let resolved: Record<string, unknown> = {};
@@ -1695,7 +2060,14 @@ export function create(params: CreateParams = {}) {
1695
2060
  // on top.
1696
2061
  const allClasses: ClsxClassValue[] = [computedBaseClass];
1697
2062
  const style: StyleValue = {};
1698
- compute(resolved, userVariantProps, null, null, allClasses, style);
2063
+ const variants = compute(
2064
+ resolved,
2065
+ userVariantProps,
2066
+ null,
2067
+ null,
2068
+ allClasses,
2069
+ style,
2070
+ );
1699
2071
 
1700
2072
  // Apply user-provided class / className. Own-property checks keep these
1701
2073
  // consistent with the variant props read above: only what the caller
@@ -1723,9 +2095,24 @@ export function create(params: CreateParams = {}) {
1723
2095
  return {
1724
2096
  className: transformClass(clsx(allClasses)),
1725
2097
  style,
2098
+ variants,
1726
2099
  };
1727
2100
  };
1728
2101
 
2102
+ // Adds the debug attribute to a mode result. Call only when `debug` is
2103
+ // true.
2104
+ const setDebugAttribute = (
2105
+ result: Record<string, unknown>,
2106
+ props: RecipeProps<MergedVariants>,
2107
+ variants: Record<string, unknown>,
2108
+ ) => {
2109
+ result[DEBUG_ATTRIBUTE] = formatDebugAttribute({
2110
+ variantKeys,
2111
+ variants,
2112
+ incoming: getOwn(props as Record<string, unknown>, DEBUG_ATTRIBUTE),
2113
+ });
2114
+ };
2115
+
1729
2116
  const getVariants = (variants?: VariantValues<MergedVariants>) => {
1730
2117
  const variantsRecord = variants ?? EMPTY_DEFAULTS;
1731
2118
  let variantProps = variantsRecord;
@@ -1751,6 +2138,10 @@ export function create(params: CreateParams = {}) {
1751
2138
  variantProps,
1752
2139
  false,
1753
2140
  );
2141
+ } else if (canResolveVariants) {
2142
+ // No callback in this chain can restore a removed value, so one pass
2143
+ // reaches the state that render ends with.
2144
+ resolvedVariants = resolveRefineOnce(resolvedVariants, variantProps);
1754
2145
  }
1755
2146
  return resolvedVariants as VariantValues<MergedVariants>;
1756
2147
  };
@@ -1777,9 +2168,13 @@ export function create(params: CreateParams = {}) {
1777
2168
  staticDefaults,
1778
2169
  compute: computeOnce,
1779
2170
  resolveRefine: resolveRefine ? resolveRefineOnce : null,
2171
+ resolveVariants: canResolveVariants ? resolveRefineOnce : undefined,
1780
2172
  transformClass,
1781
2173
  functionVariantKeys,
1782
2174
  computedDefaultKeys,
2175
+ activeComputedDefaultKeys,
2176
+ disabledKeys,
2177
+ isDisabledValue,
1783
2178
  };
1784
2179
 
1785
2180
  const initRecipe = <
@@ -1801,8 +2196,12 @@ export function create(params: CreateParams = {}) {
1801
2196
 
1802
2197
  // Default recipe
1803
2198
  const defaultRecipe = ((props: RecipeProps<MergedVariants> = {}) => {
1804
- const { className, style } = computeResult(props);
1805
- return { class: className, style };
2199
+ const { className, style, variants } = computeResult(props);
2200
+ const result = { class: className, style };
2201
+ if (debug) {
2202
+ setDebugAttribute(result, props, variants);
2203
+ }
2204
+ return result;
1806
2205
  }) as Recipe<V, E>;
1807
2206
  initRecipe(defaultRecipe, inputPropsKeys, (props = {}) => {
1808
2207
  return computeResult(props).style;
@@ -1810,8 +2209,12 @@ export function create(params: CreateParams = {}) {
1810
2209
 
1811
2210
  // JSX recipe
1812
2211
  const jsxRecipe = ((props: RecipeProps<MergedVariants> = {}) => {
1813
- const { className, style } = computeResult(props);
1814
- return { className, style };
2212
+ const { className, style, variants } = computeResult(props);
2213
+ const result = { className, style };
2214
+ if (debug) {
2215
+ setDebugAttribute(result, props, variants);
2216
+ }
2217
+ return result;
1815
2218
  }) as ModalRecipe<MergedVariants, JSXProps>;
1816
2219
  initRecipe(
1817
2220
  jsxRecipe,
@@ -1821,8 +2224,15 @@ export function create(params: CreateParams = {}) {
1821
2224
 
1822
2225
  // HTML recipe
1823
2226
  const htmlRecipe = ((props: RecipeProps<MergedVariants> = {}) => {
1824
- const { className, style } = computeResult(props);
1825
- return { class: className, style: styleValueToHTMLStyle(style) };
2227
+ const { className, style, variants } = computeResult(props);
2228
+ const result = {
2229
+ class: className,
2230
+ style: styleValueToHTMLStyle(style),
2231
+ };
2232
+ if (debug) {
2233
+ setDebugAttribute(result, props, variants);
2234
+ }
2235
+ return result;
1826
2236
  }) as ModalRecipe<MergedVariants, HTMLProps>;
1827
2237
  initRecipe(htmlRecipe, ["class", "style", ...variantKeys], (props = {}) =>
1828
2238
  styleValueToHTMLStyle(computeResult(props).style),
@@ -1830,8 +2240,15 @@ export function create(params: CreateParams = {}) {
1830
2240
 
1831
2241
  // HTMLObj recipe
1832
2242
  const htmlObjRecipe = ((props: RecipeProps<MergedVariants> = {}) => {
1833
- const { className, style } = computeResult(props);
1834
- return { class: className, style: styleValueToHTMLObjStyle(style) };
2243
+ const { className, style, variants } = computeResult(props);
2244
+ const result = {
2245
+ class: className,
2246
+ style: styleValueToHTMLObjStyle(style),
2247
+ };
2248
+ if (debug) {
2249
+ setDebugAttribute(result, props, variants);
2250
+ }
2251
+ return result;
1835
2252
  }) as ModalRecipe<MergedVariants, HTMLObjProps>;
1836
2253
  initRecipe(
1837
2254
  htmlObjRecipe,
@@ -1850,9 +2267,9 @@ export function create(params: CreateParams = {}) {
1850
2267
 
1851
2268
  const cv = <V extends Variants = {}, const E extends AnyRecipe[] = []>(
1852
2269
  config: CVConfig<V, E> = {},
1853
- ): Recipe<V, E> => buildRecipe(config).recipe;
2270
+ ): InferredRecipe<V, E> => buildRecipe(config).recipe;
1854
2271
 
1855
- return { cv, cx };
2272
+ return { cv, cx: transformedCx };
1856
2273
  }
1857
2274
 
1858
2275
  export const cv = /* @__PURE__ */ (() => create().cv)();