clava 0.6.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import clsx, { type ClassValue as ClsxClassValue } from "clsx";
2
2
  import {
3
3
  REFINE_UNSTABLE_TRACKING_WINDOW,
4
- type RefineRunState,
5
4
  type VariantChange,
6
5
  accumulateUnstableVariantChanges,
7
6
  captureCreationFrame,
@@ -18,6 +17,7 @@ import type {
18
17
  HTMLObjProps,
19
18
  HTMLProps,
20
19
  JSXProps,
20
+ KeySourceComponent,
21
21
  MergeVariants,
22
22
  ModalComponent,
23
23
  Refine,
@@ -29,13 +29,11 @@ import type {
29
29
  Variants,
30
30
  } from "./types.ts";
31
31
  import {
32
+ hasOwn,
32
33
  htmlObjStyleToStyleValue,
33
34
  htmlStyleToStyleValue,
34
- isHTMLObjStyle,
35
- jsxStyleToStyleValue,
36
35
  styleValueToHTMLObjStyle,
37
36
  styleValueToHTMLStyle,
38
- styleValueToJSXStyle,
39
37
  } from "./utils.ts";
40
38
 
41
39
  // Internal compute path: pushes the variant classes contributed by this
@@ -50,9 +48,16 @@ type ComputeFn = (
50
48
  skipValues: Record<string, Set<string>> | null,
51
49
  classesOut: ClsxClassValue[],
52
50
  styleOut: StyleValue,
53
- runState?: RefineRunState,
51
+ ) => Record<string, unknown>;
52
+
53
+ type ComputeOnceFn = (
54
+ resolved: Record<string, unknown>,
55
+ userVariantProps: Record<string, unknown>,
56
+ skipKeys: Set<string> | null,
57
+ skipValues: Record<string, Set<string>> | null,
58
+ classesOut: ClsxClassValue[],
59
+ styleOut: StyleValue,
54
60
  protectedVariants?: Record<string, unknown> | null,
55
- pendingProtectedVariants?: Record<string, unknown> | null,
56
61
  protectedVariantKeys?: Set<string> | null,
57
62
  defaultResolved?: Record<string, unknown>,
58
63
  renderOnly?: boolean,
@@ -62,9 +67,13 @@ type ResolveRefineFn = (
62
67
  resolved: Record<string, unknown>,
63
68
  userVariantProps: Record<string, unknown>,
64
69
  filterOwnVariants?: boolean,
65
- runState?: RefineRunState,
70
+ ) => Record<string, unknown>;
71
+
72
+ type ResolveRefineOnceFn = (
73
+ resolved: Record<string, unknown>,
74
+ userVariantProps: Record<string, unknown>,
75
+ filterOwnVariants?: boolean,
66
76
  protectedVariants?: Record<string, unknown> | null,
67
- pendingProtectedVariants?: Record<string, unknown> | null,
68
77
  protectedVariantKeys?: Set<string> | null,
69
78
  defaultResolved?: Record<string, unknown>,
70
79
  ) => Record<string, unknown>;
@@ -78,10 +87,10 @@ type ComputedDefaultVariantFn = (
78
87
  interface ComponentMeta {
79
88
  baseClass: string;
80
89
  staticDefaults: Record<string, unknown>;
81
- // Returns variant classes + style for this component, used by extending
82
- // components. Top-level rendering also routes through this.
83
- compute: ComputeFn;
84
- resolveRefine: ResolveRefineFn | null;
90
+ // Performs a single compute pass for extending components, returning the
91
+ // resolved variants while pushing classes and styles into the output values.
92
+ compute: ComputeOnceFn;
93
+ resolveRefine: ResolveRefineOnceFn | null;
85
94
  // Reference identity is used to detect mixed-factory `extend`. When a
86
95
  // component is extended by a parent from a different `create()` call, the
87
96
  // parent applies this transform to the extend's contribution before joining,
@@ -105,10 +114,7 @@ interface ComponentWithMeta {
105
114
  [META_KEY]?: ComponentMeta;
106
115
  }
107
116
 
108
- const EMPTY_DEFAULTS: Record<string, unknown> = Object.freeze({}) as Record<
109
- string,
110
- unknown
111
- >;
117
+ const EMPTY_DEFAULTS: Record<string, unknown> = {};
112
118
 
113
119
  const MAX_REFINE_RUNS = 50;
114
120
 
@@ -117,12 +123,12 @@ function areVariantsEqual(
117
123
  b: Record<string, unknown>,
118
124
  ): boolean {
119
125
  for (const key in a) {
120
- if (!Object.hasOwn(a, key)) continue;
126
+ if (!hasOwn(a, key)) continue;
121
127
  if (!Object.is(a[key], b[key])) return false;
122
128
  }
123
129
  for (const key in b) {
124
- if (!Object.hasOwn(b, key)) continue;
125
- if (!Object.hasOwn(a, key)) return false;
130
+ if (!hasOwn(b, key)) continue;
131
+ if (!hasOwn(a, key)) return false;
126
132
  }
127
133
  return true;
128
134
  }
@@ -130,81 +136,17 @@ function areVariantsEqual(
130
136
  function getExtUserVariantProps(
131
137
  userVariantProps: Record<string, unknown>,
132
138
  protectedVariants: Record<string, unknown> | null,
133
- changedVariants: Record<string, unknown> | null,
134
139
  ): Record<string, unknown> {
135
- const extUserVariantProps: Record<string, unknown> = {};
136
- Object.assign(extUserVariantProps, userVariantProps);
137
- if (protectedVariants) {
138
- Object.assign(extUserVariantProps, protectedVariants);
139
- }
140
- if (changedVariants) {
141
- Object.assign(extUserVariantProps, changedVariants);
142
- }
143
- return extUserVariantProps;
144
- }
145
-
146
- function mergeVariants(
147
- target: Record<string, unknown>,
148
- source: Record<string, unknown>,
149
- skipKeys?: Set<string> | null,
150
- ): boolean {
151
- let changed = false;
152
- if (!skipKeys || skipKeys.size === 0) {
153
- for (const key in source) {
154
- if (!Object.hasOwn(source, key)) continue;
155
- const value = source[key];
156
- if (!Object.is(target[key], value)) {
157
- changed = true;
158
- }
159
- target[key] = value;
160
- }
161
- return changed;
162
- }
163
- for (const key in source) {
164
- if (!Object.hasOwn(source, key)) continue;
165
- if (skipKeys.has(key)) continue;
166
- const value = source[key];
167
- if (!Object.is(target[key], value)) {
168
- changed = true;
169
- }
170
- target[key] = value;
171
- }
172
- return changed;
140
+ return Object.assign({}, userVariantProps, protectedVariants);
173
141
  }
174
142
 
175
143
  function mergeProtectedIntoBase(
176
144
  baseResolved: Record<string, unknown>,
177
- protectedVariants: Record<string, unknown> | null | undefined,
145
+ protectedVariants: Record<string, unknown>,
146
+ protectedVariantKeys: Set<string>,
178
147
  ): Record<string, unknown> {
179
- if (!protectedVariants) {
180
- return baseResolved;
181
- }
182
- let hasProtected = false;
183
- for (const key in protectedVariants) {
184
- if (!Object.hasOwn(protectedVariants, key)) continue;
185
- hasProtected = true;
186
- break;
187
- }
188
- if (!hasProtected) {
189
- return baseResolved;
190
- }
191
- const resolved: Record<string, unknown> = {};
192
- Object.assign(resolved, baseResolved);
193
- for (const key in protectedVariants) {
194
- if (!Object.hasOwn(protectedVariants, key)) continue;
195
- resolved[key] = protectedVariants[key];
196
- }
197
- return resolved;
198
- }
199
-
200
- // Components carry internal metadata on a non-public property so user-facing
201
- // component types stay clean.
202
- function getComponentMeta(component: AnyComponent): ComponentMeta | undefined {
203
- return (component as AnyComponent & ComponentWithMeta)[META_KEY];
204
- }
205
-
206
- function setComponentMeta(component: AnyComponent, meta: ComponentMeta): void {
207
- (component as AnyComponent & ComponentWithMeta)[META_KEY] = meta;
148
+ if (protectedVariantKeys.size === 0) return baseResolved;
149
+ return Object.assign({}, baseResolved, protectedVariants);
208
150
  }
209
151
 
210
152
  export type {
@@ -326,11 +268,6 @@ interface CreateParams {
326
268
  transformClass?: (className: string) => string;
327
269
  }
328
270
 
329
- interface VariantConfigLike {
330
- extend?: AnyComponent[];
331
- variants?: Record<string, unknown>;
332
- }
333
-
334
271
  function isRecordObject(value: unknown): value is Record<string, unknown> {
335
272
  if (typeof value !== "object") return false;
336
273
  if (value == null) return false;
@@ -338,14 +275,6 @@ function isRecordObject(value: unknown): value is Record<string, unknown> {
338
275
  return true;
339
276
  }
340
277
 
341
- /**
342
- * Checks if a value is a style-class object (`{ style, class? }`).
343
- */
344
- function isStyleClassValue(value: unknown): value is StyleClassValue {
345
- if (!isRecordObject(value)) return false;
346
- return "style" in value || "class" in value;
347
- }
348
-
349
278
  /**
350
279
  * Converts any style input (string, JSX object, or HTML object) to a normalized
351
280
  * StyleValue.
@@ -355,10 +284,7 @@ function normalizeStyle(style: unknown): StyleValue {
355
284
  return htmlStyleToStyleValue(style);
356
285
  }
357
286
  if (typeof style === "object" && style != null) {
358
- if (isHTMLObjStyle(style as Record<string, unknown>)) {
359
- return htmlObjStyleToStyleValue(style as Record<string, string | number>);
360
- }
361
- return jsxStyleToStyleValue(style as Record<string, string | number>);
287
+ return htmlObjStyleToStyleValue(style);
362
288
  }
363
289
  return {};
364
290
  }
@@ -372,53 +298,19 @@ interface PrebuiltValue {
372
298
  style: StyleValue | null;
373
299
  }
374
300
 
375
- function extractStyleClassPrebuilt(value: StyleClassValue): PrebuiltValue {
376
- const styleNorm = normalizeStyle(value.style);
377
- return {
378
- class: value.class ?? null,
379
- style: styleNorm && Object.keys(styleNorm).length > 0 ? styleNorm : null,
380
- };
381
- }
382
-
383
301
  function extractClassAndStylePrebuilt(value: unknown): PrebuiltValue {
384
- if (isStyleClassValue(value)) {
385
- return extractStyleClassPrebuilt(value);
302
+ if (!isRecordObject(value)) {
303
+ return { class: value as ClassValue, style: null };
386
304
  }
387
- if (isRecordObject(value)) {
305
+ if (!("style" in value || "class" in value)) {
388
306
  return { class: null, style: null };
389
307
  }
390
- return { class: value as ClassValue, style: null };
391
- }
392
-
393
- /**
394
- * Gets all variant keys from a component's config, including extended
395
- * components.
396
- */
397
- function collectVariantKeys(config: VariantConfigLike): string[] {
398
- const keys = new Set<string>();
399
-
400
- if (config.extend) {
401
- for (const ext of config.extend) {
402
- const extKeys = ext.variantKeys as readonly string[];
403
- for (let i = 0; i < extKeys.length; i++) {
404
- keys.add(extKeys[i]);
405
- }
406
- }
407
- }
408
-
409
- if (config.variants) {
410
- for (const key in config.variants) {
411
- if (!Object.hasOwn(config.variants, key)) continue;
412
- const variant = config.variants[key];
413
- if (variant === null) {
414
- keys.delete(key);
415
- continue;
416
- }
417
- keys.add(key);
418
- }
419
- }
420
-
421
- return Array.from(keys);
308
+ const styleClassValue = value as StyleClassValue;
309
+ const style = normalizeStyle(styleClassValue.style);
310
+ return {
311
+ class: styleClassValue.class ?? null,
312
+ style: Object.keys(style).length > 0 ? style : null,
313
+ };
422
314
  }
423
315
 
424
316
  function getVariantValueKey(value: unknown): string | undefined {
@@ -434,88 +326,17 @@ function getVariantValueKey(value: unknown): string | undefined {
434
326
  return undefined;
435
327
  }
436
328
 
437
- function collectDisabledVariantKeys(config: VariantConfigLike): Set<string> {
438
- const keys = new Set<string>();
439
- if (!config.variants) {
440
- return keys;
441
- }
442
- for (const key in config.variants) {
443
- if (!Object.hasOwn(config.variants, key)) continue;
444
- if (config.variants[key] === null) {
445
- keys.add(key);
446
- }
447
- }
448
- return keys;
449
- }
329
+ const EMPTY_KEYS: readonly string[] = [];
450
330
 
451
- function collectDisabledVariantValues(
452
- config: VariantConfigLike,
453
- ): Record<string, Set<string>> {
454
- const values: Record<string, Set<string>> = {};
455
- if (!config.variants) {
456
- return values;
457
- }
458
- for (const key in config.variants) {
459
- if (!Object.hasOwn(config.variants, key)) continue;
460
- const variant = config.variants[key];
461
- if (!isRecordObject(variant)) continue;
462
- let bucket: Set<string> | undefined;
463
- for (const variantValue in variant) {
464
- if (!Object.hasOwn(variant, variantValue)) continue;
465
- if (variant[variantValue] !== null) continue;
466
- if (!bucket) {
467
- bucket = new Set<string>();
468
- values[key] = bucket;
469
- }
470
- bucket.add(variantValue);
471
- }
472
- }
473
- return values;
474
- }
475
-
476
- interface NormalizedSource {
477
- propKeys: string[];
478
- variantKeys: string[];
479
- isComponent: boolean;
480
- }
481
-
482
- const EMPTY_SOURCE: NormalizedSource = {
483
- propKeys: [],
484
- variantKeys: [],
485
- isComponent: false,
486
- };
487
-
488
- function normalizeKeySource(source: unknown): NormalizedSource {
489
- if (Array.isArray(source)) {
490
- return {
491
- propKeys: source as string[],
492
- variantKeys: source as string[],
493
- isComponent: false,
494
- };
495
- }
496
-
497
- if (!source) {
498
- return EMPTY_SOURCE;
499
- }
331
+ function isComponentKeySource(source: unknown): source is KeySourceComponent {
332
+ if (!source) return false;
500
333
  if (typeof source !== "object" && typeof source !== "function") {
501
- return EMPTY_SOURCE;
334
+ return false;
502
335
  }
503
336
  const typed = source as Record<string, unknown>;
504
- if (typeof typed.getVariants !== "function") {
505
- return EMPTY_SOURCE;
506
- }
507
- if (!Array.isArray(typed.propKeys)) {
508
- return EMPTY_SOURCE;
509
- }
510
- if (!Array.isArray(typed.variantKeys)) {
511
- return EMPTY_SOURCE;
512
- }
513
-
514
- return {
515
- propKeys: typed.propKeys as string[],
516
- variantKeys: typed.variantKeys as string[],
517
- isComponent: true,
518
- };
337
+ if (typeof typed.getVariants !== "function") return false;
338
+ if (!Array.isArray(typed.propKeys)) return false;
339
+ return Array.isArray(typed.variantKeys);
519
340
  }
520
341
 
521
342
  /**
@@ -525,7 +346,7 @@ function normalizeKeySource(source: unknown): NormalizedSource {
525
346
  * claim styling props.
526
347
  */
527
348
  function splitPropsImpl(
528
- selfKeys: string[],
349
+ selfKeys: readonly string[],
529
350
  selfIsComponent: boolean,
530
351
  props: Record<string, unknown>,
531
352
  sources: unknown[],
@@ -538,7 +359,7 @@ function splitPropsImpl(
538
359
  const selfKeysLength = selfKeys.length;
539
360
  for (let i = 0; i < selfKeysLength; i++) {
540
361
  const key = selfKeys[i];
541
- if (key !== undefined && key in props) {
362
+ if (key !== undefined && hasOwn(props, key)) {
542
363
  selfResult[key] = props[key];
543
364
  }
544
365
  }
@@ -546,28 +367,33 @@ function splitPropsImpl(
546
367
 
547
368
  // Track effective key arrays for the rest computation — for typical inputs
548
369
  // a linear scan beats building a Set up-front.
549
- const effectiveKeyArrays: string[][] = [selfKeys];
370
+ const effectiveKeyArrays: Array<readonly string[]> = [selfKeys];
550
371
 
551
372
  for (let s = 0; s < sourcesLength; s++) {
552
- const source = normalizeKeySource(sources[s]);
373
+ const source = sources[s];
553
374
  const sourceResult: Record<string, unknown> = {};
554
-
555
- const effectiveKeys =
556
- source.isComponent && stylingClaimed
557
- ? source.variantKeys
558
- : source.propKeys;
375
+ let sourceIsComponent = false;
376
+ let effectiveKeys: readonly string[];
377
+ if (Array.isArray(source)) {
378
+ effectiveKeys = source;
379
+ } else if (isComponentKeySource(source)) {
380
+ sourceIsComponent = true;
381
+ effectiveKeys = stylingClaimed ? source.variantKeys : source.propKeys;
382
+ } else {
383
+ effectiveKeys = EMPTY_KEYS;
384
+ }
559
385
 
560
386
  const effectiveKeysLength = effectiveKeys.length;
561
387
  for (let i = 0; i < effectiveKeysLength; i++) {
562
388
  const key = effectiveKeys[i];
563
- if (key !== undefined && key in props) {
389
+ if (key !== undefined && hasOwn(props, key)) {
564
390
  sourceResult[key] = props[key];
565
391
  }
566
392
  }
567
393
  results.push(sourceResult);
568
394
  effectiveKeyArrays.push(effectiveKeys);
569
395
 
570
- if (source.isComponent && !stylingClaimed) {
396
+ if (sourceIsComponent && !stylingClaimed) {
571
397
  stylingClaimed = true;
572
398
  }
573
399
  }
@@ -607,13 +433,13 @@ export const splitProps: SplitPropsFunction = ((
607
433
  source1: unknown,
608
434
  ...sources: unknown[]
609
435
  ) => {
610
- const normalizedSource1 = normalizeKeySource(source1);
611
- return splitPropsImpl(
612
- normalizedSource1.propKeys,
613
- normalizedSource1.isComponent,
614
- props,
615
- sources,
616
- );
436
+ if (Array.isArray(source1)) {
437
+ return splitPropsImpl(source1, false, props, sources);
438
+ }
439
+ if (isComponentKeySource(source1)) {
440
+ return splitPropsImpl(source1.propKeys, true, props, sources);
441
+ }
442
+ return splitPropsImpl(EMPTY_KEYS, false, props, sources);
617
443
  }) as SplitPropsFunction;
618
444
 
619
445
  /**
@@ -642,7 +468,7 @@ function buildPrebuiltVariant(variantDef: unknown): PrebuiltVariant {
642
468
  const values: Record<string, PrebuiltValue> = {};
643
469
  let disabledValues: Set<string> | null = null;
644
470
  for (const key in variantDef) {
645
- if (!Object.hasOwn(variantDef, key)) continue;
471
+ if (!hasOwn(variantDef, key)) continue;
646
472
  const value = variantDef[key];
647
473
  if (value === null) {
648
474
  if (!disabledValues) {
@@ -666,7 +492,7 @@ function buildPrebuiltVariant(variantDef: unknown): PrebuiltVariant {
666
492
  export function create({
667
493
  transformClass = (className) => className,
668
494
  }: CreateParams = {}) {
669
- const cx = (...classes: ClsxClassValue[]) => transformClass(clsx(...classes));
495
+ const cx = (...classes: ClsxClassValue[]) => transformClass(clsx(classes));
670
496
 
671
497
  const cv = <V extends Variants = {}, const E extends AnyComponent[] = []>(
672
498
  config: CVConfig<V, E> = {},
@@ -674,17 +500,6 @@ export function create({
674
500
  type MergedVariants = MergeVariants<V, E>;
675
501
 
676
502
  // ----- Pre-computed at creation time -----
677
- const variantKeys = collectVariantKeys(config);
678
- const variantKeysLength = variantKeys.length;
679
- const disabledVariantKeys = collectDisabledVariantKeys(config);
680
- const disabledVariantValues = collectDisabledVariantValues(config);
681
- const hasDisabledVariantKeys = disabledVariantKeys.size > 0;
682
- const disabledVariantValueKeys = Object.keys(disabledVariantValues);
683
- const hasDisabledVariantValues = disabledVariantValueKeys.length > 0;
684
- const hasAnyDisabled = hasDisabledVariantKeys || hasDisabledVariantValues;
685
-
686
- const inputPropsKeys = ["class", "className", "style", ...variantKeys];
687
-
688
503
  const extend = config.extend;
689
504
  const hasExtend = !!extend && extend.length > 0;
690
505
  const variants = config.variants;
@@ -692,30 +507,93 @@ export function create({
692
507
  const baseStyle = config.style;
693
508
  const hasBaseStyle = !!baseStyle;
694
509
 
695
- // Split `variants` entries into static entries (object/shorthand) and
696
- // function-variant entries. Static entries are pre-built into
697
- // PrebuiltVariant for fast iteration. Function-variant entries override
698
- // any same-key inherited variant (see `staticExtSkipKeys`).
510
+ const variantKeySet = new Set<string>();
511
+ const staticDefaults: Record<string, unknown> = {};
512
+
513
+ // Pre-build extended component info, so the render path doesn't need to
514
+ // read component metadata. Extends from a different `create()`
515
+ // factory (different `transformClass` identity) need their contribution
516
+ // transformed by their own `transformClass` before being joined into our
517
+ // class string.
518
+ const extMetas: ComponentMeta[] = [];
519
+ const extBaseClassesArr: string[] = [];
520
+ const extIsolated: boolean[] = [];
521
+ let hasIsolatedExt = false;
522
+ if (hasExtend) {
523
+ for (const ext of extend) {
524
+ const extKeys = ext.variantKeys as readonly string[];
525
+ for (let i = 0; i < extKeys.length; i++) {
526
+ const key = extKeys[i];
527
+ if (key === undefined) continue;
528
+ variantKeySet.add(key);
529
+ }
530
+
531
+ const meta = (ext as AnyComponent & ComponentWithMeta)[META_KEY];
532
+ if (!meta) continue;
533
+ extMetas.push(meta);
534
+ Object.assign(staticDefaults, meta.staticDefaults);
535
+
536
+ const isolated = meta.transformClass !== transformClass;
537
+ extIsolated.push(isolated);
538
+ if (isolated) {
539
+ hasIsolatedExt = true;
540
+ extBaseClassesArr.push(meta.transformClass(meta.baseClass));
541
+ } else {
542
+ extBaseClassesArr.push(meta.baseClass);
543
+ }
544
+ }
545
+ }
546
+ const extCount = extMetas.length;
547
+
548
+ // Build all own variant metadata in one pass. Disabled value sets are
549
+ // shared with their prebuilt variants instead of scanning and allocating
550
+ // them twice.
551
+ const disabledVariantKeys = new Set<string>();
552
+ const disabledVariantValues: Record<string, Set<string>> = {};
553
+ let hasDisabledVariantValues = false;
699
554
  const variantEntryNames: string[] = [];
700
555
  const variantEntryDefs: PrebuiltVariant[] = [];
701
556
  const functionVariantNames: string[] = [];
702
557
  const functionVariantFns: Array<(value: unknown) => unknown> = [];
703
558
  if (variants) {
704
559
  for (const name in variants) {
705
- if (!Object.hasOwn(variants, name)) continue;
560
+ if (!hasOwn(variants, name)) continue;
706
561
  const variant = (variants as Record<string, unknown>)[name];
707
- if (variant === null) continue;
562
+ if (variant === null) {
563
+ variantKeySet.delete(name);
564
+ disabledVariantKeys.add(name);
565
+ continue;
566
+ }
567
+ variantKeySet.add(name);
708
568
  if (typeof variant === "function") {
709
569
  functionVariantNames.push(name);
710
570
  functionVariantFns.push(variant as (value: unknown) => unknown);
711
571
  continue;
712
572
  }
573
+ const prebuiltVariant = buildPrebuiltVariant(variant);
713
574
  variantEntryNames.push(name);
714
- variantEntryDefs.push(buildPrebuiltVariant(variant));
575
+ variantEntryDefs.push(prebuiltVariant);
576
+ if (prebuiltVariant.disabledValues) {
577
+ disabledVariantValues[name] = prebuiltVariant.disabledValues;
578
+ hasDisabledVariantValues = true;
579
+ }
580
+ if (
581
+ prebuiltVariant.values &&
582
+ hasOwn(prebuiltVariant.values, "false") &&
583
+ staticDefaults[name] === undefined
584
+ ) {
585
+ staticDefaults[name] = false;
586
+ }
715
587
  }
716
588
  }
589
+ const variantKeys = Array.from(variantKeySet);
590
+ const variantKeysLength = variantKeys.length;
717
591
  const variantEntryCount = variantEntryNames.length;
718
592
  const functionVariantCount = functionVariantNames.length;
593
+ const hasDisabledVariantKeys = disabledVariantKeys.size > 0;
594
+ const hasAnyDisabled = hasDisabledVariantKeys || hasDisabledVariantValues;
595
+
596
+ const inputPropsKeys = ["class", "className", "style", ...variantKeys];
719
597
 
720
598
  const computedDefaultNames: string[] = [];
721
599
  const computedDefaultFns: ComputedDefaultVariantFn[] = [];
@@ -731,31 +609,9 @@ export function create({
731
609
  // Function entries in defaultVariants are computed defaults. They run in
732
610
  // the refine loop so they can react to setVariants updates.
733
611
  // Then filtered through disabled-variants.
734
- const staticDefaults: Record<string, unknown> = {};
735
- if (extend) {
736
- for (const ext of extend) {
737
- const meta = getComponentMeta(ext);
738
- if (meta) {
739
- Object.assign(staticDefaults, meta.staticDefaults);
740
- }
741
- }
742
- }
743
- if (variants) {
744
- for (const name in variants) {
745
- if (!Object.hasOwn(variants, name)) continue;
746
- const variantDef = (variants as Record<string, unknown>)[name];
747
- if (!isRecordObject(variantDef)) continue;
748
- if (
749
- Object.hasOwn(variantDef, "false") &&
750
- staticDefaults[name] === undefined
751
- ) {
752
- staticDefaults[name] = false;
753
- }
754
- }
755
- }
756
612
  if (defaultVariants) {
757
613
  for (const name in defaultVariants) {
758
- if (!Object.hasOwn(defaultVariants, name)) continue;
614
+ if (!hasOwn(defaultVariants, name)) continue;
759
615
  const value = defaultVariants[name];
760
616
  if (typeof value === "function") {
761
617
  computedDefaultNames.push(name);
@@ -763,7 +619,7 @@ export function create({
763
619
  continue;
764
620
  }
765
621
  if (value === undefined) {
766
- Reflect.deleteProperty(staticDefaults, name);
622
+ delete staticDefaults[name];
767
623
  continue;
768
624
  }
769
625
  staticDefaults[name] = value;
@@ -773,7 +629,7 @@ export function create({
773
629
  if (hasAnyDisabled) {
774
630
  // Filter disabled variants in-place
775
631
  for (const key in staticDefaults) {
776
- if (!Object.hasOwn(staticDefaults, key)) continue;
632
+ if (!hasOwn(staticDefaults, key)) continue;
777
633
  if (disabledVariantKeys.has(key)) {
778
634
  delete staticDefaults[key];
779
635
  continue;
@@ -788,37 +644,6 @@ export function create({
788
644
  }
789
645
  }
790
646
 
791
- // Pre-build extended component info, so we don't have to call
792
- // `getComponentMeta` per render. Extends from a different `create()`
793
- // factory (different `transformClass` identity) need their contribution
794
- // transformed by their own `transformClass` before being joined into our
795
- // class string — otherwise our outer `transformClass(clsx(allClasses))`
796
- // would be the only transform that runs, and the extend's factory would
797
- // be silently bypassed for any base coming from `extend: [otherFactoryCv]`.
798
- const extMetas: ComponentMeta[] = [];
799
- const extBaseClassesArr: string[] = [];
800
- const extIsolated: boolean[] = [];
801
- let hasIsolatedExt = false;
802
- if (hasExtend) {
803
- for (const ext of extend) {
804
- const meta = getComponentMeta(ext);
805
- if (!meta) continue;
806
- extMetas.push(meta);
807
- const isolated = meta.transformClass !== transformClass;
808
- extIsolated.push(isolated);
809
- if (isolated) {
810
- hasIsolatedExt = true;
811
- // Apply the extend's own transformClass to its base class so it
812
- // survives our outer transform (which still applies on top, matching
813
- // the original public-component round-trip behavior).
814
- extBaseClassesArr.push(meta.transformClass(meta.baseClass));
815
- } else {
816
- extBaseClassesArr.push(meta.baseClass);
817
- }
818
- }
819
- }
820
- const extCount = extMetas.length;
821
-
822
647
  const inheritedComputedDefaultKeys = new Set<string>();
823
648
  for (let i = 0; i < extCount; i++) {
824
649
  const keys = extMetas[i].computedDefaultKeys;
@@ -838,7 +663,6 @@ export function create({
838
663
  }
839
664
  }
840
665
  const extMetasWithRefineCount = extMetasWithRefine.length;
841
- const shouldCollectChangedVariants = extMetasWithRefineCount > 0;
842
666
 
843
667
  // Call-site frame captured at the `cv()` call site so refine-limit warnings
844
668
  // can point developers at the component definition. Skipped entirely for
@@ -849,9 +673,10 @@ export function create({
849
673
  // unless the warning actually fires.
850
674
  const canTriggerRefineWarning =
851
675
  !!refine || computedDefaultCount > 0 || extMetasWithRefineCount > 0;
852
- const creationFrame = canTriggerRefineWarning
853
- ? captureCreationFrame(cv)
854
- : undefined;
676
+ const creationFrame =
677
+ canTriggerRefineWarning && process.env.NODE_ENV !== "production"
678
+ ? captureCreationFrame(cv)
679
+ : undefined;
855
680
 
856
681
  // Function variant keys inherited from extends, filtered through this
857
682
  // component's own variants: a static (object/shorthand) variant in this
@@ -882,34 +707,11 @@ export function create({
882
707
  computedDefaultKeys.add(computedDefaultNames[i]);
883
708
  }
884
709
 
885
- // Static-variant keys in this component that override an inherited
886
- // function variant. Type-level merge says child fully replaces, so the
887
- // ancestor's function must not run with the child's (object-typed) value.
888
- let staticVariantsOverridingExtFn: string[] | null = null;
889
- if (variantEntryCount > 0 && extCount > 0) {
890
- for (let i = 0; i < variantEntryCount; i++) {
891
- const name = variantEntryNames[i];
892
- for (let j = 0; j < extCount; j++) {
893
- if (extMetas[j].functionVariantKeys.has(name)) {
894
- if (!staticVariantsOverridingExtFn) {
895
- staticVariantsOverridingExtFn = [];
896
- }
897
- staticVariantsOverridingExtFn.push(name);
898
- break;
899
- }
900
- }
901
- }
902
- }
903
-
904
710
  // Pre-compute static skip key/value sets to pass to extends. These never
905
711
  // change across calls — when caller passes no skip sets, we reuse the same
906
712
  // object and avoid Set allocation.
907
713
  let staticExtSkipKeys: Set<string> | null = null;
908
- if (
909
- hasDisabledVariantKeys ||
910
- functionVariantCount > 0 ||
911
- staticVariantsOverridingExtFn !== null
912
- ) {
714
+ if (hasDisabledVariantKeys || functionVariantCount > 0) {
913
715
  staticExtSkipKeys = new Set<string>();
914
716
  for (const k of disabledVariantKeys) {
915
717
  staticExtSkipKeys.add(k);
@@ -917,9 +719,19 @@ export function create({
917
719
  for (let i = 0; i < functionVariantCount; i++) {
918
720
  staticExtSkipKeys.add(functionVariantNames[i]);
919
721
  }
920
- if (staticVariantsOverridingExtFn) {
921
- for (const k of staticVariantsOverridingExtFn) {
922
- staticExtSkipKeys.add(k);
722
+ }
723
+
724
+ // A static variant in this component replaces an inherited function
725
+ // variant, so the ancestor must skip that key.
726
+ if (variantEntryCount > 0 && extCount > 0) {
727
+ for (let i = 0; i < variantEntryCount; i++) {
728
+ const name = variantEntryNames[i];
729
+ for (let j = 0; j < extCount; j++) {
730
+ if (extMetas[j].functionVariantKeys.has(name)) {
731
+ staticExtSkipKeys ??= new Set<string>();
732
+ staticExtSkipKeys.add(name);
733
+ break;
734
+ }
923
735
  }
924
736
  }
925
737
  }
@@ -928,23 +740,13 @@ export function create({
928
740
  const staticExtSkipValues: Record<string, Set<string>> | null =
929
741
  hasDisabledVariantValues ? disabledVariantValues : null;
930
742
 
931
- // Branches on `hasAnyDisabled` so the no-disabled path skips the per-key
932
- // filter checks entirely — most components have no disabled variants and
933
- // hit only the plain copy.
743
+ // Callers use this only when disabled keys or values exist.
934
744
  function filterDisabledInto(
935
745
  input: Record<string, unknown>,
936
746
  out: Record<string, unknown>,
937
747
  ): void {
938
- if (!hasAnyDisabled) {
939
- for (const key in input) {
940
- if (Object.hasOwn(input, key)) {
941
- out[key] = input[key];
942
- }
943
- }
944
- return;
945
- }
946
748
  for (const key in input) {
947
- if (!Object.hasOwn(input, key)) continue;
749
+ if (!hasOwn(input, key)) continue;
948
750
  if (disabledVariantKeys.has(key)) continue;
949
751
  const value = input[key];
950
752
  if (hasDisabledVariantValues) {
@@ -980,7 +782,7 @@ export function create({
980
782
 
981
783
  let hasOwnDisabledValue = false;
982
784
  for (const key in input) {
983
- if (!Object.hasOwn(input, key)) continue;
785
+ if (!hasOwn(input, key)) continue;
984
786
  const value = input[key];
985
787
  if (isOwnDisabledValue(key, value)) {
986
788
  hasOwnDisabledValue = true;
@@ -993,7 +795,7 @@ export function create({
993
795
 
994
796
  const filtered: Record<string, unknown> = {};
995
797
  for (const key in input) {
996
- if (!Object.hasOwn(input, key)) continue;
798
+ if (!hasOwn(input, key)) continue;
997
799
  const value = input[key];
998
800
  if (!isOwnDisabledValue(key, value)) {
999
801
  filtered[key] = value;
@@ -1017,14 +819,12 @@ export function create({
1017
819
  propsVariants: Record<string, unknown>,
1018
820
  ): Record<string, unknown> {
1019
821
  // Start with static defaults
1020
- const defaults: Record<string, unknown> = {};
1021
- Object.assign(defaults, staticDefaults);
1022
-
1023
- // Apply propsVariants on top (filter undefined). propsVariants is
1024
- // contractually variant-only here — callers building from a full props
1025
- // object filter to variant keys before calling.
1026
- for (const k in propsVariants) {
1027
- if (!Object.hasOwn(propsVariants, k)) continue;
822
+ const defaults = Object.assign({}, staticDefaults);
823
+
824
+ // Apply propsVariants on top (filter undefined).
825
+ for (let i = 0; i < variantKeysLength; i++) {
826
+ const k = variantKeys[i];
827
+ if (!hasOwn(propsVariants, k)) continue;
1028
828
  const v = propsVariants[k];
1029
829
  if (v === undefined) continue;
1030
830
  defaults[k] = v;
@@ -1046,14 +846,7 @@ export function create({
1046
846
  userVariantProps: Record<string, unknown>,
1047
847
  filterOwnVariants: boolean,
1048
848
  protectedVariantKeys: Set<string> | null | undefined,
1049
- ): {
1050
- workingResolved: Record<string, unknown>;
1051
- changedVariants: Record<string, unknown> | null;
1052
- } => {
1053
- if (computedDefaultCount === 0) {
1054
- return { workingResolved: resolved, changedVariants: null };
1055
- }
1056
-
849
+ ): Record<string, unknown> => {
1057
850
  let ownVariants = filterOwnVariants ? null : resolved;
1058
851
  const getOwnVariants = (): Record<string, unknown> => {
1059
852
  if (ownVariants) {
@@ -1062,7 +855,7 @@ export function create({
1062
855
  const filteredVariants: Record<string, unknown> = {};
1063
856
  for (let i = 0; i < variantKeysLength; i++) {
1064
857
  const key = variantKeys[i];
1065
- if (Object.hasOwn(resolved, key)) {
858
+ if (hasOwn(resolved, key)) {
1066
859
  filteredVariants[key] = resolved[key];
1067
860
  }
1068
861
  }
@@ -1071,20 +864,18 @@ export function create({
1071
864
  };
1072
865
 
1073
866
  let updatedVariants: Record<string, unknown> | null = null;
1074
- let changedVariants: Record<string, unknown> | null = null;
1075
867
  const ensureUpdated = (): Record<string, unknown> => {
1076
868
  if (updatedVariants) {
1077
869
  return updatedVariants;
1078
870
  }
1079
- const updated: Record<string, unknown> = {};
1080
- Object.assign(updated, resolved);
871
+ const updated = Object.assign({}, resolved);
1081
872
  updatedVariants = updated;
1082
873
  return updated;
1083
874
  };
1084
875
 
1085
876
  for (let i = 0; i < computedDefaultCount; i++) {
1086
877
  const key = computedDefaultNames[i];
1087
- if (Object.hasOwn(userVariantProps, key)) {
878
+ if (hasOwn(userVariantProps, key)) {
1088
879
  if (userVariantProps[key] !== undefined) continue;
1089
880
  }
1090
881
  if (protectedVariantKeys?.has(key)) continue;
@@ -1103,47 +894,32 @@ export function create({
1103
894
  }
1104
895
 
1105
896
  if (value === undefined) {
1106
- if (!Object.hasOwn(variantSnapshot, key)) continue;
1107
- if (shouldCollectChangedVariants) {
1108
- changedVariants ??= {};
1109
- changedVariants[key] = value;
1110
- }
1111
- Reflect.deleteProperty(ensureUpdated(), key);
897
+ if (!hasOwn(variantSnapshot, key)) continue;
898
+ delete ensureUpdated()[key];
1112
899
  continue;
1113
900
  }
1114
901
  if (Object.is(variantSnapshot[key], value)) continue;
1115
- if (shouldCollectChangedVariants) {
1116
- changedVariants ??= {};
1117
- changedVariants[key] = value;
1118
- }
1119
902
  ensureUpdated()[key] = value;
1120
903
  }
1121
904
 
1122
- return {
1123
- workingResolved: updatedVariants ?? resolved,
1124
- changedVariants,
1125
- };
905
+ return updatedVariants ?? resolved;
1126
906
  };
1127
907
 
1128
908
  const runRefineContext = (
1129
909
  resolved: Record<string, unknown>,
1130
- userVariantProps: Record<string, unknown>,
1131
910
  filterOwnVariants: boolean,
1132
911
  collectOutput: boolean,
1133
912
  applyVariantUpdates: boolean,
1134
913
  protectedVariants: Record<string, unknown> | null | undefined,
1135
- pendingProtectedVariants: Record<string, unknown> | null | undefined,
1136
914
  protectedVariantKeys: Set<string> | null | undefined,
1137
915
  ): {
1138
916
  workingResolved: Record<string, unknown>;
1139
- changedVariants: Record<string, unknown> | null;
1140
917
  classes: ClassValue[] | null;
1141
918
  style: StyleValue | null;
1142
919
  } => {
1143
920
  let workingResolved = resolved;
1144
921
  let cClasses: ClassValue[] | null = null;
1145
922
  let cStyle: StyleValue | null = null;
1146
- let changedVariants: Record<string, unknown> | null = null;
1147
923
 
1148
924
  if (refine) {
1149
925
  let ownVariants = resolved;
@@ -1155,7 +931,7 @@ export function create({
1155
931
  const filteredVariants: Record<string, unknown> = {};
1156
932
  for (let i = 0; i < variantKeysLength; i++) {
1157
933
  const k = variantKeys[i];
1158
- if (Object.hasOwn(resolved, k)) {
934
+ if (hasOwn(resolved, k)) {
1159
935
  filteredVariants[k] = resolved[k];
1160
936
  }
1161
937
  }
@@ -1170,30 +946,16 @@ export function create({
1170
946
  if (updatedVariants) {
1171
947
  return updatedVariants;
1172
948
  }
1173
- const u: Record<string, unknown> = {};
1174
- Object.assign(u, ownVariants);
949
+ const u = Object.assign({}, ownVariants);
1175
950
  updatedVariants = u;
1176
951
  return u;
1177
952
  };
1178
- const setChangedVariant = (
1179
- key: string,
1180
- value: unknown,
1181
- protect = false,
1182
- ) => {
1183
- if (shouldCollectChangedVariants) {
1184
- if (!changedVariants) {
1185
- changedVariants = {};
1186
- }
1187
- changedVariants[key] = value;
1188
- }
1189
- if (protect && protectedVariants) {
953
+ const protectVariant = (key: string, value: unknown) => {
954
+ if (protectedVariants) {
1190
955
  protectedVariants[key] = value;
1191
956
  protectedVariantKeys?.add(key);
1192
957
  }
1193
958
  };
1194
- const getCurrentVariantValue = (key: string) => {
1195
- return updatedVariants ? updatedVariants[key] : ownVariants[key];
1196
- };
1197
959
  const ctx = {
1198
960
  variants: ownVariants as VariantValues<Record<string, unknown>>,
1199
961
  setVariants: (
@@ -1204,16 +966,18 @@ export function create({
1204
966
  }
1205
967
  if (!hasAnyDisabled) {
1206
968
  for (const key in newVariants) {
1207
- if (!Object.hasOwn(newVariants, key)) continue;
969
+ if (!hasOwn(newVariants, key)) continue;
1208
970
  const value = (newVariants as Record<string, unknown>)[key];
1209
- setChangedVariant(key, value, true);
1210
- if (Object.is(getCurrentVariantValue(key), value)) continue;
971
+ protectVariant(key, value);
972
+ if (Object.is((updatedVariants ?? ownVariants)[key], value)) {
973
+ continue;
974
+ }
1211
975
  ensureUpdated()[key] = value;
1212
976
  }
1213
977
  return;
1214
978
  }
1215
979
  for (const key in newVariants) {
1216
- if (!Object.hasOwn(newVariants, key)) continue;
980
+ if (!hasOwn(newVariants, key)) continue;
1217
981
  if (disabledVariantKeys.has(key)) continue;
1218
982
  const value = (newVariants as Record<string, unknown>)[key];
1219
983
  if (hasDisabledVariantValues) {
@@ -1225,8 +989,10 @@ export function create({
1225
989
  continue;
1226
990
  }
1227
991
  }
1228
- setChangedVariant(key, value, true);
1229
- if (Object.is(getCurrentVariantValue(key), value)) continue;
992
+ protectVariant(key, value);
993
+ if (Object.is((updatedVariants ?? ownVariants)[key], value)) {
994
+ continue;
995
+ }
1230
996
  ensureUpdated()[key] = value;
1231
997
  }
1232
998
  },
@@ -1257,12 +1023,9 @@ export function create({
1257
1023
  cClasses = localCClasses;
1258
1024
  cStyle = localCStyle;
1259
1025
  if (updatedVariants) {
1260
- const nextResolved: Record<string, unknown> = {};
1261
- Object.assign(nextResolved, workingResolved);
1026
+ const nextResolved = Object.assign({}, workingResolved);
1262
1027
  if (hasAnyDisabled) {
1263
- const filteredUpdated: Record<string, unknown> = {};
1264
- filterDisabledInto(updatedVariants, filteredUpdated);
1265
- Object.assign(nextResolved, filteredUpdated);
1028
+ filterDisabledInto(updatedVariants, nextResolved);
1266
1029
  } else {
1267
1030
  Object.assign(nextResolved, updatedVariants);
1268
1031
  }
@@ -1272,7 +1035,6 @@ export function create({
1272
1035
 
1273
1036
  return {
1274
1037
  workingResolved,
1275
- changedVariants,
1276
1038
  classes: cClasses,
1277
1039
  style: cStyle,
1278
1040
  };
@@ -1282,16 +1044,14 @@ export function create({
1282
1044
  // `computeResult`) and recursively when this component is used as an
1283
1045
  // `extend` target by another component. Pushes variant classes (excluding
1284
1046
  // base class) into `classesOut` and merges styles into `styleOut`.
1285
- const computeOnce: ComputeFn = (
1047
+ const computeOnce: ComputeOnceFn = (
1286
1048
  resolved,
1287
1049
  userVariantProps,
1288
1050
  skipKeys,
1289
1051
  skipValues,
1290
1052
  classesOut,
1291
1053
  styleOut,
1292
- runState,
1293
1054
  protectedVariants,
1294
- pendingProtectedVariants,
1295
1055
  protectedVariantKeys,
1296
1056
  defaultResolved = resolved,
1297
1057
  renderOnly = false,
@@ -1299,7 +1059,6 @@ export function create({
1299
1059
  let workingResolved = resolved;
1300
1060
  let cClasses: ClassValue[] | null = null;
1301
1061
  let cStyle: StyleValue | null = null;
1302
- let changedVariants: Record<string, unknown> | null = null;
1303
1062
 
1304
1063
  // Run extends' contributions first (their full classes + styles) so our
1305
1064
  // own base style and variants apply on top, matching the original
@@ -1348,7 +1107,6 @@ export function create({
1348
1107
  ? getExtUserVariantProps(
1349
1108
  userVariantProps,
1350
1109
  protectedVariants ?? null,
1351
- changedVariants,
1352
1110
  )
1353
1111
  : userVariantProps;
1354
1112
  for (let i = 0; i < extCount; i++) {
@@ -1364,9 +1122,7 @@ export function create({
1364
1122
  extSkipVals,
1365
1123
  extClasses,
1366
1124
  styleOut,
1367
- runState,
1368
1125
  protectedVariants,
1369
- pendingProtectedVariants,
1370
1126
  protectedVariantKeys,
1371
1127
  defaultResolved,
1372
1128
  renderOnly,
@@ -1374,9 +1130,7 @@ export function create({
1374
1130
  if (extClasses.length > 0) {
1375
1131
  const joined = clsx(extClasses);
1376
1132
  if (joined.length > 0) {
1377
- classesOut.push(
1378
- extMetas[i].transformClass(joined) as ClsxClassValue,
1379
- );
1133
+ classesOut.push(extMetas[i].transformClass(joined));
1380
1134
  }
1381
1135
  }
1382
1136
  } else {
@@ -1387,9 +1141,7 @@ export function create({
1387
1141
  extSkipVals,
1388
1142
  classesOut,
1389
1143
  styleOut,
1390
- runState,
1391
1144
  protectedVariants,
1392
- pendingProtectedVariants,
1393
1145
  protectedVariantKeys,
1394
1146
  defaultResolved,
1395
1147
  renderOnly,
@@ -1410,15 +1162,13 @@ export function create({
1410
1162
  // Run own computed defaults after extended components so defaults resolve
1411
1163
  // from base to child. They still run before this component's `refine`.
1412
1164
  if (!renderOnly && computedDefaultCount > 0) {
1413
- const computedResult = runComputedDefaults(
1165
+ workingResolved = runComputedDefaults(
1414
1166
  workingResolved,
1415
1167
  defaultResolved,
1416
1168
  userVariantProps,
1417
1169
  true,
1418
1170
  protectedVariantKeys,
1419
1171
  );
1420
- workingResolved = computedResult.workingResolved;
1421
- changedVariants = computedResult.changedVariants;
1422
1172
  }
1423
1173
 
1424
1174
  // Run own `refine` (if any). May modify resolved variants and emit
@@ -1426,21 +1176,15 @@ export function create({
1426
1176
  if (refine) {
1427
1177
  const refineResult = runRefineContext(
1428
1178
  workingResolved,
1429
- userVariantProps,
1430
1179
  true,
1431
1180
  true,
1432
1181
  !renderOnly,
1433
1182
  protectedVariants,
1434
- pendingProtectedVariants,
1435
1183
  protectedVariantKeys,
1436
1184
  );
1437
1185
  workingResolved = refineResult.workingResolved;
1438
1186
  cClasses = refineResult.classes;
1439
1187
  cStyle = refineResult.style;
1440
- if (refineResult.changedVariants) {
1441
- changedVariants ??= {};
1442
- Object.assign(changedVariants, refineResult.changedVariants);
1443
- }
1444
1188
  }
1445
1189
 
1446
1190
  // Apply own base style (after extends' styles, matching original order).
@@ -1477,24 +1221,19 @@ export function create({
1477
1221
  continue;
1478
1222
  }
1479
1223
 
1224
+ let value: PrebuiltValue | null | undefined;
1480
1225
  if (variant.values) {
1481
1226
  if (selectedKey == null) continue;
1482
- const v = variant.values[selectedKey];
1483
- if (!v) continue;
1484
- if (v.class != null) {
1485
- classesOut.push(v.class as ClsxClassValue);
1486
- }
1487
- if (v.style) {
1488
- Object.assign(styleOut, v.style);
1489
- }
1490
- } else if (variant.shorthand && selectedValue === true) {
1491
- const v = variant.shorthand;
1492
- if (v.class != null) {
1493
- classesOut.push(v.class as ClsxClassValue);
1494
- }
1495
- if (v.style) {
1496
- Object.assign(styleOut, v.style);
1497
- }
1227
+ value = variant.values[selectedKey];
1228
+ } else if (selectedValue === true) {
1229
+ value = variant.shorthand;
1230
+ }
1231
+ if (!value) continue;
1232
+ if (value.class != null) {
1233
+ classesOut.push(value.class);
1234
+ }
1235
+ if (value.style) {
1236
+ Object.assign(styleOut, value.style);
1498
1237
  }
1499
1238
  }
1500
1239
 
@@ -1519,7 +1258,7 @@ export function create({
1519
1258
  if (computedResult == null) continue;
1520
1259
  const r = extractClassAndStylePrebuilt(computedResult);
1521
1260
  if (r.class != null) {
1522
- classesOut.push(r.class as ClsxClassValue);
1261
+ classesOut.push(r.class);
1523
1262
  }
1524
1263
  if (r.style) {
1525
1264
  Object.assign(styleOut, r.style);
@@ -1550,32 +1289,11 @@ export function create({
1550
1289
  skipValues,
1551
1290
  classesOut,
1552
1291
  styleOut,
1553
- runState,
1554
- protectedVariants,
1555
- pendingProtectedVariants,
1556
- protectedVariantKeys,
1557
- incomingDefaultResolved = resolved,
1558
- renderOnly = false,
1559
1292
  ) => {
1560
- if (renderOnly) {
1561
- return computeOnce(
1562
- resolved,
1563
- userVariantProps,
1564
- skipKeys,
1565
- skipValues,
1566
- classesOut,
1567
- styleOut,
1568
- runState,
1569
- protectedVariants,
1570
- pendingProtectedVariants,
1571
- protectedVariantKeys,
1572
- incomingDefaultResolved,
1573
- true,
1574
- );
1575
- }
1576
- runState ??= { remaining: MAX_REFINE_RUNS };
1577
- protectedVariants ??= {};
1578
- protectedVariantKeys ??= new Set<string>();
1293
+ let remaining = MAX_REFINE_RUNS;
1294
+ const protectedVariants: Record<string, unknown> = {};
1295
+ const protectedVariantKeys = new Set<string>();
1296
+ const incomingDefaultResolved = resolved;
1579
1297
  let workingResolved = resolved;
1580
1298
  // Latest variant changes from non-converging iterations inside the
1581
1299
  // tracking window. Lazy-init keeps convergent loops allocation-free.
@@ -1584,19 +1302,10 @@ export function create({
1584
1302
  let lastStyle: StyleValue = {};
1585
1303
  let isFirstRun = true;
1586
1304
 
1587
- while (runState.remaining > 0) {
1588
- runState.remaining -= 1;
1589
- let useDirectOutput = isFirstRun;
1590
- if (useDirectOutput) {
1591
- for (const key in styleOut) {
1592
- if (Object.hasOwn(styleOut, key)) {
1593
- useDirectOutput = false;
1594
- break;
1595
- }
1596
- }
1597
- }
1305
+ while (remaining > 0) {
1306
+ remaining -= 1;
1307
+ const useDirectOutput = isFirstRun;
1598
1308
  const classCount = classesOut.length;
1599
- const nextPendingProtectedVariants: Record<string, unknown> = {};
1600
1309
  const nextClasses: ClsxClassValue[] = useDirectOutput
1601
1310
  ? classesOut
1602
1311
  : [];
@@ -1604,6 +1313,7 @@ export function create({
1604
1313
  const defaultResolved = mergeProtectedIntoBase(
1605
1314
  incomingDefaultResolved,
1606
1315
  protectedVariants,
1316
+ protectedVariantKeys,
1607
1317
  );
1608
1318
  const nextResolved = computeOnce(
1609
1319
  workingResolved,
@@ -1612,38 +1322,20 @@ export function create({
1612
1322
  skipValues,
1613
1323
  nextClasses,
1614
1324
  nextStyle,
1615
- runState,
1616
1325
  protectedVariants,
1617
- nextPendingProtectedVariants,
1618
1326
  protectedVariantKeys,
1619
1327
  defaultResolved,
1620
1328
  );
1621
1329
 
1622
- let protectedChanged: boolean;
1623
- if (pendingProtectedVariants) {
1624
- protectedChanged = mergeVariants(
1625
- pendingProtectedVariants,
1626
- nextPendingProtectedVariants,
1627
- protectedVariantKeys,
1628
- );
1629
- } else {
1630
- protectedChanged = mergeVariants(
1631
- protectedVariants,
1632
- nextPendingProtectedVariants,
1633
- protectedVariantKeys,
1634
- );
1635
- }
1636
-
1637
1330
  if (
1638
- !protectedChanged &&
1639
- (nextResolved === workingResolved ||
1640
- areVariantsEqual(workingResolved, nextResolved))
1331
+ nextResolved === workingResolved ||
1332
+ areVariantsEqual(workingResolved, nextResolved)
1641
1333
  ) {
1642
1334
  if (nextResolved !== workingResolved) {
1643
1335
  if (useDirectOutput) {
1644
1336
  classesOut.length = classCount;
1645
1337
  for (const key in styleOut) {
1646
- if (Object.hasOwn(styleOut, key)) {
1338
+ if (hasOwn(styleOut, key)) {
1647
1339
  Reflect.deleteProperty(styleOut, key);
1648
1340
  }
1649
1341
  }
@@ -1655,9 +1347,7 @@ export function create({
1655
1347
  skipValues,
1656
1348
  classesOut,
1657
1349
  styleOut,
1658
- runState,
1659
1350
  protectedVariants,
1660
- null,
1661
1351
  protectedVariantKeys,
1662
1352
  defaultResolved,
1663
1353
  true,
@@ -1673,7 +1363,7 @@ export function create({
1673
1363
 
1674
1364
  if (
1675
1365
  process.env.NODE_ENV !== "production" &&
1676
- runState.remaining < REFINE_UNSTABLE_TRACKING_WINDOW
1366
+ remaining < REFINE_UNSTABLE_TRACKING_WINDOW
1677
1367
  ) {
1678
1368
  if (!unstableChanges) {
1679
1369
  unstableChanges = new Map<string, VariantChange>();
@@ -1685,21 +1375,10 @@ export function create({
1685
1375
  );
1686
1376
  }
1687
1377
 
1688
- if (useDirectOutput && runState.remaining === 0) {
1689
- // Keep the direct output from the last allowed run. Rolling
1690
- // back here would drop it before the fallback copy below.
1691
- warnRefineLimit({
1692
- runState,
1693
- creationFrame,
1694
- unstableChanges,
1695
- });
1696
- return nextResolved;
1697
- }
1698
-
1699
1378
  if (useDirectOutput) {
1700
1379
  classesOut.length = classCount;
1701
1380
  for (const key in styleOut) {
1702
- if (Object.hasOwn(styleOut, key)) {
1381
+ if (hasOwn(styleOut, key)) {
1703
1382
  Reflect.deleteProperty(styleOut, key);
1704
1383
  }
1705
1384
  }
@@ -1712,11 +1391,12 @@ export function create({
1712
1391
  isFirstRun = false;
1713
1392
  }
1714
1393
 
1715
- warnRefineLimit({
1716
- runState,
1717
- creationFrame,
1718
- unstableChanges,
1719
- });
1394
+ if (process.env.NODE_ENV !== "production") {
1395
+ warnRefineLimit({
1396
+ creationFrame,
1397
+ unstableChanges,
1398
+ });
1399
+ }
1720
1400
 
1721
1401
  for (let i = 0; i < lastClasses.length; i++) {
1722
1402
  classesOut.push(lastClasses[i]);
@@ -1725,24 +1405,20 @@ export function create({
1725
1405
  return workingResolved;
1726
1406
  };
1727
1407
 
1728
- const resolveRefineOnce: ResolveRefineFn = (
1408
+ const resolveRefineOnce: ResolveRefineOnceFn = (
1729
1409
  resolved,
1730
1410
  userVariantProps,
1731
1411
  filterOwnVariants = true,
1732
- runState,
1733
1412
  protectedVariants,
1734
- pendingProtectedVariants,
1735
1413
  protectedVariantKeys,
1736
1414
  defaultResolved = resolved,
1737
1415
  ) => {
1738
1416
  let workingResolved = resolved;
1739
- let changedVariants: Record<string, unknown> | null = null;
1740
1417
 
1741
1418
  if (extMetasWithRefineCount > 0) {
1742
1419
  const extUserVariantProps = getExtUserVariantProps(
1743
1420
  userVariantProps,
1744
1421
  protectedVariants ?? null,
1745
- changedVariants,
1746
1422
  );
1747
1423
  for (let i = 0; i < extMetasWithRefineCount; i++) {
1748
1424
  const meta = extMetasWithRefine[i];
@@ -1752,9 +1428,7 @@ export function create({
1752
1428
  workingResolved,
1753
1429
  extUserVariantProps,
1754
1430
  true,
1755
- runState,
1756
1431
  protectedVariants,
1757
- pendingProtectedVariants,
1758
1432
  protectedVariantKeys,
1759
1433
  defaultResolved,
1760
1434
  );
@@ -1769,32 +1443,24 @@ export function create({
1769
1443
  }
1770
1444
 
1771
1445
  if (computedDefaultCount > 0) {
1772
- const computedResult = runComputedDefaults(
1446
+ workingResolved = runComputedDefaults(
1773
1447
  workingResolved,
1774
1448
  defaultResolved,
1775
1449
  userVariantProps,
1776
1450
  filterOwnVariants,
1777
1451
  protectedVariantKeys,
1778
1452
  );
1779
- workingResolved = computedResult.workingResolved;
1780
- changedVariants = computedResult.changedVariants;
1781
1453
  }
1782
1454
  if (refine) {
1783
1455
  const refineResult = runRefineContext(
1784
1456
  workingResolved,
1785
- userVariantProps,
1786
1457
  filterOwnVariants,
1787
1458
  false,
1788
1459
  true,
1789
1460
  protectedVariants,
1790
- pendingProtectedVariants,
1791
1461
  protectedVariantKeys,
1792
1462
  );
1793
1463
  workingResolved = refineResult.workingResolved;
1794
- if (refineResult.changedVariants) {
1795
- changedVariants ??= {};
1796
- Object.assign(changedVariants, refineResult.changedVariants);
1797
- }
1798
1464
  }
1799
1465
 
1800
1466
  return workingResolved;
@@ -1802,71 +1468,43 @@ export function create({
1802
1468
 
1803
1469
  const resolveRefine: ResolveRefineFn | null =
1804
1470
  refine || computedDefaultCount > 0 || extMetasWithRefineCount > 0
1805
- ? (
1806
- resolved,
1807
- userVariantProps,
1808
- filterOwnVariants = true,
1809
- runState,
1810
- protectedVariants,
1811
- pendingProtectedVariants,
1812
- protectedVariantKeys,
1813
- incomingDefaultResolved = resolved,
1814
- ) => {
1815
- runState ??= { remaining: MAX_REFINE_RUNS };
1816
- protectedVariants ??= {};
1817
- protectedVariantKeys ??= new Set<string>();
1471
+ ? (resolved, userVariantProps, filterOwnVariants = true) => {
1472
+ let remaining = MAX_REFINE_RUNS;
1473
+ const protectedVariants: Record<string, unknown> = {};
1474
+ const protectedVariantKeys = new Set<string>();
1475
+ const incomingDefaultResolved = resolved;
1818
1476
  let workingResolved = resolved;
1819
1477
  // Latest variant changes from non-converging iterations inside the
1820
1478
  // tracking window. See the compute loop above for the shared
1821
1479
  // rationale.
1822
1480
  let unstableChanges: Map<string, VariantChange> | null = null;
1823
- let reachedLimit = true;
1824
1481
 
1825
- while (runState.remaining > 0) {
1826
- runState.remaining -= 1;
1827
- const nextPendingProtectedVariants: Record<string, unknown> = {};
1482
+ while (remaining > 0) {
1483
+ remaining -= 1;
1828
1484
  const defaultResolved = mergeProtectedIntoBase(
1829
1485
  incomingDefaultResolved,
1830
1486
  protectedVariants,
1487
+ protectedVariantKeys,
1831
1488
  );
1832
1489
  const nextResolved = resolveRefineOnce(
1833
1490
  workingResolved,
1834
1491
  userVariantProps,
1835
1492
  filterOwnVariants,
1836
- runState,
1837
1493
  protectedVariants,
1838
- nextPendingProtectedVariants,
1839
1494
  protectedVariantKeys,
1840
1495
  defaultResolved,
1841
1496
  );
1842
- let protectedChanged: boolean;
1843
- if (pendingProtectedVariants) {
1844
- protectedChanged = mergeVariants(
1845
- pendingProtectedVariants,
1846
- nextPendingProtectedVariants,
1847
- protectedVariantKeys,
1848
- );
1849
- } else {
1850
- protectedChanged = mergeVariants(
1851
- protectedVariants,
1852
- nextPendingProtectedVariants,
1853
- protectedVariantKeys,
1854
- );
1855
- }
1856
1497
 
1857
1498
  if (
1858
- !protectedChanged &&
1859
- (nextResolved === workingResolved ||
1860
- areVariantsEqual(workingResolved, nextResolved))
1499
+ nextResolved === workingResolved ||
1500
+ areVariantsEqual(workingResolved, nextResolved)
1861
1501
  ) {
1862
- workingResolved = nextResolved;
1863
- reachedLimit = false;
1864
- break;
1502
+ return nextResolved;
1865
1503
  }
1866
1504
 
1867
1505
  if (
1868
1506
  process.env.NODE_ENV !== "production" &&
1869
- runState.remaining < REFINE_UNSTABLE_TRACKING_WINDOW
1507
+ remaining < REFINE_UNSTABLE_TRACKING_WINDOW
1870
1508
  ) {
1871
1509
  if (!unstableChanges) {
1872
1510
  unstableChanges = new Map<string, VariantChange>();
@@ -1880,9 +1518,8 @@ export function create({
1880
1518
  workingResolved = nextResolved;
1881
1519
  }
1882
1520
 
1883
- if (reachedLimit) {
1521
+ if (process.env.NODE_ENV !== "production") {
1884
1522
  warnRefineLimit({
1885
- runState,
1886
1523
  creationFrame,
1887
1524
  unstableChanges,
1888
1525
  });
@@ -1907,24 +1544,24 @@ export function create({
1907
1544
  const variantProps: Record<string, unknown> = {};
1908
1545
  for (let i = 0; i < variantKeysLength; i++) {
1909
1546
  const key = variantKeys[i];
1910
- if (Object.hasOwn(propsRecord, key)) {
1547
+ if (hasOwn(propsRecord, key)) {
1911
1548
  variantProps[key] = propsRecord[key];
1912
1549
  }
1913
1550
  }
1914
1551
  for (const k in variantProps) {
1915
- if (!Object.hasOwn(variantProps, k)) continue;
1552
+ if (!hasOwn(variantProps, k)) continue;
1916
1553
  const v = variantProps[k];
1917
1554
  if (v === undefined) continue;
1918
1555
  resolved[k] = v;
1919
1556
  }
1920
1557
  userVariantProps = variantProps;
1921
1558
  } else {
1922
- // Fast path: walk variantKeys directly against propsRecord. Use
1923
- // Object.hasOwn so a polluted Object.prototype can't introduce variant
1559
+ // Fast path: walk variantKeys directly against propsRecord.
1560
+ // Own-property checks ensure a polluted Object.prototype can't add
1924
1561
  // values the user didn't pass.
1925
1562
  for (let i = 0; i < variantKeysLength; i++) {
1926
1563
  const key = variantKeys[i];
1927
- if (!Object.hasOwn(propsRecord, key)) continue;
1564
+ if (!hasOwn(propsRecord, key)) continue;
1928
1565
  const v = propsRecord[key];
1929
1566
  if (v === undefined) continue;
1930
1567
  resolved[key] = v;
@@ -1958,18 +1595,10 @@ export function create({
1958
1595
  if (psv != null) {
1959
1596
  if (typeof psv === "string") {
1960
1597
  if (psv.length > 0) {
1961
- Object.assign(style, htmlStyleToStyleValue(psv));
1598
+ htmlStyleToStyleValue(psv, style);
1962
1599
  }
1963
1600
  } else if (typeof psv === "object") {
1964
- // Don't allocate when empty.
1965
- let hasAnyKey = false;
1966
- for (const _ in psv) {
1967
- hasAnyKey = true;
1968
- break;
1969
- }
1970
- if (hasAnyKey) {
1971
- Object.assign(style, normalizeStyle(psv));
1972
- }
1601
+ htmlObjStyleToStyleValue(psv, style);
1973
1602
  }
1974
1603
  }
1975
1604
 
@@ -1980,7 +1609,19 @@ export function create({
1980
1609
  };
1981
1610
 
1982
1611
  const getVariants = (variants?: VariantValues<MergedVariants>) => {
1983
- const variantProps = variants ?? EMPTY_DEFAULTS;
1612
+ const variantsRecord = variants ?? EMPTY_DEFAULTS;
1613
+ let variantProps = variantsRecord;
1614
+ // Extended refinement copies user props, so filter before unknown
1615
+ // accessors or prototype keys can be observed by Object.assign.
1616
+ if (variants && extMetasWithRefineCount > 0) {
1617
+ variantProps = {};
1618
+ for (let i = 0; i < variantKeysLength; i++) {
1619
+ const key = variantKeys[i];
1620
+ if (hasOwn(variantsRecord, key)) {
1621
+ variantProps[key] = variantsRecord[key];
1622
+ }
1623
+ }
1624
+ }
1984
1625
  let resolvedVariants = resolveVariantsHot(variantProps);
1985
1626
  if (resolveRefine) {
1986
1627
  resolvedVariants = resolveRefine(resolvedVariants, variantProps, false);
@@ -1995,10 +1636,7 @@ export function create({
1995
1636
  // would compound (double for own-render, triple+ for extend chains) and
1996
1637
  // misbehave for non-idempotent transforms.
1997
1638
  const computedBaseClass = hasExtend
1998
- ? clsx(
1999
- ...(extBaseClassesArr as ClsxClassValue[]),
2000
- config.class as ClsxClassValue,
2001
- )
1639
+ ? clsx(extBaseClassesArr, config.class as ClsxClassValue)
2002
1640
  : clsx(config.class as ClsxClassValue);
2003
1641
 
2004
1642
  // Shared closures across the default and modal components.
@@ -2008,8 +1646,8 @@ export function create({
2008
1646
  const meta: ComponentMeta = {
2009
1647
  baseClass: computedBaseClass,
2010
1648
  staticDefaults,
2011
- compute,
2012
- resolveRefine,
1649
+ compute: computeOnce,
1650
+ resolveRefine: resolveRefine ? resolveRefineOnce : null,
2013
1651
  transformClass,
2014
1652
  functionVariantKeys,
2015
1653
  computedDefaultKeys,
@@ -2028,7 +1666,7 @@ export function create({
2028
1666
  c.getVariants = getVariants;
2029
1667
  c.variantKeys = variantKeys;
2030
1668
  c.propKeys = propKeys;
2031
- setComponentMeta(c, meta);
1669
+ (c as AnyComponent & ComponentWithMeta)[META_KEY] = meta;
2032
1670
  return c;
2033
1671
  };
2034
1672
 
@@ -2044,12 +1682,12 @@ export function create({
2044
1682
  // JSX component
2045
1683
  const jsxComponent = ((props: ComponentProps<MergedVariants> = {}) => {
2046
1684
  const { className, style } = computeResult(props);
2047
- return { className, style: styleValueToJSXStyle(style) };
1685
+ return { className, style };
2048
1686
  }) as ModalComponent<MergedVariants, JSXProps>;
2049
1687
  initComponent(
2050
1688
  jsxComponent,
2051
1689
  ["className", "style", ...variantKeys],
2052
- (props = {}) => styleValueToJSXStyle(computeResult(props).style),
1690
+ (props = {}) => computeResult(props).style,
2053
1691
  );
2054
1692
 
2055
1693
  // HTML component
@@ -2084,4 +1722,13 @@ export function create({
2084
1722
  return { cv, cx };
2085
1723
  }
2086
1724
 
2087
- export const { cv, cx } = create();
1725
+ export const cv = /* @__PURE__ */ (() => create().cv)();
1726
+
1727
+ /**
1728
+ * Joins class values without applying a class transform.
1729
+ *
1730
+ * This behaves like the `cx` function returned by `create()` with no options.
1731
+ */
1732
+ export function cx(...classes: ClsxClassValue[]) {
1733
+ return clsx(classes);
1734
+ }