clava 0.7.0 → 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 +38 -0
- package/README.md +44 -8
- package/dist/index.d.ts +84 -11
- package/dist/index.js +168 -45
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
- package/src/index.ts +403 -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
|
/**
|
|
@@ -247,6 +340,9 @@ export type VariantProps<T extends Pick<AnyRecipe, "getVariants">> = ReturnType<
|
|
|
247
340
|
* and required when the selected recipe adds variants. Pass a third type
|
|
248
341
|
* argument to use another prop name.
|
|
249
342
|
*
|
|
343
|
+
* When `R` is a union of recipes, the variant props include only the
|
|
344
|
+
* variants that every member defines.
|
|
345
|
+
*
|
|
250
346
|
* When inferring `R`, passing `undefined` or forwarding an optional recipe
|
|
251
347
|
* can fail the recipe constraint. Resolve the optional value before passing
|
|
252
348
|
* it, for example with `recipe: override ?? disclosure`.
|
|
@@ -271,8 +367,22 @@ export type VariantPropsWithRecipe<
|
|
|
271
367
|
R extends RecipeLike<Base, R>,
|
|
272
368
|
Name extends string = "recipe",
|
|
273
369
|
> = VariantProps<R> &
|
|
370
|
+
PartialVariantKeys<R> &
|
|
274
371
|
([R] extends [Base] ? { [Key in Name]?: R } : { [Key in Name]: R });
|
|
275
372
|
|
|
373
|
+
// Blocks variants that only some union members define, because the selected
|
|
374
|
+
// member may not process them. With no such keys, this is `unknown` so props
|
|
375
|
+
// keep their exact shape instead of gaining an empty `{}` intersection member.
|
|
376
|
+
type PartialVariantKeys<
|
|
377
|
+
R extends Pick<AnyRecipe, "getVariants">,
|
|
378
|
+
Key extends PropertyKey = Exclude<
|
|
379
|
+
UnionKeys<VariantProps<R>>,
|
|
380
|
+
keyof VariantProps<R>
|
|
381
|
+
>,
|
|
382
|
+
> = [Key] extends [never] ? unknown : { [K in Key]?: never };
|
|
383
|
+
|
|
384
|
+
type UnionKeys<T> = T extends unknown ? keyof T : never;
|
|
385
|
+
|
|
276
386
|
// Variant props expose booleans, but variant object keys are always strings.
|
|
277
387
|
type VariantKey<T> = T extends boolean ? "true" | "false" : Extract<T, string>;
|
|
278
388
|
|
|
@@ -588,7 +698,11 @@ export function create(params: CreateParams = {}) {
|
|
|
588
698
|
// produces, including those of the package-level `cv`.
|
|
589
699
|
const transformClass = getOwn(params, "transformClass") ?? identityClass;
|
|
590
700
|
|
|
591
|
-
|
|
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
|
+
};
|
|
592
706
|
|
|
593
707
|
const buildRecipe = <
|
|
594
708
|
V extends Variants = {},
|
|
@@ -641,6 +755,8 @@ export function create(params: CreateParams = {}) {
|
|
|
641
755
|
const variantEntryDefs: PrebuiltVariant[] = [];
|
|
642
756
|
const functionVariantNames: string[] = [];
|
|
643
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>();
|
|
644
760
|
if (variants) {
|
|
645
761
|
for (const name in variants) {
|
|
646
762
|
if (!hasOwn(variants, name)) continue;
|
|
@@ -654,9 +770,11 @@ export function create(params: CreateParams = {}) {
|
|
|
654
770
|
if (typeof variant === "function") {
|
|
655
771
|
functionVariantNames.push(name);
|
|
656
772
|
functionVariantFns.push(variant as (value: unknown) => unknown);
|
|
773
|
+
ownVariantDefs.set(name, null);
|
|
657
774
|
continue;
|
|
658
775
|
}
|
|
659
776
|
const prebuiltVariant = buildPrebuiltVariant(variant);
|
|
777
|
+
ownVariantDefs.set(name, prebuiltVariant);
|
|
660
778
|
variantEntryNames.push(name);
|
|
661
779
|
variantEntryDefs.push(prebuiltVariant);
|
|
662
780
|
if (prebuiltVariant.disabledValues) {
|
|
@@ -672,6 +790,17 @@ export function create(params: CreateParams = {}) {
|
|
|
672
790
|
}
|
|
673
791
|
}
|
|
674
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
|
+
|
|
675
804
|
const variantKeys = Array.from(variantKeySet);
|
|
676
805
|
const variantKeysLength = variantKeys.length;
|
|
677
806
|
const variantEntryCount = variantEntryNames.length;
|
|
@@ -764,17 +893,26 @@ export function create(params: CreateParams = {}) {
|
|
|
764
893
|
}
|
|
765
894
|
}
|
|
766
895
|
|
|
767
|
-
//
|
|
768
|
-
//
|
|
896
|
+
// Count only extends with computed default or refine work in their chain.
|
|
897
|
+
// Those are the recipes that can change resolved variants across
|
|
769
898
|
// fixed-point iterations.
|
|
770
|
-
|
|
899
|
+
let extMetasWithRefineCount = 0;
|
|
771
900
|
for (let i = 0; i < extCount; i++) {
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
extMetasWithRefine.push(meta);
|
|
901
|
+
if (extMetas[i].resolveRefine) {
|
|
902
|
+
extMetasWithRefineCount += 1;
|
|
775
903
|
}
|
|
776
904
|
}
|
|
777
|
-
|
|
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);
|
|
778
916
|
|
|
779
917
|
// Only a `refine` callback protects variants, through `setVariants`. This
|
|
780
918
|
// over-approximates: `extMetasWithRefineCount` also counts extends that
|
|
@@ -891,6 +1029,97 @@ export function create(params: CreateParams = {}) {
|
|
|
891
1029
|
return false;
|
|
892
1030
|
};
|
|
893
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
|
+
|
|
894
1123
|
const filterOwnDisabledVariants = (
|
|
895
1124
|
input: Record<string, unknown>,
|
|
896
1125
|
fallback: Record<string, unknown>,
|
|
@@ -932,6 +1161,26 @@ export function create(params: CreateParams = {}) {
|
|
|
932
1161
|
return filtered;
|
|
933
1162
|
};
|
|
934
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
|
+
|
|
935
1184
|
// Hot path: resolve variants by merging static defaults + extends'
|
|
936
1185
|
// static defaults + user-provided props.
|
|
937
1186
|
function resolveVariantsHot(
|
|
@@ -965,6 +1214,7 @@ export function create(params: CreateParams = {}) {
|
|
|
965
1214
|
userVariantProps: Record<string, unknown>,
|
|
966
1215
|
filterOwnVariants: boolean,
|
|
967
1216
|
protection: RefineProtection | null | undefined,
|
|
1217
|
+
pass: RefinePass | null | undefined,
|
|
968
1218
|
): Record<string, unknown> => {
|
|
969
1219
|
const protectedVariants = protection?.variants;
|
|
970
1220
|
let ownVariants = filterOwnVariants ? null : resolved;
|
|
@@ -1001,29 +1251,54 @@ export function create(params: CreateParams = {}) {
|
|
|
1001
1251
|
if (protectedVariants && hasOwn(protectedVariants, key)) continue;
|
|
1002
1252
|
|
|
1003
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;
|
|
1004
1263
|
const defaultValue = inheritedComputedDefaultKeys.has(key)
|
|
1005
|
-
?
|
|
1264
|
+
? latestValue
|
|
1006
1265
|
: getOwn(defaultResolved, key);
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
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;
|
|
1018
1290
|
}
|
|
1019
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);
|
|
1020
1295
|
|
|
1021
1296
|
if (value === undefined) {
|
|
1022
1297
|
if (!hasOwn(variantSnapshot, key)) continue;
|
|
1023
1298
|
delete ensureUpdated()[key];
|
|
1024
1299
|
continue;
|
|
1025
1300
|
}
|
|
1026
|
-
if (Object.is(
|
|
1301
|
+
if (Object.is(currentValue, value)) continue;
|
|
1027
1302
|
ensureUpdated()[key] = value;
|
|
1028
1303
|
}
|
|
1029
1304
|
|
|
@@ -1173,6 +1448,7 @@ export function create(params: CreateParams = {}) {
|
|
|
1173
1448
|
protection,
|
|
1174
1449
|
defaultResolved = resolved,
|
|
1175
1450
|
renderOnly = false,
|
|
1451
|
+
pass,
|
|
1176
1452
|
) => {
|
|
1177
1453
|
let workingResolved = resolved;
|
|
1178
1454
|
let cClasses: ClassValue[] | null = null;
|
|
@@ -1224,6 +1500,11 @@ export function create(params: CreateParams = {}) {
|
|
|
1224
1500
|
}
|
|
1225
1501
|
|
|
1226
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;
|
|
1227
1508
|
if (hasIsolatedExt && extIsolated[i]) {
|
|
1228
1509
|
// Isolated extend (different factory): gather its variant classes
|
|
1229
1510
|
// into a scratch array, then push the joined string after applying
|
|
@@ -1239,6 +1520,7 @@ export function create(params: CreateParams = {}) {
|
|
|
1239
1520
|
protection,
|
|
1240
1521
|
defaultResolved,
|
|
1241
1522
|
renderOnly,
|
|
1523
|
+
pass,
|
|
1242
1524
|
);
|
|
1243
1525
|
if (extClasses.length > 0) {
|
|
1244
1526
|
const joined = clsx(extClasses);
|
|
@@ -1257,12 +1539,28 @@ export function create(params: CreateParams = {}) {
|
|
|
1257
1539
|
protection,
|
|
1258
1540
|
defaultResolved,
|
|
1259
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,
|
|
1260
1555
|
);
|
|
1261
1556
|
}
|
|
1262
1557
|
workingResolved = filterOwnDisabledVariants(
|
|
1263
1558
|
workingResolved,
|
|
1264
1559
|
defaultResolved,
|
|
1265
1560
|
);
|
|
1561
|
+
if (pass?.chainValues) {
|
|
1562
|
+
filterOwnDisabledChainValues(pass.chainValues, defaultResolved);
|
|
1563
|
+
}
|
|
1266
1564
|
}
|
|
1267
1565
|
}
|
|
1268
1566
|
|
|
@@ -1275,6 +1573,7 @@ export function create(params: CreateParams = {}) {
|
|
|
1275
1573
|
userVariantProps,
|
|
1276
1574
|
true,
|
|
1277
1575
|
protection,
|
|
1576
|
+
pass,
|
|
1278
1577
|
);
|
|
1279
1578
|
}
|
|
1280
1579
|
|
|
@@ -1413,6 +1712,7 @@ export function create(params: CreateParams = {}) {
|
|
|
1413
1712
|
let lastClasses: ClsxClassValue[] = [];
|
|
1414
1713
|
let lastStyle: StyleValue = {};
|
|
1415
1714
|
let isFirstRun = true;
|
|
1715
|
+
let pass: RefinePass | null = null;
|
|
1416
1716
|
|
|
1417
1717
|
while (remaining > 0) {
|
|
1418
1718
|
remaining -= 1;
|
|
@@ -1426,6 +1726,10 @@ export function create(params: CreateParams = {}) {
|
|
|
1426
1726
|
incomingDefaultResolved,
|
|
1427
1727
|
protection,
|
|
1428
1728
|
);
|
|
1729
|
+
if (!isFirstRun) {
|
|
1730
|
+
pass ??= { frames: [], chainValues: null };
|
|
1731
|
+
pass.chainValues?.clear();
|
|
1732
|
+
}
|
|
1429
1733
|
const nextResolved = computeOnce(
|
|
1430
1734
|
workingResolved,
|
|
1431
1735
|
userVariantProps,
|
|
@@ -1435,21 +1739,19 @@ export function create(params: CreateParams = {}) {
|
|
|
1435
1739
|
nextStyle,
|
|
1436
1740
|
protection,
|
|
1437
1741
|
defaultResolved,
|
|
1742
|
+
false,
|
|
1743
|
+
pass,
|
|
1438
1744
|
);
|
|
1439
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.
|
|
1440
1749
|
if (
|
|
1441
1750
|
nextResolved === workingResolved ||
|
|
1442
|
-
|
|
1751
|
+
(!isFirstRun &&
|
|
1752
|
+
areVariantsEqual(workingResolved, nextResolved))
|
|
1443
1753
|
) {
|
|
1444
1754
|
if (nextResolved !== workingResolved) {
|
|
1445
|
-
if (useDirectOutput) {
|
|
1446
|
-
classesOut.length = classCount;
|
|
1447
|
-
for (const key in styleOut) {
|
|
1448
|
-
if (hasOwn(styleOut, key)) {
|
|
1449
|
-
Reflect.deleteProperty(styleOut, key);
|
|
1450
|
-
}
|
|
1451
|
-
}
|
|
1452
|
-
}
|
|
1453
1755
|
computeOnce(
|
|
1454
1756
|
nextResolved,
|
|
1455
1757
|
userVariantProps,
|
|
@@ -1524,26 +1826,54 @@ export function create(params: CreateParams = {}) {
|
|
|
1524
1826
|
filterOwnVariants = true,
|
|
1525
1827
|
protection,
|
|
1526
1828
|
defaultResolved = resolved,
|
|
1829
|
+
pass,
|
|
1830
|
+
renderOnly = false,
|
|
1527
1831
|
) => {
|
|
1528
1832
|
let workingResolved = resolved;
|
|
1529
1833
|
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
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
|
+
}
|
|
1541
1866
|
workingResolved = filterOwnDisabledVariants(
|
|
1542
1867
|
workingResolved,
|
|
1543
1868
|
defaultResolved,
|
|
1544
1869
|
);
|
|
1870
|
+
if (pass?.chainValues) {
|
|
1871
|
+
filterOwnDisabledChainValues(pass.chainValues, defaultResolved);
|
|
1872
|
+
}
|
|
1545
1873
|
}
|
|
1546
1874
|
|
|
1875
|
+
if (renderOnly) return workingResolved;
|
|
1876
|
+
|
|
1547
1877
|
if (computedDefaultCount > 0) {
|
|
1548
1878
|
workingResolved = runComputedDefaults(
|
|
1549
1879
|
workingResolved,
|
|
@@ -1551,6 +1881,7 @@ export function create(params: CreateParams = {}) {
|
|
|
1551
1881
|
userVariantProps,
|
|
1552
1882
|
filterOwnVariants,
|
|
1553
1883
|
protection,
|
|
1884
|
+
pass,
|
|
1554
1885
|
);
|
|
1555
1886
|
}
|
|
1556
1887
|
if (refine) {
|
|
@@ -1580,6 +1911,7 @@ export function create(params: CreateParams = {}) {
|
|
|
1580
1911
|
// tracking window. See the compute loop above for the shared
|
|
1581
1912
|
// rationale.
|
|
1582
1913
|
let unstableChanges: Map<string, VariantChange> | null = null;
|
|
1914
|
+
let pass: RefinePass | null = null;
|
|
1583
1915
|
|
|
1584
1916
|
while (remaining > 0) {
|
|
1585
1917
|
remaining -= 1;
|
|
@@ -1593,14 +1925,28 @@ export function create(params: CreateParams = {}) {
|
|
|
1593
1925
|
filterOwnVariants,
|
|
1594
1926
|
protection,
|
|
1595
1927
|
defaultResolved,
|
|
1928
|
+
pass,
|
|
1596
1929
|
);
|
|
1597
1930
|
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
) {
|
|
1931
|
+
// The first pass settles only when it changed nothing, for the
|
|
1932
|
+
// reason given in the compute loop above.
|
|
1933
|
+
if (nextResolved === workingResolved) {
|
|
1602
1934
|
return nextResolved;
|
|
1603
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
|
+
}
|
|
1604
1950
|
|
|
1605
1951
|
// `remaining` is checked first for the reason given in the
|
|
1606
1952
|
// compute loop above.
|
|
@@ -1618,6 +1964,8 @@ export function create(params: CreateParams = {}) {
|
|
|
1618
1964
|
);
|
|
1619
1965
|
}
|
|
1620
1966
|
workingResolved = nextResolved;
|
|
1967
|
+
pass ??= { frames: [], chainValues: null };
|
|
1968
|
+
pass.chainValues?.clear();
|
|
1621
1969
|
}
|
|
1622
1970
|
|
|
1623
1971
|
if (process.env.NODE_ENV !== "production") {
|
|
@@ -1734,6 +2082,10 @@ export function create(params: CreateParams = {}) {
|
|
|
1734
2082
|
variantProps,
|
|
1735
2083
|
false,
|
|
1736
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);
|
|
1737
2089
|
}
|
|
1738
2090
|
return resolvedVariants as VariantValues<MergedVariants>;
|
|
1739
2091
|
};
|
|
@@ -1760,9 +2112,13 @@ export function create(params: CreateParams = {}) {
|
|
|
1760
2112
|
staticDefaults,
|
|
1761
2113
|
compute: computeOnce,
|
|
1762
2114
|
resolveRefine: resolveRefine ? resolveRefineOnce : null,
|
|
2115
|
+
resolveVariants: canResolveVariants ? resolveRefineOnce : undefined,
|
|
1763
2116
|
transformClass,
|
|
1764
2117
|
functionVariantKeys,
|
|
1765
2118
|
computedDefaultKeys,
|
|
2119
|
+
activeComputedDefaultKeys,
|
|
2120
|
+
disabledKeys,
|
|
2121
|
+
isDisabledValue,
|
|
1766
2122
|
};
|
|
1767
2123
|
|
|
1768
2124
|
const initRecipe = <
|
|
@@ -1833,9 +2189,9 @@ export function create(params: CreateParams = {}) {
|
|
|
1833
2189
|
|
|
1834
2190
|
const cv = <V extends Variants = {}, const E extends AnyRecipe[] = []>(
|
|
1835
2191
|
config: CVConfig<V, E> = {},
|
|
1836
|
-
):
|
|
2192
|
+
): InferredRecipe<V, E> => buildRecipe(config).recipe;
|
|
1837
2193
|
|
|
1838
|
-
return { cv, cx };
|
|
2194
|
+
return { cv, cx: transformedCx };
|
|
1839
2195
|
}
|
|
1840
2196
|
|
|
1841
2197
|
export const cv = /* @__PURE__ */ (() => create().cv)();
|