clava 0.5.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,26 +67,30 @@ 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>;
71
80
 
72
- type ComputedDefaultVariantFn = (context: {
73
- defaultValue: unknown;
74
- variants: Readonly<Record<string, unknown>>;
75
- }) => unknown;
81
+ type ComputedDefaultVariantFn = (
82
+ defaultValue: unknown,
83
+ variants: Readonly<Record<string, unknown>>,
84
+ ) => unknown;
76
85
 
77
86
  // Internal metadata stored on components but hidden from public types.
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 {
@@ -218,12 +160,64 @@ export type {
218
160
  CVComponent,
219
161
  };
220
162
 
163
+ /**
164
+ * Extracts the variant props inferred for a Clava component. Use it to add a
165
+ * component's variant props to framework component props.
166
+ *
167
+ * @example
168
+ * ```ts
169
+ * import { type VariantProps, cv } from "clava";
170
+ * import type { ComponentProps } from "react";
171
+ *
172
+ * const button = cv({
173
+ * variants: {
174
+ * size: { sm: "button-sm", lg: "button-lg" },
175
+ * disabled: { true: "button-disabled", false: "" },
176
+ * },
177
+ * });
178
+ *
179
+ * interface ButtonProps
180
+ * extends ComponentProps<"button">,
181
+ * VariantProps<typeof button> {}
182
+ *
183
+ * const props: ButtonProps = {
184
+ * size: "lg",
185
+ * disabled: true,
186
+ * };
187
+ * ```
188
+ */
221
189
  export type VariantProps<T extends Pick<AnyComponent, "getVariants">> =
222
190
  ReturnType<T["getVariants"]>;
223
191
 
224
192
  // Variant props expose booleans, but variant object keys are always strings.
225
193
  type VariantKey<T> = T extends boolean ? "true" | "false" : Extract<T, string>;
226
194
 
195
+ /**
196
+ * Constrains a variant map to the same value keys as a variant on another
197
+ * component. Boolean variants are represented with `"true"` and `"false"`
198
+ * object keys.
199
+ *
200
+ * @example
201
+ * ```ts
202
+ * import { type Variant, cv } from "clava";
203
+ *
204
+ * const button = cv({
205
+ * variants: {
206
+ * size: { sm: "button-sm", lg: "button-lg" },
207
+ * },
208
+ * });
209
+ *
210
+ * const icon = cv({
211
+ * extend: [button],
212
+ * variants: {
213
+ * size: {
214
+ * sm: "icon-sm",
215
+ * lg: "icon-lg",
216
+ * } satisfies Variant<typeof button, "size">,
217
+ * },
218
+ * });
219
+ * ```
220
+ */
227
221
  export type Variant<
228
222
  T extends Pick<AnyComponent, "getVariants">,
229
223
  K extends keyof VariantProps<T>,
@@ -232,6 +226,32 @@ export type Variant<
232
226
  ClassValue | StyleClassValue
233
227
  >;
234
228
 
229
+ /**
230
+ * The configuration object accepted by `cv()`. It defines base class/style
231
+ * output, variants, default variants, component extensions, and refinement
232
+ * logic.
233
+ *
234
+ * @example
235
+ * ```ts
236
+ * import { type CVConfig, cv } from "clava";
237
+ *
238
+ * const config: CVConfig<{
239
+ * tone: { info: string; danger: string };
240
+ * }> = {
241
+ * variants: {
242
+ * tone: {
243
+ * info: "alert-info",
244
+ * danger: "alert-danger",
245
+ * },
246
+ * },
247
+ * defaultVariants: {
248
+ * tone: "info",
249
+ * },
250
+ * };
251
+ *
252
+ * const alert = cv(config);
253
+ * ```
254
+ */
235
255
  export interface CVConfig<
236
256
  V extends Variants = {},
237
257
  E extends AnyComponent[] = [],
@@ -248,11 +268,6 @@ interface CreateParams {
248
268
  transformClass?: (className: string) => string;
249
269
  }
250
270
 
251
- interface VariantConfigLike {
252
- extend?: AnyComponent[];
253
- variants?: Record<string, unknown>;
254
- }
255
-
256
271
  function isRecordObject(value: unknown): value is Record<string, unknown> {
257
272
  if (typeof value !== "object") return false;
258
273
  if (value == null) return false;
@@ -260,14 +275,6 @@ function isRecordObject(value: unknown): value is Record<string, unknown> {
260
275
  return true;
261
276
  }
262
277
 
263
- /**
264
- * Checks if a value is a style-class object (`{ style, class? }`).
265
- */
266
- function isStyleClassValue(value: unknown): value is StyleClassValue {
267
- if (!isRecordObject(value)) return false;
268
- return "style" in value || "class" in value;
269
- }
270
-
271
278
  /**
272
279
  * Converts any style input (string, JSX object, or HTML object) to a normalized
273
280
  * StyleValue.
@@ -277,10 +284,7 @@ function normalizeStyle(style: unknown): StyleValue {
277
284
  return htmlStyleToStyleValue(style);
278
285
  }
279
286
  if (typeof style === "object" && style != null) {
280
- if (isHTMLObjStyle(style as Record<string, unknown>)) {
281
- return htmlObjStyleToStyleValue(style as Record<string, string | number>);
282
- }
283
- return jsxStyleToStyleValue(style as Record<string, string | number>);
287
+ return htmlObjStyleToStyleValue(style);
284
288
  }
285
289
  return {};
286
290
  }
@@ -294,53 +298,19 @@ interface PrebuiltValue {
294
298
  style: StyleValue | null;
295
299
  }
296
300
 
297
- function extractStyleClassPrebuilt(value: StyleClassValue): PrebuiltValue {
298
- const styleNorm = normalizeStyle(value.style);
299
- return {
300
- class: value.class ?? null,
301
- style: styleNorm && Object.keys(styleNorm).length > 0 ? styleNorm : null,
302
- };
303
- }
304
-
305
301
  function extractClassAndStylePrebuilt(value: unknown): PrebuiltValue {
306
- if (isStyleClassValue(value)) {
307
- return extractStyleClassPrebuilt(value);
302
+ if (!isRecordObject(value)) {
303
+ return { class: value as ClassValue, style: null };
308
304
  }
309
- if (isRecordObject(value)) {
305
+ if (!("style" in value || "class" in value)) {
310
306
  return { class: null, style: null };
311
307
  }
312
- return { class: value as ClassValue, style: null };
313
- }
314
-
315
- /**
316
- * Gets all variant keys from a component's config, including extended
317
- * components.
318
- */
319
- function collectVariantKeys(config: VariantConfigLike): string[] {
320
- const keys = new Set<string>();
321
-
322
- if (config.extend) {
323
- for (const ext of config.extend) {
324
- const extKeys = ext.variantKeys as readonly string[];
325
- for (let i = 0; i < extKeys.length; i++) {
326
- keys.add(extKeys[i]);
327
- }
328
- }
329
- }
330
-
331
- if (config.variants) {
332
- for (const key in config.variants) {
333
- if (!Object.hasOwn(config.variants, key)) continue;
334
- const variant = config.variants[key];
335
- if (variant === null) {
336
- keys.delete(key);
337
- continue;
338
- }
339
- keys.add(key);
340
- }
341
- }
342
-
343
- 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
+ };
344
314
  }
345
315
 
346
316
  function getVariantValueKey(value: unknown): string | undefined {
@@ -356,88 +326,17 @@ function getVariantValueKey(value: unknown): string | undefined {
356
326
  return undefined;
357
327
  }
358
328
 
359
- function collectDisabledVariantKeys(config: VariantConfigLike): Set<string> {
360
- const keys = new Set<string>();
361
- if (!config.variants) {
362
- return keys;
363
- }
364
- for (const key in config.variants) {
365
- if (!Object.hasOwn(config.variants, key)) continue;
366
- if (config.variants[key] === null) {
367
- keys.add(key);
368
- }
369
- }
370
- return keys;
371
- }
329
+ const EMPTY_KEYS: readonly string[] = [];
372
330
 
373
- function collectDisabledVariantValues(
374
- config: VariantConfigLike,
375
- ): Record<string, Set<string>> {
376
- const values: Record<string, Set<string>> = {};
377
- if (!config.variants) {
378
- return values;
379
- }
380
- for (const key in config.variants) {
381
- if (!Object.hasOwn(config.variants, key)) continue;
382
- const variant = config.variants[key];
383
- if (!isRecordObject(variant)) continue;
384
- let bucket: Set<string> | undefined;
385
- for (const variantValue in variant) {
386
- if (!Object.hasOwn(variant, variantValue)) continue;
387
- if (variant[variantValue] !== null) continue;
388
- if (!bucket) {
389
- bucket = new Set<string>();
390
- values[key] = bucket;
391
- }
392
- bucket.add(variantValue);
393
- }
394
- }
395
- return values;
396
- }
397
-
398
- interface NormalizedSource {
399
- propKeys: string[];
400
- variantKeys: string[];
401
- isComponent: boolean;
402
- }
403
-
404
- const EMPTY_SOURCE: NormalizedSource = {
405
- propKeys: [],
406
- variantKeys: [],
407
- isComponent: false,
408
- };
409
-
410
- function normalizeKeySource(source: unknown): NormalizedSource {
411
- if (Array.isArray(source)) {
412
- return {
413
- propKeys: source as string[],
414
- variantKeys: source as string[],
415
- isComponent: false,
416
- };
417
- }
418
-
419
- if (!source) {
420
- return EMPTY_SOURCE;
421
- }
331
+ function isComponentKeySource(source: unknown): source is KeySourceComponent {
332
+ if (!source) return false;
422
333
  if (typeof source !== "object" && typeof source !== "function") {
423
- return EMPTY_SOURCE;
334
+ return false;
424
335
  }
425
336
  const typed = source as Record<string, unknown>;
426
- if (typeof typed.getVariants !== "function") {
427
- return EMPTY_SOURCE;
428
- }
429
- if (!Array.isArray(typed.propKeys)) {
430
- return EMPTY_SOURCE;
431
- }
432
- if (!Array.isArray(typed.variantKeys)) {
433
- return EMPTY_SOURCE;
434
- }
435
-
436
- return {
437
- propKeys: typed.propKeys as string[],
438
- variantKeys: typed.variantKeys as string[],
439
- isComponent: true,
440
- };
337
+ if (typeof typed.getVariants !== "function") return false;
338
+ if (!Array.isArray(typed.propKeys)) return false;
339
+ return Array.isArray(typed.variantKeys);
441
340
  }
442
341
 
443
342
  /**
@@ -447,7 +346,7 @@ function normalizeKeySource(source: unknown): NormalizedSource {
447
346
  * claim styling props.
448
347
  */
449
348
  function splitPropsImpl(
450
- selfKeys: string[],
349
+ selfKeys: readonly string[],
451
350
  selfIsComponent: boolean,
452
351
  props: Record<string, unknown>,
453
352
  sources: unknown[],
@@ -460,7 +359,7 @@ function splitPropsImpl(
460
359
  const selfKeysLength = selfKeys.length;
461
360
  for (let i = 0; i < selfKeysLength; i++) {
462
361
  const key = selfKeys[i];
463
- if (key !== undefined && key in props) {
362
+ if (key !== undefined && hasOwn(props, key)) {
464
363
  selfResult[key] = props[key];
465
364
  }
466
365
  }
@@ -468,28 +367,33 @@ function splitPropsImpl(
468
367
 
469
368
  // Track effective key arrays for the rest computation — for typical inputs
470
369
  // a linear scan beats building a Set up-front.
471
- const effectiveKeyArrays: string[][] = [selfKeys];
370
+ const effectiveKeyArrays: Array<readonly string[]> = [selfKeys];
472
371
 
473
372
  for (let s = 0; s < sourcesLength; s++) {
474
- const source = normalizeKeySource(sources[s]);
373
+ const source = sources[s];
475
374
  const sourceResult: Record<string, unknown> = {};
476
-
477
- const effectiveKeys =
478
- source.isComponent && stylingClaimed
479
- ? source.variantKeys
480
- : 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
+ }
481
385
 
482
386
  const effectiveKeysLength = effectiveKeys.length;
483
387
  for (let i = 0; i < effectiveKeysLength; i++) {
484
388
  const key = effectiveKeys[i];
485
- if (key !== undefined && key in props) {
389
+ if (key !== undefined && hasOwn(props, key)) {
486
390
  sourceResult[key] = props[key];
487
391
  }
488
392
  }
489
393
  results.push(sourceResult);
490
394
  effectiveKeyArrays.push(effectiveKeys);
491
395
 
492
- if (source.isComponent && !stylingClaimed) {
396
+ if (sourceIsComponent && !stylingClaimed) {
493
397
  stylingClaimed = true;
494
398
  }
495
399
  }
@@ -529,13 +433,13 @@ export const splitProps: SplitPropsFunction = ((
529
433
  source1: unknown,
530
434
  ...sources: unknown[]
531
435
  ) => {
532
- const normalizedSource1 = normalizeKeySource(source1);
533
- return splitPropsImpl(
534
- normalizedSource1.propKeys,
535
- normalizedSource1.isComponent,
536
- props,
537
- sources,
538
- );
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);
539
443
  }) as SplitPropsFunction;
540
444
 
541
445
  /**
@@ -564,7 +468,7 @@ function buildPrebuiltVariant(variantDef: unknown): PrebuiltVariant {
564
468
  const values: Record<string, PrebuiltValue> = {};
565
469
  let disabledValues: Set<string> | null = null;
566
470
  for (const key in variantDef) {
567
- if (!Object.hasOwn(variantDef, key)) continue;
471
+ if (!hasOwn(variantDef, key)) continue;
568
472
  const value = variantDef[key];
569
473
  if (value === null) {
570
474
  if (!disabledValues) {
@@ -588,7 +492,7 @@ function buildPrebuiltVariant(variantDef: unknown): PrebuiltVariant {
588
492
  export function create({
589
493
  transformClass = (className) => className,
590
494
  }: CreateParams = {}) {
591
- const cx = (...classes: ClsxClassValue[]) => transformClass(clsx(...classes));
495
+ const cx = (...classes: ClsxClassValue[]) => transformClass(clsx(classes));
592
496
 
593
497
  const cv = <V extends Variants = {}, const E extends AnyComponent[] = []>(
594
498
  config: CVConfig<V, E> = {},
@@ -596,17 +500,6 @@ export function create({
596
500
  type MergedVariants = MergeVariants<V, E>;
597
501
 
598
502
  // ----- Pre-computed at creation time -----
599
- const variantKeys = collectVariantKeys(config);
600
- const variantKeysLength = variantKeys.length;
601
- const disabledVariantKeys = collectDisabledVariantKeys(config);
602
- const disabledVariantValues = collectDisabledVariantValues(config);
603
- const hasDisabledVariantKeys = disabledVariantKeys.size > 0;
604
- const disabledVariantValueKeys = Object.keys(disabledVariantValues);
605
- const hasDisabledVariantValues = disabledVariantValueKeys.length > 0;
606
- const hasAnyDisabled = hasDisabledVariantKeys || hasDisabledVariantValues;
607
-
608
- const inputPropsKeys = ["class", "className", "style", ...variantKeys];
609
-
610
503
  const extend = config.extend;
611
504
  const hasExtend = !!extend && extend.length > 0;
612
505
  const variants = config.variants;
@@ -614,30 +507,93 @@ export function create({
614
507
  const baseStyle = config.style;
615
508
  const hasBaseStyle = !!baseStyle;
616
509
 
617
- // Split `variants` entries into static entries (object/shorthand) and
618
- // function-variant entries. Static entries are pre-built into
619
- // PrebuiltVariant for fast iteration. Function-variant entries override
620
- // 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;
621
554
  const variantEntryNames: string[] = [];
622
555
  const variantEntryDefs: PrebuiltVariant[] = [];
623
556
  const functionVariantNames: string[] = [];
624
557
  const functionVariantFns: Array<(value: unknown) => unknown> = [];
625
558
  if (variants) {
626
559
  for (const name in variants) {
627
- if (!Object.hasOwn(variants, name)) continue;
560
+ if (!hasOwn(variants, name)) continue;
628
561
  const variant = (variants as Record<string, unknown>)[name];
629
- if (variant === null) continue;
562
+ if (variant === null) {
563
+ variantKeySet.delete(name);
564
+ disabledVariantKeys.add(name);
565
+ continue;
566
+ }
567
+ variantKeySet.add(name);
630
568
  if (typeof variant === "function") {
631
569
  functionVariantNames.push(name);
632
570
  functionVariantFns.push(variant as (value: unknown) => unknown);
633
571
  continue;
634
572
  }
573
+ const prebuiltVariant = buildPrebuiltVariant(variant);
635
574
  variantEntryNames.push(name);
636
- 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
+ }
637
587
  }
638
588
  }
589
+ const variantKeys = Array.from(variantKeySet);
590
+ const variantKeysLength = variantKeys.length;
639
591
  const variantEntryCount = variantEntryNames.length;
640
592
  const functionVariantCount = functionVariantNames.length;
593
+ const hasDisabledVariantKeys = disabledVariantKeys.size > 0;
594
+ const hasAnyDisabled = hasDisabledVariantKeys || hasDisabledVariantValues;
595
+
596
+ const inputPropsKeys = ["class", "className", "style", ...variantKeys];
641
597
 
642
598
  const computedDefaultNames: string[] = [];
643
599
  const computedDefaultFns: ComputedDefaultVariantFn[] = [];
@@ -653,31 +609,9 @@ export function create({
653
609
  // Function entries in defaultVariants are computed defaults. They run in
654
610
  // the refine loop so they can react to setVariants updates.
655
611
  // Then filtered through disabled-variants.
656
- const staticDefaults: Record<string, unknown> = {};
657
- if (extend) {
658
- for (const ext of extend) {
659
- const meta = getComponentMeta(ext);
660
- if (meta) {
661
- Object.assign(staticDefaults, meta.staticDefaults);
662
- }
663
- }
664
- }
665
- if (variants) {
666
- for (const name in variants) {
667
- if (!Object.hasOwn(variants, name)) continue;
668
- const variantDef = (variants as Record<string, unknown>)[name];
669
- if (!isRecordObject(variantDef)) continue;
670
- if (
671
- Object.hasOwn(variantDef, "false") &&
672
- staticDefaults[name] === undefined
673
- ) {
674
- staticDefaults[name] = false;
675
- }
676
- }
677
- }
678
612
  if (defaultVariants) {
679
613
  for (const name in defaultVariants) {
680
- if (!Object.hasOwn(defaultVariants, name)) continue;
614
+ if (!hasOwn(defaultVariants, name)) continue;
681
615
  const value = defaultVariants[name];
682
616
  if (typeof value === "function") {
683
617
  computedDefaultNames.push(name);
@@ -685,7 +619,7 @@ export function create({
685
619
  continue;
686
620
  }
687
621
  if (value === undefined) {
688
- Reflect.deleteProperty(staticDefaults, name);
622
+ delete staticDefaults[name];
689
623
  continue;
690
624
  }
691
625
  staticDefaults[name] = value;
@@ -695,7 +629,7 @@ export function create({
695
629
  if (hasAnyDisabled) {
696
630
  // Filter disabled variants in-place
697
631
  for (const key in staticDefaults) {
698
- if (!Object.hasOwn(staticDefaults, key)) continue;
632
+ if (!hasOwn(staticDefaults, key)) continue;
699
633
  if (disabledVariantKeys.has(key)) {
700
634
  delete staticDefaults[key];
701
635
  continue;
@@ -710,37 +644,6 @@ export function create({
710
644
  }
711
645
  }
712
646
 
713
- // Pre-build extended component info, so we don't have to call
714
- // `getComponentMeta` per render. Extends from a different `create()`
715
- // factory (different `transformClass` identity) need their contribution
716
- // transformed by their own `transformClass` before being joined into our
717
- // class string — otherwise our outer `transformClass(clsx(allClasses))`
718
- // would be the only transform that runs, and the extend's factory would
719
- // be silently bypassed for any base coming from `extend: [otherFactoryCv]`.
720
- const extMetas: ComponentMeta[] = [];
721
- const extBaseClassesArr: string[] = [];
722
- const extIsolated: boolean[] = [];
723
- let hasIsolatedExt = false;
724
- if (hasExtend) {
725
- for (const ext of extend) {
726
- const meta = getComponentMeta(ext);
727
- if (!meta) continue;
728
- extMetas.push(meta);
729
- const isolated = meta.transformClass !== transformClass;
730
- extIsolated.push(isolated);
731
- if (isolated) {
732
- hasIsolatedExt = true;
733
- // Apply the extend's own transformClass to its base class so it
734
- // survives our outer transform (which still applies on top, matching
735
- // the original public-component round-trip behavior).
736
- extBaseClassesArr.push(meta.transformClass(meta.baseClass));
737
- } else {
738
- extBaseClassesArr.push(meta.baseClass);
739
- }
740
- }
741
- }
742
- const extCount = extMetas.length;
743
-
744
647
  const inheritedComputedDefaultKeys = new Set<string>();
745
648
  for (let i = 0; i < extCount; i++) {
746
649
  const keys = extMetas[i].computedDefaultKeys;
@@ -760,7 +663,6 @@ export function create({
760
663
  }
761
664
  }
762
665
  const extMetasWithRefineCount = extMetasWithRefine.length;
763
- const shouldCollectChangedVariants = extMetasWithRefineCount > 0;
764
666
 
765
667
  // Call-site frame captured at the `cv()` call site so refine-limit warnings
766
668
  // can point developers at the component definition. Skipped entirely for
@@ -771,9 +673,10 @@ export function create({
771
673
  // unless the warning actually fires.
772
674
  const canTriggerRefineWarning =
773
675
  !!refine || computedDefaultCount > 0 || extMetasWithRefineCount > 0;
774
- const creationFrame = canTriggerRefineWarning
775
- ? captureCreationFrame(cv)
776
- : undefined;
676
+ const creationFrame =
677
+ canTriggerRefineWarning && process.env.NODE_ENV !== "production"
678
+ ? captureCreationFrame(cv)
679
+ : undefined;
777
680
 
778
681
  // Function variant keys inherited from extends, filtered through this
779
682
  // component's own variants: a static (object/shorthand) variant in this
@@ -804,34 +707,11 @@ export function create({
804
707
  computedDefaultKeys.add(computedDefaultNames[i]);
805
708
  }
806
709
 
807
- // Static-variant keys in this component that override an inherited
808
- // function variant. Type-level merge says child fully replaces, so the
809
- // ancestor's function must not run with the child's (object-typed) value.
810
- let staticVariantsOverridingExtFn: string[] | null = null;
811
- if (variantEntryCount > 0 && extCount > 0) {
812
- for (let i = 0; i < variantEntryCount; i++) {
813
- const name = variantEntryNames[i];
814
- for (let j = 0; j < extCount; j++) {
815
- if (extMetas[j].functionVariantKeys.has(name)) {
816
- if (!staticVariantsOverridingExtFn) {
817
- staticVariantsOverridingExtFn = [];
818
- }
819
- staticVariantsOverridingExtFn.push(name);
820
- break;
821
- }
822
- }
823
- }
824
- }
825
-
826
710
  // Pre-compute static skip key/value sets to pass to extends. These never
827
711
  // change across calls — when caller passes no skip sets, we reuse the same
828
712
  // object and avoid Set allocation.
829
713
  let staticExtSkipKeys: Set<string> | null = null;
830
- if (
831
- hasDisabledVariantKeys ||
832
- functionVariantCount > 0 ||
833
- staticVariantsOverridingExtFn !== null
834
- ) {
714
+ if (hasDisabledVariantKeys || functionVariantCount > 0) {
835
715
  staticExtSkipKeys = new Set<string>();
836
716
  for (const k of disabledVariantKeys) {
837
717
  staticExtSkipKeys.add(k);
@@ -839,9 +719,19 @@ export function create({
839
719
  for (let i = 0; i < functionVariantCount; i++) {
840
720
  staticExtSkipKeys.add(functionVariantNames[i]);
841
721
  }
842
- if (staticVariantsOverridingExtFn) {
843
- for (const k of staticVariantsOverridingExtFn) {
844
- 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
+ }
845
735
  }
846
736
  }
847
737
  }
@@ -850,23 +740,13 @@ export function create({
850
740
  const staticExtSkipValues: Record<string, Set<string>> | null =
851
741
  hasDisabledVariantValues ? disabledVariantValues : null;
852
742
 
853
- // Branches on `hasAnyDisabled` so the no-disabled path skips the per-key
854
- // filter checks entirely — most components have no disabled variants and
855
- // hit only the plain copy.
743
+ // Callers use this only when disabled keys or values exist.
856
744
  function filterDisabledInto(
857
745
  input: Record<string, unknown>,
858
746
  out: Record<string, unknown>,
859
747
  ): void {
860
- if (!hasAnyDisabled) {
861
- for (const key in input) {
862
- if (Object.hasOwn(input, key)) {
863
- out[key] = input[key];
864
- }
865
- }
866
- return;
867
- }
868
748
  for (const key in input) {
869
- if (!Object.hasOwn(input, key)) continue;
749
+ if (!hasOwn(input, key)) continue;
870
750
  if (disabledVariantKeys.has(key)) continue;
871
751
  const value = input[key];
872
752
  if (hasDisabledVariantValues) {
@@ -902,7 +782,7 @@ export function create({
902
782
 
903
783
  let hasOwnDisabledValue = false;
904
784
  for (const key in input) {
905
- if (!Object.hasOwn(input, key)) continue;
785
+ if (!hasOwn(input, key)) continue;
906
786
  const value = input[key];
907
787
  if (isOwnDisabledValue(key, value)) {
908
788
  hasOwnDisabledValue = true;
@@ -915,7 +795,7 @@ export function create({
915
795
 
916
796
  const filtered: Record<string, unknown> = {};
917
797
  for (const key in input) {
918
- if (!Object.hasOwn(input, key)) continue;
798
+ if (!hasOwn(input, key)) continue;
919
799
  const value = input[key];
920
800
  if (!isOwnDisabledValue(key, value)) {
921
801
  filtered[key] = value;
@@ -939,14 +819,12 @@ export function create({
939
819
  propsVariants: Record<string, unknown>,
940
820
  ): Record<string, unknown> {
941
821
  // Start with static defaults
942
- const defaults: Record<string, unknown> = {};
943
- Object.assign(defaults, staticDefaults);
944
-
945
- // Apply propsVariants on top (filter undefined). propsVariants is
946
- // contractually variant-only here — callers building from a full props
947
- // object filter to variant keys before calling.
948
- for (const k in propsVariants) {
949
- 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;
950
828
  const v = propsVariants[k];
951
829
  if (v === undefined) continue;
952
830
  defaults[k] = v;
@@ -968,14 +846,7 @@ export function create({
968
846
  userVariantProps: Record<string, unknown>,
969
847
  filterOwnVariants: boolean,
970
848
  protectedVariantKeys: Set<string> | null | undefined,
971
- ): {
972
- workingResolved: Record<string, unknown>;
973
- changedVariants: Record<string, unknown> | null;
974
- } => {
975
- if (computedDefaultCount === 0) {
976
- return { workingResolved: resolved, changedVariants: null };
977
- }
978
-
849
+ ): Record<string, unknown> => {
979
850
  let ownVariants = filterOwnVariants ? null : resolved;
980
851
  const getOwnVariants = (): Record<string, unknown> => {
981
852
  if (ownVariants) {
@@ -984,7 +855,7 @@ export function create({
984
855
  const filteredVariants: Record<string, unknown> = {};
985
856
  for (let i = 0; i < variantKeysLength; i++) {
986
857
  const key = variantKeys[i];
987
- if (Object.hasOwn(resolved, key)) {
858
+ if (hasOwn(resolved, key)) {
988
859
  filteredVariants[key] = resolved[key];
989
860
  }
990
861
  }
@@ -993,20 +864,18 @@ export function create({
993
864
  };
994
865
 
995
866
  let updatedVariants: Record<string, unknown> | null = null;
996
- let changedVariants: Record<string, unknown> | null = null;
997
867
  const ensureUpdated = (): Record<string, unknown> => {
998
868
  if (updatedVariants) {
999
869
  return updatedVariants;
1000
870
  }
1001
- const updated: Record<string, unknown> = {};
1002
- Object.assign(updated, resolved);
871
+ const updated = Object.assign({}, resolved);
1003
872
  updatedVariants = updated;
1004
873
  return updated;
1005
874
  };
1006
875
 
1007
876
  for (let i = 0; i < computedDefaultCount; i++) {
1008
877
  const key = computedDefaultNames[i];
1009
- if (Object.hasOwn(userVariantProps, key)) {
878
+ if (hasOwn(userVariantProps, key)) {
1010
879
  if (userVariantProps[key] !== undefined) continue;
1011
880
  }
1012
881
  if (protectedVariantKeys?.has(key)) continue;
@@ -1015,10 +884,7 @@ export function create({
1015
884
  const defaultValue = inheritedComputedDefaultKeys.has(key)
1016
885
  ? variantSnapshot[key]
1017
886
  : defaultResolved[key];
1018
- const value = computedDefaultFns[i]({
1019
- defaultValue,
1020
- variants: variantSnapshot,
1021
- });
887
+ const value = computedDefaultFns[i](defaultValue, variantSnapshot);
1022
888
  if (hasAnyDisabled) {
1023
889
  if (disabledVariantKeys.has(key)) continue;
1024
890
  const valueKey = getVariantValueKey(value);
@@ -1028,47 +894,32 @@ export function create({
1028
894
  }
1029
895
 
1030
896
  if (value === undefined) {
1031
- if (!Object.hasOwn(variantSnapshot, key)) continue;
1032
- if (shouldCollectChangedVariants) {
1033
- changedVariants ??= {};
1034
- changedVariants[key] = value;
1035
- }
1036
- Reflect.deleteProperty(ensureUpdated(), key);
897
+ if (!hasOwn(variantSnapshot, key)) continue;
898
+ delete ensureUpdated()[key];
1037
899
  continue;
1038
900
  }
1039
901
  if (Object.is(variantSnapshot[key], value)) continue;
1040
- if (shouldCollectChangedVariants) {
1041
- changedVariants ??= {};
1042
- changedVariants[key] = value;
1043
- }
1044
902
  ensureUpdated()[key] = value;
1045
903
  }
1046
904
 
1047
- return {
1048
- workingResolved: updatedVariants ?? resolved,
1049
- changedVariants,
1050
- };
905
+ return updatedVariants ?? resolved;
1051
906
  };
1052
907
 
1053
908
  const runRefineContext = (
1054
909
  resolved: Record<string, unknown>,
1055
- userVariantProps: Record<string, unknown>,
1056
910
  filterOwnVariants: boolean,
1057
911
  collectOutput: boolean,
1058
912
  applyVariantUpdates: boolean,
1059
913
  protectedVariants: Record<string, unknown> | null | undefined,
1060
- pendingProtectedVariants: Record<string, unknown> | null | undefined,
1061
914
  protectedVariantKeys: Set<string> | null | undefined,
1062
915
  ): {
1063
916
  workingResolved: Record<string, unknown>;
1064
- changedVariants: Record<string, unknown> | null;
1065
917
  classes: ClassValue[] | null;
1066
918
  style: StyleValue | null;
1067
919
  } => {
1068
920
  let workingResolved = resolved;
1069
921
  let cClasses: ClassValue[] | null = null;
1070
922
  let cStyle: StyleValue | null = null;
1071
- let changedVariants: Record<string, unknown> | null = null;
1072
923
 
1073
924
  if (refine) {
1074
925
  let ownVariants = resolved;
@@ -1080,7 +931,7 @@ export function create({
1080
931
  const filteredVariants: Record<string, unknown> = {};
1081
932
  for (let i = 0; i < variantKeysLength; i++) {
1082
933
  const k = variantKeys[i];
1083
- if (Object.hasOwn(resolved, k)) {
934
+ if (hasOwn(resolved, k)) {
1084
935
  filteredVariants[k] = resolved[k];
1085
936
  }
1086
937
  }
@@ -1095,30 +946,16 @@ export function create({
1095
946
  if (updatedVariants) {
1096
947
  return updatedVariants;
1097
948
  }
1098
- const u: Record<string, unknown> = {};
1099
- Object.assign(u, ownVariants);
949
+ const u = Object.assign({}, ownVariants);
1100
950
  updatedVariants = u;
1101
951
  return u;
1102
952
  };
1103
- const setChangedVariant = (
1104
- key: string,
1105
- value: unknown,
1106
- protect = false,
1107
- ) => {
1108
- if (shouldCollectChangedVariants) {
1109
- if (!changedVariants) {
1110
- changedVariants = {};
1111
- }
1112
- changedVariants[key] = value;
1113
- }
1114
- if (protect && protectedVariants) {
953
+ const protectVariant = (key: string, value: unknown) => {
954
+ if (protectedVariants) {
1115
955
  protectedVariants[key] = value;
1116
956
  protectedVariantKeys?.add(key);
1117
957
  }
1118
958
  };
1119
- const getCurrentVariantValue = (key: string) => {
1120
- return updatedVariants ? updatedVariants[key] : ownVariants[key];
1121
- };
1122
959
  const ctx = {
1123
960
  variants: ownVariants as VariantValues<Record<string, unknown>>,
1124
961
  setVariants: (
@@ -1129,16 +966,18 @@ export function create({
1129
966
  }
1130
967
  if (!hasAnyDisabled) {
1131
968
  for (const key in newVariants) {
1132
- if (!Object.hasOwn(newVariants, key)) continue;
969
+ if (!hasOwn(newVariants, key)) continue;
1133
970
  const value = (newVariants as Record<string, unknown>)[key];
1134
- setChangedVariant(key, value, true);
1135
- if (Object.is(getCurrentVariantValue(key), value)) continue;
971
+ protectVariant(key, value);
972
+ if (Object.is((updatedVariants ?? ownVariants)[key], value)) {
973
+ continue;
974
+ }
1136
975
  ensureUpdated()[key] = value;
1137
976
  }
1138
977
  return;
1139
978
  }
1140
979
  for (const key in newVariants) {
1141
- if (!Object.hasOwn(newVariants, key)) continue;
980
+ if (!hasOwn(newVariants, key)) continue;
1142
981
  if (disabledVariantKeys.has(key)) continue;
1143
982
  const value = (newVariants as Record<string, unknown>)[key];
1144
983
  if (hasDisabledVariantValues) {
@@ -1150,8 +989,10 @@ export function create({
1150
989
  continue;
1151
990
  }
1152
991
  }
1153
- setChangedVariant(key, value, true);
1154
- if (Object.is(getCurrentVariantValue(key), value)) continue;
992
+ protectVariant(key, value);
993
+ if (Object.is((updatedVariants ?? ownVariants)[key], value)) {
994
+ continue;
995
+ }
1155
996
  ensureUpdated()[key] = value;
1156
997
  }
1157
998
  },
@@ -1182,12 +1023,9 @@ export function create({
1182
1023
  cClasses = localCClasses;
1183
1024
  cStyle = localCStyle;
1184
1025
  if (updatedVariants) {
1185
- const nextResolved: Record<string, unknown> = {};
1186
- Object.assign(nextResolved, workingResolved);
1026
+ const nextResolved = Object.assign({}, workingResolved);
1187
1027
  if (hasAnyDisabled) {
1188
- const filteredUpdated: Record<string, unknown> = {};
1189
- filterDisabledInto(updatedVariants, filteredUpdated);
1190
- Object.assign(nextResolved, filteredUpdated);
1028
+ filterDisabledInto(updatedVariants, nextResolved);
1191
1029
  } else {
1192
1030
  Object.assign(nextResolved, updatedVariants);
1193
1031
  }
@@ -1197,7 +1035,6 @@ export function create({
1197
1035
 
1198
1036
  return {
1199
1037
  workingResolved,
1200
- changedVariants,
1201
1038
  classes: cClasses,
1202
1039
  style: cStyle,
1203
1040
  };
@@ -1207,16 +1044,14 @@ export function create({
1207
1044
  // `computeResult`) and recursively when this component is used as an
1208
1045
  // `extend` target by another component. Pushes variant classes (excluding
1209
1046
  // base class) into `classesOut` and merges styles into `styleOut`.
1210
- const computeOnce: ComputeFn = (
1047
+ const computeOnce: ComputeOnceFn = (
1211
1048
  resolved,
1212
1049
  userVariantProps,
1213
1050
  skipKeys,
1214
1051
  skipValues,
1215
1052
  classesOut,
1216
1053
  styleOut,
1217
- runState,
1218
1054
  protectedVariants,
1219
- pendingProtectedVariants,
1220
1055
  protectedVariantKeys,
1221
1056
  defaultResolved = resolved,
1222
1057
  renderOnly = false,
@@ -1224,7 +1059,6 @@ export function create({
1224
1059
  let workingResolved = resolved;
1225
1060
  let cClasses: ClassValue[] | null = null;
1226
1061
  let cStyle: StyleValue | null = null;
1227
- let changedVariants: Record<string, unknown> | null = null;
1228
1062
 
1229
1063
  // Run extends' contributions first (their full classes + styles) so our
1230
1064
  // own base style and variants apply on top, matching the original
@@ -1273,7 +1107,6 @@ export function create({
1273
1107
  ? getExtUserVariantProps(
1274
1108
  userVariantProps,
1275
1109
  protectedVariants ?? null,
1276
- changedVariants,
1277
1110
  )
1278
1111
  : userVariantProps;
1279
1112
  for (let i = 0; i < extCount; i++) {
@@ -1289,9 +1122,7 @@ export function create({
1289
1122
  extSkipVals,
1290
1123
  extClasses,
1291
1124
  styleOut,
1292
- runState,
1293
1125
  protectedVariants,
1294
- pendingProtectedVariants,
1295
1126
  protectedVariantKeys,
1296
1127
  defaultResolved,
1297
1128
  renderOnly,
@@ -1299,9 +1130,7 @@ export function create({
1299
1130
  if (extClasses.length > 0) {
1300
1131
  const joined = clsx(extClasses);
1301
1132
  if (joined.length > 0) {
1302
- classesOut.push(
1303
- extMetas[i].transformClass(joined) as ClsxClassValue,
1304
- );
1133
+ classesOut.push(extMetas[i].transformClass(joined));
1305
1134
  }
1306
1135
  }
1307
1136
  } else {
@@ -1312,9 +1141,7 @@ export function create({
1312
1141
  extSkipVals,
1313
1142
  classesOut,
1314
1143
  styleOut,
1315
- runState,
1316
1144
  protectedVariants,
1317
- pendingProtectedVariants,
1318
1145
  protectedVariantKeys,
1319
1146
  defaultResolved,
1320
1147
  renderOnly,
@@ -1335,15 +1162,13 @@ export function create({
1335
1162
  // Run own computed defaults after extended components so defaults resolve
1336
1163
  // from base to child. They still run before this component's `refine`.
1337
1164
  if (!renderOnly && computedDefaultCount > 0) {
1338
- const computedResult = runComputedDefaults(
1165
+ workingResolved = runComputedDefaults(
1339
1166
  workingResolved,
1340
1167
  defaultResolved,
1341
1168
  userVariantProps,
1342
1169
  true,
1343
1170
  protectedVariantKeys,
1344
1171
  );
1345
- workingResolved = computedResult.workingResolved;
1346
- changedVariants = computedResult.changedVariants;
1347
1172
  }
1348
1173
 
1349
1174
  // Run own `refine` (if any). May modify resolved variants and emit
@@ -1351,21 +1176,15 @@ export function create({
1351
1176
  if (refine) {
1352
1177
  const refineResult = runRefineContext(
1353
1178
  workingResolved,
1354
- userVariantProps,
1355
1179
  true,
1356
1180
  true,
1357
1181
  !renderOnly,
1358
1182
  protectedVariants,
1359
- pendingProtectedVariants,
1360
1183
  protectedVariantKeys,
1361
1184
  );
1362
1185
  workingResolved = refineResult.workingResolved;
1363
1186
  cClasses = refineResult.classes;
1364
1187
  cStyle = refineResult.style;
1365
- if (refineResult.changedVariants) {
1366
- changedVariants ??= {};
1367
- Object.assign(changedVariants, refineResult.changedVariants);
1368
- }
1369
1188
  }
1370
1189
 
1371
1190
  // Apply own base style (after extends' styles, matching original order).
@@ -1402,24 +1221,19 @@ export function create({
1402
1221
  continue;
1403
1222
  }
1404
1223
 
1224
+ let value: PrebuiltValue | null | undefined;
1405
1225
  if (variant.values) {
1406
1226
  if (selectedKey == null) continue;
1407
- const v = variant.values[selectedKey];
1408
- if (!v) continue;
1409
- if (v.class != null) {
1410
- classesOut.push(v.class as ClsxClassValue);
1411
- }
1412
- if (v.style) {
1413
- Object.assign(styleOut, v.style);
1414
- }
1415
- } else if (variant.shorthand && selectedValue === true) {
1416
- const v = variant.shorthand;
1417
- if (v.class != null) {
1418
- classesOut.push(v.class as ClsxClassValue);
1419
- }
1420
- if (v.style) {
1421
- Object.assign(styleOut, v.style);
1422
- }
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);
1423
1237
  }
1424
1238
  }
1425
1239
 
@@ -1444,7 +1258,7 @@ export function create({
1444
1258
  if (computedResult == null) continue;
1445
1259
  const r = extractClassAndStylePrebuilt(computedResult);
1446
1260
  if (r.class != null) {
1447
- classesOut.push(r.class as ClsxClassValue);
1261
+ classesOut.push(r.class);
1448
1262
  }
1449
1263
  if (r.style) {
1450
1264
  Object.assign(styleOut, r.style);
@@ -1475,32 +1289,11 @@ export function create({
1475
1289
  skipValues,
1476
1290
  classesOut,
1477
1291
  styleOut,
1478
- runState,
1479
- protectedVariants,
1480
- pendingProtectedVariants,
1481
- protectedVariantKeys,
1482
- incomingDefaultResolved = resolved,
1483
- renderOnly = false,
1484
1292
  ) => {
1485
- if (renderOnly) {
1486
- return computeOnce(
1487
- resolved,
1488
- userVariantProps,
1489
- skipKeys,
1490
- skipValues,
1491
- classesOut,
1492
- styleOut,
1493
- runState,
1494
- protectedVariants,
1495
- pendingProtectedVariants,
1496
- protectedVariantKeys,
1497
- incomingDefaultResolved,
1498
- true,
1499
- );
1500
- }
1501
- runState ??= { remaining: MAX_REFINE_RUNS };
1502
- protectedVariants ??= {};
1503
- 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;
1504
1297
  let workingResolved = resolved;
1505
1298
  // Latest variant changes from non-converging iterations inside the
1506
1299
  // tracking window. Lazy-init keeps convergent loops allocation-free.
@@ -1509,19 +1302,10 @@ export function create({
1509
1302
  let lastStyle: StyleValue = {};
1510
1303
  let isFirstRun = true;
1511
1304
 
1512
- while (runState.remaining > 0) {
1513
- runState.remaining -= 1;
1514
- let useDirectOutput = isFirstRun;
1515
- if (useDirectOutput) {
1516
- for (const key in styleOut) {
1517
- if (Object.hasOwn(styleOut, key)) {
1518
- useDirectOutput = false;
1519
- break;
1520
- }
1521
- }
1522
- }
1305
+ while (remaining > 0) {
1306
+ remaining -= 1;
1307
+ const useDirectOutput = isFirstRun;
1523
1308
  const classCount = classesOut.length;
1524
- const nextPendingProtectedVariants: Record<string, unknown> = {};
1525
1309
  const nextClasses: ClsxClassValue[] = useDirectOutput
1526
1310
  ? classesOut
1527
1311
  : [];
@@ -1529,6 +1313,7 @@ export function create({
1529
1313
  const defaultResolved = mergeProtectedIntoBase(
1530
1314
  incomingDefaultResolved,
1531
1315
  protectedVariants,
1316
+ protectedVariantKeys,
1532
1317
  );
1533
1318
  const nextResolved = computeOnce(
1534
1319
  workingResolved,
@@ -1537,38 +1322,20 @@ export function create({
1537
1322
  skipValues,
1538
1323
  nextClasses,
1539
1324
  nextStyle,
1540
- runState,
1541
1325
  protectedVariants,
1542
- nextPendingProtectedVariants,
1543
1326
  protectedVariantKeys,
1544
1327
  defaultResolved,
1545
1328
  );
1546
1329
 
1547
- let protectedChanged: boolean;
1548
- if (pendingProtectedVariants) {
1549
- protectedChanged = mergeVariants(
1550
- pendingProtectedVariants,
1551
- nextPendingProtectedVariants,
1552
- protectedVariantKeys,
1553
- );
1554
- } else {
1555
- protectedChanged = mergeVariants(
1556
- protectedVariants,
1557
- nextPendingProtectedVariants,
1558
- protectedVariantKeys,
1559
- );
1560
- }
1561
-
1562
1330
  if (
1563
- !protectedChanged &&
1564
- (nextResolved === workingResolved ||
1565
- areVariantsEqual(workingResolved, nextResolved))
1331
+ nextResolved === workingResolved ||
1332
+ areVariantsEqual(workingResolved, nextResolved)
1566
1333
  ) {
1567
1334
  if (nextResolved !== workingResolved) {
1568
1335
  if (useDirectOutput) {
1569
1336
  classesOut.length = classCount;
1570
1337
  for (const key in styleOut) {
1571
- if (Object.hasOwn(styleOut, key)) {
1338
+ if (hasOwn(styleOut, key)) {
1572
1339
  Reflect.deleteProperty(styleOut, key);
1573
1340
  }
1574
1341
  }
@@ -1580,9 +1347,7 @@ export function create({
1580
1347
  skipValues,
1581
1348
  classesOut,
1582
1349
  styleOut,
1583
- runState,
1584
1350
  protectedVariants,
1585
- null,
1586
1351
  protectedVariantKeys,
1587
1352
  defaultResolved,
1588
1353
  true,
@@ -1598,7 +1363,7 @@ export function create({
1598
1363
 
1599
1364
  if (
1600
1365
  process.env.NODE_ENV !== "production" &&
1601
- runState.remaining < REFINE_UNSTABLE_TRACKING_WINDOW
1366
+ remaining < REFINE_UNSTABLE_TRACKING_WINDOW
1602
1367
  ) {
1603
1368
  if (!unstableChanges) {
1604
1369
  unstableChanges = new Map<string, VariantChange>();
@@ -1610,21 +1375,10 @@ export function create({
1610
1375
  );
1611
1376
  }
1612
1377
 
1613
- if (useDirectOutput && runState.remaining === 0) {
1614
- // Keep the direct output from the last allowed run. Rolling
1615
- // back here would drop it before the fallback copy below.
1616
- warnRefineLimit({
1617
- runState,
1618
- creationFrame,
1619
- unstableChanges,
1620
- });
1621
- return nextResolved;
1622
- }
1623
-
1624
1378
  if (useDirectOutput) {
1625
1379
  classesOut.length = classCount;
1626
1380
  for (const key in styleOut) {
1627
- if (Object.hasOwn(styleOut, key)) {
1381
+ if (hasOwn(styleOut, key)) {
1628
1382
  Reflect.deleteProperty(styleOut, key);
1629
1383
  }
1630
1384
  }
@@ -1637,11 +1391,12 @@ export function create({
1637
1391
  isFirstRun = false;
1638
1392
  }
1639
1393
 
1640
- warnRefineLimit({
1641
- runState,
1642
- creationFrame,
1643
- unstableChanges,
1644
- });
1394
+ if (process.env.NODE_ENV !== "production") {
1395
+ warnRefineLimit({
1396
+ creationFrame,
1397
+ unstableChanges,
1398
+ });
1399
+ }
1645
1400
 
1646
1401
  for (let i = 0; i < lastClasses.length; i++) {
1647
1402
  classesOut.push(lastClasses[i]);
@@ -1650,24 +1405,20 @@ export function create({
1650
1405
  return workingResolved;
1651
1406
  };
1652
1407
 
1653
- const resolveRefineOnce: ResolveRefineFn = (
1408
+ const resolveRefineOnce: ResolveRefineOnceFn = (
1654
1409
  resolved,
1655
1410
  userVariantProps,
1656
1411
  filterOwnVariants = true,
1657
- runState,
1658
1412
  protectedVariants,
1659
- pendingProtectedVariants,
1660
1413
  protectedVariantKeys,
1661
1414
  defaultResolved = resolved,
1662
1415
  ) => {
1663
1416
  let workingResolved = resolved;
1664
- let changedVariants: Record<string, unknown> | null = null;
1665
1417
 
1666
1418
  if (extMetasWithRefineCount > 0) {
1667
1419
  const extUserVariantProps = getExtUserVariantProps(
1668
1420
  userVariantProps,
1669
1421
  protectedVariants ?? null,
1670
- changedVariants,
1671
1422
  );
1672
1423
  for (let i = 0; i < extMetasWithRefineCount; i++) {
1673
1424
  const meta = extMetasWithRefine[i];
@@ -1677,9 +1428,7 @@ export function create({
1677
1428
  workingResolved,
1678
1429
  extUserVariantProps,
1679
1430
  true,
1680
- runState,
1681
1431
  protectedVariants,
1682
- pendingProtectedVariants,
1683
1432
  protectedVariantKeys,
1684
1433
  defaultResolved,
1685
1434
  );
@@ -1694,32 +1443,24 @@ export function create({
1694
1443
  }
1695
1444
 
1696
1445
  if (computedDefaultCount > 0) {
1697
- const computedResult = runComputedDefaults(
1446
+ workingResolved = runComputedDefaults(
1698
1447
  workingResolved,
1699
1448
  defaultResolved,
1700
1449
  userVariantProps,
1701
1450
  filterOwnVariants,
1702
1451
  protectedVariantKeys,
1703
1452
  );
1704
- workingResolved = computedResult.workingResolved;
1705
- changedVariants = computedResult.changedVariants;
1706
1453
  }
1707
1454
  if (refine) {
1708
1455
  const refineResult = runRefineContext(
1709
1456
  workingResolved,
1710
- userVariantProps,
1711
1457
  filterOwnVariants,
1712
1458
  false,
1713
1459
  true,
1714
1460
  protectedVariants,
1715
- pendingProtectedVariants,
1716
1461
  protectedVariantKeys,
1717
1462
  );
1718
1463
  workingResolved = refineResult.workingResolved;
1719
- if (refineResult.changedVariants) {
1720
- changedVariants ??= {};
1721
- Object.assign(changedVariants, refineResult.changedVariants);
1722
- }
1723
1464
  }
1724
1465
 
1725
1466
  return workingResolved;
@@ -1727,71 +1468,43 @@ export function create({
1727
1468
 
1728
1469
  const resolveRefine: ResolveRefineFn | null =
1729
1470
  refine || computedDefaultCount > 0 || extMetasWithRefineCount > 0
1730
- ? (
1731
- resolved,
1732
- userVariantProps,
1733
- filterOwnVariants = true,
1734
- runState,
1735
- protectedVariants,
1736
- pendingProtectedVariants,
1737
- protectedVariantKeys,
1738
- incomingDefaultResolved = resolved,
1739
- ) => {
1740
- runState ??= { remaining: MAX_REFINE_RUNS };
1741
- protectedVariants ??= {};
1742
- 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;
1743
1476
  let workingResolved = resolved;
1744
1477
  // Latest variant changes from non-converging iterations inside the
1745
1478
  // tracking window. See the compute loop above for the shared
1746
1479
  // rationale.
1747
1480
  let unstableChanges: Map<string, VariantChange> | null = null;
1748
- let reachedLimit = true;
1749
1481
 
1750
- while (runState.remaining > 0) {
1751
- runState.remaining -= 1;
1752
- const nextPendingProtectedVariants: Record<string, unknown> = {};
1482
+ while (remaining > 0) {
1483
+ remaining -= 1;
1753
1484
  const defaultResolved = mergeProtectedIntoBase(
1754
1485
  incomingDefaultResolved,
1755
1486
  protectedVariants,
1487
+ protectedVariantKeys,
1756
1488
  );
1757
1489
  const nextResolved = resolveRefineOnce(
1758
1490
  workingResolved,
1759
1491
  userVariantProps,
1760
1492
  filterOwnVariants,
1761
- runState,
1762
1493
  protectedVariants,
1763
- nextPendingProtectedVariants,
1764
1494
  protectedVariantKeys,
1765
1495
  defaultResolved,
1766
1496
  );
1767
- let protectedChanged: boolean;
1768
- if (pendingProtectedVariants) {
1769
- protectedChanged = mergeVariants(
1770
- pendingProtectedVariants,
1771
- nextPendingProtectedVariants,
1772
- protectedVariantKeys,
1773
- );
1774
- } else {
1775
- protectedChanged = mergeVariants(
1776
- protectedVariants,
1777
- nextPendingProtectedVariants,
1778
- protectedVariantKeys,
1779
- );
1780
- }
1781
1497
 
1782
1498
  if (
1783
- !protectedChanged &&
1784
- (nextResolved === workingResolved ||
1785
- areVariantsEqual(workingResolved, nextResolved))
1499
+ nextResolved === workingResolved ||
1500
+ areVariantsEqual(workingResolved, nextResolved)
1786
1501
  ) {
1787
- workingResolved = nextResolved;
1788
- reachedLimit = false;
1789
- break;
1502
+ return nextResolved;
1790
1503
  }
1791
1504
 
1792
1505
  if (
1793
1506
  process.env.NODE_ENV !== "production" &&
1794
- runState.remaining < REFINE_UNSTABLE_TRACKING_WINDOW
1507
+ remaining < REFINE_UNSTABLE_TRACKING_WINDOW
1795
1508
  ) {
1796
1509
  if (!unstableChanges) {
1797
1510
  unstableChanges = new Map<string, VariantChange>();
@@ -1805,9 +1518,8 @@ export function create({
1805
1518
  workingResolved = nextResolved;
1806
1519
  }
1807
1520
 
1808
- if (reachedLimit) {
1521
+ if (process.env.NODE_ENV !== "production") {
1809
1522
  warnRefineLimit({
1810
- runState,
1811
1523
  creationFrame,
1812
1524
  unstableChanges,
1813
1525
  });
@@ -1832,24 +1544,24 @@ export function create({
1832
1544
  const variantProps: Record<string, unknown> = {};
1833
1545
  for (let i = 0; i < variantKeysLength; i++) {
1834
1546
  const key = variantKeys[i];
1835
- if (Object.hasOwn(propsRecord, key)) {
1547
+ if (hasOwn(propsRecord, key)) {
1836
1548
  variantProps[key] = propsRecord[key];
1837
1549
  }
1838
1550
  }
1839
1551
  for (const k in variantProps) {
1840
- if (!Object.hasOwn(variantProps, k)) continue;
1552
+ if (!hasOwn(variantProps, k)) continue;
1841
1553
  const v = variantProps[k];
1842
1554
  if (v === undefined) continue;
1843
1555
  resolved[k] = v;
1844
1556
  }
1845
1557
  userVariantProps = variantProps;
1846
1558
  } else {
1847
- // Fast path: walk variantKeys directly against propsRecord. Use
1848
- // 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
1849
1561
  // values the user didn't pass.
1850
1562
  for (let i = 0; i < variantKeysLength; i++) {
1851
1563
  const key = variantKeys[i];
1852
- if (!Object.hasOwn(propsRecord, key)) continue;
1564
+ if (!hasOwn(propsRecord, key)) continue;
1853
1565
  const v = propsRecord[key];
1854
1566
  if (v === undefined) continue;
1855
1567
  resolved[key] = v;
@@ -1883,18 +1595,10 @@ export function create({
1883
1595
  if (psv != null) {
1884
1596
  if (typeof psv === "string") {
1885
1597
  if (psv.length > 0) {
1886
- Object.assign(style, htmlStyleToStyleValue(psv));
1598
+ htmlStyleToStyleValue(psv, style);
1887
1599
  }
1888
1600
  } else if (typeof psv === "object") {
1889
- // Don't allocate when empty.
1890
- let hasAnyKey = false;
1891
- for (const _ in psv) {
1892
- hasAnyKey = true;
1893
- break;
1894
- }
1895
- if (hasAnyKey) {
1896
- Object.assign(style, normalizeStyle(psv));
1897
- }
1601
+ htmlObjStyleToStyleValue(psv, style);
1898
1602
  }
1899
1603
  }
1900
1604
 
@@ -1905,7 +1609,19 @@ export function create({
1905
1609
  };
1906
1610
 
1907
1611
  const getVariants = (variants?: VariantValues<MergedVariants>) => {
1908
- 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
+ }
1909
1625
  let resolvedVariants = resolveVariantsHot(variantProps);
1910
1626
  if (resolveRefine) {
1911
1627
  resolvedVariants = resolveRefine(resolvedVariants, variantProps, false);
@@ -1920,10 +1636,7 @@ export function create({
1920
1636
  // would compound (double for own-render, triple+ for extend chains) and
1921
1637
  // misbehave for non-idempotent transforms.
1922
1638
  const computedBaseClass = hasExtend
1923
- ? clsx(
1924
- ...(extBaseClassesArr as ClsxClassValue[]),
1925
- config.class as ClsxClassValue,
1926
- )
1639
+ ? clsx(extBaseClassesArr, config.class as ClsxClassValue)
1927
1640
  : clsx(config.class as ClsxClassValue);
1928
1641
 
1929
1642
  // Shared closures across the default and modal components.
@@ -1933,8 +1646,8 @@ export function create({
1933
1646
  const meta: ComponentMeta = {
1934
1647
  baseClass: computedBaseClass,
1935
1648
  staticDefaults,
1936
- compute,
1937
- resolveRefine,
1649
+ compute: computeOnce,
1650
+ resolveRefine: resolveRefine ? resolveRefineOnce : null,
1938
1651
  transformClass,
1939
1652
  functionVariantKeys,
1940
1653
  computedDefaultKeys,
@@ -1953,7 +1666,7 @@ export function create({
1953
1666
  c.getVariants = getVariants;
1954
1667
  c.variantKeys = variantKeys;
1955
1668
  c.propKeys = propKeys;
1956
- setComponentMeta(c, meta);
1669
+ (c as AnyComponent & ComponentWithMeta)[META_KEY] = meta;
1957
1670
  return c;
1958
1671
  };
1959
1672
 
@@ -1969,12 +1682,12 @@ export function create({
1969
1682
  // JSX component
1970
1683
  const jsxComponent = ((props: ComponentProps<MergedVariants> = {}) => {
1971
1684
  const { className, style } = computeResult(props);
1972
- return { className, style: styleValueToJSXStyle(style) };
1685
+ return { className, style };
1973
1686
  }) as ModalComponent<MergedVariants, JSXProps>;
1974
1687
  initComponent(
1975
1688
  jsxComponent,
1976
1689
  ["className", "style", ...variantKeys],
1977
- (props = {}) => styleValueToJSXStyle(computeResult(props).style),
1690
+ (props = {}) => computeResult(props).style,
1978
1691
  );
1979
1692
 
1980
1693
  // HTML component
@@ -2009,4 +1722,13 @@ export function create({
2009
1722
  return { cv, cx };
2010
1723
  }
2011
1724
 
2012
- 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
+ }