clava 0.7.1 → 0.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/README.md +32 -8
- package/dist/index.d.ts +78 -10
- package/dist/index.js +168 -45
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
- package/src/index.ts +386 -47
- package/src/types.ts +110 -9
package/src/index.ts
CHANGED
|
@@ -11,8 +11,11 @@ import type {
|
|
|
11
11
|
ClassValue,
|
|
12
12
|
DefaultVariants,
|
|
13
13
|
ExtendableVariants,
|
|
14
|
+
HTMLCSSProperties,
|
|
14
15
|
HTMLObjProps,
|
|
15
16
|
HTMLProps,
|
|
17
|
+
InferredRecipe,
|
|
18
|
+
JSXCSSProperties,
|
|
16
19
|
JSXProps,
|
|
17
20
|
KeySourceRecipe,
|
|
18
21
|
MergeVariants,
|
|
@@ -62,6 +65,7 @@ type ComputeOnceFn = (
|
|
|
62
65
|
protection?: RefineProtection | null,
|
|
63
66
|
defaultResolved?: Record<string, unknown>,
|
|
64
67
|
renderOnly?: boolean,
|
|
68
|
+
pass?: RefinePass | null,
|
|
65
69
|
) => Record<string, unknown>;
|
|
66
70
|
|
|
67
71
|
type ResolveRefineFn = (
|
|
@@ -76,6 +80,8 @@ type ResolveRefineOnceFn = (
|
|
|
76
80
|
filterOwnVariants?: boolean,
|
|
77
81
|
protection?: RefineProtection | null,
|
|
78
82
|
defaultResolved?: Record<string, unknown>,
|
|
83
|
+
pass?: RefinePass | null,
|
|
84
|
+
renderOnly?: boolean,
|
|
79
85
|
) => Record<string, unknown>;
|
|
80
86
|
|
|
81
87
|
type ComputedDefaultVariantFn = (
|
|
@@ -93,7 +99,13 @@ interface RecipeMeta {
|
|
|
93
99
|
// Performs a single compute pass for extending recipes, returning the
|
|
94
100
|
// resolved variants while pushing classes and styles into the output values.
|
|
95
101
|
compute: ComputeOnceFn;
|
|
102
|
+
// Set only when this recipe's chain has computed defaults or `refine`.
|
|
96
103
|
resolveRefine: ResolveRefineOnceFn | null;
|
|
104
|
+
// Mirrors the variant changes of `compute` without rendering. A recipe with
|
|
105
|
+
// no callbacks in its chain can still have it, because it can filter disabled
|
|
106
|
+
// values that a later extend's callbacks would otherwise read. Missing when
|
|
107
|
+
// the chain can't change variants, and in older Clava copies.
|
|
108
|
+
resolveVariants?: ResolveRefineOnceFn;
|
|
97
109
|
// Reference identity is used to detect mixed-factory `extend`. When a
|
|
98
110
|
// recipe is extended by a parent from a different `create()` call, the
|
|
99
111
|
// parent applies this transform to the extend's contribution before joining,
|
|
@@ -109,6 +121,16 @@ interface RecipeMeta {
|
|
|
109
121
|
// Child recipes use this to preserve inherited computed defaults through
|
|
110
122
|
// `defaultValue` without preserving their own prior computed result.
|
|
111
123
|
computedDefaultKeys: Set<string>;
|
|
124
|
+
// Variant keys whose computed defaults run in this recipe's pruned chain.
|
|
125
|
+
// Unlike `computedDefaultKeys`, a pruned shared ancestor doesn't add keys.
|
|
126
|
+
// Older Clava copies don't expose it.
|
|
127
|
+
activeComputedDefaultKeys?: Set<string>;
|
|
128
|
+
// Variant keys that this recipe's chain disables entirely or partly. Older
|
|
129
|
+
// Clava copies don't expose it or `isDisabledValue`.
|
|
130
|
+
disabledKeys?: Set<string>;
|
|
131
|
+
// Whether this recipe's chain removes the value for the key from the variants
|
|
132
|
+
// it returns. A recipe that defines a value again stops its removal.
|
|
133
|
+
isDisabledValue?: (key: string, value: unknown) => boolean;
|
|
112
134
|
}
|
|
113
135
|
|
|
114
136
|
const META_KEY = "__meta";
|
|
@@ -199,6 +221,72 @@ function mergeProtectedIntoBase(
|
|
|
199
221
|
return Object.assign({}, baseResolved, protectedVariants);
|
|
200
222
|
}
|
|
201
223
|
|
|
224
|
+
// A pass after the first one of a resolution loop. A computed default that
|
|
225
|
+
// writes a key that a later computed default computes again, such as a base that
|
|
226
|
+
// resets it to its static default, writes it to `chainValues` instead of the
|
|
227
|
+
// working variants. Only the later computed defaults for that key read it, so
|
|
228
|
+
// other callbacks keep the value that the previous pass settled on.
|
|
229
|
+
// See https://github.com/ariakit/clava/issues/613
|
|
230
|
+
interface RefinePass {
|
|
231
|
+
// One frame for each recipe on the current extend path.
|
|
232
|
+
frames: PendingFrame[];
|
|
233
|
+
// Values for keys that a later computed default still computes. The working
|
|
234
|
+
// variants hold `undefined` the same way as a missing key, so it marks a
|
|
235
|
+
// cleared key here too.
|
|
236
|
+
chainValues: Map<string, unknown> | null;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
interface PendingFrame {
|
|
240
|
+
// Keys that computed defaults compute after the current extend.
|
|
241
|
+
keys: ReadonlySet<string> | null;
|
|
242
|
+
// Whether the recipe removes a value after each extend. A computed default
|
|
243
|
+
// below it writes such a value to the working variants, as before, so the
|
|
244
|
+
// removal applies.
|
|
245
|
+
isDisabled: ((key: string, value: unknown) => boolean) | null;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function isPendingKey(frames: PendingFrame[], key: string): boolean {
|
|
249
|
+
for (let i = 0; i < frames.length; i++) {
|
|
250
|
+
if (frames[i]?.keys?.has(key)) return true;
|
|
251
|
+
}
|
|
252
|
+
return false;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function isDisabledOnPath(
|
|
256
|
+
frames: PendingFrame[],
|
|
257
|
+
key: string,
|
|
258
|
+
value: unknown,
|
|
259
|
+
): boolean {
|
|
260
|
+
for (let i = 0; i < frames.length; i++) {
|
|
261
|
+
if (frames[i]?.isDisabled?.(key, value)) return true;
|
|
262
|
+
}
|
|
263
|
+
return false;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// Chains that mix in recipes from an older Clava copy aren't supported. Their
|
|
267
|
+
// metadata lacks these keys, which then count as none instead of throwing.
|
|
268
|
+
const NO_KEYS: ReadonlySet<string> = new Set();
|
|
269
|
+
|
|
270
|
+
function getActiveComputedDefaultKeys(meta: RecipeMeta): ReadonlySet<string> {
|
|
271
|
+
return getOwn(meta, "activeComputedDefaultKeys") ?? NO_KEYS;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// Returns a copy of `variants` where `key` holds `value`, or is missing when
|
|
275
|
+
// `value` is `undefined`.
|
|
276
|
+
function withVariant(
|
|
277
|
+
variants: Record<string, unknown>,
|
|
278
|
+
key: string,
|
|
279
|
+
value: unknown,
|
|
280
|
+
): Record<string, unknown> {
|
|
281
|
+
const copy = Object.assign({}, variants);
|
|
282
|
+
if (value === undefined) {
|
|
283
|
+
delete copy[key];
|
|
284
|
+
} else {
|
|
285
|
+
copy[key] = value;
|
|
286
|
+
}
|
|
287
|
+
return copy;
|
|
288
|
+
}
|
|
289
|
+
|
|
202
290
|
export type {
|
|
203
291
|
ClassValue,
|
|
204
292
|
StyleValue,
|
|
@@ -207,8 +295,13 @@ export type {
|
|
|
207
295
|
JSXProps,
|
|
208
296
|
HTMLProps,
|
|
209
297
|
HTMLObjProps,
|
|
298
|
+
JSXCSSProperties,
|
|
299
|
+
HTMLCSSProperties,
|
|
300
|
+
ModalRecipe,
|
|
210
301
|
Recipe,
|
|
211
302
|
RecipeLike,
|
|
303
|
+
RecipeProps,
|
|
304
|
+
SplitPropsFunction,
|
|
212
305
|
};
|
|
213
306
|
|
|
214
307
|
/**
|
|
@@ -605,7 +698,11 @@ export function create(params: CreateParams = {}) {
|
|
|
605
698
|
// produces, including those of the package-level `cv`.
|
|
606
699
|
const transformClass = getOwn(params, "transformClass") ?? identityClass;
|
|
607
700
|
|
|
608
|
-
|
|
701
|
+
// The root `cx` type lets consumers emit `typeof cx` for an exported result.
|
|
702
|
+
// Otherwise, emit must name clsx's `ClassValue`, which pnpm installs hide.
|
|
703
|
+
const transformedCx: typeof cx = (...classes) => {
|
|
704
|
+
return transformClass(clsx(classes));
|
|
705
|
+
};
|
|
609
706
|
|
|
610
707
|
const buildRecipe = <
|
|
611
708
|
V extends Variants = {},
|
|
@@ -658,6 +755,8 @@ export function create(params: CreateParams = {}) {
|
|
|
658
755
|
const variantEntryDefs: PrebuiltVariant[] = [];
|
|
659
756
|
const functionVariantNames: string[] = [];
|
|
660
757
|
const functionVariantFns: Array<(value: unknown) => unknown> = [];
|
|
758
|
+
// Own variant definitions by name. `null` stands for a function variant.
|
|
759
|
+
const ownVariantDefs = new Map<string, PrebuiltVariant | null>();
|
|
661
760
|
if (variants) {
|
|
662
761
|
for (const name in variants) {
|
|
663
762
|
if (!hasOwn(variants, name)) continue;
|
|
@@ -671,9 +770,11 @@ export function create(params: CreateParams = {}) {
|
|
|
671
770
|
if (typeof variant === "function") {
|
|
672
771
|
functionVariantNames.push(name);
|
|
673
772
|
functionVariantFns.push(variant as (value: unknown) => unknown);
|
|
773
|
+
ownVariantDefs.set(name, null);
|
|
674
774
|
continue;
|
|
675
775
|
}
|
|
676
776
|
const prebuiltVariant = buildPrebuiltVariant(variant);
|
|
777
|
+
ownVariantDefs.set(name, prebuiltVariant);
|
|
677
778
|
variantEntryNames.push(name);
|
|
678
779
|
variantEntryDefs.push(prebuiltVariant);
|
|
679
780
|
if (prebuiltVariant.disabledValues) {
|
|
@@ -689,6 +790,17 @@ export function create(params: CreateParams = {}) {
|
|
|
689
790
|
}
|
|
690
791
|
}
|
|
691
792
|
}
|
|
793
|
+
const definesOwnValue = (key: string, value: unknown): boolean => {
|
|
794
|
+
const variant = ownVariantDefs.get(key);
|
|
795
|
+
if (variant === undefined) return false;
|
|
796
|
+
if (variant === null) return true;
|
|
797
|
+
if (!variant.values) return typeof value === "boolean";
|
|
798
|
+
// `false` is a valid value for a variant that only defines `true`.
|
|
799
|
+
if (value === false && variant.values.has("true")) return true;
|
|
800
|
+
const valueKey = getVariantValueKey(value);
|
|
801
|
+
return valueKey != null && variant.values.has(valueKey);
|
|
802
|
+
};
|
|
803
|
+
|
|
692
804
|
const variantKeys = Array.from(variantKeySet);
|
|
693
805
|
const variantKeysLength = variantKeys.length;
|
|
694
806
|
const variantEntryCount = variantEntryNames.length;
|
|
@@ -781,17 +893,26 @@ export function create(params: CreateParams = {}) {
|
|
|
781
893
|
}
|
|
782
894
|
}
|
|
783
895
|
|
|
784
|
-
//
|
|
785
|
-
//
|
|
896
|
+
// Count only extends with computed default or refine work in their chain.
|
|
897
|
+
// Those are the recipes that can change resolved variants across
|
|
786
898
|
// fixed-point iterations.
|
|
787
|
-
|
|
899
|
+
let extMetasWithRefineCount = 0;
|
|
788
900
|
for (let i = 0; i < extCount; i++) {
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
extMetasWithRefine.push(meta);
|
|
901
|
+
if (extMetas[i].resolveRefine) {
|
|
902
|
+
extMetasWithRefineCount += 1;
|
|
792
903
|
}
|
|
793
904
|
}
|
|
794
|
-
|
|
905
|
+
|
|
906
|
+
// The extends that can change variants for `getVariants`. Built once so
|
|
907
|
+
// that each pass skips the others without reading their metadata.
|
|
908
|
+
const extResolvers = extMetas.map(
|
|
909
|
+
(meta) => getOwn(meta, "resolveVariants") ?? meta.resolveRefine,
|
|
910
|
+
);
|
|
911
|
+
const canResolveVariants =
|
|
912
|
+
(hasExtend && hasAnyDisabled) ||
|
|
913
|
+
!!refine ||
|
|
914
|
+
computedDefaultCount > 0 ||
|
|
915
|
+
extResolvers.some(Boolean);
|
|
795
916
|
|
|
796
917
|
// Only a `refine` callback protects variants, through `setVariants`. This
|
|
797
918
|
// over-approximates: `extMetasWithRefineCount` also counts extends that
|
|
@@ -908,6 +1029,97 @@ export function create(params: CreateParams = {}) {
|
|
|
908
1029
|
return false;
|
|
909
1030
|
};
|
|
910
1031
|
|
|
1032
|
+
const isDisabledValue = (key: string, value: unknown): boolean => {
|
|
1033
|
+
if (isOwnDisabledValue(key, value)) return true;
|
|
1034
|
+
if (definesOwnValue(key, value)) return false;
|
|
1035
|
+
return extMetas.some((meta) =>
|
|
1036
|
+
getOwn(meta, "isDisabledValue")?.(key, value),
|
|
1037
|
+
);
|
|
1038
|
+
};
|
|
1039
|
+
|
|
1040
|
+
const disabledKeys = new Set(disabledVariantKeys);
|
|
1041
|
+
for (const key in disabledVariantValues) {
|
|
1042
|
+
if (!hasOwn(disabledVariantValues, key)) continue;
|
|
1043
|
+
disabledKeys.add(key);
|
|
1044
|
+
}
|
|
1045
|
+
// Own keys that an extend may disable, so an extend that doesn't disable
|
|
1046
|
+
// anything this recipe defines adds no work to each compute pass.
|
|
1047
|
+
const restorableKeys: string[] = [];
|
|
1048
|
+
for (const meta of extMetas) {
|
|
1049
|
+
const extDisabledKeys = getOwn(meta, "disabledKeys");
|
|
1050
|
+
if (!extDisabledKeys) continue;
|
|
1051
|
+
for (const key of extDisabledKeys) {
|
|
1052
|
+
disabledKeys.add(key);
|
|
1053
|
+
if (ownVariantDefs.has(key) && !restorableKeys.includes(key)) {
|
|
1054
|
+
restorableKeys.push(key);
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
// An extend removes the values that its chain disables, but this recipe
|
|
1060
|
+
// defines some of them again. Its value comes back after the extend runs,
|
|
1061
|
+
// so the extend's own callbacks still read the variants that it declares.
|
|
1062
|
+
const restoreOwnValues = (
|
|
1063
|
+
before: Record<string, unknown>,
|
|
1064
|
+
after: Record<string, unknown>,
|
|
1065
|
+
extMeta: RecipeMeta,
|
|
1066
|
+
fallback: Record<string, unknown>,
|
|
1067
|
+
protection: RefineProtection | null | undefined,
|
|
1068
|
+
): Record<string, unknown> => {
|
|
1069
|
+
let restored = after;
|
|
1070
|
+
for (const key of restorableKeys) {
|
|
1071
|
+
const value = getOwn(before, key);
|
|
1072
|
+
if (value === undefined) continue;
|
|
1073
|
+
if (!definesOwnValue(key, value)) continue;
|
|
1074
|
+
if (!getOwn(extMeta, "isDisabledValue")?.(key, value)) continue;
|
|
1075
|
+
const current = getOwn(after, key);
|
|
1076
|
+
// A disabling recipe without extends removes nothing.
|
|
1077
|
+
if (Object.is(current, value)) continue;
|
|
1078
|
+
// A different protected value comes from a callback, even a clear.
|
|
1079
|
+
const protectedVariants = protection?.variants;
|
|
1080
|
+
if (
|
|
1081
|
+
protectedVariants &&
|
|
1082
|
+
hasOwn(protectedVariants, key) &&
|
|
1083
|
+
!Object.is(protectedVariants[key], value)
|
|
1084
|
+
) {
|
|
1085
|
+
continue;
|
|
1086
|
+
}
|
|
1087
|
+
// The extend replaces a disabled value with the fallback. Any other
|
|
1088
|
+
// value comes from its callbacks.
|
|
1089
|
+
if (
|
|
1090
|
+
current !== undefined &&
|
|
1091
|
+
!Object.is(current, getOwn(fallback, key))
|
|
1092
|
+
) {
|
|
1093
|
+
continue;
|
|
1094
|
+
}
|
|
1095
|
+
if (restored === after) {
|
|
1096
|
+
restored = Object.assign({}, after);
|
|
1097
|
+
}
|
|
1098
|
+
restored[key] = value;
|
|
1099
|
+
}
|
|
1100
|
+
return restored;
|
|
1101
|
+
};
|
|
1102
|
+
|
|
1103
|
+
// The frame for each extend lists the keys that computed defaults
|
|
1104
|
+
// compute after it runs: those of later extends and this recipe's own.
|
|
1105
|
+
const extPendingFrames: Array<PendingFrame | null> = [];
|
|
1106
|
+
const laterComputedDefaultKeys = new Set(computedDefaultNames);
|
|
1107
|
+
for (let i = extCount - 1; i >= 0; i--) {
|
|
1108
|
+
const keys =
|
|
1109
|
+
laterComputedDefaultKeys.size > 0
|
|
1110
|
+
? new Set(laterComputedDefaultKeys)
|
|
1111
|
+
: null;
|
|
1112
|
+
extPendingFrames.unshift(
|
|
1113
|
+
keys || hasAnyDisabled
|
|
1114
|
+
? { keys, isDisabled: hasAnyDisabled ? isOwnDisabledValue : null }
|
|
1115
|
+
: null,
|
|
1116
|
+
);
|
|
1117
|
+
for (const key of getActiveComputedDefaultKeys(extMetas[i])) {
|
|
1118
|
+
laterComputedDefaultKeys.add(key);
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
const activeComputedDefaultKeys = laterComputedDefaultKeys;
|
|
1122
|
+
|
|
911
1123
|
const filterOwnDisabledVariants = (
|
|
912
1124
|
input: Record<string, unknown>,
|
|
913
1125
|
fallback: Record<string, unknown>,
|
|
@@ -949,6 +1161,26 @@ export function create(params: CreateParams = {}) {
|
|
|
949
1161
|
return filtered;
|
|
950
1162
|
};
|
|
951
1163
|
|
|
1164
|
+
// Applies `filterOwnDisabledVariants` to the values that computed defaults
|
|
1165
|
+
// keep out of the working variants, so later ones read the same value.
|
|
1166
|
+
const filterOwnDisabledChainValues = (
|
|
1167
|
+
chainValues: Map<string, unknown>,
|
|
1168
|
+
fallback: Record<string, unknown>,
|
|
1169
|
+
): void => {
|
|
1170
|
+
if (!hasAnyDisabled) return;
|
|
1171
|
+
for (const [key, value] of chainValues) {
|
|
1172
|
+
if (!isOwnDisabledValue(key, value)) continue;
|
|
1173
|
+
const fallbackValue = getOwn(fallback, key);
|
|
1174
|
+
chainValues.set(
|
|
1175
|
+
key,
|
|
1176
|
+
fallbackValue !== undefined &&
|
|
1177
|
+
!isOwnDisabledValue(key, fallbackValue)
|
|
1178
|
+
? fallbackValue
|
|
1179
|
+
: undefined,
|
|
1180
|
+
);
|
|
1181
|
+
}
|
|
1182
|
+
};
|
|
1183
|
+
|
|
952
1184
|
// Hot path: resolve variants by merging static defaults + extends'
|
|
953
1185
|
// static defaults + user-provided props.
|
|
954
1186
|
function resolveVariantsHot(
|
|
@@ -982,6 +1214,7 @@ export function create(params: CreateParams = {}) {
|
|
|
982
1214
|
userVariantProps: Record<string, unknown>,
|
|
983
1215
|
filterOwnVariants: boolean,
|
|
984
1216
|
protection: RefineProtection | null | undefined,
|
|
1217
|
+
pass: RefinePass | null | undefined,
|
|
985
1218
|
): Record<string, unknown> => {
|
|
986
1219
|
const protectedVariants = protection?.variants;
|
|
987
1220
|
let ownVariants = filterOwnVariants ? null : resolved;
|
|
@@ -1018,29 +1251,54 @@ export function create(params: CreateParams = {}) {
|
|
|
1018
1251
|
if (protectedVariants && hasOwn(protectedVariants, key)) continue;
|
|
1019
1252
|
|
|
1020
1253
|
const variantSnapshot = getOwnVariants();
|
|
1254
|
+
const currentValue = getOwn(variantSnapshot, key);
|
|
1255
|
+
// An earlier computed default may have kept its value for this key
|
|
1256
|
+
// out of the working variants. This one still reads that value as the
|
|
1257
|
+
// one it replaces, through `defaultValue` and through its own key.
|
|
1258
|
+
const chainValues = pass?.chainValues;
|
|
1259
|
+
const latestValue =
|
|
1260
|
+
chainValues && chainValues.has(key)
|
|
1261
|
+
? chainValues.get(key)
|
|
1262
|
+
: currentValue;
|
|
1021
1263
|
const defaultValue = inheritedComputedDefaultKeys.has(key)
|
|
1022
|
-
?
|
|
1264
|
+
? latestValue
|
|
1023
1265
|
: getOwn(defaultResolved, key);
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1266
|
+
let value = computedDefaultFns[i](
|
|
1267
|
+
defaultValue,
|
|
1268
|
+
Object.is(latestValue, currentValue)
|
|
1269
|
+
? variantSnapshot
|
|
1270
|
+
: withVariant(variantSnapshot, key, latestValue),
|
|
1271
|
+
);
|
|
1272
|
+
if (hasAnyDisabled && isOwnDisabledValue(key, value)) {
|
|
1273
|
+
// The latest value stays, as if this computed default didn't run.
|
|
1274
|
+
// When no later one computes the key, it becomes the final value.
|
|
1275
|
+
if (Object.is(latestValue, currentValue)) continue;
|
|
1276
|
+
if (pass && isPendingKey(pass.frames, key)) continue;
|
|
1277
|
+
value = latestValue;
|
|
1278
|
+
} else if (pass) {
|
|
1279
|
+
if (Object.is(value, currentValue)) {
|
|
1280
|
+
chainValues?.delete(key);
|
|
1281
|
+
continue;
|
|
1282
|
+
}
|
|
1283
|
+
if (
|
|
1284
|
+
isPendingKey(pass.frames, key) &&
|
|
1285
|
+
!isDisabledOnPath(pass.frames, key, value)
|
|
1286
|
+
) {
|
|
1287
|
+
pass.chainValues ??= new Map();
|
|
1288
|
+
pass.chainValues.set(key, value);
|
|
1289
|
+
continue;
|
|
1035
1290
|
}
|
|
1036
1291
|
}
|
|
1292
|
+
// The working variants receive this value, so a value that an
|
|
1293
|
+
// earlier computed default kept out of them no longer applies.
|
|
1294
|
+
chainValues?.delete(key);
|
|
1037
1295
|
|
|
1038
1296
|
if (value === undefined) {
|
|
1039
1297
|
if (!hasOwn(variantSnapshot, key)) continue;
|
|
1040
1298
|
delete ensureUpdated()[key];
|
|
1041
1299
|
continue;
|
|
1042
1300
|
}
|
|
1043
|
-
if (Object.is(
|
|
1301
|
+
if (Object.is(currentValue, value)) continue;
|
|
1044
1302
|
ensureUpdated()[key] = value;
|
|
1045
1303
|
}
|
|
1046
1304
|
|
|
@@ -1190,6 +1448,7 @@ export function create(params: CreateParams = {}) {
|
|
|
1190
1448
|
protection,
|
|
1191
1449
|
defaultResolved = resolved,
|
|
1192
1450
|
renderOnly = false,
|
|
1451
|
+
pass,
|
|
1193
1452
|
) => {
|
|
1194
1453
|
let workingResolved = resolved;
|
|
1195
1454
|
let cClasses: ClassValue[] | null = null;
|
|
@@ -1241,6 +1500,11 @@ export function create(params: CreateParams = {}) {
|
|
|
1241
1500
|
}
|
|
1242
1501
|
|
|
1243
1502
|
for (let i = 0; i < extCount; i++) {
|
|
1503
|
+
const pendingFrame = pass ? extPendingFrames[i] : null;
|
|
1504
|
+
if (pass && pendingFrame) {
|
|
1505
|
+
pass.frames.push(pendingFrame);
|
|
1506
|
+
}
|
|
1507
|
+
const beforeExtend = workingResolved;
|
|
1244
1508
|
if (hasIsolatedExt && extIsolated[i]) {
|
|
1245
1509
|
// Isolated extend (different factory): gather its variant classes
|
|
1246
1510
|
// into a scratch array, then push the joined string after applying
|
|
@@ -1256,6 +1520,7 @@ export function create(params: CreateParams = {}) {
|
|
|
1256
1520
|
protection,
|
|
1257
1521
|
defaultResolved,
|
|
1258
1522
|
renderOnly,
|
|
1523
|
+
pass,
|
|
1259
1524
|
);
|
|
1260
1525
|
if (extClasses.length > 0) {
|
|
1261
1526
|
const joined = clsx(extClasses);
|
|
@@ -1274,12 +1539,28 @@ export function create(params: CreateParams = {}) {
|
|
|
1274
1539
|
protection,
|
|
1275
1540
|
defaultResolved,
|
|
1276
1541
|
renderOnly,
|
|
1542
|
+
pass,
|
|
1543
|
+
);
|
|
1544
|
+
}
|
|
1545
|
+
if (pass && pendingFrame) {
|
|
1546
|
+
pass.frames.pop();
|
|
1547
|
+
}
|
|
1548
|
+
if (restorableKeys.length > 0) {
|
|
1549
|
+
workingResolved = restoreOwnValues(
|
|
1550
|
+
beforeExtend,
|
|
1551
|
+
workingResolved,
|
|
1552
|
+
extMetas[i],
|
|
1553
|
+
defaultResolved,
|
|
1554
|
+
protection,
|
|
1277
1555
|
);
|
|
1278
1556
|
}
|
|
1279
1557
|
workingResolved = filterOwnDisabledVariants(
|
|
1280
1558
|
workingResolved,
|
|
1281
1559
|
defaultResolved,
|
|
1282
1560
|
);
|
|
1561
|
+
if (pass?.chainValues) {
|
|
1562
|
+
filterOwnDisabledChainValues(pass.chainValues, defaultResolved);
|
|
1563
|
+
}
|
|
1283
1564
|
}
|
|
1284
1565
|
}
|
|
1285
1566
|
|
|
@@ -1292,6 +1573,7 @@ export function create(params: CreateParams = {}) {
|
|
|
1292
1573
|
userVariantProps,
|
|
1293
1574
|
true,
|
|
1294
1575
|
protection,
|
|
1576
|
+
pass,
|
|
1295
1577
|
);
|
|
1296
1578
|
}
|
|
1297
1579
|
|
|
@@ -1430,6 +1712,7 @@ export function create(params: CreateParams = {}) {
|
|
|
1430
1712
|
let lastClasses: ClsxClassValue[] = [];
|
|
1431
1713
|
let lastStyle: StyleValue = {};
|
|
1432
1714
|
let isFirstRun = true;
|
|
1715
|
+
let pass: RefinePass | null = null;
|
|
1433
1716
|
|
|
1434
1717
|
while (remaining > 0) {
|
|
1435
1718
|
remaining -= 1;
|
|
@@ -1443,6 +1726,10 @@ export function create(params: CreateParams = {}) {
|
|
|
1443
1726
|
incomingDefaultResolved,
|
|
1444
1727
|
protection,
|
|
1445
1728
|
);
|
|
1729
|
+
if (!isFirstRun) {
|
|
1730
|
+
pass ??= { frames: [], chainValues: null };
|
|
1731
|
+
pass.chainValues?.clear();
|
|
1732
|
+
}
|
|
1446
1733
|
const nextResolved = computeOnce(
|
|
1447
1734
|
workingResolved,
|
|
1448
1735
|
userVariantProps,
|
|
@@ -1452,21 +1739,19 @@ export function create(params: CreateParams = {}) {
|
|
|
1452
1739
|
nextStyle,
|
|
1453
1740
|
protection,
|
|
1454
1741
|
defaultResolved,
|
|
1742
|
+
false,
|
|
1743
|
+
pass,
|
|
1455
1744
|
);
|
|
1456
1745
|
|
|
1746
|
+
// No pass has settled before the first one, so it reads the
|
|
1747
|
+
// latest values. It settles only when it changed nothing,
|
|
1748
|
+
// because a later computed default may replace a value it read.
|
|
1457
1749
|
if (
|
|
1458
1750
|
nextResolved === workingResolved ||
|
|
1459
|
-
|
|
1751
|
+
(!isFirstRun &&
|
|
1752
|
+
areVariantsEqual(workingResolved, nextResolved))
|
|
1460
1753
|
) {
|
|
1461
1754
|
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
1755
|
computeOnce(
|
|
1471
1756
|
nextResolved,
|
|
1472
1757
|
userVariantProps,
|
|
@@ -1541,26 +1826,54 @@ export function create(params: CreateParams = {}) {
|
|
|
1541
1826
|
filterOwnVariants = true,
|
|
1542
1827
|
protection,
|
|
1543
1828
|
defaultResolved = resolved,
|
|
1829
|
+
pass,
|
|
1830
|
+
renderOnly = false,
|
|
1544
1831
|
) => {
|
|
1545
1832
|
let workingResolved = resolved;
|
|
1546
1833
|
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1834
|
+
// Visit every extend, as `computeOnce` does, so that disabled values are
|
|
1835
|
+
// removed at the same points. A later extend's callbacks can read them.
|
|
1836
|
+
for (let i = 0; i < extCount; i++) {
|
|
1837
|
+
const resolveExtend = extResolvers[i];
|
|
1838
|
+
const beforeExtend = workingResolved;
|
|
1839
|
+
if (resolveExtend) {
|
|
1840
|
+
const pendingFrame = pass ? extPendingFrames[i] : null;
|
|
1841
|
+
if (pass && pendingFrame) {
|
|
1842
|
+
pass.frames.push(pendingFrame);
|
|
1843
|
+
}
|
|
1844
|
+
workingResolved = resolveExtend(
|
|
1845
|
+
workingResolved,
|
|
1846
|
+
userVariantProps,
|
|
1847
|
+
true,
|
|
1848
|
+
protection,
|
|
1849
|
+
defaultResolved,
|
|
1850
|
+
pass,
|
|
1851
|
+
renderOnly,
|
|
1852
|
+
);
|
|
1853
|
+
if (pass && pendingFrame) {
|
|
1854
|
+
pass.frames.pop();
|
|
1855
|
+
}
|
|
1856
|
+
if (restorableKeys.length > 0) {
|
|
1857
|
+
workingResolved = restoreOwnValues(
|
|
1858
|
+
beforeExtend,
|
|
1859
|
+
workingResolved,
|
|
1860
|
+
extMetas[i],
|
|
1861
|
+
defaultResolved,
|
|
1862
|
+
protection,
|
|
1863
|
+
);
|
|
1864
|
+
}
|
|
1865
|
+
}
|
|
1558
1866
|
workingResolved = filterOwnDisabledVariants(
|
|
1559
1867
|
workingResolved,
|
|
1560
1868
|
defaultResolved,
|
|
1561
1869
|
);
|
|
1870
|
+
if (pass?.chainValues) {
|
|
1871
|
+
filterOwnDisabledChainValues(pass.chainValues, defaultResolved);
|
|
1872
|
+
}
|
|
1562
1873
|
}
|
|
1563
1874
|
|
|
1875
|
+
if (renderOnly) return workingResolved;
|
|
1876
|
+
|
|
1564
1877
|
if (computedDefaultCount > 0) {
|
|
1565
1878
|
workingResolved = runComputedDefaults(
|
|
1566
1879
|
workingResolved,
|
|
@@ -1568,6 +1881,7 @@ export function create(params: CreateParams = {}) {
|
|
|
1568
1881
|
userVariantProps,
|
|
1569
1882
|
filterOwnVariants,
|
|
1570
1883
|
protection,
|
|
1884
|
+
pass,
|
|
1571
1885
|
);
|
|
1572
1886
|
}
|
|
1573
1887
|
if (refine) {
|
|
@@ -1597,6 +1911,7 @@ export function create(params: CreateParams = {}) {
|
|
|
1597
1911
|
// tracking window. See the compute loop above for the shared
|
|
1598
1912
|
// rationale.
|
|
1599
1913
|
let unstableChanges: Map<string, VariantChange> | null = null;
|
|
1914
|
+
let pass: RefinePass | null = null;
|
|
1600
1915
|
|
|
1601
1916
|
while (remaining > 0) {
|
|
1602
1917
|
remaining -= 1;
|
|
@@ -1610,14 +1925,28 @@ export function create(params: CreateParams = {}) {
|
|
|
1610
1925
|
filterOwnVariants,
|
|
1611
1926
|
protection,
|
|
1612
1927
|
defaultResolved,
|
|
1928
|
+
pass,
|
|
1613
1929
|
);
|
|
1614
1930
|
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
) {
|
|
1931
|
+
// The first pass settles only when it changed nothing, for the
|
|
1932
|
+
// reason given in the compute loop above.
|
|
1933
|
+
if (nextResolved === workingResolved) {
|
|
1619
1934
|
return nextResolved;
|
|
1620
1935
|
}
|
|
1936
|
+
if (pass && areVariantsEqual(workingResolved, nextResolved)) {
|
|
1937
|
+
// Match the final pass of the compute loop above, which
|
|
1938
|
+
// removes a disabled value that a callback set after the
|
|
1939
|
+
// extend that disables it.
|
|
1940
|
+
return resolveRefineOnce(
|
|
1941
|
+
nextResolved,
|
|
1942
|
+
userVariantProps,
|
|
1943
|
+
filterOwnVariants,
|
|
1944
|
+
protection,
|
|
1945
|
+
defaultResolved,
|
|
1946
|
+
null,
|
|
1947
|
+
true,
|
|
1948
|
+
);
|
|
1949
|
+
}
|
|
1621
1950
|
|
|
1622
1951
|
// `remaining` is checked first for the reason given in the
|
|
1623
1952
|
// compute loop above.
|
|
@@ -1635,6 +1964,8 @@ export function create(params: CreateParams = {}) {
|
|
|
1635
1964
|
);
|
|
1636
1965
|
}
|
|
1637
1966
|
workingResolved = nextResolved;
|
|
1967
|
+
pass ??= { frames: [], chainValues: null };
|
|
1968
|
+
pass.chainValues?.clear();
|
|
1638
1969
|
}
|
|
1639
1970
|
|
|
1640
1971
|
if (process.env.NODE_ENV !== "production") {
|
|
@@ -1751,6 +2082,10 @@ export function create(params: CreateParams = {}) {
|
|
|
1751
2082
|
variantProps,
|
|
1752
2083
|
false,
|
|
1753
2084
|
);
|
|
2085
|
+
} else if (canResolveVariants) {
|
|
2086
|
+
// No callback in this chain can restore a removed value, so one pass
|
|
2087
|
+
// reaches the state that render ends with.
|
|
2088
|
+
resolvedVariants = resolveRefineOnce(resolvedVariants, variantProps);
|
|
1754
2089
|
}
|
|
1755
2090
|
return resolvedVariants as VariantValues<MergedVariants>;
|
|
1756
2091
|
};
|
|
@@ -1777,9 +2112,13 @@ export function create(params: CreateParams = {}) {
|
|
|
1777
2112
|
staticDefaults,
|
|
1778
2113
|
compute: computeOnce,
|
|
1779
2114
|
resolveRefine: resolveRefine ? resolveRefineOnce : null,
|
|
2115
|
+
resolveVariants: canResolveVariants ? resolveRefineOnce : undefined,
|
|
1780
2116
|
transformClass,
|
|
1781
2117
|
functionVariantKeys,
|
|
1782
2118
|
computedDefaultKeys,
|
|
2119
|
+
activeComputedDefaultKeys,
|
|
2120
|
+
disabledKeys,
|
|
2121
|
+
isDisabledValue,
|
|
1783
2122
|
};
|
|
1784
2123
|
|
|
1785
2124
|
const initRecipe = <
|
|
@@ -1850,9 +2189,9 @@ export function create(params: CreateParams = {}) {
|
|
|
1850
2189
|
|
|
1851
2190
|
const cv = <V extends Variants = {}, const E extends AnyRecipe[] = []>(
|
|
1852
2191
|
config: CVConfig<V, E> = {},
|
|
1853
|
-
):
|
|
2192
|
+
): InferredRecipe<V, E> => buildRecipe(config).recipe;
|
|
1854
2193
|
|
|
1855
|
-
return { cv, cx };
|
|
2194
|
+
return { cv, cx: transformedCx };
|
|
1856
2195
|
}
|
|
1857
2196
|
|
|
1858
2197
|
export const cv = /* @__PURE__ */ (() => create().cv)();
|