clava 0.6.3 → 0.7.0

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
@@ -7,19 +7,20 @@ import {
7
7
  warnRefineLimit,
8
8
  } from "./refine-warning.ts";
9
9
  import type {
10
- AnyComponent,
11
- CVComponent,
10
+ AnyRecipe,
12
11
  ClassValue,
13
- ComponentProps,
14
- ComponentResult,
15
12
  DefaultVariants,
16
13
  ExtendableVariants,
17
14
  HTMLObjProps,
18
15
  HTMLProps,
19
16
  JSXProps,
20
- KeySourceComponent,
17
+ KeySourceRecipe,
21
18
  MergeVariants,
22
- ModalComponent,
19
+ ModalRecipe,
20
+ Recipe,
21
+ RecipeLike,
22
+ RecipeProps,
23
+ RecipeResult,
23
24
  Refine,
24
25
  SplitPropsFunction,
25
26
  StyleClassProps,
@@ -38,8 +39,8 @@ import {
38
39
  } from "./utils.ts";
39
40
 
40
41
  // Internal compute path: pushes the variant classes contributed by this
41
- // component (and its extends chain) into `classesOut` and merges any styles
42
- // into `styleOut`. Base class is handled by callers via ComponentMeta.baseClass
42
+ // recipe (and its extends chain) into `classesOut` and merges any styles
43
+ // into `styleOut`. Base class is handled by callers via RecipeMeta.baseClass
43
44
  // to avoid string-parsing round trips. Both outputs are mutated in place to
44
45
  // avoid intermediate allocations.
45
46
  type ComputeFn = (
@@ -82,39 +83,79 @@ type ComputedDefaultVariantFn = (
82
83
  variants: Readonly<Record<string, unknown>>,
83
84
  ) => unknown;
84
85
 
85
- // Internal metadata stored on components but hidden from public types.
86
- interface ComponentMeta {
86
+ // Internal metadata stored on recipes but hidden from public types.
87
+ interface RecipeMeta {
88
+ identity?: object;
89
+ extends?: RecipeMeta[];
90
+ rebuild?: (extensions: RecipeMeta[]) => RecipeMeta;
87
91
  baseClass: string;
88
92
  staticDefaults: Record<string, unknown>;
89
- // Performs a single compute pass for extending components, returning the
93
+ // Performs a single compute pass for extending recipes, returning the
90
94
  // resolved variants while pushing classes and styles into the output values.
91
95
  compute: ComputeOnceFn;
92
96
  resolveRefine: ResolveRefineOnceFn | null;
93
97
  // Reference identity is used to detect mixed-factory `extend`. When a
94
- // component is extended by a parent from a different `create()` call, the
98
+ // recipe is extended by a parent from a different `create()` call, the
95
99
  // parent applies this transform to the extend's contribution before joining,
96
100
  // preserving each factory's transform boundary.
97
101
  transformClass: (className: string) => string;
98
- // Variant keys whose effective definition in this component's chain is a
99
- // function. An extending component that supplies a non-function variant for
102
+ // Variant keys whose effective definition in this recipe's chain is a
103
+ // function. An extending recipe that supplies a non-function variant for
100
104
  // the same key uses this to tell us to skip that key (matching the
101
105
  // type-level "function variant is replaced by anything in the child" rule).
102
106
  // Empty when no key in this chain is a function variant.
103
107
  functionVariantKeys: Set<string>;
104
- // Variant keys with computed defaults anywhere in this component's chain.
105
- // Child components use this to preserve inherited computed defaults through
108
+ // Variant keys with computed defaults anywhere in this recipe's chain.
109
+ // Child recipes use this to preserve inherited computed defaults through
106
110
  // `defaultValue` without preserving their own prior computed result.
107
111
  computedDefaultKeys: Set<string>;
108
112
  }
109
113
 
110
114
  const META_KEY = "__meta";
111
115
 
112
- interface ComponentWithMeta {
113
- [META_KEY]?: ComponentMeta;
116
+ interface RecipeWithMeta {
117
+ [META_KEY]?: RecipeMeta;
114
118
  }
115
119
 
116
120
  const EMPTY_DEFAULTS: Record<string, unknown> = {};
117
121
 
122
+ // Prune shared recipes before compiling their parent's compute path. Keeping
123
+ // the first path preserves its overrides and factory transform boundaries.
124
+ function deduplicateExtensions(
125
+ extensions: RecipeMeta[],
126
+ seen = new Set<object>(),
127
+ ): RecipeMeta[] {
128
+ if (extensions.length === 0) {
129
+ return extensions;
130
+ }
131
+ const result: RecipeMeta[] = [];
132
+ let changed = false;
133
+ for (const meta of extensions) {
134
+ const identity = getOwn(meta, "identity") ?? meta;
135
+ if (seen.has(identity)) {
136
+ changed = true;
137
+ continue;
138
+ }
139
+ seen.add(identity);
140
+ // Older Clava copies expose the compute metadata but not their graph.
141
+ // Keep their contribution intact instead of traversing hidden ancestors.
142
+ const inheritedExtensions = getOwn(meta, "extends");
143
+ const rebuild = getOwn(meta, "rebuild");
144
+ if (!inheritedExtensions || !rebuild) {
145
+ result.push(meta);
146
+ continue;
147
+ }
148
+ const children = deduplicateExtensions(inheritedExtensions, seen);
149
+ if (children === inheritedExtensions) {
150
+ result.push(meta);
151
+ } else {
152
+ result.push(rebuild(children));
153
+ changed = true;
154
+ }
155
+ }
156
+ return changed ? result : extensions;
157
+ }
158
+
118
159
  const MAX_REFINE_RUNS = 50;
119
160
 
120
161
  function areVariantsEqual(
@@ -166,12 +207,13 @@ export type {
166
207
  JSXProps,
167
208
  HTMLProps,
168
209
  HTMLObjProps,
169
- CVComponent,
210
+ Recipe,
211
+ RecipeLike,
170
212
  };
171
213
 
172
214
  /**
173
- * Extracts the variant props inferred for a Clava component. Use it to add a
174
- * component's variant props to framework component props.
215
+ * Extracts the variant props inferred for a Clava recipe. Use it to add a
216
+ * recipe's variant props to framework component props.
175
217
  *
176
218
  * @example
177
219
  * ```ts
@@ -195,15 +237,48 @@ export type {
195
237
  * };
196
238
  * ```
197
239
  */
198
- export type VariantProps<T extends Pick<AnyComponent, "getVariants">> =
199
- ReturnType<T["getVariants"]>;
240
+ export type VariantProps<T extends Pick<AnyRecipe, "getVariants">> = ReturnType<
241
+ T["getVariants"]
242
+ >;
243
+
244
+ /**
245
+ * Combines variant props with a recipe prop named `recipe` by default. The
246
+ * prop is optional when the selected recipe is assignable to the base type,
247
+ * and required when the selected recipe adds variants. Pass a third type
248
+ * argument to use another prop name.
249
+ *
250
+ * When inferring `R`, passing `undefined` or forwarding an optional recipe
251
+ * can fail the recipe constraint. Resolve the optional value before passing
252
+ * it, for example with `recipe: override ?? disclosure`.
253
+ *
254
+ * @example
255
+ * ```ts
256
+ * import { type RecipeLike, type VariantPropsWithRecipe, cv } from "clava";
257
+ *
258
+ * const disclosure = cv({ variants: { $open: "open" } });
259
+ *
260
+ * type DisclosureProps<
261
+ * R extends RecipeLike<typeof disclosure, R> = typeof disclosure,
262
+ * > = VariantPropsWithRecipe<typeof disclosure, R>;
263
+ *
264
+ * type StyledDisclosureProps<
265
+ * R extends RecipeLike<typeof disclosure, R> = typeof disclosure,
266
+ * > = VariantPropsWithRecipe<typeof disclosure, R, "styles">;
267
+ * ```
268
+ */
269
+ export type VariantPropsWithRecipe<
270
+ Base extends Recipe<any, any, any>,
271
+ R extends RecipeLike<Base, R>,
272
+ Name extends string = "recipe",
273
+ > = VariantProps<R> &
274
+ ([R] extends [Base] ? { [Key in Name]?: R } : { [Key in Name]: R });
200
275
 
201
276
  // Variant props expose booleans, but variant object keys are always strings.
202
277
  type VariantKey<T> = T extends boolean ? "true" | "false" : Extract<T, string>;
203
278
 
204
279
  /**
205
280
  * Constrains a variant map to the same value keys as a variant on another
206
- * component. Boolean variants are represented with `"true"` and `"false"`
281
+ * recipe. Boolean variants are represented with `"true"` and `"false"`
207
282
  * object keys.
208
283
  *
209
284
  * @example
@@ -228,7 +303,7 @@ type VariantKey<T> = T extends boolean ? "true" | "false" : Extract<T, string>;
228
303
  * ```
229
304
  */
230
305
  export type Variant<
231
- T extends Pick<AnyComponent, "getVariants">,
306
+ T extends Pick<AnyRecipe, "getVariants">,
232
307
  K extends keyof VariantProps<T>,
233
308
  > = Record<
234
309
  VariantKey<NonNullable<VariantProps<T>[K]>>,
@@ -237,7 +312,7 @@ export type Variant<
237
312
 
238
313
  /**
239
314
  * The configuration object accepted by `cv()`. It defines base class/style
240
- * output, variants, default variants, component extensions, and refinement
315
+ * output, variants, default variants, recipe extensions, and refinement
241
316
  * logic.
242
317
  *
243
318
  * @example
@@ -261,10 +336,7 @@ export type Variant<
261
336
  * const alert = cv(config);
262
337
  * ```
263
338
  */
264
- export interface CVConfig<
265
- V extends Variants = {},
266
- E extends AnyComponent[] = [],
267
- > {
339
+ export interface CVConfig<V extends Variants = {}, E extends AnyRecipe[] = []> {
268
340
  extend?: E;
269
341
  class?: ClassValue;
270
342
  style?: StyleValue;
@@ -304,7 +376,7 @@ function normalizeStyle(style: unknown): StyleValue {
304
376
 
305
377
  /**
306
378
  * Pre-extracts the class and (normalized) style from a variant value once at
307
- * component creation time. Returns `null` if the value contributes nothing.
379
+ * recipe creation time. Returns `null` if the value contributes nothing.
308
380
  */
309
381
  interface PrebuiltValue {
310
382
  class: ClassValue;
@@ -347,7 +419,7 @@ function getVariantValueKey(value: unknown): string | undefined {
347
419
 
348
420
  const EMPTY_KEYS: readonly string[] = [];
349
421
 
350
- function isComponentKeySource(source: unknown): source is KeySourceComponent {
422
+ function isRecipeKeySource(source: unknown): source is KeySourceRecipe {
351
423
  if (!source) return false;
352
424
  if (typeof source !== "object" && typeof source !== "function") {
353
425
  return false;
@@ -360,19 +432,19 @@ function isComponentKeySource(source: unknown): source is KeySourceComponent {
360
432
 
361
433
  /**
362
434
  * Splits props into multiple groups based on key sources. Only the first
363
- * component claims styling props (class/className/style). Subsequent components
435
+ * recipe claims styling props (class/className/style). Subsequent recipes
364
436
  * only receive variant props. Arrays always receive their listed keys but don't
365
437
  * claim styling props.
366
438
  */
367
439
  function splitPropsImpl(
368
440
  selfKeys: readonly string[],
369
- selfIsComponent: boolean,
441
+ selfIsRecipe: boolean,
370
442
  props: Record<string, unknown>,
371
443
  sources: unknown[],
372
444
  ): Record<string, unknown>[] {
373
445
  const sourcesLength = sources.length;
374
446
  const results: Record<string, unknown>[] = [];
375
- let stylingClaimed = selfIsComponent;
447
+ let stylingClaimed = selfIsRecipe;
376
448
 
377
449
  const selfResult: Record<string, unknown> = {};
378
450
  const selfKeysLength = selfKeys.length;
@@ -391,12 +463,12 @@ function splitPropsImpl(
391
463
  for (let s = 0; s < sourcesLength; s++) {
392
464
  const source = sources[s];
393
465
  const sourceResult: Record<string, unknown> = {};
394
- let sourceIsComponent = false;
466
+ let sourceIsRecipe = false;
395
467
  let effectiveKeys: readonly string[];
396
468
  if (Array.isArray(source)) {
397
469
  effectiveKeys = source;
398
- } else if (isComponentKeySource(source)) {
399
- sourceIsComponent = true;
470
+ } else if (isRecipeKeySource(source)) {
471
+ sourceIsRecipe = true;
400
472
  effectiveKeys = stylingClaimed ? source.variantKeys : source.propKeys;
401
473
  } else {
402
474
  effectiveKeys = EMPTY_KEYS;
@@ -412,7 +484,7 @@ function splitPropsImpl(
412
484
  results.push(sourceResult);
413
485
  effectiveKeyArrays.push(effectiveKeys);
414
486
 
415
- if (sourceIsComponent && !stylingClaimed) {
487
+ if (sourceIsRecipe && !stylingClaimed) {
416
488
  stylingClaimed = true;
417
489
  }
418
490
  }
@@ -441,8 +513,8 @@ function splitPropsImpl(
441
513
 
442
514
  /**
443
515
  * Splits props into multiple groups based on key sources. Each source gets its
444
- * own result object containing all its matching keys. The first component
445
- * source claims styling props (class/className/style). Subsequent components
516
+ * own result object containing all its matching keys. The first recipe
517
+ * source claims styling props (class/className/style). Subsequent recipes
446
518
  * only receive variant props. Arrays receive their listed keys but don't claim
447
519
  * styling props. The last element is always the "rest" containing keys not
448
520
  * claimed by any source.
@@ -455,7 +527,7 @@ export const splitProps: SplitPropsFunction = ((
455
527
  if (Array.isArray(source1)) {
456
528
  return splitPropsImpl(source1, false, props, sources);
457
529
  }
458
- if (isComponentKeySource(source1)) {
530
+ if (isRecipeKeySource(source1)) {
459
531
  return splitPropsImpl(source1.propKeys, true, props, sources);
460
532
  }
461
533
  return splitPropsImpl(EMPTY_KEYS, false, props, sources);
@@ -518,36 +590,26 @@ export function create(params: CreateParams = {}) {
518
590
 
519
591
  const cx = (...classes: ClsxClassValue[]) => transformClass(clsx(classes));
520
592
 
521
- const cv = <V extends Variants = {}, const E extends AnyComponent[] = []>(
593
+ const buildRecipe = <
594
+ V extends Variants = {},
595
+ const E extends AnyRecipe[] = [],
596
+ >(
522
597
  config: CVConfig<V, E> = {},
523
- ): CVComponent<V, E> => {
598
+ ): { recipe: Recipe<V, E>; meta: RecipeMeta } => {
524
599
  type MergedVariants = MergeVariants<V, E>;
525
600
 
526
- // ----- Pre-computed at creation time -----
527
- // Every setting is optional, so each one is read only when the caller owns
528
- // the key. A plain read would let a polluted `Object.prototype` supply a
529
- // class, a style, or a refine callback to every component.
601
+ // Compile caller-owned configuration once. Rebuilding a pruned branch
602
+ // reuses these values, including references that rendering already retains.
530
603
  const extend = getOwn(config, "extend");
531
- const hasExtend = !!extend && extend.length > 0;
532
604
  const variants = getOwn(config, "variants");
533
605
  const refine = getOwn(config, "refine");
534
606
  const baseStyle = getOwn(config, "style");
535
607
  const hasBaseStyle = !!baseStyle;
536
- const baseClass = getOwn(config, "class");
537
-
608
+ const baseClassValue = getOwn(config, "class");
538
609
  const variantKeySet = new Set<string>();
539
- const staticDefaults: Record<string, unknown> = {};
540
-
541
- // Pre-build extended component info, so the render path doesn't need to
542
- // read component metadata. Extends from a different `create()`
543
- // factory (different `transformClass` identity) need their contribution
544
- // transformed by their own `transformClass` before being joined into our
545
- // class string.
546
- const extMetas: ComponentMeta[] = [];
547
- const extBaseClassesArr: string[] = [];
548
- const extIsolated: boolean[] = [];
549
- let hasIsolatedExt = false;
550
- if (hasExtend) {
610
+ const inheritedComputedDefaultKeys = new Set<string>();
611
+ const originalExtMetas: RecipeMeta[] = [];
612
+ if (extend) {
551
613
  for (const ext of extend) {
552
614
  const extKeys = ext.variantKeys as readonly string[];
553
615
  for (let i = 0; i < extKeys.length; i++) {
@@ -555,27 +617,24 @@ export function create(params: CreateParams = {}) {
555
617
  if (key === undefined) continue;
556
618
  variantKeySet.add(key);
557
619
  }
558
-
559
- const meta = (ext as AnyComponent & ComponentWithMeta)[META_KEY];
620
+ const meta = (ext as AnyRecipe & RecipeWithMeta)[META_KEY];
560
621
  if (!meta) continue;
561
- extMetas.push(meta);
562
- Object.assign(staticDefaults, meta.staticDefaults);
563
-
564
- const isolated = meta.transformClass !== transformClass;
565
- extIsolated.push(isolated);
566
- if (isolated) {
567
- hasIsolatedExt = true;
568
- extBaseClassesArr.push(meta.transformClass(meta.baseClass));
569
- } else {
570
- extBaseClassesArr.push(meta.baseClass);
622
+ originalExtMetas.push(meta);
623
+ // A later branch can read a computed default contributed by the first
624
+ // path, even when that shared ancestor is pruned from this branch.
625
+ for (const key of meta.computedDefaultKeys) {
626
+ inheritedComputedDefaultKeys.add(key);
571
627
  }
572
628
  }
573
629
  }
574
- const extCount = extMetas.length;
575
-
576
- // Build all own variant metadata in one pass. Disabled value sets are
577
- // shared with their prebuilt variants instead of scanning and allocating
578
- // them twice.
630
+ const initialExtensions =
631
+ originalExtMetas.length > 1
632
+ ? deduplicateExtensions(originalExtMetas)
633
+ : originalExtMetas;
634
+
635
+ // Keep own default operations separate from inherited values. An explicit
636
+ // undefined must still clear a default when the retained extensions change.
637
+ const ownStaticDefaults: Record<string, unknown> = {};
579
638
  const disabledVariantKeys = new Set<string>();
580
639
  let disabledValuesBuilder: Record<string, Set<string>> | null = null;
581
640
  const variantEntryNames: string[] = [];
@@ -604,11 +663,12 @@ export function create(params: CreateParams = {}) {
604
663
  disabledValuesBuilder ??= {};
605
664
  disabledValuesBuilder[name] = prebuiltVariant.disabledValues;
606
665
  }
666
+ // Capture this decision before pruning can remove an inherited default.
607
667
  if (
608
668
  prebuiltVariant.values?.has("false") &&
609
- !hasOwn(staticDefaults, name)
669
+ !initialExtensions.some((meta) => hasOwn(meta.staticDefaults, name))
610
670
  ) {
611
- staticDefaults[name] = false;
671
+ ownStaticDefaults[name] = false;
612
672
  }
613
673
  }
614
674
  }
@@ -617,13 +677,9 @@ export function create(params: CreateParams = {}) {
617
677
  const variantEntryCount = variantEntryNames.length;
618
678
  const functionVariantCount = functionVariantNames.length;
619
679
  const hasDisabledVariantKeys = disabledVariantKeys.size > 0;
620
- // The table is allocated only when a variant declares a disabled value,
621
- // so its presence is the flag. Capturing it in a `const` lets the closures
622
- // below narrow it instead of repeating an optional chain.
623
680
  const disabledVariantValues = disabledValuesBuilder;
624
681
  const hasAnyDisabled =
625
682
  hasDisabledVariantKeys || disabledVariantValues !== null;
626
-
627
683
  const inputPropsKeys = ["class", "className", "style", ...variantKeys];
628
684
 
629
685
  const computedDefaultNames: string[] = [];
@@ -631,15 +687,6 @@ export function create(params: CreateParams = {}) {
631
687
  const defaultVariants = getOwn(config, "defaultVariants") as
632
688
  | Record<string, unknown>
633
689
  | undefined;
634
-
635
- // Pre-compute static defaults. Includes:
636
- // - extended components' static defaults
637
- // - implicit boolean defaults (variants with a `false` key default to false)
638
- // - this config's literal defaultVariants (overriding the above)
639
- //
640
- // Function entries in defaultVariants are computed defaults. They run in
641
- // the refine loop so they can react to setVariants updates.
642
- // Then filtered through disabled-variants.
643
690
  if (defaultVariants) {
644
691
  for (const name in defaultVariants) {
645
692
  if (!hasOwn(defaultVariants, name)) continue;
@@ -647,1119 +694,1147 @@ export function create(params: CreateParams = {}) {
647
694
  if (typeof value === "function") {
648
695
  computedDefaultNames.push(name);
649
696
  computedDefaultFns.push(value as ComputedDefaultVariantFn);
650
- continue;
651
- }
652
- if (value === undefined) {
653
- delete staticDefaults[name];
654
- continue;
697
+ } else {
698
+ ownStaticDefaults[name] = value;
655
699
  }
656
- staticDefaults[name] = value;
657
700
  }
658
701
  }
659
702
  const computedDefaultCount = computedDefaultNames.length;
660
- if (hasAnyDisabled) {
661
- // Filter disabled variants in-place
662
- for (const key in staticDefaults) {
663
- if (!hasOwn(staticDefaults, key)) continue;
664
- if (disabledVariantKeys.has(key)) {
665
- delete staticDefaults[key];
666
- continue;
667
- }
668
- if (disabledVariantValues) {
669
- const value = staticDefaults[key];
670
- const valueKey = getVariantValueKey(value);
671
- if (
672
- valueKey != null &&
673
- getOwn(disabledVariantValues, key)?.has(valueKey)
674
- ) {
675
- delete staticDefaults[key];
676
- }
677
- }
678
- }
679
- }
680
-
681
- const inheritedComputedDefaultKeys = new Set<string>();
682
- for (let i = 0; i < extCount; i++) {
683
- const keys = extMetas[i].computedDefaultKeys;
684
- for (const key of keys) {
685
- inheritedComputedDefaultKeys.add(key);
686
- }
687
- }
688
-
689
- // Filter to only extends with computed default or refine work in their
690
- // chain. Those are the components that can change resolved variants across
691
- // fixed-point iterations.
692
- const extMetasWithRefine: ComponentMeta[] = [];
693
- for (let i = 0; i < extCount; i++) {
694
- const meta = extMetas[i];
695
- if (meta.resolveRefine) {
696
- extMetasWithRefine.push(meta);
697
- }
698
- }
699
- const extMetasWithRefineCount = extMetasWithRefine.length;
700
-
701
- // Only a `refine` callback protects variants, through `setVariants`. This
702
- // over-approximates: `extMetasWithRefineCount` also counts extends that
703
- // only have computed defaults. Tightening it would mean tracking `refine`
704
- // separately in `ComponentMeta` to save one allocation. The record inside
705
- // the holder is created only when a callback assigns something.
706
- const canProtectVariants = !!refine || extMetasWithRefineCount > 0;
707
-
708
- // `userVariantProps` tells computed defaults which variants the user set
709
- // explicitly, and `runComputedDefaults` is its only reader anywhere in the
710
- // chain: a `refine` callback never receives it. `computedDefaultKeys` is
711
- // transitive, so an empty inherited set proves no extend runs computed
712
- // defaults either, and nothing reads the record. The record is then the
713
- // caller's own props object, which no stage may write to.
714
- const needsUserVariantProps =
715
- computedDefaultCount > 0 || inheritedComputedDefaultKeys.size > 0;
716
-
717
- // Call-site frame captured at the `cv()` call site so refine-limit warnings
718
- // can point developers at the component definition. Skipped entirely for
719
- // components that can never enter the refine loop, and stripped in
720
- // production via the NODE_ENV guard inside `captureCreationFrame`. The
721
- // frame is captured at creation time but the underlying `.stack` string is
722
- // formatted lazily on first access, so component creation stays cheap
723
- // unless the warning actually fires.
703
+ const baseClass = clsx(baseClassValue as ClsxClassValue);
704
+ const identity = {};
724
705
  const canTriggerRefineWarning =
725
- !!refine || computedDefaultCount > 0 || extMetasWithRefineCount > 0;
706
+ !!refine ||
707
+ computedDefaultCount > 0 ||
708
+ initialExtensions.some((meta) => meta.resolveRefine);
726
709
  const creationFrame =
727
710
  canTriggerRefineWarning && process.env.NODE_ENV !== "production"
728
711
  ? captureCreationFrame(cv)
729
712
  : undefined;
730
713
 
731
- // Function variant keys inherited from extends, filtered through this
732
- // component's own variants: a static (object/shorthand) variant in this
733
- // component replaces an inherited function variant for the same key.
734
- // The closure is exposed on `ComponentMeta` so any further extending
735
- // component can detect "ancestor's effective variant for K is a function"
736
- // and skip it when overriding K with a non-function.
737
- const functionVariantKeys = new Set<string>();
738
- for (let i = 0; i < extCount; i++) {
739
- const fnKeys = extMetas[i].functionVariantKeys;
740
- for (const k of fnKeys) {
741
- if (disabledVariantKeys.has(k)) continue;
742
- functionVariantKeys.add(k);
743
- }
744
- }
745
- for (let i = 0; i < functionVariantCount; i++) {
746
- functionVariantKeys.add(functionVariantNames[i]);
747
- }
748
- for (let i = 0; i < variantEntryCount; i++) {
749
- // A static variant in this component replaces an inherited function
750
- // variant for the same key; from this component onward, the key is no
751
- // longer a function variant.
752
- functionVariantKeys.delete(variantEntryNames[i]);
753
- }
754
-
755
- const computedDefaultKeys = new Set(inheritedComputedDefaultKeys);
756
- for (let i = 0; i < computedDefaultCount; i++) {
757
- computedDefaultKeys.add(computedDefaultNames[i]);
758
- }
759
-
760
- // Pre-compute static skip key/value sets to pass to extends. These never
761
- // change across calls — when caller passes no skip sets, we reuse the same
762
- // object and avoid Set allocation.
763
- let staticExtSkipKeys: Set<string> | null = null;
764
- if (hasDisabledVariantKeys || functionVariantCount > 0) {
765
- staticExtSkipKeys = new Set<string>();
766
- for (const k of disabledVariantKeys) {
767
- staticExtSkipKeys.add(k);
768
- }
769
- for (let i = 0; i < functionVariantCount; i++) {
770
- staticExtSkipKeys.add(functionVariantNames[i]);
714
+ // Only extension-dependent state is rebuilt. Own tables, callbacks,
715
+ // inheritance visibility, and the creation frame stay shared and unchanged.
716
+ const build = (
717
+ extMetas: RecipeMeta[],
718
+ ): {
719
+ recipe: Recipe<V, E>;
720
+ meta: RecipeMeta;
721
+ } => {
722
+ const hasExtend = extMetas.length > 0;
723
+ const extCount = extMetas.length;
724
+ const staticDefaults: Record<string, unknown> = {};
725
+ const extBaseClassesArr: string[] = [];
726
+ const extIsolated: boolean[] = [];
727
+ let hasIsolatedExt = false;
728
+ for (const meta of extMetas) {
729
+ Object.assign(staticDefaults, meta.staticDefaults);
730
+ const isolated = meta.transformClass !== transformClass;
731
+ extIsolated.push(isolated);
732
+ if (isolated) {
733
+ hasIsolatedExt = true;
734
+ extBaseClassesArr.push(meta.transformClass(meta.baseClass));
735
+ } else {
736
+ extBaseClassesArr.push(meta.baseClass);
737
+ }
771
738
  }
772
- }
773
-
774
- // A static variant in this component replaces an inherited function
775
- // variant, so the ancestor must skip that key.
776
- if (variantEntryCount > 0 && extCount > 0) {
777
- for (let i = 0; i < variantEntryCount; i++) {
778
- const name = variantEntryNames[i];
779
- for (let j = 0; j < extCount; j++) {
780
- if (extMetas[j].functionVariantKeys.has(name)) {
781
- staticExtSkipKeys ??= new Set<string>();
782
- staticExtSkipKeys.add(name);
783
- break;
784
- }
739
+ for (const name in ownStaticDefaults) {
740
+ if (!hasOwn(ownStaticDefaults, name)) continue;
741
+ const value = ownStaticDefaults[name];
742
+ if (value === undefined) {
743
+ delete staticDefaults[name];
744
+ } else {
745
+ staticDefaults[name] = value;
785
746
  }
786
747
  }
787
- }
788
- // Skip values are passed directly to extends. We can reuse the same object
789
- // when no caller-provided values need merging.
790
- const staticExtSkipValues = disabledVariantValues;
791
-
792
- // Callers use this only when disabled keys or values exist.
793
- function filterDisabledInto(
794
- input: Record<string, unknown>,
795
- out: Record<string, unknown>,
796
- ): void {
797
- for (const key in input) {
798
- if (!hasOwn(input, key)) continue;
799
- if (disabledVariantKeys.has(key)) continue;
800
- const value = input[key];
801
- if (disabledVariantValues) {
802
- const valueKey = getVariantValueKey(value);
803
- if (
804
- valueKey != null &&
805
- getOwn(disabledVariantValues, key)?.has(valueKey)
806
- ) {
748
+ if (hasAnyDisabled) {
749
+ for (const key in staticDefaults) {
750
+ if (!hasOwn(staticDefaults, key)) continue;
751
+ if (disabledVariantKeys.has(key)) {
752
+ delete staticDefaults[key];
807
753
  continue;
808
754
  }
755
+ if (disabledVariantValues) {
756
+ const valueKey = getVariantValueKey(staticDefaults[key]);
757
+ if (
758
+ valueKey != null &&
759
+ getOwn(disabledVariantValues, key)?.has(valueKey)
760
+ ) {
761
+ delete staticDefaults[key];
762
+ }
763
+ }
809
764
  }
810
- out[key] = value;
811
765
  }
812
- }
813
766
 
814
- const isOwnDisabledValue = (key: string, value: unknown): boolean => {
815
- if (disabledVariantKeys.has(key)) {
816
- return true;
767
+ // Filter to only extends with computed default or refine work in their
768
+ // chain. Those are the recipes that can change resolved variants across
769
+ // fixed-point iterations.
770
+ const extMetasWithRefine: RecipeMeta[] = [];
771
+ for (let i = 0; i < extCount; i++) {
772
+ const meta = extMetas[i];
773
+ if (meta.resolveRefine) {
774
+ extMetasWithRefine.push(meta);
775
+ }
817
776
  }
818
- if (disabledVariantValues) {
819
- const valueKey = getVariantValueKey(value);
820
- if (
821
- valueKey != null &&
822
- getOwn(disabledVariantValues, key)?.has(valueKey)
823
- ) {
824
- return true;
777
+ const extMetasWithRefineCount = extMetasWithRefine.length;
778
+
779
+ // Only a `refine` callback protects variants, through `setVariants`. This
780
+ // over-approximates: `extMetasWithRefineCount` also counts extends that
781
+ // only have computed defaults. Tightening it would mean tracking `refine`
782
+ // separately in `RecipeMeta` to save one allocation. The record inside
783
+ // the holder is created only when a callback assigns something.
784
+ const canProtectVariants = !!refine || extMetasWithRefineCount > 0;
785
+
786
+ // `userVariantProps` tells computed defaults which variants the user set
787
+ // explicitly, and `runComputedDefaults` is its only reader anywhere in the
788
+ // chain: a `refine` callback never receives it. `computedDefaultKeys` is
789
+ // transitive, so an empty inherited set proves no extend runs computed
790
+ // defaults either, and nothing reads the record. The record is then the
791
+ // caller's own props object, which no stage may write to.
792
+ const needsUserVariantProps =
793
+ computedDefaultCount > 0 || inheritedComputedDefaultKeys.size > 0;
794
+
795
+ // Function variant keys inherited from extends, filtered through this
796
+ // recipe's own variants: a static (object/shorthand) variant in this
797
+ // recipe replaces an inherited function variant for the same key.
798
+ // The closure is exposed on `RecipeMeta` so any further extending
799
+ // recipe can detect "ancestor's effective variant for K is a function"
800
+ // and skip it when overriding K with a non-function.
801
+ const functionVariantKeys = new Set<string>();
802
+ for (let i = 0; i < extCount; i++) {
803
+ const fnKeys = extMetas[i].functionVariantKeys;
804
+ for (const k of fnKeys) {
805
+ if (disabledVariantKeys.has(k)) continue;
806
+ functionVariantKeys.add(k);
825
807
  }
826
808
  }
827
- return false;
828
- };
829
-
830
- const filterOwnDisabledVariants = (
831
- input: Record<string, unknown>,
832
- fallback: Record<string, unknown>,
833
- ): Record<string, unknown> => {
834
- if (!hasAnyDisabled) {
835
- return input;
809
+ for (let i = 0; i < functionVariantCount; i++) {
810
+ functionVariantKeys.add(functionVariantNames[i]);
836
811
  }
837
-
838
- let hasOwnDisabledValue = false;
839
- for (const key in input) {
840
- if (!hasOwn(input, key)) continue;
841
- const value = input[key];
842
- if (isOwnDisabledValue(key, value)) {
843
- hasOwnDisabledValue = true;
844
- break;
845
- }
812
+ for (let i = 0; i < variantEntryCount; i++) {
813
+ // A static variant in this recipe replaces an inherited function
814
+ // variant for the same key; from this recipe onward, the key is no
815
+ // longer a function variant.
816
+ functionVariantKeys.delete(variantEntryNames[i]);
846
817
  }
847
- if (!hasOwnDisabledValue) {
848
- return input;
818
+
819
+ const computedDefaultKeys = new Set(inheritedComputedDefaultKeys);
820
+ for (let i = 0; i < computedDefaultCount; i++) {
821
+ computedDefaultKeys.add(computedDefaultNames[i]);
849
822
  }
850
823
 
851
- const filtered: Record<string, unknown> = {};
852
- for (const key in input) {
853
- if (!hasOwn(input, key)) continue;
854
- const value = input[key];
855
- if (!isOwnDisabledValue(key, value)) {
856
- filtered[key] = value;
857
- continue;
824
+ // Pre-compute static skip key/value sets to pass to extends. These never
825
+ // change across calls — when caller passes no skip sets, we reuse the same
826
+ // object and avoid Set allocation.
827
+ let staticExtSkipKeys: Set<string> | null = null;
828
+ if (hasDisabledVariantKeys || functionVariantCount > 0) {
829
+ staticExtSkipKeys = new Set<string>();
830
+ for (const k of disabledVariantKeys) {
831
+ staticExtSkipKeys.add(k);
858
832
  }
859
- const fallbackValue = getOwn(fallback, key);
860
- if (
861
- fallbackValue !== undefined &&
862
- !isOwnDisabledValue(key, fallbackValue)
863
- ) {
864
- filtered[key] = fallbackValue;
833
+ for (let i = 0; i < functionVariantCount; i++) {
834
+ staticExtSkipKeys.add(functionVariantNames[i]);
865
835
  }
866
836
  }
867
837
 
868
- return filtered;
869
- };
870
-
871
- // Hot path: resolve variants by merging static defaults + extends'
872
- // static defaults + user-provided props.
873
- function resolveVariantsHot(
874
- propsVariants: Record<string, unknown>,
875
- ): Record<string, unknown> {
876
- // Start with static defaults
877
- const defaults = Object.assign({}, staticDefaults);
878
-
879
- // Apply propsVariants on top (filter undefined).
880
- for (let i = 0; i < variantKeysLength; i++) {
881
- const k = variantKeys[i];
882
- if (!hasOwn(propsVariants, k)) continue;
883
- const v = propsVariants[k];
884
- if (v === undefined) continue;
885
- defaults[k] = v;
838
+ // A static variant in this recipe replaces an inherited function
839
+ // variant, so the ancestor must skip that key.
840
+ if (variantEntryCount > 0 && extCount > 0) {
841
+ for (let i = 0; i < variantEntryCount; i++) {
842
+ const name = variantEntryNames[i];
843
+ for (let j = 0; j < extCount; j++) {
844
+ if (extMetas[j].functionVariantKeys.has(name)) {
845
+ staticExtSkipKeys ??= new Set<string>();
846
+ staticExtSkipKeys.add(name);
847
+ break;
848
+ }
849
+ }
850
+ }
886
851
  }
887
-
888
- if (!hasAnyDisabled) {
889
- return defaults;
852
+ // Skip values are passed directly to extends. We can reuse the same object
853
+ // when no caller-provided values need merging.
854
+ const staticExtSkipValues = disabledVariantValues;
855
+
856
+ // Callers use this only when disabled keys or values exist.
857
+ function filterDisabledInto(
858
+ input: Record<string, unknown>,
859
+ out: Record<string, unknown>,
860
+ ): void {
861
+ for (const key in input) {
862
+ if (!hasOwn(input, key)) continue;
863
+ if (disabledVariantKeys.has(key)) continue;
864
+ const value = input[key];
865
+ if (disabledVariantValues) {
866
+ const valueKey = getVariantValueKey(value);
867
+ if (
868
+ valueKey != null &&
869
+ getOwn(disabledVariantValues, key)?.has(valueKey)
870
+ ) {
871
+ continue;
872
+ }
873
+ }
874
+ out[key] = value;
875
+ }
890
876
  }
891
877
 
892
- // Filter disabled
893
- const result: Record<string, unknown> = {};
894
- filterDisabledInto(defaults, result);
895
- return result;
896
- }
897
-
898
- const runComputedDefaults = (
899
- resolved: Record<string, unknown>,
900
- defaultResolved: Record<string, unknown>,
901
- userVariantProps: Record<string, unknown>,
902
- filterOwnVariants: boolean,
903
- protection: RefineProtection | null | undefined,
904
- ): Record<string, unknown> => {
905
- const protectedVariants = protection?.variants;
906
- let ownVariants = filterOwnVariants ? null : resolved;
907
- const getOwnVariants = (): Record<string, unknown> => {
908
- if (ownVariants) {
909
- return ownVariants;
878
+ const isOwnDisabledValue = (key: string, value: unknown): boolean => {
879
+ if (disabledVariantKeys.has(key)) {
880
+ return true;
910
881
  }
911
- const filteredVariants: Record<string, unknown> = {};
912
- for (let i = 0; i < variantKeysLength; i++) {
913
- const key = variantKeys[i];
914
- if (hasOwn(resolved, key)) {
915
- filteredVariants[key] = resolved[key];
882
+ if (disabledVariantValues) {
883
+ const valueKey = getVariantValueKey(value);
884
+ if (
885
+ valueKey != null &&
886
+ getOwn(disabledVariantValues, key)?.has(valueKey)
887
+ ) {
888
+ return true;
916
889
  }
917
890
  }
918
- ownVariants = filteredVariants;
919
- return filteredVariants;
891
+ return false;
920
892
  };
921
893
 
922
- let updatedVariants: Record<string, unknown> | null = null;
923
- const ensureUpdated = (): Record<string, unknown> => {
924
- if (updatedVariants) {
925
- return updatedVariants;
894
+ const filterOwnDisabledVariants = (
895
+ input: Record<string, unknown>,
896
+ fallback: Record<string, unknown>,
897
+ ): Record<string, unknown> => {
898
+ if (!hasAnyDisabled) {
899
+ return input;
926
900
  }
927
- const updated = Object.assign({}, resolved);
928
- updatedVariants = updated;
929
- return updated;
930
- };
931
901
 
932
- for (let i = 0; i < computedDefaultCount; i++) {
933
- const key = computedDefaultNames[i];
934
- if (hasOwn(userVariantProps, key)) {
935
- if (userVariantProps[key] !== undefined) continue;
902
+ let hasOwnDisabledValue = false;
903
+ for (const key in input) {
904
+ if (!hasOwn(input, key)) continue;
905
+ const value = input[key];
906
+ if (isOwnDisabledValue(key, value)) {
907
+ hasOwnDisabledValue = true;
908
+ break;
909
+ }
910
+ }
911
+ if (!hasOwnDisabledValue) {
912
+ return input;
936
913
  }
937
- if (protectedVariants && hasOwn(protectedVariants, key)) continue;
938
914
 
939
- const variantSnapshot = getOwnVariants();
940
- const defaultValue = inheritedComputedDefaultKeys.has(key)
941
- ? getOwn(variantSnapshot, key)
942
- : getOwn(defaultResolved, key);
943
- const value = computedDefaultFns[i](defaultValue, variantSnapshot);
944
- if (hasAnyDisabled) {
945
- if (disabledVariantKeys.has(key)) continue;
946
- if (disabledVariantValues) {
947
- const valueKey = getVariantValueKey(value);
948
- if (
949
- valueKey != null &&
950
- getOwn(disabledVariantValues, key)?.has(valueKey)
951
- ) {
952
- continue;
953
- }
915
+ const filtered: Record<string, unknown> = {};
916
+ for (const key in input) {
917
+ if (!hasOwn(input, key)) continue;
918
+ const value = input[key];
919
+ if (!isOwnDisabledValue(key, value)) {
920
+ filtered[key] = value;
921
+ continue;
922
+ }
923
+ const fallbackValue = getOwn(fallback, key);
924
+ if (
925
+ fallbackValue !== undefined &&
926
+ !isOwnDisabledValue(key, fallbackValue)
927
+ ) {
928
+ filtered[key] = fallbackValue;
954
929
  }
955
930
  }
956
931
 
957
- if (value === undefined) {
958
- if (!hasOwn(variantSnapshot, key)) continue;
959
- delete ensureUpdated()[key];
960
- continue;
932
+ return filtered;
933
+ };
934
+
935
+ // Hot path: resolve variants by merging static defaults + extends'
936
+ // static defaults + user-provided props.
937
+ function resolveVariantsHot(
938
+ propsVariants: Record<string, unknown>,
939
+ ): Record<string, unknown> {
940
+ // Start with static defaults
941
+ const defaults = Object.assign({}, staticDefaults);
942
+
943
+ // Apply propsVariants on top (filter undefined).
944
+ for (let i = 0; i < variantKeysLength; i++) {
945
+ const k = variantKeys[i];
946
+ if (!hasOwn(propsVariants, k)) continue;
947
+ const v = propsVariants[k];
948
+ if (v === undefined) continue;
949
+ defaults[k] = v;
961
950
  }
962
- if (Object.is(getOwn(variantSnapshot, key), value)) continue;
963
- ensureUpdated()[key] = value;
964
- }
965
951
 
966
- return updatedVariants ?? resolved;
967
- };
952
+ if (!hasAnyDisabled) {
953
+ return defaults;
954
+ }
968
955
 
969
- const runRefineContext = (
970
- resolved: Record<string, unknown>,
971
- filterOwnVariants: boolean,
972
- collectOutput: boolean,
973
- applyVariantUpdates: boolean,
974
- protection: RefineProtection | null | undefined,
975
- ): {
976
- workingResolved: Record<string, unknown>;
977
- classes: ClassValue[] | null;
978
- style: StyleValue | null;
979
- } => {
980
- let workingResolved = resolved;
981
- let cClasses: ClassValue[] | null = null;
982
- let cStyle: StyleValue | null = null;
983
-
984
- if (refine) {
985
- let ownVariants = resolved;
986
- if (filterOwnVariants) {
987
- // When this component is being extended, `resolved` is the parent's
988
- // workingResolved (a superset of our variant keys). Filter to our own
989
- // keys for `ctx.variants` so the user's `refine` callback sees the
990
- // shape declared by `VariantValues<V>` and not foreign parent keys.
956
+ // Filter disabled
957
+ const result: Record<string, unknown> = {};
958
+ filterDisabledInto(defaults, result);
959
+ return result;
960
+ }
961
+
962
+ const runComputedDefaults = (
963
+ resolved: Record<string, unknown>,
964
+ defaultResolved: Record<string, unknown>,
965
+ userVariantProps: Record<string, unknown>,
966
+ filterOwnVariants: boolean,
967
+ protection: RefineProtection | null | undefined,
968
+ ): Record<string, unknown> => {
969
+ const protectedVariants = protection?.variants;
970
+ let ownVariants = filterOwnVariants ? null : resolved;
971
+ const getOwnVariants = (): Record<string, unknown> => {
972
+ if (ownVariants) {
973
+ return ownVariants;
974
+ }
991
975
  const filteredVariants: Record<string, unknown> = {};
992
976
  for (let i = 0; i < variantKeysLength; i++) {
993
- const k = variantKeys[i];
994
- if (hasOwn(resolved, k)) {
995
- filteredVariants[k] = resolved[k];
977
+ const key = variantKeys[i];
978
+ if (hasOwn(resolved, key)) {
979
+ filteredVariants[key] = resolved[key];
996
980
  }
997
981
  }
998
982
  ownVariants = filteredVariants;
983
+ return filteredVariants;
984
+ };
985
+
986
+ let updatedVariants: Record<string, unknown> | null = null;
987
+ const ensureUpdated = (): Record<string, unknown> => {
988
+ if (updatedVariants) {
989
+ return updatedVariants;
990
+ }
991
+ const updated = Object.assign({}, resolved);
992
+ updatedVariants = updated;
993
+ return updated;
994
+ };
995
+
996
+ for (let i = 0; i < computedDefaultCount; i++) {
997
+ const key = computedDefaultNames[i];
998
+ if (hasOwn(userVariantProps, key)) {
999
+ if (userVariantProps[key] !== undefined) continue;
1000
+ }
1001
+ if (protectedVariants && hasOwn(protectedVariants, key)) continue;
1002
+
1003
+ const variantSnapshot = getOwnVariants();
1004
+ const defaultValue = inheritedComputedDefaultKeys.has(key)
1005
+ ? getOwn(variantSnapshot, key)
1006
+ : getOwn(defaultResolved, key);
1007
+ const value = computedDefaultFns[i](defaultValue, variantSnapshot);
1008
+ if (hasAnyDisabled) {
1009
+ if (disabledVariantKeys.has(key)) continue;
1010
+ if (disabledVariantValues) {
1011
+ const valueKey = getVariantValueKey(value);
1012
+ if (
1013
+ valueKey != null &&
1014
+ getOwn(disabledVariantValues, key)?.has(valueKey)
1015
+ ) {
1016
+ continue;
1017
+ }
1018
+ }
1019
+ }
1020
+
1021
+ if (value === undefined) {
1022
+ if (!hasOwn(variantSnapshot, key)) continue;
1023
+ delete ensureUpdated()[key];
1024
+ continue;
1025
+ }
1026
+ if (Object.is(getOwn(variantSnapshot, key), value)) continue;
1027
+ ensureUpdated()[key] = value;
999
1028
  }
1000
- // `assignedVariants`, `localCClasses` and `localCStyle` are created on
1001
- // first use, so a callback that only inspects `variants` allocates
1002
- // none of them. The `setVariants` bookkeeping is inlined for the same
1003
- // reason: helper closures would be allocated on every call, including
1004
- // the calls that never assign anything.
1005
- // `assignedVariants` holds only the changed keys, because copying all
1006
- // of `ownVariants` into it would repeat values `workingResolved` has.
1007
- let assignedVariants: Record<string, unknown> | null = null;
1008
- let localCClasses: ClassValue[] | null = null;
1009
- let localCStyle: StyleValue | null = null;
1010
- const ctx = {
1011
- variants: ownVariants as VariantValues<Record<string, unknown>>,
1012
- setVariants: (
1013
- newVariants: VariantValues<Record<string, unknown>>,
1014
- ) => {
1015
- if (!applyVariantUpdates) {
1016
- return;
1029
+
1030
+ return updatedVariants ?? resolved;
1031
+ };
1032
+
1033
+ const runRefineContext = (
1034
+ resolved: Record<string, unknown>,
1035
+ filterOwnVariants: boolean,
1036
+ collectOutput: boolean,
1037
+ applyVariantUpdates: boolean,
1038
+ protection: RefineProtection | null | undefined,
1039
+ ): {
1040
+ workingResolved: Record<string, unknown>;
1041
+ classes: ClassValue[] | null;
1042
+ style: StyleValue | null;
1043
+ } => {
1044
+ let workingResolved = resolved;
1045
+ let cClasses: ClassValue[] | null = null;
1046
+ let cStyle: StyleValue | null = null;
1047
+
1048
+ if (refine) {
1049
+ let ownVariants = resolved;
1050
+ if (filterOwnVariants) {
1051
+ // When this recipe is being extended, `resolved` is the parent's
1052
+ // workingResolved (a superset of our variant keys). Filter to our own
1053
+ // keys for `ctx.variants` so the user's `refine` callback sees the
1054
+ // shape declared by `VariantValues<V>` and not foreign parent keys.
1055
+ const filteredVariants: Record<string, unknown> = {};
1056
+ for (let i = 0; i < variantKeysLength; i++) {
1057
+ const k = variantKeys[i];
1058
+ if (hasOwn(resolved, k)) {
1059
+ filteredVariants[k] = resolved[k];
1060
+ }
1017
1061
  }
1018
- for (const key in newVariants) {
1019
- if (!hasOwn(newVariants, key)) continue;
1020
- // `disabledVariantKeys` is empty unless this component disables a
1021
- // variant, so the lookup replaces a `hasAnyDisabled` branch. The
1022
- // key is checked before the value is read, because reading it can
1023
- // run a caller-defined accessor.
1024
- if (disabledVariantKeys.has(key)) continue;
1025
- const value = (newVariants as Record<string, unknown>)[key];
1026
- if (disabledVariantValues) {
1027
- const valueKey = getVariantValueKey(value);
1028
- if (
1029
- valueKey != null &&
1030
- getOwn(disabledVariantValues, key)?.has(valueKey)
1031
- ) {
1062
+ ownVariants = filteredVariants;
1063
+ }
1064
+ // `assignedVariants`, `localCClasses` and `localCStyle` are created on
1065
+ // first use, so a callback that only inspects `variants` allocates
1066
+ // none of them. The `setVariants` bookkeeping is inlined for the same
1067
+ // reason: helper closures would be allocated on every call, including
1068
+ // the calls that never assign anything.
1069
+ // `assignedVariants` holds only the changed keys, because copying all
1070
+ // of `ownVariants` into it would repeat values `workingResolved` has.
1071
+ let assignedVariants: Record<string, unknown> | null = null;
1072
+ let localCClasses: ClassValue[] | null = null;
1073
+ let localCStyle: StyleValue | null = null;
1074
+ const ctx = {
1075
+ variants: ownVariants as VariantValues<Record<string, unknown>>,
1076
+ setVariants: (
1077
+ newVariants: VariantValues<Record<string, unknown>>,
1078
+ ) => {
1079
+ if (!applyVariantUpdates) {
1080
+ return;
1081
+ }
1082
+ for (const key in newVariants) {
1083
+ if (!hasOwn(newVariants, key)) continue;
1084
+ // `disabledVariantKeys` is empty unless this recipe disables a
1085
+ // variant, so the lookup replaces a `hasAnyDisabled` branch. The
1086
+ // key is checked before the value is read, because reading it can
1087
+ // run a caller-defined accessor.
1088
+ if (disabledVariantKeys.has(key)) continue;
1089
+ const value = (newVariants as Record<string, unknown>)[key];
1090
+ if (disabledVariantValues) {
1091
+ const valueKey = getVariantValueKey(value);
1092
+ if (
1093
+ valueKey != null &&
1094
+ getOwn(disabledVariantValues, key)?.has(valueKey)
1095
+ ) {
1096
+ continue;
1097
+ }
1098
+ }
1099
+ if (protection) {
1100
+ const protectedVariants = (protection.variants ??= {});
1101
+ protectedVariants[key] = value;
1102
+ }
1103
+ // The record holds only assigned keys, so both halves matter: a
1104
+ // key it owns can hold a clear that `??` on the value would
1105
+ // discard, and a key it does not own must fall back to
1106
+ // `ownVariants`, or a clear compares against `undefined`.
1107
+ const current =
1108
+ assignedVariants && hasOwn(assignedVariants, key)
1109
+ ? assignedVariants[key]
1110
+ : getOwn(ownVariants, key);
1111
+ if (Object.is(current, value)) {
1032
1112
  continue;
1033
1113
  }
1114
+ assignedVariants ??= {};
1115
+ assignedVariants[key] = value;
1034
1116
  }
1035
- if (protection) {
1036
- const protectedVariants = (protection.variants ??= {});
1037
- protectedVariants[key] = value;
1038
- }
1039
- // The record holds only assigned keys, so both halves matter: a
1040
- // key it owns can hold a clear that `??` on the value would
1041
- // discard, and a key it does not own must fall back to
1042
- // `ownVariants`, or a clear compares against `undefined`.
1043
- const current =
1044
- assignedVariants && hasOwn(assignedVariants, key)
1045
- ? assignedVariants[key]
1046
- : getOwn(ownVariants, key);
1047
- if (Object.is(current, value)) {
1048
- continue;
1049
- }
1050
- assignedVariants ??= {};
1051
- assignedVariants[key] = value;
1117
+ },
1118
+ addClass: (className: ClassValue) => {
1119
+ if (!collectOutput) return;
1120
+ localCClasses ??= [];
1121
+ localCClasses.push(className);
1122
+ },
1123
+ addStyle: (newStyle: StyleValue) => {
1124
+ if (!collectOutput) return;
1125
+ localCStyle ??= {};
1126
+ Object.assign(localCStyle, newStyle);
1127
+ },
1128
+ };
1129
+ const result = refine(ctx);
1130
+ if (collectOutput && result != null) {
1131
+ const r = extractClassAndStylePrebuilt(result);
1132
+ if (r.class != null) {
1133
+ localCClasses ??= [];
1134
+ localCClasses.push(r.class);
1135
+ }
1136
+ if (r.style) {
1137
+ localCStyle ??= {};
1138
+ Object.assign(localCStyle, r.style);
1052
1139
  }
1053
- },
1054
- addClass: (className: ClassValue) => {
1055
- if (!collectOutput) return;
1056
- localCClasses ??= [];
1057
- localCClasses.push(className);
1058
- },
1059
- addStyle: (newStyle: StyleValue) => {
1060
- if (!collectOutput) return;
1061
- localCStyle ??= {};
1062
- Object.assign(localCStyle, newStyle);
1063
- },
1064
- };
1065
- const result = refine(ctx);
1066
- if (collectOutput && result != null) {
1067
- const r = extractClassAndStylePrebuilt(result);
1068
- if (r.class != null) {
1069
- localCClasses ??= [];
1070
- localCClasses.push(r.class);
1071
1140
  }
1072
- if (r.style) {
1073
- localCStyle ??= {};
1074
- Object.assign(localCStyle, r.style);
1141
+ cClasses = localCClasses;
1142
+ cStyle = localCStyle;
1143
+ if (assignedVariants) {
1144
+ // No disabled filtering here: `setVariants` drops a disabled key or
1145
+ // value before it records one, so every assigned key already passed
1146
+ // the same two checks.
1147
+ workingResolved = Object.assign(
1148
+ {},
1149
+ workingResolved,
1150
+ assignedVariants,
1151
+ );
1075
1152
  }
1076
1153
  }
1077
- cClasses = localCClasses;
1078
- cStyle = localCStyle;
1079
- if (assignedVariants) {
1080
- // No disabled filtering here: `setVariants` drops a disabled key or
1081
- // value before it records one, so every assigned key already passed
1082
- // the same two checks.
1083
- workingResolved = Object.assign(
1084
- {},
1085
- workingResolved,
1086
- assignedVariants,
1087
- );
1088
- }
1089
- }
1090
1154
 
1091
- return {
1092
- workingResolved,
1093
- classes: cClasses,
1094
- style: cStyle,
1155
+ return {
1156
+ workingResolved,
1157
+ classes: cClasses,
1158
+ style: cStyle,
1159
+ };
1095
1160
  };
1096
- };
1097
1161
 
1098
- // Core compute path. Called both for top-level rendering (via
1099
- // `computeResult`) and recursively when this component is used as an
1100
- // `extend` target by another component. Pushes variant classes (excluding
1101
- // base class) into `classesOut` and merges styles into `styleOut`.
1102
- const computeOnce: ComputeOnceFn = (
1103
- resolved,
1104
- userVariantProps,
1105
- skipKeys,
1106
- skipValues,
1107
- classesOut,
1108
- styleOut,
1109
- protection,
1110
- defaultResolved = resolved,
1111
- renderOnly = false,
1112
- ) => {
1113
- let workingResolved = resolved;
1114
- let cClasses: ClassValue[] | null = null;
1115
- let cStyle: StyleValue | null = null;
1116
-
1117
- // Run extends' contributions first (their full classes + styles) so our
1118
- // own base style and variants apply on top, matching the original
1119
- // ext1 → ext2 → … → current ordering.
1120
- if (hasExtend) {
1121
- // Build skip sets to pass to extends. Reuse precomputed values when no
1122
- // caller-provided sets need merging.
1123
- let extSkipKeys: Set<string> | null;
1124
- if (skipKeys === null) {
1125
- extSkipKeys = staticExtSkipKeys;
1126
- } else if (staticExtSkipKeys === null) {
1127
- extSkipKeys = skipKeys;
1128
- } else {
1129
- extSkipKeys = new Set(skipKeys);
1130
- for (const k of staticExtSkipKeys) {
1131
- extSkipKeys.add(k);
1162
+ // Core compute path. Called both for top-level rendering (via
1163
+ // `computeResult`) and recursively when this recipe is used as an
1164
+ // `extend` target by another recipe. Pushes variant classes (excluding
1165
+ // base class) into `classesOut` and merges styles into `styleOut`.
1166
+ const computeOnce: ComputeOnceFn = (
1167
+ resolved,
1168
+ userVariantProps,
1169
+ skipKeys,
1170
+ skipValues,
1171
+ classesOut,
1172
+ styleOut,
1173
+ protection,
1174
+ defaultResolved = resolved,
1175
+ renderOnly = false,
1176
+ ) => {
1177
+ let workingResolved = resolved;
1178
+ let cClasses: ClassValue[] | null = null;
1179
+ let cStyle: StyleValue | null = null;
1180
+
1181
+ // Run extends' contributions first (their full classes + styles) so our
1182
+ // own base style and variants apply on top, matching the original
1183
+ // ext1 → ext2 → … → current ordering.
1184
+ if (hasExtend) {
1185
+ // Build skip sets to pass to extends. Reuse precomputed values when no
1186
+ // caller-provided sets need merging.
1187
+ let extSkipKeys: Set<string> | null;
1188
+ if (skipKeys === null) {
1189
+ extSkipKeys = staticExtSkipKeys;
1190
+ } else if (staticExtSkipKeys === null) {
1191
+ extSkipKeys = skipKeys;
1192
+ } else {
1193
+ extSkipKeys = new Set(skipKeys);
1194
+ for (const k of staticExtSkipKeys) {
1195
+ extSkipKeys.add(k);
1196
+ }
1132
1197
  }
1133
- }
1134
1198
 
1135
- let extSkipVals: Record<string, Set<string>> | null;
1136
- if (skipValues === null) {
1137
- extSkipVals = staticExtSkipValues;
1138
- } else if (staticExtSkipValues === null) {
1139
- extSkipVals = skipValues;
1140
- } else {
1141
- extSkipVals = {};
1142
- for (const k in skipValues) {
1143
- if (!hasOwn(skipValues, k)) continue;
1144
- extSkipVals[k] = skipValues[k];
1145
- }
1146
- for (const k in staticExtSkipValues) {
1147
- if (!hasOwn(staticExtSkipValues, k)) continue;
1148
- const values = staticExtSkipValues[k];
1149
- const existing = getOwn(extSkipVals, k);
1150
- if (existing) {
1151
- const merged = new Set<string>(existing);
1152
- for (const v of values) {
1153
- merged.add(v);
1199
+ let extSkipVals: Record<string, Set<string>> | null;
1200
+ if (skipValues === null) {
1201
+ extSkipVals = staticExtSkipValues;
1202
+ } else if (staticExtSkipValues === null) {
1203
+ extSkipVals = skipValues;
1204
+ } else {
1205
+ extSkipVals = {};
1206
+ for (const k in skipValues) {
1207
+ if (!hasOwn(skipValues, k)) continue;
1208
+ extSkipVals[k] = skipValues[k];
1209
+ }
1210
+ for (const k in staticExtSkipValues) {
1211
+ if (!hasOwn(staticExtSkipValues, k)) continue;
1212
+ const values = staticExtSkipValues[k];
1213
+ const existing = getOwn(extSkipVals, k);
1214
+ if (existing) {
1215
+ const merged = new Set<string>(existing);
1216
+ for (const v of values) {
1217
+ merged.add(v);
1218
+ }
1219
+ extSkipVals[k] = merged;
1220
+ } else {
1221
+ extSkipVals[k] = values;
1154
1222
  }
1155
- extSkipVals[k] = merged;
1156
- } else {
1157
- extSkipVals[k] = values;
1158
1223
  }
1159
1224
  }
1160
- }
1161
1225
 
1162
- for (let i = 0; i < extCount; i++) {
1163
- if (hasIsolatedExt && extIsolated[i]) {
1164
- // Isolated extend (different factory): gather its variant classes
1165
- // into a scratch array, then push the joined string after applying
1166
- // its own transformClass. Our outer transform applies on top.
1167
- const extClasses: ClsxClassValue[] = [];
1168
- workingResolved = extMetas[i].compute(
1169
- workingResolved,
1170
- userVariantProps,
1171
- extSkipKeys,
1172
- extSkipVals,
1173
- extClasses,
1174
- styleOut,
1175
- protection,
1176
- defaultResolved,
1177
- renderOnly,
1178
- );
1179
- if (extClasses.length > 0) {
1180
- const joined = clsx(extClasses);
1181
- if (joined.length > 0) {
1182
- classesOut.push(extMetas[i].transformClass(joined));
1226
+ for (let i = 0; i < extCount; i++) {
1227
+ if (hasIsolatedExt && extIsolated[i]) {
1228
+ // Isolated extend (different factory): gather its variant classes
1229
+ // into a scratch array, then push the joined string after applying
1230
+ // its own transformClass. Our outer transform applies on top.
1231
+ const extClasses: ClsxClassValue[] = [];
1232
+ workingResolved = extMetas[i].compute(
1233
+ workingResolved,
1234
+ userVariantProps,
1235
+ extSkipKeys,
1236
+ extSkipVals,
1237
+ extClasses,
1238
+ styleOut,
1239
+ protection,
1240
+ defaultResolved,
1241
+ renderOnly,
1242
+ );
1243
+ if (extClasses.length > 0) {
1244
+ const joined = clsx(extClasses);
1245
+ if (joined.length > 0) {
1246
+ classesOut.push(extMetas[i].transformClass(joined));
1247
+ }
1183
1248
  }
1249
+ } else {
1250
+ workingResolved = extMetas[i].compute(
1251
+ workingResolved,
1252
+ userVariantProps,
1253
+ extSkipKeys,
1254
+ extSkipVals,
1255
+ classesOut,
1256
+ styleOut,
1257
+ protection,
1258
+ defaultResolved,
1259
+ renderOnly,
1260
+ );
1184
1261
  }
1185
- } else {
1186
- workingResolved = extMetas[i].compute(
1262
+ workingResolved = filterOwnDisabledVariants(
1187
1263
  workingResolved,
1188
- userVariantProps,
1189
- extSkipKeys,
1190
- extSkipVals,
1191
- classesOut,
1192
- styleOut,
1193
- protection,
1194
1264
  defaultResolved,
1195
- renderOnly,
1196
1265
  );
1197
1266
  }
1198
- workingResolved = filterOwnDisabledVariants(
1267
+ }
1268
+
1269
+ // Run own computed defaults after extended recipes so defaults resolve
1270
+ // from base to child. They still run before this recipe's `refine`.
1271
+ if (!renderOnly && computedDefaultCount > 0) {
1272
+ workingResolved = runComputedDefaults(
1199
1273
  workingResolved,
1200
1274
  defaultResolved,
1275
+ userVariantProps,
1276
+ true,
1277
+ protection,
1201
1278
  );
1202
1279
  }
1203
- }
1204
1280
 
1205
- // Run own computed defaults after extended components so defaults resolve
1206
- // from base to child. They still run before this component's `refine`.
1207
- if (!renderOnly && computedDefaultCount > 0) {
1208
- workingResolved = runComputedDefaults(
1209
- workingResolved,
1210
- defaultResolved,
1211
- userVariantProps,
1212
- true,
1213
- protection,
1214
- );
1215
- }
1281
+ // Run own `refine` (if any). May modify resolved variants and emit
1282
+ // classes and styles that are applied after this recipe's variants.
1283
+ if (refine) {
1284
+ const refineResult = runRefineContext(
1285
+ workingResolved,
1286
+ true,
1287
+ true,
1288
+ !renderOnly,
1289
+ protection,
1290
+ );
1291
+ workingResolved = refineResult.workingResolved;
1292
+ cClasses = refineResult.classes;
1293
+ cStyle = refineResult.style;
1294
+ }
1216
1295
 
1217
- // Run own `refine` (if any). May modify resolved variants and emit
1218
- // classes and styles that are applied after this component's variants.
1219
- if (refine) {
1220
- const refineResult = runRefineContext(
1221
- workingResolved,
1222
- true,
1223
- true,
1224
- !renderOnly,
1225
- protection,
1226
- );
1227
- workingResolved = refineResult.workingResolved;
1228
- cClasses = refineResult.classes;
1229
- cStyle = refineResult.style;
1230
- }
1296
+ // Apply own base style (after extends' styles, matching original order).
1297
+ if (hasBaseStyle) {
1298
+ Object.assign(styleOut, baseStyle);
1299
+ }
1231
1300
 
1232
- // Apply own base style (after extends' styles, matching original order).
1233
- if (hasBaseStyle) {
1234
- Object.assign(styleOut, baseStyle);
1235
- }
1301
+ // Apply own variants. Skip keys/values come from caller (e.g., parent
1302
+ // wants its own function variant to override this variant).
1303
+ // `variantEntryNames` already excludes disabled keys (those with `null`
1304
+ // value in config) and function variants, so we don't re-check
1305
+ // `disabledVariantKeys` here.
1306
+ const ownSkipKeys = skipKeys;
1307
+ const ownSkipValues = skipValues;
1308
+ for (let i = 0; i < variantEntryCount; i++) {
1309
+ const variantName = variantEntryNames[i];
1310
+ if (ownSkipKeys && ownSkipKeys.has(variantName)) continue;
1311
+ // Read first: an inherited value that could change the output is
1312
+ // never `undefined`, so the own-property check only runs for the
1313
+ // variants that carry a value.
1314
+ const selectedValue = workingResolved[variantName];
1315
+ if (selectedValue === undefined) continue;
1316
+ if (!hasOwn(workingResolved, variantName)) continue;
1317
+ const selectedKey = getVariantValueKey(selectedValue);
1318
+ const variant = variantEntryDefs[i];
1319
+ if (
1320
+ variant.disabledValues &&
1321
+ selectedKey != null &&
1322
+ variant.disabledValues.has(selectedKey)
1323
+ ) {
1324
+ continue;
1325
+ }
1326
+ if (
1327
+ ownSkipValues &&
1328
+ selectedKey != null &&
1329
+ getOwn(ownSkipValues, variantName)?.has(selectedKey)
1330
+ ) {
1331
+ continue;
1332
+ }
1236
1333
 
1237
- // Apply own variants. Skip keys/values come from caller (e.g., parent
1238
- // wants its own function variant to override this variant).
1239
- // `variantEntryNames` already excludes disabled keys (those with `null`
1240
- // value in config) and function variants, so we don't re-check
1241
- // `disabledVariantKeys` here.
1242
- const ownSkipKeys = skipKeys;
1243
- const ownSkipValues = skipValues;
1244
- for (let i = 0; i < variantEntryCount; i++) {
1245
- const variantName = variantEntryNames[i];
1246
- if (ownSkipKeys && ownSkipKeys.has(variantName)) continue;
1247
- // Read first: an inherited value that could change the output is
1248
- // never `undefined`, so the own-property check only runs for the
1249
- // variants that carry a value.
1250
- const selectedValue = workingResolved[variantName];
1251
- if (selectedValue === undefined) continue;
1252
- if (!hasOwn(workingResolved, variantName)) continue;
1253
- const selectedKey = getVariantValueKey(selectedValue);
1254
- const variant = variantEntryDefs[i];
1255
- if (
1256
- variant.disabledValues &&
1257
- selectedKey != null &&
1258
- variant.disabledValues.has(selectedKey)
1259
- ) {
1260
- continue;
1261
- }
1262
- if (
1263
- ownSkipValues &&
1264
- selectedKey != null &&
1265
- getOwn(ownSkipValues, variantName)?.has(selectedKey)
1266
- ) {
1267
- continue;
1334
+ let value: PrebuiltValue | null | undefined;
1335
+ if (variant.values) {
1336
+ if (selectedKey == null) continue;
1337
+ value = variant.values.get(selectedKey);
1338
+ } else if (selectedValue === true) {
1339
+ value = variant.shorthand;
1340
+ }
1341
+ if (!value) continue;
1342
+ if (value.class != null) {
1343
+ classesOut.push(value.class);
1344
+ }
1345
+ if (value.style) {
1346
+ Object.assign(styleOut, value.style);
1347
+ }
1268
1348
  }
1269
1349
 
1270
- let value: PrebuiltValue | null | undefined;
1271
- if (variant.values) {
1272
- if (selectedKey == null) continue;
1273
- value = variant.values.get(selectedKey);
1274
- } else if (selectedValue === true) {
1275
- value = variant.shorthand;
1276
- }
1277
- if (!value) continue;
1278
- if (value.class != null) {
1279
- classesOut.push(value.class);
1280
- }
1281
- if (value.style) {
1282
- Object.assign(styleOut, value.style);
1350
+ // Apply function variants — entries in `variants` whose value is a
1351
+ // function. They run after static variants and override any same-key
1352
+ // inherited variant via `staticExtSkipKeys`.
1353
+ for (let i = 0; i < functionVariantCount; i++) {
1354
+ const variantName = functionVariantNames[i];
1355
+ if (ownSkipKeys && ownSkipKeys.has(variantName)) continue;
1356
+ const selectedValue = workingResolved[variantName];
1357
+ if (selectedValue === undefined) continue;
1358
+ if (!hasOwn(workingResolved, variantName)) continue;
1359
+ const selectedKey = getVariantValueKey(selectedValue);
1360
+ if (
1361
+ ownSkipValues &&
1362
+ selectedKey != null &&
1363
+ getOwn(ownSkipValues, variantName)?.has(selectedKey)
1364
+ ) {
1365
+ continue;
1366
+ }
1367
+ const fn = functionVariantFns[i];
1368
+ const computedResult = fn(selectedValue);
1369
+ if (computedResult == null) continue;
1370
+ const r = extractClassAndStylePrebuilt(computedResult);
1371
+ if (r.class != null) {
1372
+ classesOut.push(r.class);
1373
+ }
1374
+ if (r.style) {
1375
+ Object.assign(styleOut, r.style);
1376
+ }
1283
1377
  }
1284
- }
1285
1378
 
1286
- // Apply function variants — entries in `variants` whose value is a
1287
- // function. They run after static variants and override any same-key
1288
- // inherited variant via `staticExtSkipKeys`.
1289
- for (let i = 0; i < functionVariantCount; i++) {
1290
- const variantName = functionVariantNames[i];
1291
- if (ownSkipKeys && ownSkipKeys.has(variantName)) continue;
1292
- const selectedValue = workingResolved[variantName];
1293
- if (selectedValue === undefined) continue;
1294
- if (!hasOwn(workingResolved, variantName)) continue;
1295
- const selectedKey = getVariantValueKey(selectedValue);
1296
- if (
1297
- ownSkipValues &&
1298
- selectedKey != null &&
1299
- getOwn(ownSkipValues, variantName)?.has(selectedKey)
1300
- ) {
1301
- continue;
1302
- }
1303
- const fn = functionVariantFns[i];
1304
- const computedResult = fn(selectedValue);
1305
- if (computedResult == null) continue;
1306
- const r = extractClassAndStylePrebuilt(computedResult);
1307
- if (r.class != null) {
1308
- classesOut.push(r.class);
1379
+ // Apply `refine` results — must come after own variants (static and
1380
+ // function).
1381
+ if (cClasses) {
1382
+ for (let i = 0; i < cClasses.length; i++) {
1383
+ classesOut.push(cClasses[i] as ClsxClassValue);
1384
+ }
1309
1385
  }
1310
- if (r.style) {
1311
- Object.assign(styleOut, r.style);
1386
+ if (cStyle) {
1387
+ Object.assign(styleOut, cStyle);
1312
1388
  }
1313
- }
1314
1389
 
1315
- // Apply `refine` results — must come after own variants (static and
1316
- // function).
1317
- if (cClasses) {
1318
- for (let i = 0; i < cClasses.length; i++) {
1319
- classesOut.push(cClasses[i] as ClsxClassValue);
1320
- }
1321
- }
1322
- if (cStyle) {
1323
- Object.assign(styleOut, cStyle);
1324
- }
1325
-
1326
- return workingResolved;
1327
- };
1390
+ return workingResolved;
1391
+ };
1328
1392
 
1329
- const compute: ComputeFn =
1330
- !refine && computedDefaultCount === 0 && extMetasWithRefineCount === 0
1331
- ? computeOnce
1332
- : (
1333
- resolved,
1334
- userVariantProps,
1335
- skipKeys,
1336
- skipValues,
1337
- classesOut,
1338
- styleOut,
1339
- ) => {
1340
- let remaining = MAX_REFINE_RUNS;
1341
- const protection: RefineProtection | null = canProtectVariants
1342
- ? { variants: null }
1343
- : null;
1344
- const incomingDefaultResolved = resolved;
1345
- let workingResolved = resolved;
1346
- // Latest variant changes from non-converging iterations inside the
1347
- // tracking window. Lazy-init keeps convergent loops allocation-free.
1348
- let unstableChanges: Map<string, VariantChange> | null = null;
1349
- let lastClasses: ClsxClassValue[] = [];
1350
- let lastStyle: StyleValue = {};
1351
- let isFirstRun = true;
1352
-
1353
- while (remaining > 0) {
1354
- remaining -= 1;
1355
- const useDirectOutput = isFirstRun;
1356
- const classCount = classesOut.length;
1357
- const nextClasses: ClsxClassValue[] = useDirectOutput
1358
- ? classesOut
1359
- : [];
1360
- const nextStyle: StyleValue = useDirectOutput ? styleOut : {};
1361
- const defaultResolved = mergeProtectedIntoBase(
1362
- incomingDefaultResolved,
1363
- protection,
1364
- );
1365
- const nextResolved = computeOnce(
1366
- workingResolved,
1367
- userVariantProps,
1368
- skipKeys,
1369
- skipValues,
1370
- nextClasses,
1371
- nextStyle,
1372
- protection,
1373
- defaultResolved,
1374
- );
1393
+ const compute: ComputeFn =
1394
+ !refine && computedDefaultCount === 0 && extMetasWithRefineCount === 0
1395
+ ? computeOnce
1396
+ : (
1397
+ resolved,
1398
+ userVariantProps,
1399
+ skipKeys,
1400
+ skipValues,
1401
+ classesOut,
1402
+ styleOut,
1403
+ ) => {
1404
+ let remaining = MAX_REFINE_RUNS;
1405
+ const protection: RefineProtection | null = canProtectVariants
1406
+ ? { variants: null }
1407
+ : null;
1408
+ const incomingDefaultResolved = resolved;
1409
+ let workingResolved = resolved;
1410
+ // Latest variant changes from non-converging iterations inside the
1411
+ // tracking window. Lazy-init keeps convergent loops allocation-free.
1412
+ let unstableChanges: Map<string, VariantChange> | null = null;
1413
+ let lastClasses: ClsxClassValue[] = [];
1414
+ let lastStyle: StyleValue = {};
1415
+ let isFirstRun = true;
1416
+
1417
+ while (remaining > 0) {
1418
+ remaining -= 1;
1419
+ const useDirectOutput = isFirstRun;
1420
+ const classCount = classesOut.length;
1421
+ const nextClasses: ClsxClassValue[] = useDirectOutput
1422
+ ? classesOut
1423
+ : [];
1424
+ const nextStyle: StyleValue = useDirectOutput ? styleOut : {};
1425
+ const defaultResolved = mergeProtectedIntoBase(
1426
+ incomingDefaultResolved,
1427
+ protection,
1428
+ );
1429
+ const nextResolved = computeOnce(
1430
+ workingResolved,
1431
+ userVariantProps,
1432
+ skipKeys,
1433
+ skipValues,
1434
+ nextClasses,
1435
+ nextStyle,
1436
+ protection,
1437
+ defaultResolved,
1438
+ );
1375
1439
 
1376
- if (
1377
- nextResolved === workingResolved ||
1378
- areVariantsEqual(workingResolved, nextResolved)
1379
- ) {
1380
- if (nextResolved !== workingResolved) {
1381
- if (useDirectOutput) {
1382
- classesOut.length = classCount;
1383
- for (const key in styleOut) {
1384
- if (hasOwn(styleOut, key)) {
1385
- Reflect.deleteProperty(styleOut, key);
1440
+ if (
1441
+ nextResolved === workingResolved ||
1442
+ areVariantsEqual(workingResolved, nextResolved)
1443
+ ) {
1444
+ 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
+ }
1386
1451
  }
1387
1452
  }
1453
+ computeOnce(
1454
+ nextResolved,
1455
+ userVariantProps,
1456
+ skipKeys,
1457
+ skipValues,
1458
+ classesOut,
1459
+ styleOut,
1460
+ protection,
1461
+ defaultResolved,
1462
+ true,
1463
+ );
1464
+ } else if (!useDirectOutput) {
1465
+ for (let i = 0; i < nextClasses.length; i++) {
1466
+ classesOut.push(nextClasses[i]);
1467
+ }
1468
+ Object.assign(styleOut, nextStyle);
1388
1469
  }
1389
- computeOnce(
1470
+ return nextResolved;
1471
+ }
1472
+
1473
+ // `remaining` is checked first: `process.env.NODE_ENV` is an
1474
+ // environment lookup in Node, not a property load, and only the
1475
+ // last `REFINE_UNSTABLE_TRACKING_WINDOW` passes need it. Bundlers
1476
+ // still inline it and drop the block.
1477
+ if (
1478
+ remaining < REFINE_UNSTABLE_TRACKING_WINDOW &&
1479
+ process.env.NODE_ENV !== "production"
1480
+ ) {
1481
+ if (!unstableChanges) {
1482
+ unstableChanges = new Map<string, VariantChange>();
1483
+ }
1484
+ accumulateUnstableVariantChanges(
1485
+ unstableChanges,
1486
+ workingResolved,
1390
1487
  nextResolved,
1391
- userVariantProps,
1392
- skipKeys,
1393
- skipValues,
1394
- classesOut,
1395
- styleOut,
1396
- protection,
1397
- defaultResolved,
1398
- true,
1399
1488
  );
1400
- } else if (!useDirectOutput) {
1401
- for (let i = 0; i < nextClasses.length; i++) {
1402
- classesOut.push(nextClasses[i]);
1489
+ }
1490
+
1491
+ if (useDirectOutput) {
1492
+ classesOut.length = classCount;
1493
+ for (const key in styleOut) {
1494
+ if (hasOwn(styleOut, key)) {
1495
+ Reflect.deleteProperty(styleOut, key);
1496
+ }
1403
1497
  }
1404
- Object.assign(styleOut, nextStyle);
1498
+ } else {
1499
+ lastClasses = nextClasses;
1500
+ lastStyle = nextStyle;
1405
1501
  }
1406
- return nextResolved;
1502
+
1503
+ workingResolved = nextResolved;
1504
+ isFirstRun = false;
1407
1505
  }
1408
1506
 
1409
- // `remaining` is checked first: `process.env.NODE_ENV` is an
1410
- // environment lookup in Node, not a property load, and only the
1411
- // last `REFINE_UNSTABLE_TRACKING_WINDOW` passes need it. Bundlers
1412
- // still inline it and drop the block.
1413
- if (
1414
- remaining < REFINE_UNSTABLE_TRACKING_WINDOW &&
1415
- process.env.NODE_ENV !== "production"
1416
- ) {
1417
- if (!unstableChanges) {
1418
- unstableChanges = new Map<string, VariantChange>();
1419
- }
1420
- accumulateUnstableVariantChanges(
1507
+ if (process.env.NODE_ENV !== "production") {
1508
+ warnRefineLimit({
1509
+ creationFrame,
1421
1510
  unstableChanges,
1422
- workingResolved,
1423
- nextResolved,
1424
- );
1511
+ });
1425
1512
  }
1426
1513
 
1427
- if (useDirectOutput) {
1428
- classesOut.length = classCount;
1429
- for (const key in styleOut) {
1430
- if (hasOwn(styleOut, key)) {
1431
- Reflect.deleteProperty(styleOut, key);
1432
- }
1433
- }
1434
- } else {
1435
- lastClasses = nextClasses;
1436
- lastStyle = nextStyle;
1514
+ for (let i = 0; i < lastClasses.length; i++) {
1515
+ classesOut.push(lastClasses[i]);
1437
1516
  }
1517
+ Object.assign(styleOut, lastStyle);
1518
+ return workingResolved;
1519
+ };
1520
+
1521
+ const resolveRefineOnce: ResolveRefineOnceFn = (
1522
+ resolved,
1523
+ userVariantProps,
1524
+ filterOwnVariants = true,
1525
+ protection,
1526
+ defaultResolved = resolved,
1527
+ ) => {
1528
+ let workingResolved = resolved;
1529
+
1530
+ for (let i = 0; i < extMetasWithRefineCount; i++) {
1531
+ const meta = extMetasWithRefine[i];
1532
+ const resolveRefine = meta.resolveRefine;
1533
+ if (!resolveRefine) continue;
1534
+ workingResolved = resolveRefine(
1535
+ workingResolved,
1536
+ userVariantProps,
1537
+ true,
1538
+ protection,
1539
+ defaultResolved,
1540
+ );
1541
+ workingResolved = filterOwnDisabledVariants(
1542
+ workingResolved,
1543
+ defaultResolved,
1544
+ );
1545
+ }
1438
1546
 
1439
- workingResolved = nextResolved;
1440
- isFirstRun = false;
1441
- }
1442
-
1443
- if (process.env.NODE_ENV !== "production") {
1444
- warnRefineLimit({
1445
- creationFrame,
1446
- unstableChanges,
1447
- });
1448
- }
1449
-
1450
- for (let i = 0; i < lastClasses.length; i++) {
1451
- classesOut.push(lastClasses[i]);
1452
- }
1453
- Object.assign(styleOut, lastStyle);
1454
- return workingResolved;
1455
- };
1456
-
1457
- const resolveRefineOnce: ResolveRefineOnceFn = (
1458
- resolved,
1459
- userVariantProps,
1460
- filterOwnVariants = true,
1461
- protection,
1462
- defaultResolved = resolved,
1463
- ) => {
1464
- let workingResolved = resolved;
1465
-
1466
- for (let i = 0; i < extMetasWithRefineCount; i++) {
1467
- const meta = extMetasWithRefine[i];
1468
- const resolveRefine = meta.resolveRefine;
1469
- if (!resolveRefine) continue;
1470
- workingResolved = resolveRefine(
1471
- workingResolved,
1472
- userVariantProps,
1473
- true,
1474
- protection,
1475
- defaultResolved,
1476
- );
1477
- workingResolved = filterOwnDisabledVariants(
1478
- workingResolved,
1479
- defaultResolved,
1480
- );
1481
- }
1547
+ if (computedDefaultCount > 0) {
1548
+ workingResolved = runComputedDefaults(
1549
+ workingResolved,
1550
+ defaultResolved,
1551
+ userVariantProps,
1552
+ filterOwnVariants,
1553
+ protection,
1554
+ );
1555
+ }
1556
+ if (refine) {
1557
+ const refineResult = runRefineContext(
1558
+ workingResolved,
1559
+ filterOwnVariants,
1560
+ false,
1561
+ true,
1562
+ protection,
1563
+ );
1564
+ workingResolved = refineResult.workingResolved;
1565
+ }
1482
1566
 
1483
- if (computedDefaultCount > 0) {
1484
- workingResolved = runComputedDefaults(
1485
- workingResolved,
1486
- defaultResolved,
1487
- userVariantProps,
1488
- filterOwnVariants,
1489
- protection,
1490
- );
1491
- }
1492
- if (refine) {
1493
- const refineResult = runRefineContext(
1494
- workingResolved,
1495
- filterOwnVariants,
1496
- false,
1497
- true,
1498
- protection,
1499
- );
1500
- workingResolved = refineResult.workingResolved;
1501
- }
1567
+ return workingResolved;
1568
+ };
1502
1569
 
1503
- return workingResolved;
1504
- };
1570
+ const resolveRefine: ResolveRefineFn | null =
1571
+ refine || computedDefaultCount > 0 || extMetasWithRefineCount > 0
1572
+ ? (resolved, userVariantProps, filterOwnVariants = true) => {
1573
+ let remaining = MAX_REFINE_RUNS;
1574
+ const protection: RefineProtection | null = canProtectVariants
1575
+ ? { variants: null }
1576
+ : null;
1577
+ const incomingDefaultResolved = resolved;
1578
+ let workingResolved = resolved;
1579
+ // Latest variant changes from non-converging iterations inside the
1580
+ // tracking window. See the compute loop above for the shared
1581
+ // rationale.
1582
+ let unstableChanges: Map<string, VariantChange> | null = null;
1583
+
1584
+ while (remaining > 0) {
1585
+ remaining -= 1;
1586
+ const defaultResolved = mergeProtectedIntoBase(
1587
+ incomingDefaultResolved,
1588
+ protection,
1589
+ );
1590
+ const nextResolved = resolveRefineOnce(
1591
+ workingResolved,
1592
+ userVariantProps,
1593
+ filterOwnVariants,
1594
+ protection,
1595
+ defaultResolved,
1596
+ );
1505
1597
 
1506
- const resolveRefine: ResolveRefineFn | null =
1507
- refine || computedDefaultCount > 0 || extMetasWithRefineCount > 0
1508
- ? (resolved, userVariantProps, filterOwnVariants = true) => {
1509
- let remaining = MAX_REFINE_RUNS;
1510
- const protection: RefineProtection | null = canProtectVariants
1511
- ? { variants: null }
1512
- : null;
1513
- const incomingDefaultResolved = resolved;
1514
- let workingResolved = resolved;
1515
- // Latest variant changes from non-converging iterations inside the
1516
- // tracking window. See the compute loop above for the shared
1517
- // rationale.
1518
- let unstableChanges: Map<string, VariantChange> | null = null;
1519
-
1520
- while (remaining > 0) {
1521
- remaining -= 1;
1522
- const defaultResolved = mergeProtectedIntoBase(
1523
- incomingDefaultResolved,
1524
- protection,
1525
- );
1526
- const nextResolved = resolveRefineOnce(
1527
- workingResolved,
1528
- userVariantProps,
1529
- filterOwnVariants,
1530
- protection,
1531
- defaultResolved,
1532
- );
1598
+ if (
1599
+ nextResolved === workingResolved ||
1600
+ areVariantsEqual(workingResolved, nextResolved)
1601
+ ) {
1602
+ return nextResolved;
1603
+ }
1533
1604
 
1534
- if (
1535
- nextResolved === workingResolved ||
1536
- areVariantsEqual(workingResolved, nextResolved)
1537
- ) {
1538
- return nextResolved;
1605
+ // `remaining` is checked first for the reason given in the
1606
+ // compute loop above.
1607
+ if (
1608
+ remaining < REFINE_UNSTABLE_TRACKING_WINDOW &&
1609
+ process.env.NODE_ENV !== "production"
1610
+ ) {
1611
+ if (!unstableChanges) {
1612
+ unstableChanges = new Map<string, VariantChange>();
1613
+ }
1614
+ accumulateUnstableVariantChanges(
1615
+ unstableChanges,
1616
+ workingResolved,
1617
+ nextResolved,
1618
+ );
1619
+ }
1620
+ workingResolved = nextResolved;
1539
1621
  }
1540
1622
 
1541
- // `remaining` is checked first for the reason given in the
1542
- // compute loop above.
1543
- if (
1544
- remaining < REFINE_UNSTABLE_TRACKING_WINDOW &&
1545
- process.env.NODE_ENV !== "production"
1546
- ) {
1547
- if (!unstableChanges) {
1548
- unstableChanges = new Map<string, VariantChange>();
1549
- }
1550
- accumulateUnstableVariantChanges(
1623
+ if (process.env.NODE_ENV !== "production") {
1624
+ warnRefineLimit({
1625
+ creationFrame,
1551
1626
  unstableChanges,
1552
- workingResolved,
1553
- nextResolved,
1554
- );
1627
+ });
1555
1628
  }
1556
- workingResolved = nextResolved;
1557
- }
1558
1629
 
1559
- if (process.env.NODE_ENV !== "production") {
1560
- warnRefineLimit({
1561
- creationFrame,
1562
- unstableChanges,
1563
- });
1630
+ return workingResolved;
1564
1631
  }
1632
+ : null;
1565
1633
 
1566
- return workingResolved;
1567
- }
1568
- : null;
1634
+ // Top-level: resolves variants from user props, calls compute, then
1635
+ // assembles className and style with user-provided class/style overrides.
1636
+ const computeResult = (
1637
+ props: RecipeProps<MergedVariants> = EMPTY_DEFAULTS as RecipeProps<MergedVariants>,
1638
+ ): { className: string; style: StyleValue } => {
1639
+ const propsRecord = props as Record<string, unknown>;
1569
1640
 
1570
- // Top-level: resolves variants from user props, calls compute, then
1571
- // assembles className and style with user-provided class/style overrides.
1572
- const computeResult = (
1573
- props: ComponentProps<MergedVariants> = EMPTY_DEFAULTS as ComponentProps<MergedVariants>,
1574
- ): { className: string; style: StyleValue } => {
1575
- const propsRecord = props as Record<string, unknown>;
1641
+ let resolved: Record<string, unknown> = {};
1642
+ Object.assign(resolved, staticDefaults);
1576
1643
 
1577
- let resolved: Record<string, unknown> = {};
1578
- Object.assign(resolved, staticDefaults);
1579
-
1580
- let userVariantProps: Record<string, unknown>;
1581
- if (needsUserVariantProps) {
1582
- const variantProps: Record<string, unknown> = {};
1583
- for (let i = 0; i < variantKeysLength; i++) {
1584
- const key = variantKeys[i];
1585
- if (!hasOwn(propsRecord, key)) continue;
1586
- const value = propsRecord[key];
1587
- variantProps[key] = value;
1588
- if (value === undefined) continue;
1589
- resolved[key] = value;
1590
- }
1591
- userVariantProps = variantProps;
1592
- } else {
1593
- // Fast path: walk variantKeys directly against propsRecord.
1594
- // Own-property checks ensure a polluted Object.prototype can't add
1595
- // values the user didn't pass.
1596
- for (let i = 0; i < variantKeysLength; i++) {
1597
- const key = variantKeys[i];
1598
- if (!hasOwn(propsRecord, key)) continue;
1599
- const v = propsRecord[key];
1600
- if (v === undefined) continue;
1601
- resolved[key] = v;
1644
+ let userVariantProps: Record<string, unknown>;
1645
+ if (needsUserVariantProps) {
1646
+ const variantProps: Record<string, unknown> = {};
1647
+ for (let i = 0; i < variantKeysLength; i++) {
1648
+ const key = variantKeys[i];
1649
+ if (!hasOwn(propsRecord, key)) continue;
1650
+ const value = propsRecord[key];
1651
+ variantProps[key] = value;
1652
+ if (value === undefined) continue;
1653
+ resolved[key] = value;
1654
+ }
1655
+ userVariantProps = variantProps;
1656
+ } else {
1657
+ // Fast path: walk variantKeys directly against propsRecord.
1658
+ // Own-property checks ensure a polluted Object.prototype can't add
1659
+ // values the user didn't pass.
1660
+ for (let i = 0; i < variantKeysLength; i++) {
1661
+ const key = variantKeys[i];
1662
+ if (!hasOwn(propsRecord, key)) continue;
1663
+ const v = propsRecord[key];
1664
+ if (v === undefined) continue;
1665
+ resolved[key] = v;
1666
+ }
1667
+ userVariantProps = propsRecord;
1602
1668
  }
1603
- userVariantProps = propsRecord;
1604
- }
1605
1669
 
1606
- if (hasAnyDisabled) {
1607
- const filtered: Record<string, unknown> = {};
1608
- filterDisabledInto(resolved, filtered);
1609
- resolved = filtered;
1610
- }
1670
+ if (hasAnyDisabled) {
1671
+ const filtered: Record<string, unknown> = {};
1672
+ filterDisabledInto(resolved, filtered);
1673
+ resolved = filtered;
1674
+ }
1611
1675
 
1612
- // Build allClasses directly. computedBaseClass already has all extend
1613
- // bases joined with config.class — `compute` only adds variant classes
1614
- // on top.
1615
- const allClasses: ClsxClassValue[] = [computedBaseClass];
1616
- const style: StyleValue = {};
1617
- compute(resolved, userVariantProps, null, null, allClasses, style);
1618
-
1619
- // Apply user-provided class / className. Own-property checks keep these
1620
- // consistent with the variant props read above: only what the caller
1621
- // passed is applied, never a key inherited from Object.prototype.
1622
- if (hasOwn(propsRecord, "class")) {
1623
- allClasses.push(propsRecord.class as ClsxClassValue);
1624
- }
1625
- if (hasOwn(propsRecord, "className")) {
1626
- allClasses.push(propsRecord.className as ClsxClassValue);
1627
- }
1676
+ // Build allClasses directly. computedBaseClass already has all extend
1677
+ // bases joined with config.class — `compute` only adds variant classes
1678
+ // on top.
1679
+ const allClasses: ClsxClassValue[] = [computedBaseClass];
1680
+ const style: StyleValue = {};
1681
+ compute(resolved, userVariantProps, null, null, allClasses, style);
1682
+
1683
+ // Apply user-provided class / className. Own-property checks keep these
1684
+ // consistent with the variant props read above: only what the caller
1685
+ // passed is applied, never a key inherited from Object.prototype.
1686
+ if (hasOwn(propsRecord, "class")) {
1687
+ allClasses.push(propsRecord.class as ClsxClassValue);
1688
+ }
1689
+ if (hasOwn(propsRecord, "className")) {
1690
+ allClasses.push(propsRecord.className as ClsxClassValue);
1691
+ }
1628
1692
 
1629
- // Apply user-provided style, read before the own-property check for the
1630
- // same reason as the variant loops above.
1631
- const psv = propsRecord.style;
1632
- if (psv != null && hasOwn(propsRecord, "style")) {
1633
- if (typeof psv === "string") {
1634
- if (psv.length > 0) {
1635
- htmlStyleToStyleValue(psv, style);
1693
+ // Apply user-provided style, read before the own-property check for the
1694
+ // same reason as the variant loops above.
1695
+ const psv = propsRecord.style;
1696
+ if (psv != null && hasOwn(propsRecord, "style")) {
1697
+ if (typeof psv === "string") {
1698
+ if (psv.length > 0) {
1699
+ htmlStyleToStyleValue(psv, style);
1700
+ }
1701
+ } else if (typeof psv === "object") {
1702
+ htmlObjStyleToStyleValue(psv, style);
1636
1703
  }
1637
- } else if (typeof psv === "object") {
1638
- htmlObjStyleToStyleValue(psv, style);
1639
1704
  }
1640
- }
1641
1705
 
1642
- return {
1643
- className: transformClass(clsx(allClasses)),
1644
- style,
1706
+ return {
1707
+ className: transformClass(clsx(allClasses)),
1708
+ style,
1709
+ };
1645
1710
  };
1646
- };
1647
1711
 
1648
- const getVariants = (variants?: VariantValues<MergedVariants>) => {
1649
- const variantsRecord = variants ?? EMPTY_DEFAULTS;
1650
- let variantProps = variantsRecord;
1651
- // Copy to the declared variant keys, so the computed defaults that read
1652
- // this record cannot see an undeclared key as an explicitly passed
1653
- // variant, and cannot re-run a caller accessor once per refine pass. The
1654
- // gate is narrower than that purpose: it misses a chain whose only
1655
- // computed default is this component's own.
1656
- // See https://github.com/ariakit/clava/issues/494
1657
- if (variants && extMetasWithRefineCount > 0) {
1658
- variantProps = {};
1659
- for (let i = 0; i < variantKeysLength; i++) {
1660
- const key = variantKeys[i];
1661
- if (hasOwn(variantsRecord, key)) {
1662
- variantProps[key] = variantsRecord[key];
1712
+ const getVariants = (variants?: VariantValues<MergedVariants>) => {
1713
+ const variantsRecord = variants ?? EMPTY_DEFAULTS;
1714
+ let variantProps = variantsRecord;
1715
+ // Copy to the declared variant keys, so the computed defaults that read
1716
+ // this record cannot see an undeclared key as an explicitly passed
1717
+ // variant, and cannot re-run a caller accessor once per refine pass. The
1718
+ // gate is narrower than that purpose: it misses a chain whose only
1719
+ // computed default is this recipe's own.
1720
+ // See https://github.com/ariakit/clava/issues/494
1721
+ if (variants && extMetasWithRefineCount > 0) {
1722
+ variantProps = {};
1723
+ for (let i = 0; i < variantKeysLength; i++) {
1724
+ const key = variantKeys[i];
1725
+ if (hasOwn(variantsRecord, key)) {
1726
+ variantProps[key] = variantsRecord[key];
1727
+ }
1663
1728
  }
1664
1729
  }
1665
- }
1666
- let resolvedVariants = resolveVariantsHot(variantProps);
1667
- if (resolveRefine) {
1668
- resolvedVariants = resolveRefine(resolvedVariants, variantProps, false);
1669
- }
1670
- return resolvedVariants as VariantValues<MergedVariants>;
1671
- };
1730
+ let resolvedVariants = resolveVariantsHot(variantProps);
1731
+ if (resolveRefine) {
1732
+ resolvedVariants = resolveRefine(
1733
+ resolvedVariants,
1734
+ variantProps,
1735
+ false,
1736
+ );
1737
+ }
1738
+ return resolvedVariants as VariantValues<MergedVariants>;
1739
+ };
1672
1740
 
1673
- // Compute base class (without variants) — includes extended base classes.
1674
- // Plain `clsx` (no `transformClass`): `meta.baseClass` flows back into
1675
- // parent extends as `clsx` input and then through the single
1676
- // `transformClass(clsx(allClasses))` at render time, so applying it here
1677
- // would compound (double for own-render, triple+ for extend chains) and
1678
- // misbehave for non-idempotent transforms.
1679
- const computedBaseClass = hasExtend
1680
- ? clsx(extBaseClassesArr, baseClass as ClsxClassValue)
1681
- : clsx(baseClass as ClsxClassValue);
1682
-
1683
- // Shared closures across the default and modal components.
1684
- const classFn = (props: ComponentProps<MergedVariants> = {}) => {
1685
- return computeResult(props).className;
1686
- };
1687
- const meta: ComponentMeta = {
1688
- baseClass: computedBaseClass,
1689
- staticDefaults,
1690
- compute: computeOnce,
1691
- resolveRefine: resolveRefine ? resolveRefineOnce : null,
1692
- transformClass,
1693
- functionVariantKeys,
1694
- computedDefaultKeys,
1695
- };
1741
+ // Compute base class (without variants) — includes extended base classes.
1742
+ // Plain `clsx` (no `transformClass`): `meta.baseClass` flows back into
1743
+ // parent extends as `clsx` input and then through the single
1744
+ // `transformClass(clsx(allClasses))` at render time, so applying it here
1745
+ // would compound (double for own-render, triple+ for extend chains) and
1746
+ // misbehave for non-idempotent transforms.
1747
+ const computedBaseClass = hasExtend
1748
+ ? clsx(extBaseClassesArr, baseClass)
1749
+ : baseClass;
1750
+
1751
+ // Shared closures across the default and modal recipes.
1752
+ const classFn = (props: RecipeProps<MergedVariants> = {}) => {
1753
+ return computeResult(props).className;
1754
+ };
1755
+ const meta: RecipeMeta = {
1756
+ identity,
1757
+ extends: extMetas,
1758
+ rebuild: (extensions) => build(extensions).meta,
1759
+ baseClass: computedBaseClass,
1760
+ staticDefaults,
1761
+ compute: computeOnce,
1762
+ resolveRefine: resolveRefine ? resolveRefineOnce : null,
1763
+ transformClass,
1764
+ functionVariantKeys,
1765
+ computedDefaultKeys,
1766
+ };
1696
1767
 
1697
- const initComponent = <
1698
- R extends ComponentResult,
1699
- T extends ModalComponent<MergedVariants, R>,
1700
- >(
1701
- c: T,
1702
- propKeys: string[],
1703
- style: T["style"],
1704
- ): T => {
1705
- c.class = classFn;
1706
- c.style = style;
1707
- c.getVariants = getVariants;
1708
- c.variantKeys = variantKeys;
1709
- c.propKeys = propKeys;
1710
- (c as AnyComponent & ComponentWithMeta)[META_KEY] = meta;
1711
- return c;
1712
- };
1768
+ const initRecipe = <
1769
+ R extends RecipeResult,
1770
+ T extends ModalRecipe<MergedVariants, R>,
1771
+ >(
1772
+ recipe: T,
1773
+ propKeys: string[],
1774
+ style: T["style"],
1775
+ ): T => {
1776
+ recipe.class = classFn;
1777
+ recipe.style = style;
1778
+ recipe.getVariants = getVariants;
1779
+ recipe.variantKeys = variantKeys;
1780
+ recipe.propKeys = propKeys;
1781
+ (recipe as AnyRecipe & RecipeWithMeta)[META_KEY] = meta;
1782
+ return recipe;
1783
+ };
1713
1784
 
1714
- // Default component
1715
- const defaultComponent = ((props: ComponentProps<MergedVariants> = {}) => {
1716
- const { className, style } = computeResult(props);
1717
- return { class: className, style };
1718
- }) as CVComponent<V, E>;
1719
- initComponent(defaultComponent, inputPropsKeys, (props = {}) => {
1720
- return computeResult(props).style;
1721
- });
1722
-
1723
- // JSX component
1724
- const jsxComponent = ((props: ComponentProps<MergedVariants> = {}) => {
1725
- const { className, style } = computeResult(props);
1726
- return { className, style };
1727
- }) as ModalComponent<MergedVariants, JSXProps>;
1728
- initComponent(
1729
- jsxComponent,
1730
- ["className", "style", ...variantKeys],
1731
- (props = {}) => computeResult(props).style,
1732
- );
1733
-
1734
- // HTML component
1735
- const htmlComponent = ((props: ComponentProps<MergedVariants> = {}) => {
1736
- const { className, style } = computeResult(props);
1737
- return { class: className, style: styleValueToHTMLStyle(style) };
1738
- }) as ModalComponent<MergedVariants, HTMLProps>;
1739
- initComponent(
1740
- htmlComponent,
1741
- ["class", "style", ...variantKeys],
1742
- (props = {}) => styleValueToHTMLStyle(computeResult(props).style),
1743
- );
1744
-
1745
- // HTMLObj component
1746
- const htmlObjComponent = ((props: ComponentProps<MergedVariants> = {}) => {
1747
- const { className, style } = computeResult(props);
1748
- return { class: className, style: styleValueToHTMLObjStyle(style) };
1749
- }) as ModalComponent<MergedVariants, HTMLObjProps>;
1750
- initComponent(
1751
- htmlObjComponent,
1752
- ["class", "style", ...variantKeys],
1753
- (props = {}) => styleValueToHTMLObjStyle(computeResult(props).style),
1754
- );
1755
-
1756
- defaultComponent.jsx = jsxComponent;
1757
- defaultComponent.html = htmlComponent;
1758
- defaultComponent.htmlObj = htmlObjComponent;
1759
-
1760
- return defaultComponent;
1785
+ // Default recipe
1786
+ const defaultRecipe = ((props: RecipeProps<MergedVariants> = {}) => {
1787
+ const { className, style } = computeResult(props);
1788
+ return { class: className, style };
1789
+ }) as Recipe<V, E>;
1790
+ initRecipe(defaultRecipe, inputPropsKeys, (props = {}) => {
1791
+ return computeResult(props).style;
1792
+ });
1793
+
1794
+ // JSX recipe
1795
+ const jsxRecipe = ((props: RecipeProps<MergedVariants> = {}) => {
1796
+ const { className, style } = computeResult(props);
1797
+ return { className, style };
1798
+ }) as ModalRecipe<MergedVariants, JSXProps>;
1799
+ initRecipe(
1800
+ jsxRecipe,
1801
+ ["className", "style", ...variantKeys],
1802
+ (props = {}) => computeResult(props).style,
1803
+ );
1804
+
1805
+ // HTML recipe
1806
+ const htmlRecipe = ((props: RecipeProps<MergedVariants> = {}) => {
1807
+ const { className, style } = computeResult(props);
1808
+ return { class: className, style: styleValueToHTMLStyle(style) };
1809
+ }) as ModalRecipe<MergedVariants, HTMLProps>;
1810
+ initRecipe(htmlRecipe, ["class", "style", ...variantKeys], (props = {}) =>
1811
+ styleValueToHTMLStyle(computeResult(props).style),
1812
+ );
1813
+
1814
+ // HTMLObj recipe
1815
+ const htmlObjRecipe = ((props: RecipeProps<MergedVariants> = {}) => {
1816
+ const { className, style } = computeResult(props);
1817
+ return { class: className, style: styleValueToHTMLObjStyle(style) };
1818
+ }) as ModalRecipe<MergedVariants, HTMLObjProps>;
1819
+ initRecipe(
1820
+ htmlObjRecipe,
1821
+ ["class", "style", ...variantKeys],
1822
+ (props = {}) => styleValueToHTMLObjStyle(computeResult(props).style),
1823
+ );
1824
+
1825
+ defaultRecipe.jsx = jsxRecipe;
1826
+ defaultRecipe.html = htmlRecipe;
1827
+ defaultRecipe.htmlObj = htmlObjRecipe;
1828
+
1829
+ return { recipe: defaultRecipe, meta };
1830
+ };
1831
+ return build(initialExtensions);
1761
1832
  };
1762
1833
 
1834
+ const cv = <V extends Variants = {}, const E extends AnyRecipe[] = []>(
1835
+ config: CVConfig<V, E> = {},
1836
+ ): Recipe<V, E> => buildRecipe(config).recipe;
1837
+
1763
1838
  return { cv, cx };
1764
1839
  }
1765
1840