cva 1.0.0-beta.0 → 1.0.0-beta.10

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/LICENSE CHANGED
@@ -175,7 +175,7 @@
175
175
 
176
176
  END OF TERMS AND CONDITIONS
177
177
 
178
- Copyright 2022 Joe Bell
178
+ Copyright 2022-present Joe Bell
179
179
 
180
180
  Licensed under the Apache License, Version 2.0 (the "License");
181
181
  you may not use this file except in compliance with the License.
@@ -0,0 +1,160 @@
1
+ //#region src/types.d.ts
2
+ type Uninferred<T> = [T][T extends any ? 0 : never];
3
+ type StringToBoolean<T> = T extends "true" | "false" ? boolean : T;
4
+ type InternalVariantKey = `_${string}`;
5
+ type InternalOnlyWarning = "cva's generic parameters are restricted to internal use only.";
6
+ type CVAComponentConfigBase<T extends ClassValue = ClassValue> = {
7
+ base?: T;
8
+ };
9
+ type CVAVariantSchema<V> = { [Variant in keyof V]?: StringToBoolean<keyof V[Variant]> | undefined; };
10
+ type CVACompoundVariantSchema<V> = { [Variant in keyof V]?: StringToBoolean<keyof V[Variant]> | StringToBoolean<keyof V[Variant]>[] | undefined; };
11
+ type CVACompoundVariants<V, T extends ClassValue> = (CVACompoundVariantSchema<V> & CVAClassProp<T>)[];
12
+ type CVAClassProp<T extends ClassValue = ClassValue> = {
13
+ class?: T;
14
+ className?: never;
15
+ } | {
16
+ class?: never;
17
+ className?: T;
18
+ };
19
+ type CVAComponentConfig<Config, Variants, ComposedSingle extends CVAComponentShape | undefined = CVAComponentShape | undefined, ComposedList extends readonly CVAComponentShape[] = readonly CVAComponentShape[], T extends ClassValue = ClassValue, Merged = Variants> = Config & {
20
+ composes?: ComposedSingle | readonly [...ComposedList];
21
+ } & (Variants extends Record<string, Record<string, T>> ? CVAComponentConfigBase<T> & {
22
+ variants?: Variants & {
23
+ __proto__?: never;
24
+ };
25
+ } : CVAComponentConfigBase<T> & {
26
+ variants?: never;
27
+ }) & ([keyof Merged] extends [never] ? {
28
+ compoundVariants?: never;
29
+ defaultVariants?: never;
30
+ } : {
31
+ compoundVariants?: CVACompoundVariants<Uninferred<Merged>, T>;
32
+ defaultVariants?: CVAVariantSchema<Uninferred<Merged>>;
33
+ });
34
+ //#endregion
35
+ //#region src/config.d.ts
36
+ type ClassValue = ClassArray | ClassDictionary | string | number | bigint | null | boolean | undefined;
37
+ type ClassDictionary = Record<string, any>;
38
+ type ClassArray = ClassValue[];
39
+ /**
40
+ * Any function usable as a `cx` concatenator.
41
+ */
42
+ type AnyCX = (...inputs: any[]) => string;
43
+ type CXInputs<TCX extends AnyCX> = TCX extends ((...inputs: readonly [...infer Inputs]) => string) ? Inputs : never;
44
+ type CXInputElement<TCX extends AnyCX> = CXInputs<TCX>[number];
45
+ type CXHasSafeArity<TCX extends AnyCX> = CXInputs<TCX> extends (infer Inputs) ? Inputs extends readonly unknown[] ? Inputs extends [] ? true : [] extends Inputs ? number extends Inputs["length"] ? true : false : false : false : false;
46
+ type CXConstraint<TCX extends AnyCX> = false extends CXHasSafeArity<TCX> ? "cva's cx must accept zero arguments and an unbounded rest parameter." : [TCX] extends [(...inputs: (string | CXInput<TCX>)[]) => string] ? unknown : "cva's cx must accept its inferred class values and composed strings.";
47
+ /**
48
+ * The class value type a concatenator accepts, inferred from its
49
+ * parameters — `defineConfig` uses this to type the authoring surface
50
+ * (`base`, variant values, `class`/`className`) against the configured
51
+ * concatenator's own input grammar.
52
+ */
53
+ type CXInput<TCX extends AnyCX> = CXInputElement<TCX> extends (infer P) ? 0 extends 1 & P ? ClassValue : [P] extends [never] ? ClassValue : [P] extends [ClassValue] ? P : [Extract<P, ClassValue>] extends [never] ? ClassValue : Extract<P, ClassValue> : ClassValue;
54
+ type OmitUndefined<T> = T extends undefined ? never : T;
55
+ type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
56
+ type ComposedTuple<S extends CVAComponentShape | undefined, L extends readonly CVAComponentShape[]> = [S] extends [CVAComponentShape] ? [S] : L;
57
+ type MergedVariants<T extends readonly unknown[]> = UnionToIntersection<{ [K in keyof T]: T[K] extends {
58
+ config: {
59
+ variants?: infer V extends CVAVariantShape;
60
+ };
61
+ } ? V : never; }[number]>;
62
+ type RightMerge<A, B> = { [K in keyof A | keyof B]: K extends keyof B ? B[K] : K extends keyof A ? A[K] : never; };
63
+ type DefaultsOf<Component> = Component extends {
64
+ config: {
65
+ defaultVariants?: infer D;
66
+ };
67
+ } ? D extends undefined ? {} : D : {};
68
+ type MergedDefaultVariants<T extends readonly unknown[]> = T extends readonly [infer Head, ...infer Rest] ? RightMerge<DefaultsOf<Head>, MergedDefaultVariants<Rest>> : {};
69
+ type ComponentProps<Component extends (...args: any) => any> = Omit<OmitUndefined<Parameters<Component>[0]>, "class" | "className">;
70
+ type VariantProps<Component extends (...args: any) => any> = Omit<OmitUndefined<Parameters<Component>[0]>, "class" | "className" | InternalVariantKey>;
71
+ /**
72
+ * @deprecated Use the `composes` property inside `cva` instead.
73
+ * @example
74
+ * // Before
75
+ * const card = compose(box, stack)
76
+ * // After
77
+ * const card = cva({ composes: [box, stack] })
78
+ */
79
+ interface Compose<T extends ClassValue = ClassValue> {
80
+ <Components extends readonly unknown[]>(...components: Components & (Components[number] extends CVAComponentShape ? unknown : never)): (props?: (UnionToIntersection<{ [K in keyof Components]: Components[K] extends CVAComponentShape ? ComponentProps<Components[K]> : never; }[number]> | undefined) & CVAClassProp<T>) => string;
81
+ }
82
+ interface CX<T extends ClassValue = ClassValue> {
83
+ (...inputs: T[]): string;
84
+ }
85
+ type CXOptions = Parameters<CX>;
86
+ type CXReturn = ReturnType<CX>;
87
+ /**
88
+ * Exported so TypeScript can name this type in your generated declarations
89
+ * (`declaration: true`) — you shouldn't really use it directly.
90
+ */
91
+ type CVAVariantShape = Record<string, Record<string, ClassValue>>;
92
+ /**
93
+ * Exported so TypeScript can name this type in your generated declarations
94
+ * (`declaration: true`) — you shouldn't really use it directly.
95
+ */
96
+ interface CVAComponent<Config, Variants, T extends ClassValue = ClassValue> {
97
+ (props?: Variants extends CVAVariantShape ? CVAVariantSchema<Variants> & CVAClassProp<T> : CVAClassProp<T>): string;
98
+ /** @internal */
99
+ config: Config;
100
+ }
101
+ /**
102
+ * Exported so TypeScript can name this type in your generated declarations
103
+ * (`declaration: true`) — you shouldn't really use it directly.
104
+ */
105
+ type CVAComponentShape = CVAComponent<any, any, any>;
106
+ type CVADefaultVariants<Config> = Config extends {
107
+ defaultVariants?: infer D;
108
+ } ? D : {};
109
+ type AllVariants<Variants, ComposedSingle extends CVAComponentShape | undefined, ComposedList extends readonly CVAComponentShape[]> = [ComposedSingle] extends [undefined] ? [ComposedList] extends [readonly []] ? Variants : Variants & MergedVariants<ComposedList> : Variants & MergedVariants<ComposedTuple<ComposedSingle, ComposedList>>;
110
+ interface CVA<T extends ClassValue = ClassValue> {
111
+ <_ extends InternalOnlyWarning, Config, Variants, ComposedSingle extends CVAComponentShape | undefined = undefined, ComposedList extends readonly CVAComponentShape[] = []>(config: CVAComponentConfig<Config, Variants, ComposedSingle, ComposedList, T, AllVariants<Variants, ComposedSingle, ComposedList>>): CVAComponent<Omit<Config, "defaultVariants"> & {
112
+ variants: AllVariants<Variants, ComposedSingle, ComposedList>;
113
+ defaultVariants: Omit<MergedDefaultVariants<ComposedTuple<ComposedSingle, ComposedList>>, keyof CVADefaultVariants<Config>> & CVADefaultVariants<Config>;
114
+ }, AllVariants<Variants, ComposedSingle, ComposedList>, T>;
115
+ }
116
+ interface DefineConfigOptions<TCX extends AnyCX = CX> {
117
+ /**
118
+ * The class name concatenator used by `cva`, `cx`, and `compose`. It owns
119
+ * the class name grammar entirely: cva assembles the authored values
120
+ * (composed component outputs, `base`, matched variant and compound
121
+ * variant values, `class`/`className`) and passes them through verbatim,
122
+ * one argument each, without interpreting them.
123
+ *
124
+ * The authoring surface adopts the concatenator's own input type
125
+ * automatically (see {@link CXInput}): pass `twMerge` and your variants
126
+ * are checked against tailwind-merge's `ClassNameValue`; pass `clsx` (or
127
+ * any function whose parameters don't narrow further) and you keep the
128
+ * full clsx-flavored `ClassValue` grammar.
129
+ */
130
+ cx: TCX & CXConstraint<TCX>;
131
+ hooks?: {
132
+ /**
133
+ * @deprecated please use the `cx` option instead
134
+ */
135
+ "cx:done"?: (className: string) => string;
136
+ /**
137
+ * @deprecated please use the `cx` option instead
138
+ */
139
+ onComplete?: (className: string) => string;
140
+ };
141
+ }
142
+ interface DefineConfig {
143
+ <TCX extends AnyCX>(options: DefineConfigOptions<TCX>): {
144
+ /**
145
+ * @deprecated Use the `composes` property inside `cva` instead.
146
+ * @example
147
+ * // Before
148
+ * const card = compose(box, stack)
149
+ * // After
150
+ * const card = cva({ composes: [box, stack] })
151
+ */
152
+ compose: Compose<CXInput<TCX>>;
153
+ cx: CX<CXInput<TCX>>;
154
+ cva: CVA<CXInput<TCX>>;
155
+ };
156
+ }
157
+ declare const defineConfig: DefineConfig;
158
+ //#endregion
159
+ export { defineConfig as _, CVAVariantShape as a, InternalVariantKey as b, CXOptions as c, ClassDictionary as d, ClassValue as f, VariantProps as g, DefineConfigOptions as h, CVAComponentShape as i, CXReturn as l, DefineConfig as m, CVA as n, CX as o, Compose as p, CVAComponent as r, CXInput as s, AnyCX as t, ClassArray as u, CVAComponentConfig as v, StringToBoolean as x, InternalOnlyWarning as y };
160
+ //# sourceMappingURL=config-CE5oXWBq.d.cts.map
@@ -0,0 +1,160 @@
1
+ //#region src/types.d.ts
2
+ type Uninferred<T> = [T][T extends any ? 0 : never];
3
+ type StringToBoolean<T> = T extends "true" | "false" ? boolean : T;
4
+ type InternalVariantKey = `_${string}`;
5
+ type InternalOnlyWarning = "cva's generic parameters are restricted to internal use only.";
6
+ type CVAComponentConfigBase<T extends ClassValue = ClassValue> = {
7
+ base?: T;
8
+ };
9
+ type CVAVariantSchema<V> = { [Variant in keyof V]?: StringToBoolean<keyof V[Variant]> | undefined; };
10
+ type CVACompoundVariantSchema<V> = { [Variant in keyof V]?: StringToBoolean<keyof V[Variant]> | StringToBoolean<keyof V[Variant]>[] | undefined; };
11
+ type CVACompoundVariants<V, T extends ClassValue> = (CVACompoundVariantSchema<V> & CVAClassProp<T>)[];
12
+ type CVAClassProp<T extends ClassValue = ClassValue> = {
13
+ class?: T;
14
+ className?: never;
15
+ } | {
16
+ class?: never;
17
+ className?: T;
18
+ };
19
+ type CVAComponentConfig<Config, Variants, ComposedSingle extends CVAComponentShape | undefined = CVAComponentShape | undefined, ComposedList extends readonly CVAComponentShape[] = readonly CVAComponentShape[], T extends ClassValue = ClassValue, Merged = Variants> = Config & {
20
+ composes?: ComposedSingle | readonly [...ComposedList];
21
+ } & (Variants extends Record<string, Record<string, T>> ? CVAComponentConfigBase<T> & {
22
+ variants?: Variants & {
23
+ __proto__?: never;
24
+ };
25
+ } : CVAComponentConfigBase<T> & {
26
+ variants?: never;
27
+ }) & ([keyof Merged] extends [never] ? {
28
+ compoundVariants?: never;
29
+ defaultVariants?: never;
30
+ } : {
31
+ compoundVariants?: CVACompoundVariants<Uninferred<Merged>, T>;
32
+ defaultVariants?: CVAVariantSchema<Uninferred<Merged>>;
33
+ });
34
+ //#endregion
35
+ //#region src/config.d.ts
36
+ type ClassValue = ClassArray | ClassDictionary | string | number | bigint | null | boolean | undefined;
37
+ type ClassDictionary = Record<string, any>;
38
+ type ClassArray = ClassValue[];
39
+ /**
40
+ * Any function usable as a `cx` concatenator.
41
+ */
42
+ type AnyCX = (...inputs: any[]) => string;
43
+ type CXInputs<TCX extends AnyCX> = TCX extends ((...inputs: readonly [...infer Inputs]) => string) ? Inputs : never;
44
+ type CXInputElement<TCX extends AnyCX> = CXInputs<TCX>[number];
45
+ type CXHasSafeArity<TCX extends AnyCX> = CXInputs<TCX> extends (infer Inputs) ? Inputs extends readonly unknown[] ? Inputs extends [] ? true : [] extends Inputs ? number extends Inputs["length"] ? true : false : false : false : false;
46
+ type CXConstraint<TCX extends AnyCX> = false extends CXHasSafeArity<TCX> ? "cva's cx must accept zero arguments and an unbounded rest parameter." : [TCX] extends [(...inputs: (string | CXInput<TCX>)[]) => string] ? unknown : "cva's cx must accept its inferred class values and composed strings.";
47
+ /**
48
+ * The class value type a concatenator accepts, inferred from its
49
+ * parameters — `defineConfig` uses this to type the authoring surface
50
+ * (`base`, variant values, `class`/`className`) against the configured
51
+ * concatenator's own input grammar.
52
+ */
53
+ type CXInput<TCX extends AnyCX> = CXInputElement<TCX> extends (infer P) ? 0 extends 1 & P ? ClassValue : [P] extends [never] ? ClassValue : [P] extends [ClassValue] ? P : [Extract<P, ClassValue>] extends [never] ? ClassValue : Extract<P, ClassValue> : ClassValue;
54
+ type OmitUndefined<T> = T extends undefined ? never : T;
55
+ type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
56
+ type ComposedTuple<S extends CVAComponentShape | undefined, L extends readonly CVAComponentShape[]> = [S] extends [CVAComponentShape] ? [S] : L;
57
+ type MergedVariants<T extends readonly unknown[]> = UnionToIntersection<{ [K in keyof T]: T[K] extends {
58
+ config: {
59
+ variants?: infer V extends CVAVariantShape;
60
+ };
61
+ } ? V : never; }[number]>;
62
+ type RightMerge<A, B> = { [K in keyof A | keyof B]: K extends keyof B ? B[K] : K extends keyof A ? A[K] : never; };
63
+ type DefaultsOf<Component> = Component extends {
64
+ config: {
65
+ defaultVariants?: infer D;
66
+ };
67
+ } ? D extends undefined ? {} : D : {};
68
+ type MergedDefaultVariants<T extends readonly unknown[]> = T extends readonly [infer Head, ...infer Rest] ? RightMerge<DefaultsOf<Head>, MergedDefaultVariants<Rest>> : {};
69
+ type ComponentProps<Component extends (...args: any) => any> = Omit<OmitUndefined<Parameters<Component>[0]>, "class" | "className">;
70
+ type VariantProps<Component extends (...args: any) => any> = Omit<OmitUndefined<Parameters<Component>[0]>, "class" | "className" | InternalVariantKey>;
71
+ /**
72
+ * @deprecated Use the `composes` property inside `cva` instead.
73
+ * @example
74
+ * // Before
75
+ * const card = compose(box, stack)
76
+ * // After
77
+ * const card = cva({ composes: [box, stack] })
78
+ */
79
+ interface Compose<T extends ClassValue = ClassValue> {
80
+ <Components extends readonly unknown[]>(...components: Components & (Components[number] extends CVAComponentShape ? unknown : never)): (props?: (UnionToIntersection<{ [K in keyof Components]: Components[K] extends CVAComponentShape ? ComponentProps<Components[K]> : never; }[number]> | undefined) & CVAClassProp<T>) => string;
81
+ }
82
+ interface CX<T extends ClassValue = ClassValue> {
83
+ (...inputs: T[]): string;
84
+ }
85
+ type CXOptions = Parameters<CX>;
86
+ type CXReturn = ReturnType<CX>;
87
+ /**
88
+ * Exported so TypeScript can name this type in your generated declarations
89
+ * (`declaration: true`) — you shouldn't really use it directly.
90
+ */
91
+ type CVAVariantShape = Record<string, Record<string, ClassValue>>;
92
+ /**
93
+ * Exported so TypeScript can name this type in your generated declarations
94
+ * (`declaration: true`) — you shouldn't really use it directly.
95
+ */
96
+ interface CVAComponent<Config, Variants, T extends ClassValue = ClassValue> {
97
+ (props?: Variants extends CVAVariantShape ? CVAVariantSchema<Variants> & CVAClassProp<T> : CVAClassProp<T>): string;
98
+ /** @internal */
99
+ config: Config;
100
+ }
101
+ /**
102
+ * Exported so TypeScript can name this type in your generated declarations
103
+ * (`declaration: true`) — you shouldn't really use it directly.
104
+ */
105
+ type CVAComponentShape = CVAComponent<any, any, any>;
106
+ type CVADefaultVariants<Config> = Config extends {
107
+ defaultVariants?: infer D;
108
+ } ? D : {};
109
+ type AllVariants<Variants, ComposedSingle extends CVAComponentShape | undefined, ComposedList extends readonly CVAComponentShape[]> = [ComposedSingle] extends [undefined] ? [ComposedList] extends [readonly []] ? Variants : Variants & MergedVariants<ComposedList> : Variants & MergedVariants<ComposedTuple<ComposedSingle, ComposedList>>;
110
+ interface CVA<T extends ClassValue = ClassValue> {
111
+ <_ extends InternalOnlyWarning, Config, Variants, ComposedSingle extends CVAComponentShape | undefined = undefined, ComposedList extends readonly CVAComponentShape[] = []>(config: CVAComponentConfig<Config, Variants, ComposedSingle, ComposedList, T, AllVariants<Variants, ComposedSingle, ComposedList>>): CVAComponent<Omit<Config, "defaultVariants"> & {
112
+ variants: AllVariants<Variants, ComposedSingle, ComposedList>;
113
+ defaultVariants: Omit<MergedDefaultVariants<ComposedTuple<ComposedSingle, ComposedList>>, keyof CVADefaultVariants<Config>> & CVADefaultVariants<Config>;
114
+ }, AllVariants<Variants, ComposedSingle, ComposedList>, T>;
115
+ }
116
+ interface DefineConfigOptions<TCX extends AnyCX = CX> {
117
+ /**
118
+ * The class name concatenator used by `cva`, `cx`, and `compose`. It owns
119
+ * the class name grammar entirely: cva assembles the authored values
120
+ * (composed component outputs, `base`, matched variant and compound
121
+ * variant values, `class`/`className`) and passes them through verbatim,
122
+ * one argument each, without interpreting them.
123
+ *
124
+ * The authoring surface adopts the concatenator's own input type
125
+ * automatically (see {@link CXInput}): pass `twMerge` and your variants
126
+ * are checked against tailwind-merge's `ClassNameValue`; pass `clsx` (or
127
+ * any function whose parameters don't narrow further) and you keep the
128
+ * full clsx-flavored `ClassValue` grammar.
129
+ */
130
+ cx: TCX & CXConstraint<TCX>;
131
+ hooks?: {
132
+ /**
133
+ * @deprecated please use the `cx` option instead
134
+ */
135
+ "cx:done"?: (className: string) => string;
136
+ /**
137
+ * @deprecated please use the `cx` option instead
138
+ */
139
+ onComplete?: (className: string) => string;
140
+ };
141
+ }
142
+ interface DefineConfig {
143
+ <TCX extends AnyCX>(options: DefineConfigOptions<TCX>): {
144
+ /**
145
+ * @deprecated Use the `composes` property inside `cva` instead.
146
+ * @example
147
+ * // Before
148
+ * const card = compose(box, stack)
149
+ * // After
150
+ * const card = cva({ composes: [box, stack] })
151
+ */
152
+ compose: Compose<CXInput<TCX>>;
153
+ cx: CX<CXInput<TCX>>;
154
+ cva: CVA<CXInput<TCX>>;
155
+ };
156
+ }
157
+ declare const defineConfig: DefineConfig;
158
+ //#endregion
159
+ export { defineConfig as _, CVAVariantShape as a, InternalVariantKey as b, CXOptions as c, ClassDictionary as d, ClassValue as f, VariantProps as g, DefineConfigOptions as h, CVAComponentShape as i, CXReturn as l, DefineConfig as m, CVA as n, CX as o, Compose as p, CVAComponent as r, CXInput as s, AnyCX as t, ClassArray as u, CVAComponentConfig as v, StringToBoolean as x, InternalOnlyWarning as y };
160
+ //# sourceMappingURL=config-CE5oXWBq.d.mts.map
@@ -0,0 +1,265 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region src/config.ts
3
+ const falsyToString = (value) => typeof value === "boolean" ? `${value}` : value === 0 ? "0" : value;
4
+ const empty = {};
5
+ const noValues = [];
6
+ const hasOwn = Object.prototype.hasOwnProperty;
7
+ const ownEnumerable = Object.prototype.propertyIsEnumerable;
8
+ const definedProps = (props, seed) => {
9
+ let merged = { ...seed };
10
+ for (const key in props) if (hasOwn.call(props, key)) {
11
+ const value = props[key];
12
+ if (key !== "class" && key !== "className" && value !== void 0) if (key === "__proto__") merged = {
13
+ ...merged,
14
+ [key]: value
15
+ };
16
+ else merged[key] = value;
17
+ }
18
+ return merged;
19
+ };
20
+ const pushDefined = (out, value) => {
21
+ if (value !== void 0) out.push(value);
22
+ };
23
+ const pushClassProps = (out, source) => {
24
+ pushDefined(out, source.class);
25
+ pushDefined(out, source.className);
26
+ return out;
27
+ };
28
+ const mergeInto = (merged, source) => {
29
+ const variants = source && source.variants;
30
+ for (const key in variants) if (hasOwn.call(variants, key)) {
31
+ const values = {
32
+ ...merged.variants[key],
33
+ ...variants[key]
34
+ };
35
+ if (key === "__proto__") merged.variants = {
36
+ ...merged.variants,
37
+ [key]: values
38
+ };
39
+ else merged.variants[key] = values;
40
+ }
41
+ merged.defaults = {
42
+ ...merged.defaults,
43
+ ...source && source.defaultVariants
44
+ };
45
+ };
46
+ const mergeConfig = (children, definition) => {
47
+ const merged = {
48
+ variants: {},
49
+ defaults: {}
50
+ };
51
+ for (let i = 0; i < children.length; i++) mergeInto(merged, children[i].config);
52
+ mergeInto(merged, definition);
53
+ return merged;
54
+ };
55
+ const prepareVariants = (localVariants, defaults) => {
56
+ const names = [];
57
+ const maps = [];
58
+ for (const key in localVariants) if (hasOwn.call(localVariants, key)) {
59
+ names.push(key);
60
+ maps.push(localVariants[key]);
61
+ }
62
+ if (!names.length) return {
63
+ variantKeys: noValues,
64
+ variantMaps: noValues,
65
+ defaultClasses: noValues
66
+ };
67
+ return {
68
+ variantKeys: names.slice(),
69
+ variantMaps: maps.slice(),
70
+ defaultClasses: names.map((key, i) => maps[i][falsyToString(defaults[key])])
71
+ };
72
+ };
73
+ const compoundMatches = (compound, indexes, selectors, values) => {
74
+ for (let i = compound.start; i < compound.end; i++) {
75
+ const selector = selectors[i];
76
+ const value = values[indexes[i]];
77
+ if (Array.isArray(selector) ? !selector.includes(value) : value !== selector) return false;
78
+ }
79
+ return true;
80
+ };
81
+ const prepareCompounds = (compoundVariants, variantKeys, defaults) => {
82
+ const keys = variantKeys.slice();
83
+ const compounds = [];
84
+ const indexes = [];
85
+ const selectors = [];
86
+ for (let i = 0; i < compoundVariants.length; i++) {
87
+ const compound = compoundVariants[i];
88
+ const start = indexes.length;
89
+ let mask = 0;
90
+ for (const key in compound) if (hasOwn.call(compound, key)) {
91
+ const selector = compound[key];
92
+ if (key !== "class" && key !== "className") {
93
+ let index = keys.indexOf(key);
94
+ if (index === -1) index = keys.push(key) - 1;
95
+ indexes.push(index);
96
+ selectors.push(selector);
97
+ mask |= 1 << index;
98
+ }
99
+ }
100
+ compounds.push({
101
+ start,
102
+ end: indexes.length,
103
+ mask,
104
+ matchesDefaults: false,
105
+ class: compound.class,
106
+ className: compound.className
107
+ });
108
+ }
109
+ const defaultValues = keys.map((key) => defaults[key]);
110
+ for (let i = 0; i < compounds.length; i++) compounds[i].matchesDefaults = compoundMatches(compounds[i], indexes, selectors, defaultValues);
111
+ return {
112
+ keys: keys.slice(),
113
+ defaultValues,
114
+ compounds: compounds.slice(),
115
+ indexes: indexes.slice(),
116
+ selectors: selectors.slice()
117
+ };
118
+ };
119
+ const createPlainComponent = (cxArray, defaultOut, singleDefaultClass) => (input) => {
120
+ const props = input || empty;
121
+ const classValue = props.class;
122
+ const classNameValue = props.className;
123
+ if (classValue === void 0 && singleDefaultClass !== void 0) return cxArray(classNameValue === void 0 ? [singleDefaultClass] : [singleDefaultClass, classNameValue]);
124
+ const out = defaultOut.slice();
125
+ pushDefined(out, classValue);
126
+ pushDefined(out, classNameValue);
127
+ return cxArray(out);
128
+ };
129
+ const defineConfig = ((options) => {
130
+ const cxArray = (inputs) => {
131
+ const className = Reflect.apply(options.cx, options, inputs);
132
+ const hooks = options.hooks || empty;
133
+ let hook = hooks["cx:done"];
134
+ if (hook == null) hook = hooks.onComplete;
135
+ return hook ? hook(className) : className;
136
+ };
137
+ const cx = (...inputs) => cxArray(inputs.filter((input) => input !== void 0));
138
+ const cva = ((config) => {
139
+ const definition = config || empty;
140
+ const composes = definition.composes;
141
+ const base = definition.base;
142
+ const children = composes == null ? noValues : Array.isArray(composes) ? composes.slice() : [composes];
143
+ const childCount = children.length;
144
+ const { variants: mergedVariants, defaults } = mergeConfig(children, definition);
145
+ const { variantKeys, variantMaps, defaultClasses } = prepareVariants(definition.variants, defaults);
146
+ const variantCount = variantKeys.length;
147
+ const prepared = definition.compoundVariants ? prepareCompounds(definition.compoundVariants, variantKeys, defaults) : void 0;
148
+ const keys = prepared ? prepared.keys : variantKeys;
149
+ const keyCount = keys.length;
150
+ const compounds = prepared ? prepared.compounds : noValues;
151
+ const compoundCount = compounds.length;
152
+ const indexes = prepared ? prepared.indexes : noValues;
153
+ const selectors = prepared ? prepared.selectors : noValues;
154
+ const defaultValues = prepared ? prepared.defaultValues : noValues;
155
+ const onlyBase = !variantCount && !compoundCount;
156
+ const assembled = onlyBase && base !== void 0 ? [base] : [];
157
+ if (!onlyBase) pushDefined(assembled, base);
158
+ for (let i = 0; i < variantCount; i++) pushDefined(assembled, defaultClasses[i]);
159
+ for (let i = 0; i < compoundCount; i++) {
160
+ const compound = compounds[i];
161
+ if (compound.matchesDefaults) pushClassProps(assembled, compound);
162
+ }
163
+ const defaultOut = onlyBase ? assembled : assembled.slice();
164
+ const singleDefaultClass = defaultOut.length === 1 ? defaultOut[0] : void 0;
165
+ const component = !keyCount && !childCount ? createPlainComponent(cxArray, defaultOut, singleDefaultClass) : (input) => {
166
+ const props = input || empty;
167
+ const classValue = props.class;
168
+ const classNameValue = props.className;
169
+ let supplied = 0;
170
+ const variantClasses = variantCount ? new Array(variantCount) : void 0;
171
+ const resolved = compoundCount ? new Array(keyCount) : void 0;
172
+ for (let i = 0; i < keyCount; i++) {
173
+ const key = keys[i];
174
+ const own = compoundCount !== 0 && ownEnumerable.call(props, key);
175
+ const value = i < variantCount || own ? props[key] : void 0;
176
+ if (value !== void 0) supplied |= i < 31 ? 1 << i : -1;
177
+ if (variantClasses && i < variantCount) {
178
+ const variantKey = falsyToString(value);
179
+ variantClasses[i] = variantKey ? variantMaps[i][variantKey] : defaultClasses[i];
180
+ }
181
+ if (resolved) resolved[i] = own && value !== void 0 ? value : defaultValues[i];
182
+ }
183
+ if (!childCount && !supplied) {
184
+ if (classValue === void 0) {
185
+ if (singleDefaultClass !== void 0) return cxArray(classNameValue === void 0 ? [singleDefaultClass] : [singleDefaultClass, classNameValue]);
186
+ if (classNameValue === void 0) return cxArray(defaultOut);
187
+ }
188
+ const out = defaultOut.slice();
189
+ pushDefined(out, classValue);
190
+ pushDefined(out, classNameValue);
191
+ return cxArray(out);
192
+ }
193
+ const out = [];
194
+ if (childCount) {
195
+ const forwarded = definedProps(props, defaults);
196
+ for (let i = 0; i < childCount; i++) {
197
+ const child = children[i];
198
+ pushDefined(out, child({ ...forwarded }));
199
+ }
200
+ }
201
+ pushDefined(out, base);
202
+ if (variantClasses) for (let i = 0; i < variantCount; i++) pushDefined(out, variantClasses[i]);
203
+ if (resolved) for (let i = 0; i < compoundCount; i++) {
204
+ const compound = compounds[i];
205
+ let matched = compound.matchesDefaults;
206
+ if (supplied & compound.mask) {
207
+ matched = true;
208
+ for (let j = compound.start; j < compound.end; j++) {
209
+ const selector = selectors[j];
210
+ const value = resolved[indexes[j]];
211
+ if (Array.isArray(selector) ? !selector.includes(value) : value !== selector) {
212
+ matched = false;
213
+ break;
214
+ }
215
+ }
216
+ }
217
+ if (matched) pushClassProps(out, compound);
218
+ }
219
+ pushDefined(out, classValue);
220
+ pushDefined(out, classNameValue);
221
+ return cxArray(out);
222
+ };
223
+ component.config = {
224
+ ...config,
225
+ variants: mergedVariants,
226
+ defaultVariants: defaults
227
+ };
228
+ return component;
229
+ });
230
+ const compose = (...components) => {
231
+ const composed = components;
232
+ const config = {};
233
+ for (let i = 0; i < composed.length; i++) {
234
+ const source = composed[i].config;
235
+ for (const key in source) if (hasOwn.call(source, key)) {
236
+ const value = source[key];
237
+ config[key] = value && typeof value === "object" && !Array.isArray(value) ? {
238
+ ...config[key],
239
+ ...value
240
+ } : value;
241
+ }
242
+ }
243
+ const component = (input) => {
244
+ const props = input || empty;
245
+ const forwarded = definedProps(props);
246
+ const out = [];
247
+ for (let i = 0; i < composed.length; i++) {
248
+ const child = composed[i];
249
+ pushDefined(out, child(forwarded));
250
+ }
251
+ return cxArray(pushClassProps(out, props));
252
+ };
253
+ component.config = config;
254
+ return component;
255
+ };
256
+ return {
257
+ compose,
258
+ cva,
259
+ cx
260
+ };
261
+ });
262
+ //#endregion
263
+ exports.defineConfig = defineConfig;
264
+
265
+ //# sourceMappingURL=config.cjs.map