cva 1.0.0-beta.7 → 1.0.0-beta.9
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 +1 -1
- package/dist/config.cjs +3 -0
- package/dist/config.d.cts +2 -0
- package/dist/config.d.mts +2 -0
- package/dist/config.mjs +2 -0
- package/dist/core-BMF1ydiV.d.cts +188 -0
- package/dist/core-BMF1ydiV.d.mts +188 -0
- package/dist/core-DPc9m0ph.mjs +99 -0
- package/dist/core-DPc9m0ph.mjs.map +1 -0
- package/dist/core-iwLcNxRW.cjs +104 -0
- package/dist/core-iwLcNxRW.cjs.map +1 -0
- package/dist/index.cjs +20 -117
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +25 -115
- package/dist/index.d.mts +25 -115
- package/dist/index.mjs +20 -117
- package/dist/index.mjs.map +1 -1
- package/dist/utils.cjs +31 -0
- package/dist/utils.cjs.map +1 -0
- package/dist/utils.d.cts +6 -0
- package/dist/utils.d.mts +6 -0
- package/dist/utils.mjs +30 -0
- package/dist/utils.mjs.map +1 -0
- package/package.json +31 -3
package/LICENSE
CHANGED
package/dist/config.cjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { _ as VariantProps, a as CVAVariantShape, c as CXOptions, d as ClassDictionary, f as ClassValue, h as DefineConfigOptions, i as CVAComponentShape, l as CXReturn, m as DefineConfig, n as CVA, o as CX, p as Compose, r as CVAComponent, s as CXInput, t as AnyCX, u as ClassArray, v as defineConfig } from "./core-BMF1ydiV.cjs";
|
|
2
|
+
export { type AnyCX, type CVA, type CVAComponent, type CVAComponentShape, type CVAVariantShape, type CX, type CXInput, type CXOptions, type CXReturn, type ClassArray, type ClassDictionary, type ClassValue, type Compose, type DefineConfig, type DefineConfigOptions, type VariantProps, defineConfig };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { _ as VariantProps, a as CVAVariantShape, c as CXOptions, d as ClassDictionary, f as ClassValue, h as DefineConfigOptions, i as CVAComponentShape, l as CXReturn, m as DefineConfig, n as CVA, o as CX, p as Compose, r as CVAComponent, s as CXInput, t as AnyCX, u as ClassArray, v as defineConfig } from "./core-BMF1ydiV.mjs";
|
|
2
|
+
export { type AnyCX, type CVA, type CVAComponent, type CVAComponentShape, type CVAVariantShape, type CX, type CXInput, type CXOptions, type CXReturn, type ClassArray, type ClassDictionary, type ClassValue, type Compose, type DefineConfig, type DefineConfigOptions, type VariantProps, defineConfig };
|
package/dist/config.mjs
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
//#region src/core.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Copyright 2022-present Joe Bell. All rights reserved.
|
|
4
|
+
*
|
|
5
|
+
* This file is licensed to you under the Apache License, Version 2.0
|
|
6
|
+
* (the "License"); you may not use this file except in compliance with the
|
|
7
|
+
* License. You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
13
|
+
* WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or implied. See the
|
|
14
|
+
* License for the specific language governing permissions and limitations under
|
|
15
|
+
* the License.
|
|
16
|
+
*/
|
|
17
|
+
type ClassValue = ClassArray | ClassDictionary | string | number | bigint | null | boolean | undefined;
|
|
18
|
+
type ClassDictionary = Record<string, any>;
|
|
19
|
+
type ClassArray = ClassValue[];
|
|
20
|
+
/**
|
|
21
|
+
* Any function usable as a `cx` concatenator.
|
|
22
|
+
*/
|
|
23
|
+
type AnyCX = (...inputs: any[]) => string;
|
|
24
|
+
type CXInputs<TCX extends AnyCX> = TCX extends ((...inputs: readonly [...infer Inputs]) => string) ? Inputs : never;
|
|
25
|
+
type CXInputElement<TCX extends AnyCX> = CXInputs<TCX>[number];
|
|
26
|
+
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;
|
|
27
|
+
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.";
|
|
28
|
+
/**
|
|
29
|
+
* The class value type a concatenator accepts, inferred from its
|
|
30
|
+
* parameters — `defineConfig` uses this to type the authoring surface
|
|
31
|
+
* (`base`, variant values, `class`/`className`) against the configured
|
|
32
|
+
* concatenator's own input grammar.
|
|
33
|
+
*/
|
|
34
|
+
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;
|
|
35
|
+
type OmitUndefined<T> = T extends undefined ? never : T;
|
|
36
|
+
type Uninferred<T> = [T][T extends any ? 0 : never];
|
|
37
|
+
type StringToBoolean<T> = T extends "true" | "false" ? boolean : T;
|
|
38
|
+
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
|
|
39
|
+
type ComposedTuple<S extends CVAComponentShape | undefined, L extends readonly CVAComponentShape[]> = [S] extends [CVAComponentShape] ? [S] : L;
|
|
40
|
+
type MergedVariants<T extends readonly unknown[]> = UnionToIntersection<{ [K in keyof T]: T[K] extends {
|
|
41
|
+
config: {
|
|
42
|
+
variants?: infer V extends CVAVariantShape;
|
|
43
|
+
};
|
|
44
|
+
} ? V : never; }[number]>;
|
|
45
|
+
type RightMerge<A, B> = { [K in keyof A | keyof B]: K extends keyof B ? B[K] : K extends keyof A ? A[K] : never; };
|
|
46
|
+
type DefaultsOf<Component> = Component extends {
|
|
47
|
+
config: {
|
|
48
|
+
defaultVariants?: infer D;
|
|
49
|
+
};
|
|
50
|
+
} ? D extends undefined ? {} : D : {};
|
|
51
|
+
type MergedDefaultVariants<T extends readonly unknown[]> = T extends readonly [infer Head, ...infer Rest] ? RightMerge<DefaultsOf<Head>, MergedDefaultVariants<Rest>> : {};
|
|
52
|
+
type ComponentProps<Component extends (...args: any) => any> = Omit<OmitUndefined<Parameters<Component>[0]>, "class" | "className">;
|
|
53
|
+
type InternalVariantKey = `_${string}`;
|
|
54
|
+
type VariantProps<Component extends (...args: any) => any> = Omit<ComponentProps<Component>, InternalVariantKey>;
|
|
55
|
+
/**
|
|
56
|
+
* @deprecated Use the `composes` property inside `cva` instead.
|
|
57
|
+
* @example
|
|
58
|
+
* // Before
|
|
59
|
+
* const card = compose(box, stack)
|
|
60
|
+
* // After
|
|
61
|
+
* const card = cva({ composes: [box, stack] })
|
|
62
|
+
*/
|
|
63
|
+
interface Compose<T extends ClassValue = ClassValue> {
|
|
64
|
+
<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;
|
|
65
|
+
}
|
|
66
|
+
interface CX<T extends ClassValue = ClassValue> {
|
|
67
|
+
(...inputs: T[]): string;
|
|
68
|
+
}
|
|
69
|
+
type CXOptions = Parameters<CX>;
|
|
70
|
+
type CXReturn = ReturnType<CX>;
|
|
71
|
+
type CVAComponentConfigBase<T extends ClassValue = ClassValue> = {
|
|
72
|
+
base?: T;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* Exported so TypeScript can name this type in your generated declarations
|
|
76
|
+
* (`declaration: true`) — you shouldn't really use it directly.
|
|
77
|
+
*/
|
|
78
|
+
type CVAVariantShape = Record<string, Record<string, ClassValue>>;
|
|
79
|
+
type CVAVariantSchema<V> = { [Variant in keyof V]?: StringToBoolean<keyof V[Variant]> | undefined; };
|
|
80
|
+
type CVAClassProp<T extends ClassValue = ClassValue> = {
|
|
81
|
+
class?: T;
|
|
82
|
+
className?: never;
|
|
83
|
+
} | {
|
|
84
|
+
class?: never;
|
|
85
|
+
className?: T;
|
|
86
|
+
};
|
|
87
|
+
type InternalOnlyWarning = "cva's generic parameters are restricted to internal use only.";
|
|
88
|
+
type CVAComponentConfig<Config, Variants, ComposedSingle extends CVAComponentShape | undefined = CVAComponentShape | undefined, ComposedList extends readonly CVAComponentShape[] = readonly CVAComponentShape[], T extends ClassValue = ClassValue, Merged = Variants> = Config & {
|
|
89
|
+
composes?: ComposedSingle | readonly [...ComposedList];
|
|
90
|
+
} & (Variants extends Record<string, Record<string, T>> ? CVAComponentConfigBase<T> & {
|
|
91
|
+
variants?: Variants;
|
|
92
|
+
} : CVAComponentConfigBase<T> & {
|
|
93
|
+
variants?: never;
|
|
94
|
+
}) & ([keyof Merged] extends [never] ? {
|
|
95
|
+
compoundVariants?: never;
|
|
96
|
+
defaultVariants?: never;
|
|
97
|
+
} : {
|
|
98
|
+
compoundVariants?: ((CVAVariantSchema<Uninferred<Merged>> | { [Variant in keyof Uninferred<Merged>]?: StringToBoolean<keyof Uninferred<Merged>[Variant]> | StringToBoolean<keyof Uninferred<Merged>[Variant]>[] | undefined; }) & CVAClassProp<T>)[];
|
|
99
|
+
defaultVariants?: CVAVariantSchema<Uninferred<Merged>>;
|
|
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
|
+
interface CVAComponent<Config, Variants, T extends ClassValue = ClassValue> {
|
|
106
|
+
(props?: Variants extends CVAVariantShape ? CVAVariantSchema<Variants> & CVAClassProp<T> : CVAClassProp<T>): string;
|
|
107
|
+
/** @internal */
|
|
108
|
+
config: Config;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Exported so TypeScript can name this type in your generated declarations
|
|
112
|
+
* (`declaration: true`) — you shouldn't really use it directly.
|
|
113
|
+
*/
|
|
114
|
+
type CVAComponentShape = CVAComponent<any, any, any>;
|
|
115
|
+
type CVADefaultVariants<Config> = Config extends {
|
|
116
|
+
defaultVariants?: infer D;
|
|
117
|
+
} ? D : {};
|
|
118
|
+
interface CVA<T extends ClassValue = ClassValue> {
|
|
119
|
+
<_ extends InternalOnlyWarning, Config, Variants, ComposedSingle extends CVAComponentShape | undefined = undefined, ComposedList extends readonly CVAComponentShape[] = []>(config: CVAComponentConfig<Config, Variants, ComposedSingle, ComposedList, T, Variants & MergedVariants<ComposedTuple<ComposedSingle, ComposedList>>>): CVAComponent<Omit<Config, "defaultVariants"> & {
|
|
120
|
+
variants: Variants & MergedVariants<ComposedTuple<ComposedSingle, ComposedList>>;
|
|
121
|
+
defaultVariants: Omit<MergedDefaultVariants<ComposedTuple<ComposedSingle, ComposedList>>, keyof CVADefaultVariants<Config>> & CVADefaultVariants<Config>;
|
|
122
|
+
}, Variants & MergedVariants<ComposedTuple<ComposedSingle, ComposedList>>, T>;
|
|
123
|
+
}
|
|
124
|
+
interface DefineConfigOptions<TCX extends AnyCX = CX> {
|
|
125
|
+
/**
|
|
126
|
+
* The class name concatenator used by `cva`, `cx`, and `compose`. It owns
|
|
127
|
+
* the class name grammar entirely: cva assembles the authored values
|
|
128
|
+
* (composed component outputs, `base`, matched variant and compound
|
|
129
|
+
* variant values, `class`/`className`) and passes them through verbatim,
|
|
130
|
+
* one argument each, without interpreting them.
|
|
131
|
+
*
|
|
132
|
+
* The authoring surface adopts the concatenator's own input type
|
|
133
|
+
* automatically (see {@link CXInput}): pass `twMerge` and your variants
|
|
134
|
+
* are checked against tailwind-merge's `ClassNameValue`; pass `clsx` (or
|
|
135
|
+
* any function whose parameters don't narrow further) and you keep the
|
|
136
|
+
* full clsx-flavored `ClassValue` grammar.
|
|
137
|
+
*/
|
|
138
|
+
cx: TCX & CXConstraint<TCX>;
|
|
139
|
+
hooks?: {
|
|
140
|
+
/**
|
|
141
|
+
* @deprecated please use the `cx` option instead
|
|
142
|
+
*/
|
|
143
|
+
"cx:done"?: (className: string) => string;
|
|
144
|
+
/**
|
|
145
|
+
* @deprecated please use the `cx` option instead
|
|
146
|
+
*/
|
|
147
|
+
onComplete?: (className: string) => string;
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
interface DefineConfig {
|
|
151
|
+
<TCX extends AnyCX>(options: DefineConfigOptions<TCX>): {
|
|
152
|
+
/**
|
|
153
|
+
* @deprecated Use the `composes` property inside `cva` instead.
|
|
154
|
+
* @example
|
|
155
|
+
* // Before
|
|
156
|
+
* const card = compose(box, stack)
|
|
157
|
+
* // After
|
|
158
|
+
* const card = cva({ composes: [box, stack] })
|
|
159
|
+
*/
|
|
160
|
+
compose: Compose<CXInput<TCX>>;
|
|
161
|
+
cx: CX<CXInput<TCX>>;
|
|
162
|
+
cva: CVA<CXInput<TCX>>;
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
declare const defineConfig: DefineConfig;
|
|
166
|
+
interface GetSchema {
|
|
167
|
+
<_ extends InternalOnlyWarning, Component, Config, Variants>(component: Component & (Component extends CVAComponentShape ? Component extends {
|
|
168
|
+
config: {
|
|
169
|
+
variants: infer V;
|
|
170
|
+
};
|
|
171
|
+
} ? unknown extends V ? unknown : {
|
|
172
|
+
config: CVAComponentConfig<Config, Variants>;
|
|
173
|
+
} : {
|
|
174
|
+
config: CVAComponentConfig<Config, Variants>;
|
|
175
|
+
} : never)): { [Variant in keyof Variants as Variant extends InternalVariantKey ? never : Variant]: Config extends CVAComponentConfig<Config, Variants> ? Variant extends keyof Config["defaultVariants"] ? Config["defaultVariants"][Variant] extends undefined ? never : {
|
|
176
|
+
values: ReadonlyArray<StringToBoolean<keyof Variants[Variant]>>;
|
|
177
|
+
defaultValue: Readonly<StringToBoolean<Config["defaultVariants"][Variant]>>;
|
|
178
|
+
} : {
|
|
179
|
+
values: ReadonlyArray<StringToBoolean<keyof Variants[Variant]>>;
|
|
180
|
+
} : never; } extends (infer Schema) ? { [K in keyof Schema as Schema[K] extends {
|
|
181
|
+
values: readonly never[];
|
|
182
|
+
} ? never : K]: Schema[K] extends {
|
|
183
|
+
defaultValue: never;
|
|
184
|
+
} ? never : Schema[K]; } : never;
|
|
185
|
+
}
|
|
186
|
+
//#endregion
|
|
187
|
+
export { VariantProps as _, CVAVariantShape as a, CXOptions as c, ClassDictionary as d, ClassValue as f, GetSchema 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, defineConfig as v };
|
|
188
|
+
//# sourceMappingURL=core-BMF1ydiV.d.cts.map
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
//#region src/core.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Copyright 2022-present Joe Bell. All rights reserved.
|
|
4
|
+
*
|
|
5
|
+
* This file is licensed to you under the Apache License, Version 2.0
|
|
6
|
+
* (the "License"); you may not use this file except in compliance with the
|
|
7
|
+
* License. You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
13
|
+
* WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or implied. See the
|
|
14
|
+
* License for the specific language governing permissions and limitations under
|
|
15
|
+
* the License.
|
|
16
|
+
*/
|
|
17
|
+
type ClassValue = ClassArray | ClassDictionary | string | number | bigint | null | boolean | undefined;
|
|
18
|
+
type ClassDictionary = Record<string, any>;
|
|
19
|
+
type ClassArray = ClassValue[];
|
|
20
|
+
/**
|
|
21
|
+
* Any function usable as a `cx` concatenator.
|
|
22
|
+
*/
|
|
23
|
+
type AnyCX = (...inputs: any[]) => string;
|
|
24
|
+
type CXInputs<TCX extends AnyCX> = TCX extends ((...inputs: readonly [...infer Inputs]) => string) ? Inputs : never;
|
|
25
|
+
type CXInputElement<TCX extends AnyCX> = CXInputs<TCX>[number];
|
|
26
|
+
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;
|
|
27
|
+
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.";
|
|
28
|
+
/**
|
|
29
|
+
* The class value type a concatenator accepts, inferred from its
|
|
30
|
+
* parameters — `defineConfig` uses this to type the authoring surface
|
|
31
|
+
* (`base`, variant values, `class`/`className`) against the configured
|
|
32
|
+
* concatenator's own input grammar.
|
|
33
|
+
*/
|
|
34
|
+
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;
|
|
35
|
+
type OmitUndefined<T> = T extends undefined ? never : T;
|
|
36
|
+
type Uninferred<T> = [T][T extends any ? 0 : never];
|
|
37
|
+
type StringToBoolean<T> = T extends "true" | "false" ? boolean : T;
|
|
38
|
+
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
|
|
39
|
+
type ComposedTuple<S extends CVAComponentShape | undefined, L extends readonly CVAComponentShape[]> = [S] extends [CVAComponentShape] ? [S] : L;
|
|
40
|
+
type MergedVariants<T extends readonly unknown[]> = UnionToIntersection<{ [K in keyof T]: T[K] extends {
|
|
41
|
+
config: {
|
|
42
|
+
variants?: infer V extends CVAVariantShape;
|
|
43
|
+
};
|
|
44
|
+
} ? V : never; }[number]>;
|
|
45
|
+
type RightMerge<A, B> = { [K in keyof A | keyof B]: K extends keyof B ? B[K] : K extends keyof A ? A[K] : never; };
|
|
46
|
+
type DefaultsOf<Component> = Component extends {
|
|
47
|
+
config: {
|
|
48
|
+
defaultVariants?: infer D;
|
|
49
|
+
};
|
|
50
|
+
} ? D extends undefined ? {} : D : {};
|
|
51
|
+
type MergedDefaultVariants<T extends readonly unknown[]> = T extends readonly [infer Head, ...infer Rest] ? RightMerge<DefaultsOf<Head>, MergedDefaultVariants<Rest>> : {};
|
|
52
|
+
type ComponentProps<Component extends (...args: any) => any> = Omit<OmitUndefined<Parameters<Component>[0]>, "class" | "className">;
|
|
53
|
+
type InternalVariantKey = `_${string}`;
|
|
54
|
+
type VariantProps<Component extends (...args: any) => any> = Omit<ComponentProps<Component>, InternalVariantKey>;
|
|
55
|
+
/**
|
|
56
|
+
* @deprecated Use the `composes` property inside `cva` instead.
|
|
57
|
+
* @example
|
|
58
|
+
* // Before
|
|
59
|
+
* const card = compose(box, stack)
|
|
60
|
+
* // After
|
|
61
|
+
* const card = cva({ composes: [box, stack] })
|
|
62
|
+
*/
|
|
63
|
+
interface Compose<T extends ClassValue = ClassValue> {
|
|
64
|
+
<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;
|
|
65
|
+
}
|
|
66
|
+
interface CX<T extends ClassValue = ClassValue> {
|
|
67
|
+
(...inputs: T[]): string;
|
|
68
|
+
}
|
|
69
|
+
type CXOptions = Parameters<CX>;
|
|
70
|
+
type CXReturn = ReturnType<CX>;
|
|
71
|
+
type CVAComponentConfigBase<T extends ClassValue = ClassValue> = {
|
|
72
|
+
base?: T;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* Exported so TypeScript can name this type in your generated declarations
|
|
76
|
+
* (`declaration: true`) — you shouldn't really use it directly.
|
|
77
|
+
*/
|
|
78
|
+
type CVAVariantShape = Record<string, Record<string, ClassValue>>;
|
|
79
|
+
type CVAVariantSchema<V> = { [Variant in keyof V]?: StringToBoolean<keyof V[Variant]> | undefined; };
|
|
80
|
+
type CVAClassProp<T extends ClassValue = ClassValue> = {
|
|
81
|
+
class?: T;
|
|
82
|
+
className?: never;
|
|
83
|
+
} | {
|
|
84
|
+
class?: never;
|
|
85
|
+
className?: T;
|
|
86
|
+
};
|
|
87
|
+
type InternalOnlyWarning = "cva's generic parameters are restricted to internal use only.";
|
|
88
|
+
type CVAComponentConfig<Config, Variants, ComposedSingle extends CVAComponentShape | undefined = CVAComponentShape | undefined, ComposedList extends readonly CVAComponentShape[] = readonly CVAComponentShape[], T extends ClassValue = ClassValue, Merged = Variants> = Config & {
|
|
89
|
+
composes?: ComposedSingle | readonly [...ComposedList];
|
|
90
|
+
} & (Variants extends Record<string, Record<string, T>> ? CVAComponentConfigBase<T> & {
|
|
91
|
+
variants?: Variants;
|
|
92
|
+
} : CVAComponentConfigBase<T> & {
|
|
93
|
+
variants?: never;
|
|
94
|
+
}) & ([keyof Merged] extends [never] ? {
|
|
95
|
+
compoundVariants?: never;
|
|
96
|
+
defaultVariants?: never;
|
|
97
|
+
} : {
|
|
98
|
+
compoundVariants?: ((CVAVariantSchema<Uninferred<Merged>> | { [Variant in keyof Uninferred<Merged>]?: StringToBoolean<keyof Uninferred<Merged>[Variant]> | StringToBoolean<keyof Uninferred<Merged>[Variant]>[] | undefined; }) & CVAClassProp<T>)[];
|
|
99
|
+
defaultVariants?: CVAVariantSchema<Uninferred<Merged>>;
|
|
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
|
+
interface CVAComponent<Config, Variants, T extends ClassValue = ClassValue> {
|
|
106
|
+
(props?: Variants extends CVAVariantShape ? CVAVariantSchema<Variants> & CVAClassProp<T> : CVAClassProp<T>): string;
|
|
107
|
+
/** @internal */
|
|
108
|
+
config: Config;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Exported so TypeScript can name this type in your generated declarations
|
|
112
|
+
* (`declaration: true`) — you shouldn't really use it directly.
|
|
113
|
+
*/
|
|
114
|
+
type CVAComponentShape = CVAComponent<any, any, any>;
|
|
115
|
+
type CVADefaultVariants<Config> = Config extends {
|
|
116
|
+
defaultVariants?: infer D;
|
|
117
|
+
} ? D : {};
|
|
118
|
+
interface CVA<T extends ClassValue = ClassValue> {
|
|
119
|
+
<_ extends InternalOnlyWarning, Config, Variants, ComposedSingle extends CVAComponentShape | undefined = undefined, ComposedList extends readonly CVAComponentShape[] = []>(config: CVAComponentConfig<Config, Variants, ComposedSingle, ComposedList, T, Variants & MergedVariants<ComposedTuple<ComposedSingle, ComposedList>>>): CVAComponent<Omit<Config, "defaultVariants"> & {
|
|
120
|
+
variants: Variants & MergedVariants<ComposedTuple<ComposedSingle, ComposedList>>;
|
|
121
|
+
defaultVariants: Omit<MergedDefaultVariants<ComposedTuple<ComposedSingle, ComposedList>>, keyof CVADefaultVariants<Config>> & CVADefaultVariants<Config>;
|
|
122
|
+
}, Variants & MergedVariants<ComposedTuple<ComposedSingle, ComposedList>>, T>;
|
|
123
|
+
}
|
|
124
|
+
interface DefineConfigOptions<TCX extends AnyCX = CX> {
|
|
125
|
+
/**
|
|
126
|
+
* The class name concatenator used by `cva`, `cx`, and `compose`. It owns
|
|
127
|
+
* the class name grammar entirely: cva assembles the authored values
|
|
128
|
+
* (composed component outputs, `base`, matched variant and compound
|
|
129
|
+
* variant values, `class`/`className`) and passes them through verbatim,
|
|
130
|
+
* one argument each, without interpreting them.
|
|
131
|
+
*
|
|
132
|
+
* The authoring surface adopts the concatenator's own input type
|
|
133
|
+
* automatically (see {@link CXInput}): pass `twMerge` and your variants
|
|
134
|
+
* are checked against tailwind-merge's `ClassNameValue`; pass `clsx` (or
|
|
135
|
+
* any function whose parameters don't narrow further) and you keep the
|
|
136
|
+
* full clsx-flavored `ClassValue` grammar.
|
|
137
|
+
*/
|
|
138
|
+
cx: TCX & CXConstraint<TCX>;
|
|
139
|
+
hooks?: {
|
|
140
|
+
/**
|
|
141
|
+
* @deprecated please use the `cx` option instead
|
|
142
|
+
*/
|
|
143
|
+
"cx:done"?: (className: string) => string;
|
|
144
|
+
/**
|
|
145
|
+
* @deprecated please use the `cx` option instead
|
|
146
|
+
*/
|
|
147
|
+
onComplete?: (className: string) => string;
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
interface DefineConfig {
|
|
151
|
+
<TCX extends AnyCX>(options: DefineConfigOptions<TCX>): {
|
|
152
|
+
/**
|
|
153
|
+
* @deprecated Use the `composes` property inside `cva` instead.
|
|
154
|
+
* @example
|
|
155
|
+
* // Before
|
|
156
|
+
* const card = compose(box, stack)
|
|
157
|
+
* // After
|
|
158
|
+
* const card = cva({ composes: [box, stack] })
|
|
159
|
+
*/
|
|
160
|
+
compose: Compose<CXInput<TCX>>;
|
|
161
|
+
cx: CX<CXInput<TCX>>;
|
|
162
|
+
cva: CVA<CXInput<TCX>>;
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
declare const defineConfig: DefineConfig;
|
|
166
|
+
interface GetSchema {
|
|
167
|
+
<_ extends InternalOnlyWarning, Component, Config, Variants>(component: Component & (Component extends CVAComponentShape ? Component extends {
|
|
168
|
+
config: {
|
|
169
|
+
variants: infer V;
|
|
170
|
+
};
|
|
171
|
+
} ? unknown extends V ? unknown : {
|
|
172
|
+
config: CVAComponentConfig<Config, Variants>;
|
|
173
|
+
} : {
|
|
174
|
+
config: CVAComponentConfig<Config, Variants>;
|
|
175
|
+
} : never)): { [Variant in keyof Variants as Variant extends InternalVariantKey ? never : Variant]: Config extends CVAComponentConfig<Config, Variants> ? Variant extends keyof Config["defaultVariants"] ? Config["defaultVariants"][Variant] extends undefined ? never : {
|
|
176
|
+
values: ReadonlyArray<StringToBoolean<keyof Variants[Variant]>>;
|
|
177
|
+
defaultValue: Readonly<StringToBoolean<Config["defaultVariants"][Variant]>>;
|
|
178
|
+
} : {
|
|
179
|
+
values: ReadonlyArray<StringToBoolean<keyof Variants[Variant]>>;
|
|
180
|
+
} : never; } extends (infer Schema) ? { [K in keyof Schema as Schema[K] extends {
|
|
181
|
+
values: readonly never[];
|
|
182
|
+
} ? never : K]: Schema[K] extends {
|
|
183
|
+
defaultValue: never;
|
|
184
|
+
} ? never : Schema[K]; } : never;
|
|
185
|
+
}
|
|
186
|
+
//#endregion
|
|
187
|
+
export { VariantProps as _, CVAVariantShape as a, CXOptions as c, ClassDictionary as d, ClassValue as f, GetSchema 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, defineConfig as v };
|
|
188
|
+
//# sourceMappingURL=core-BMF1ydiV.d.mts.map
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
//#region src/core.ts
|
|
2
|
+
const falsyToString = (value) => typeof value === "boolean" ? `${value}` : value === 0 ? "0" : value;
|
|
3
|
+
const emptyClassNames = [];
|
|
4
|
+
const defineConfig = ((options) => {
|
|
5
|
+
const cx = (...inputs) => {
|
|
6
|
+
var _options$hooks$cxDon, _options$hooks, _options$hooks2;
|
|
7
|
+
const className = options.cx(...inputs.filter((input) => input !== void 0));
|
|
8
|
+
const hook = (_options$hooks$cxDon = (_options$hooks = options.hooks) === null || _options$hooks === void 0 ? void 0 : _options$hooks["cx:done"]) !== null && _options$hooks$cxDon !== void 0 ? _options$hooks$cxDon : (_options$hooks2 = options.hooks) === null || _options$hooks2 === void 0 ? void 0 : _options$hooks2.onComplete;
|
|
9
|
+
return hook ? hook(className) : className;
|
|
10
|
+
};
|
|
11
|
+
const cva = ((config) => {
|
|
12
|
+
const components = (config === null || config === void 0 ? void 0 : config.composes) == null ? [] : Array.isArray(config.composes) ? config.composes : [config.composes];
|
|
13
|
+
const mergeVariants = (acc, variants) => {
|
|
14
|
+
if (!variants) return acc;
|
|
15
|
+
const merged = { ...acc };
|
|
16
|
+
for (const key of Object.keys(variants)) merged[key] = {
|
|
17
|
+
...merged[key],
|
|
18
|
+
...variants[key]
|
|
19
|
+
};
|
|
20
|
+
return merged;
|
|
21
|
+
};
|
|
22
|
+
const mergedVariants = mergeVariants(components.reduce((acc, component) => {
|
|
23
|
+
var _component$config;
|
|
24
|
+
return mergeVariants(acc, (_component$config = component.config) === null || _component$config === void 0 ? void 0 : _component$config.variants);
|
|
25
|
+
}, {}), config === null || config === void 0 ? void 0 : config.variants);
|
|
26
|
+
const mergedDefaultVariants = {
|
|
27
|
+
...components.reduce((acc, component) => {
|
|
28
|
+
var _component$config2;
|
|
29
|
+
return {
|
|
30
|
+
...acc,
|
|
31
|
+
...(_component$config2 = component.config) === null || _component$config2 === void 0 ? void 0 : _component$config2.defaultVariants
|
|
32
|
+
};
|
|
33
|
+
}, {}),
|
|
34
|
+
...config === null || config === void 0 ? void 0 : config.defaultVariants
|
|
35
|
+
};
|
|
36
|
+
const component = (props) => {
|
|
37
|
+
var _config$variants, _config$compoundVaria, _config$compoundVaria2;
|
|
38
|
+
const definedPropsWithoutClass = components.length || (config === null || config === void 0 ? void 0 : config.compoundVariants) ? Object.fromEntries(Object.entries(props || {}).filter(([key, value]) => key !== "class" && key !== "className" && typeof value !== "undefined")) : {};
|
|
39
|
+
const getComposedClassNames = components.length ? components.map((component) => component({
|
|
40
|
+
...mergedDefaultVariants,
|
|
41
|
+
...definedPropsWithoutClass
|
|
42
|
+
})) : emptyClassNames;
|
|
43
|
+
if (!(config === null || config === void 0 ? void 0 : config.variants) && !(config === null || config === void 0 ? void 0 : config.compoundVariants)) return getComposedClassNames.length ? cx(...getComposedClassNames, config === null || config === void 0 ? void 0 : config.base, props === null || props === void 0 ? void 0 : props.class, props === null || props === void 0 ? void 0 : props.className) : cx(config === null || config === void 0 ? void 0 : config.base, props === null || props === void 0 ? void 0 : props.class, props === null || props === void 0 ? void 0 : props.className);
|
|
44
|
+
const variants = (_config$variants = config.variants) !== null && _config$variants !== void 0 ? _config$variants : {};
|
|
45
|
+
const getVariantClassNames = Object.keys(variants).map((variant) => {
|
|
46
|
+
const variantProp = props === null || props === void 0 ? void 0 : props[variant];
|
|
47
|
+
const defaultVariantProp = mergedDefaultVariants[variant];
|
|
48
|
+
const variantKey = falsyToString(variantProp) || falsyToString(defaultVariantProp);
|
|
49
|
+
return variants[variant][variantKey];
|
|
50
|
+
});
|
|
51
|
+
const defaultsAndProps = {
|
|
52
|
+
...mergedDefaultVariants,
|
|
53
|
+
...definedPropsWithoutClass
|
|
54
|
+
};
|
|
55
|
+
const getCompoundVariantClassNames = (_config$compoundVaria = config === null || config === void 0 || (_config$compoundVaria2 = config.compoundVariants) === null || _config$compoundVaria2 === void 0 ? void 0 : _config$compoundVaria2.reduce((acc, { class: cvClass, className: cvClassName, ...cvConfig }) => Object.entries(cvConfig).every(([cvKey, cvSelector]) => {
|
|
56
|
+
const selector = defaultsAndProps[cvKey];
|
|
57
|
+
return Array.isArray(cvSelector) ? cvSelector.includes(selector) : selector === cvSelector;
|
|
58
|
+
}) ? [
|
|
59
|
+
...acc,
|
|
60
|
+
cvClass,
|
|
61
|
+
cvClassName
|
|
62
|
+
] : acc, [])) !== null && _config$compoundVaria !== void 0 ? _config$compoundVaria : emptyClassNames;
|
|
63
|
+
return cx(...getComposedClassNames, config === null || config === void 0 ? void 0 : config.base, ...getVariantClassNames, ...getCompoundVariantClassNames, props === null || props === void 0 ? void 0 : props.class, props === null || props === void 0 ? void 0 : props.className);
|
|
64
|
+
};
|
|
65
|
+
component.config = {
|
|
66
|
+
...config,
|
|
67
|
+
variants: mergedVariants,
|
|
68
|
+
defaultVariants: mergedDefaultVariants
|
|
69
|
+
};
|
|
70
|
+
return component;
|
|
71
|
+
});
|
|
72
|
+
const compose = (...components) => {
|
|
73
|
+
const composedComponents = components;
|
|
74
|
+
const config = composedComponents.reduce((acc, { config }) => {
|
|
75
|
+
Object.entries(config || {}).forEach(([key, value]) => {
|
|
76
|
+
acc[key] = typeof value === "object" && value !== null && !Array.isArray(value) ? {
|
|
77
|
+
...acc[key],
|
|
78
|
+
...value
|
|
79
|
+
} : value;
|
|
80
|
+
});
|
|
81
|
+
return acc;
|
|
82
|
+
}, {});
|
|
83
|
+
const component = (props) => {
|
|
84
|
+
const propsWithoutClass = Object.fromEntries(Object.entries(props || {}).filter(([key]) => !["class", "className"].includes(key)));
|
|
85
|
+
return cx(...composedComponents.map((component) => component(propsWithoutClass)), props === null || props === void 0 ? void 0 : props.class, props === null || props === void 0 ? void 0 : props.className);
|
|
86
|
+
};
|
|
87
|
+
component.config = config;
|
|
88
|
+
return component;
|
|
89
|
+
};
|
|
90
|
+
return {
|
|
91
|
+
compose,
|
|
92
|
+
cva,
|
|
93
|
+
cx
|
|
94
|
+
};
|
|
95
|
+
});
|
|
96
|
+
//#endregion
|
|
97
|
+
export { defineConfig as t };
|
|
98
|
+
|
|
99
|
+
//# sourceMappingURL=core-DPc9m0ph.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core-DPc9m0ph.mjs","names":[],"sources":["../src/core.ts"],"sourcesContent":["/**\n * Copyright 2022-present Joe Bell. All rights reserved.\n *\n * This file is licensed to you under the Apache License, Version 2.0\n * (the \"License\"); you may not use this file except in compliance with the\n * License. You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n * WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or implied. See the\n * License for the specific language governing permissions and limitations under\n * the License.\n */\n\n/* Types\n ============================================ */\n\n/* ClassValue\n ---------------------------------- */\n\n// Mirrors clsx's `ClassValue`; local to keep `core` dependency-free (TS2742).\n\nexport type ClassValue =\n | ClassArray\n | ClassDictionary\n | string\n | number\n | bigint\n | null\n | boolean\n | undefined;\nexport type ClassDictionary = Record<string, any>;\nexport type ClassArray = ClassValue[];\n\n/* CXInput\n ---------------------------------- */\n\n/**\n * Any function usable as a `cx` concatenator.\n */\nexport type AnyCX = (...inputs: any[]) => string;\n\n// Infer directly from a readonly rest pattern instead of `Parameters<T>[number]`:\n// the latter is `never` for zero-argument callbacks and loses readonly rest\n// element types.\ntype CXInputs<TCX extends AnyCX> = TCX extends (\n ...inputs: readonly [...infer Inputs]\n) => string\n ? Inputs\n : never;\ntype CXInputElement<TCX extends AnyCX> = CXInputs<TCX>[number];\n\n// cva may call `cx` with no values, or with any number of authored values and\n// composed component strings. A constant callback is safe; every other\n// callback must accept arbitrary lists of its own grammar plus strings.\ntype CXHasSafeArity<TCX extends AnyCX> =\n CXInputs<TCX> extends infer Inputs\n ? Inputs extends readonly unknown[]\n ? Inputs extends []\n ? true\n : [] extends Inputs\n ? number extends Inputs[\"length\"]\n ? true\n : false\n : false\n : false\n : false;\ntype CXConstraint<TCX extends AnyCX> =\n false extends CXHasSafeArity<TCX>\n ? \"cva's cx must accept zero arguments and an unbounded rest parameter.\"\n : [TCX] extends [(...inputs: (string | CXInput<TCX>)[]) => string]\n ? unknown\n : \"cva's cx must accept its inferred class values and composed strings.\";\n\n/**\n * The class value type a concatenator accepts, inferred from its\n * parameters — `defineConfig` uses this to type the authoring surface\n * (`base`, variant values, `class`/`className`) against the configured\n * concatenator's own input grammar.\n */\nexport type CXInput<TCX extends AnyCX> =\n CXInputElement<TCX> extends infer P\n ? // `0 extends 1 & P` detects `any`; `any`/`never` use `ClassValue`.\n 0 extends 1 & P\n ? ClassValue\n : [P] extends [never]\n ? ClassValue\n : [P] extends [ClassValue]\n ? P\n : // A concatenator accepting more than cva's grammar (e.g. `string |\n // URL`) narrows to the subset it shares with it, since the surface\n // can't widen beyond what the concatenator accepts; when nothing is\n // shared (e.g. `unknown`), the full grammar applies.\n [Extract<P, ClassValue>] extends [never]\n ? ClassValue\n : Extract<P, ClassValue>\n : ClassValue;\n\n/* Utils\n ---------------------------------- */\n\ntype OmitUndefined<T> = T extends undefined ? never : T;\n// Blocks inference from a site that merely checks a type parameter.\ntype Uninferred<T> = [T][T extends any ? 0 : never];\ntype StringToBoolean<T> = T extends \"true\" | \"false\" ? boolean : T;\ntype UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (\n k: infer I,\n) => void\n ? I\n : never;\n\n// `composes` accepts either a single component or a list of components. A\n// plain union (`CVAComponentShape | CVAComponentShape[]`) collapses an array\n// literal's element type to a union, silently dropping components whose\n// variants are a structural subtype of another composed component's (e.g.\n// `composes: [a, b]` where `b`'s variants are a superset of `a`'s). Splitting\n// inference across two type parameters preserves the array as a real tuple.\ntype ComposedTuple<\n S extends CVAComponentShape | undefined,\n L extends readonly CVAComponentShape[],\n> = [S] extends [CVAComponentShape] ? [S] : L;\n\ntype MergedVariants<T extends readonly unknown[]> = UnionToIntersection<\n {\n [K in keyof T]: T[K] extends {\n config: { variants?: infer V extends CVAVariantShape };\n }\n ? V\n : never;\n }[number]\n>;\n\n// Right-biased merge (`B`'s keys win on conflicts) implemented as a mapped\n// type rather than `Omit<A, keyof B> & B`: the latter stays an unresolved\n// deferred type when `A`/`B` are themselves generic (as they are here, via\n// `ReturnType<CVA>` with no concrete `Config`), which then breaks downstream\n// `any`-narrowing in unrelated code that consumes `ReturnType<CVA>` (e.g.\n// the deprecated `compose`). A mapped type resolves eagerly instead.\ntype RightMerge<A, B> = {\n [K in keyof A | keyof B]: K extends keyof B\n ? B[K]\n : K extends keyof A\n ? A[K]\n : never;\n};\n\n// `D` infers as `undefined` (not absent) when a component declares no\n// `defaultVariants` at all. `NonNullable<undefined>` would give `never`,\n// and `keyof never` is `string | number | symbol` (not `never`) — poisoning\n// `RightMerge`'s key union with every possible key. Normalize to `{}` instead,\n// matching a component that contributes nothing to the merge.\ntype DefaultsOf<Component> = Component extends {\n config: { defaultVariants?: infer D };\n}\n ? D extends undefined\n ? {}\n : D\n : {};\n\ntype MergedDefaultVariants<T extends readonly unknown[]> = T extends readonly [\n infer Head,\n ...infer Rest,\n]\n ? RightMerge<DefaultsOf<Head>, MergedDefaultVariants<Rest>>\n : {};\n\ntype ComponentProps<Component extends (...args: any) => any> = Omit<\n OmitUndefined<Parameters<Component>[0]>,\n \"class\" | \"className\"\n>;\n\n// A variant name prefixed with `_` is internal: the component still accepts\n// it, but `VariantProps` and `getSchema` omit it from the public surface.\ntype InternalVariantKey = `_${string}`;\n\nexport type VariantProps<Component extends (...args: any) => any> = Omit<\n ComponentProps<Component>,\n InternalVariantKey\n>;\n\n/* compose\n ---------------------------------- */\n\n/**\n * @deprecated Use the `composes` property inside `cva` instead.\n * @example\n * // Before\n * const card = compose(box, stack)\n * // After\n * const card = cva({ composes: [box, stack] })\n */\nexport interface Compose<T extends ClassValue = ClassValue> {\n <Components extends readonly unknown[]>(\n ...components: Components &\n (Components[number] extends CVAComponentShape ? unknown : never)\n ): (\n props?: (\n | UnionToIntersection<\n {\n [K in keyof Components]: Components[K] extends CVAComponentShape\n ? ComponentProps<Components[K]>\n : never;\n }[number]\n >\n | undefined\n ) &\n CVAClassProp<T>,\n ) => string;\n}\n\n/* cx\n ---------------------------------- */\n\nexport interface CX<T extends ClassValue = ClassValue> {\n (...inputs: T[]): string;\n}\n\nexport type CXOptions = Parameters<CX>;\nexport type CXReturn = ReturnType<CX>;\n\n/* cva\n ============================================ */\n\ntype CVAComponentConfigBase<T extends ClassValue = ClassValue> = { base?: T };\n/**\n * Exported so TypeScript can name this type in your generated declarations\n * (`declaration: true`) — you shouldn't really use it directly.\n */\nexport type CVAVariantShape = Record<string, Record<string, ClassValue>>;\n// Unconstrained so it can map over the merged (local plus composed)\n// variants, which is only known to be an intersection, not a\n// `CVAVariantShape`.\ntype CVAVariantSchema<V> = {\n [Variant in keyof V]?: StringToBoolean<keyof V[Variant]> | undefined;\n};\ntype CVAClassProp<T extends ClassValue = ClassValue> =\n | {\n class?: T;\n className?: never;\n }\n | {\n class?: never;\n className?: T;\n };\n\ntype InternalOnlyWarning =\n \"cva's generic parameters are restricted to internal use only.\";\n\ntype CVAComponentConfig<\n Config,\n Variants,\n ComposedSingle extends CVAComponentShape | undefined =\n | CVAComponentShape\n | undefined,\n ComposedList extends readonly CVAComponentShape[] =\n readonly CVAComponentShape[],\n T extends ClassValue = ClassValue,\n // `defaultVariants` and `compoundVariants` are checked against local variants\n // merged with composed variants. Defaults to local variants for introspection\n // guards.\n //\n // Gate on `keyof Merged` to preserve literal defaults, and wrap in\n // `Uninferred` to prevent reverse inference from authored values.\n Merged = Variants,\n> = Config & {\n composes?: ComposedSingle | readonly [...ComposedList];\n // The gate checks variant values against the configured concatenator's\n // input type, so e.g. object syntax fails here (on the `variants` key)\n // under a concatenator that doesn't accept objects.\n} & (Variants extends Record<string, Record<string, T>>\n ? CVAComponentConfigBase<T> & { variants?: Variants }\n : CVAComponentConfigBase<T> & { variants?: never }) &\n ([keyof Merged] extends [never]\n ? { compoundVariants?: never; defaultVariants?: never }\n : {\n compoundVariants?: ((\n | CVAVariantSchema<Uninferred<Merged>>\n | {\n [Variant in keyof Uninferred<Merged>]?:\n | StringToBoolean<keyof Uninferred<Merged>[Variant]>\n | StringToBoolean<keyof Uninferred<Merged>[Variant]>[]\n | undefined;\n }\n ) &\n CVAClassProp<T>)[];\n defaultVariants?: CVAVariantSchema<Uninferred<Merged>>;\n });\n\n/**\n * Exported so TypeScript can name this type in your generated declarations\n * (`declaration: true`) — you shouldn't really use it directly.\n */\nexport interface CVAComponent<\n Config,\n Variants,\n T extends ClassValue = ClassValue,\n> {\n (\n props?: Variants extends CVAVariantShape\n ? CVAVariantSchema<Variants> & CVAClassProp<T>\n : CVAClassProp<T>,\n ): string;\n /** @internal */\n config: Config;\n}\n\n// The loosest form a composable component can take, constraining `composes`\n// and the composition merge helpers above. Deriving it from `CVAComponent`\n// keeps the two from drifting: instantiated with `any`, the props conditional\n// and `config` both collapse to `any` (mapped types over `any` are `any`),\n// i.e. `{ (props?: any): string; config: any }`. The required `config`\n// property is what rejects plain functions and (deprecated) `compose`\n// results.\n//\n// The `any` arguments are deliberate, not lazy typing — a shaped `config`\n// (e.g. `{ variants?: CVAVariantShape }`) was tried and verifiably breaks:\n// a variant-less `cva({ base })` carries `variants: unknown`, and\n// `ReturnType<CVA>` instantiates this constraint inside the\n// `Compose`/`GetSchema` guards, where the shaped form rejects every real\n// component via props contravariance.\n//\n// Its class-value parameter must be `any`: narrowed components otherwise fail\n// props contravariance in `composes` and `getSchema`.\n/**\n * Exported so TypeScript can name this type in your generated declarations\n * (`declaration: true`) — you shouldn't really use it directly.\n */\nexport type CVAComponentShape = CVAComponent<any, any, any>;\n\ntype CVADefaultVariants<Config> = Config extends { defaultVariants?: infer D }\n ? D\n : {};\n\nexport interface CVA<T extends ClassValue = ClassValue> {\n <\n _ extends InternalOnlyWarning,\n Config,\n Variants,\n ComposedSingle extends CVAComponentShape | undefined = undefined,\n ComposedList extends readonly CVAComponentShape[] = [],\n >(\n config: CVAComponentConfig<\n Config,\n Variants,\n ComposedSingle,\n ComposedList,\n T,\n Variants & MergedVariants<ComposedTuple<ComposedSingle, ComposedList>>\n >,\n ): CVAComponent<\n Omit<Config, \"defaultVariants\"> & {\n variants: Variants &\n MergedVariants<ComposedTuple<ComposedSingle, ComposedList>>;\n // Local `defaultVariants` win over composed ones on key conflicts,\n // matching the runtime spread order. A plain intersection would collapse\n // a conflicting key's value to `never` (e.g. `\"sm\" & \"lg\"`), which then\n // silently drops the variant from `getSchema`'s inferred type.\n defaultVariants: Omit<\n MergedDefaultVariants<ComposedTuple<ComposedSingle, ComposedList>>,\n keyof CVADefaultVariants<Config>\n > &\n CVADefaultVariants<Config>;\n },\n Variants & MergedVariants<ComposedTuple<ComposedSingle, ComposedList>>,\n T\n >;\n}\n\n/* defineConfig\n ---------------------------------- */\n\nexport interface DefineConfigOptions<TCX extends AnyCX = CX> {\n /**\n * The class name concatenator used by `cva`, `cx`, and `compose`. It owns\n * the class name grammar entirely: cva assembles the authored values\n * (composed component outputs, `base`, matched variant and compound\n * variant values, `class`/`className`) and passes them through verbatim,\n * one argument each, without interpreting them.\n *\n * The authoring surface adopts the concatenator's own input type\n * automatically (see {@link CXInput}): pass `twMerge` and your variants\n * are checked against tailwind-merge's `ClassNameValue`; pass `clsx` (or\n * any function whose parameters don't narrow further) and you keep the\n * full clsx-flavored `ClassValue` grammar.\n */\n cx: TCX & CXConstraint<TCX>;\n hooks?: {\n /**\n * @deprecated please use the `cx` option instead\n */\n \"cx:done\"?: (className: string) => string;\n /**\n * @deprecated please use the `cx` option instead\n */\n onComplete?: (className: string) => string;\n };\n}\n\nexport interface DefineConfig {\n <TCX extends AnyCX>(\n options: DefineConfigOptions<TCX>,\n ): {\n /**\n * @deprecated Use the `composes` property inside `cva` instead.\n * @example\n * // Before\n * const card = compose(box, stack)\n * // After\n * const card = cva({ composes: [box, stack] })\n */\n compose: Compose<CXInput<TCX>>;\n cx: CX<CXInput<TCX>>;\n cva: CVA<CXInput<TCX>>;\n };\n}\n\n/* Exports\n ============================================ */\n\nconst falsyToString = <T extends unknown>(value: T) =>\n typeof value === \"boolean\" ? `${value}` : value === 0 ? \"0\" : value;\n\n// Shared across every non-composed call, rather than allocating a fresh `[]`\n// per call — spreading an empty array contributes no arguments.\nconst emptyClassNames: string[] = [];\n\n// Cast to `DefineConfig`: runtime uses `ClassValue`; `CXInput` is type-only.\nexport const defineConfig = ((options: DefineConfigOptions) => {\n const cx: CX = (...inputs) => {\n // Drop absent values so a narrower concatenator never receives `undefined`.\n const className = options.cx(\n ...inputs.filter((input) => input !== undefined),\n );\n\n const hook = options.hooks?.[\"cx:done\"] ?? options.hooks?.onComplete;\n return hook ? hook(className) : className;\n };\n\n const cva = (<\n _ extends InternalOnlyWarning,\n Config,\n Variants,\n ComposedSingle extends CVAComponentShape | undefined = undefined,\n ComposedList extends readonly CVAComponentShape[] = [],\n >(\n config: CVAComponentConfig<Config, Variants, ComposedSingle, ComposedList>,\n ) => {\n const components = (\n config?.composes == null\n ? []\n : Array.isArray(config.composes)\n ? config.composes\n : [config.composes]\n ) as CVAComponentShape[];\n // A one-level-deep merge per variant key, so overlapping variants (e.g.\n // multiple composed components declaring `style`) union their values\n // instead of the last component's values silently replacing the rest.\n const mergeVariants = (\n acc: CVAVariantShape,\n variants: CVAVariantShape | undefined,\n ): CVAVariantShape => {\n if (!variants) return acc;\n const merged: CVAVariantShape = { ...acc };\n for (const key of Object.keys(variants)) {\n merged[key] = { ...merged[key], ...variants[key] };\n }\n return merged;\n };\n const mergedVariantsFromComposed = components.reduce(\n (acc: CVAVariantShape, component: CVAComponentShape) =>\n mergeVariants(acc, component.config?.variants),\n {} as CVAVariantShape,\n );\n const mergedVariants = mergeVariants(\n mergedVariantsFromComposed,\n config?.variants as CVAVariantShape | undefined,\n );\n const mergedDefaultVariantsFromComposed = components.reduce(\n (acc: Record<string, unknown>, component: CVAComponentShape) => ({\n ...acc,\n ...component.config?.defaultVariants,\n }),\n {} as Record<string, unknown>,\n );\n // Local `defaultVariants` win over composed ones here too (last spread).\n const mergedDefaultVariants: Record<string, unknown> = {\n ...mergedDefaultVariantsFromComposed,\n ...config?.defaultVariants,\n };\n\n const component: CVAComponent<typeof config, typeof config.variants> = (\n props,\n ) => {\n // Strip `class`/`className` and explicit `undefined` from props once,\n // reused for both the composed-component calls and compound-variant\n // matching. An explicit `{ variant: undefined }` is dropped so it falls\n // back to the (possibly composed) default, matching variant resolution\n // below. Only built when something consumes it — a plain component with\n // no `composes` and no `compoundVariants` skips the work entirely.\n const definedPropsWithoutClass =\n components.length || config?.compoundVariants\n ? Object.fromEntries(\n Object.entries(props || {}).filter(\n ([key, value]) =>\n key !== \"class\" &&\n key !== \"className\" &&\n typeof value !== \"undefined\",\n ),\n )\n : {};\n\n const getComposedClassNames = components.length\n ? components.map((component: CVAComponentShape) =>\n component({\n ...mergedDefaultVariants,\n ...definedPropsWithoutClass,\n }),\n )\n : emptyClassNames;\n\n // Compound variants may target composed-only keys, so a component with\n // no local `variants` still resolves them.\n if (!config?.variants && !config?.compoundVariants) {\n return getComposedClassNames.length\n ? cx(\n ...getComposedClassNames,\n config?.base,\n props?.class,\n props?.className,\n )\n : cx(config?.base, props?.class, props?.className);\n }\n\n const variants = (config.variants ?? {}) as CVAVariantShape;\n\n // Resolve against the *merged* defaults (composed + local) so a variant\n // redeclared locally over a composed key uses the same effective default\n // the composed components and `getSchema` see.\n const getVariantClassNames = Object.keys(variants).map((variant) => {\n const variantProp = props?.[variant as keyof typeof props];\n const defaultVariantProp = mergedDefaultVariants[variant];\n\n const variantKey = (falsyToString(variantProp) ||\n falsyToString(defaultVariantProp)) as string;\n\n return variants[variant][variantKey];\n });\n\n const defaultsAndProps = {\n ...mergedDefaultVariants,\n ...definedPropsWithoutClass,\n };\n\n const getCompoundVariantClassNames =\n config?.compoundVariants?.reduce(\n (\n acc: ClassValue[],\n {\n class: cvClass,\n className: cvClassName,\n ...cvConfig\n }: CVAClassProp & Record<string, unknown>,\n ) =>\n Object.entries(cvConfig).every(([cvKey, cvSelector]) => {\n const selector =\n defaultsAndProps[cvKey as keyof typeof defaultsAndProps];\n\n return Array.isArray(cvSelector)\n ? cvSelector.includes(selector)\n : selector === cvSelector;\n })\n ? [...acc, cvClass, cvClassName]\n : acc,\n [] as ClassValue[],\n ) ?? emptyClassNames;\n\n return cx(\n ...getComposedClassNames,\n config?.base,\n ...getVariantClassNames,\n ...getCompoundVariantClassNames,\n props?.class,\n props?.className,\n );\n };\n\n component.config = {\n ...config,\n variants: mergedVariants,\n defaultVariants: mergedDefaultVariants,\n };\n\n return component as ReturnType<CVA>;\n }) as CVA;\n\n const compose: Compose = (...components) => {\n const composedComponents = components as CVAComponentShape[];\n const config = composedComponents.reduce(\n (acc, { config }) => {\n Object.entries(config || {}).forEach(([key, value]) => {\n acc[key] =\n typeof value === \"object\" && value !== null && !Array.isArray(value)\n ? {\n ...acc[key],\n ...value,\n }\n : value;\n });\n return acc;\n },\n // A loose accumulator: composed configs carry heterogeneous values\n // (base strings, variant maps, compoundVariant arrays), not just the\n // `CVAVariantShape` the merged `variants` key holds.\n {} as Record<string, any>,\n );\n\n const component: CVAComponent<typeof config, typeof config.variants> = (\n props,\n ) => {\n const propsWithoutClass = Object.fromEntries(\n Object.entries(props || {}).filter(\n ([key]) => ![\"class\", \"className\"].includes(key),\n ),\n );\n\n return cx(\n ...composedComponents.map((component) => component(propsWithoutClass)),\n props?.class,\n props?.className,\n );\n };\n\n component.config = config;\n\n return component;\n };\n\n return {\n compose,\n cva,\n cx,\n };\n}) as DefineConfig;\n\nexport interface GetSchema {\n <_ extends InternalOnlyWarning, Component, Config, Variants>(\n component: Component &\n (Component extends CVAComponentShape\n ? Component extends { config: { variants: infer V } }\n ? // A variant-less component carries `variants: unknown` and a\n // `defaultVariants: {}` that no `CVAComponentConfig` branch\n // accepts; leave `Variants` uninferred so the schema maps over\n // `keyof unknown` (`never`) to `{}`.\n unknown extends V\n ? unknown\n : { config: CVAComponentConfig<Config, Variants> }\n : { config: CVAComponentConfig<Config, Variants> }\n : never),\n ): {\n [Variant in keyof Variants as Variant extends InternalVariantKey\n ? never\n : Variant]: Config extends CVAComponentConfig<Config, Variants>\n ? Variant extends keyof Config[\"defaultVariants\"]\n ? Config[\"defaultVariants\"][Variant] extends undefined\n ? never\n : {\n values: ReadonlyArray<StringToBoolean<keyof Variants[Variant]>>;\n defaultValue: Readonly<\n StringToBoolean<Config[\"defaultVariants\"][Variant]>\n >;\n }\n : {\n values: ReadonlyArray<StringToBoolean<keyof Variants[Variant]>>;\n }\n : never;\n // Iterate over the returned schema and remove any keys that have no values\n } extends infer Schema\n ? {\n [K in keyof Schema as Schema[K] extends {\n values: readonly never[];\n }\n ? never\n : K]: Schema[K] extends { defaultValue: never } ? never : Schema[K];\n }\n : never;\n}\n"],"mappings":";AAqaA,MAAM,iBAAoC,UACxC,OAAO,UAAU,YAAY,GAAG,UAAU,UAAU,IAAI,MAAM;AAIhE,MAAM,kBAA4B,CAAC;AAGnC,MAAa,iBAAiB,YAAi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