cva 0.0.0 → 1.0.0-beta.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/LICENSE ADDED
@@ -0,0 +1,190 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ Copyright 2022 Joe Bell
179
+
180
+ Licensed under the Apache License, Version 2.0 (the "License");
181
+ you may not use this file except in compliance with the License.
182
+ You may obtain a copy of the License at
183
+
184
+ http://www.apache.org/licenses/LICENSE-2.0
185
+
186
+ Unless required by applicable law or agreed to in writing, software
187
+ distributed under the License is distributed on an "AS IS" BASIS,
188
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
189
+ See the License for the specific language governing permissions and
190
+ limitations under the License.
package/README.md CHANGED
@@ -1,7 +1,11 @@
1
- # Placeholder
1
+ > **Warning**
2
+ >
3
+ > `cva@beta` is not covered by semver, and may cause unexpected or broken behaviour. Use this package at your own risk.
4
+ >
5
+ > Please consider using `class-variance-authority` until stable.
2
6
 
3
- This is just a placeholder.
7
+ ---
4
8
 
5
- If you like the name, get in touch :)
9
+ # cva
6
10
 
7
- I'll be happy to give away the name.
11
+ For documentation, visit [beta.cva.style](https://beta.cva.style).
@@ -0,0 +1,66 @@
1
+ type ClassValue = ClassArray | ClassDictionary | string | number | null | boolean | undefined;
2
+ type ClassDictionary = Record<string, any>;
3
+ type ClassArray = ClassValue[];
4
+ type OmitUndefined<T> = T extends undefined ? never : T;
5
+ type StringToBoolean<T> = T extends "true" | "false" ? boolean : T;
6
+ type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
7
+ export type VariantProps<Component extends (...args: any) => any> = Omit<OmitUndefined<Parameters<Component>[0]>, "class" | "className">;
8
+ export interface Compose {
9
+ <T extends ReturnType<CVA>[]>(...components: [...T]): (props?: (UnionToIntersection<{
10
+ [K in keyof T]: VariantProps<T[K]>;
11
+ }[number]> | undefined) & CVAClassProp) => string;
12
+ }
13
+ export interface CX {
14
+ (...inputs: ClassValue[]): string;
15
+ }
16
+ export type CXOptions = Parameters<CX>;
17
+ export type CXReturn = ReturnType<CX>;
18
+ type CVAConfigBase = {
19
+ base?: ClassValue;
20
+ };
21
+ type CVAVariantShape = Record<string, Record<string, ClassValue>>;
22
+ type CVAVariantSchema<V extends CVAVariantShape> = {
23
+ [Variant in keyof V]?: StringToBoolean<keyof V[Variant]> | undefined;
24
+ };
25
+ type CVAClassProp = {
26
+ class?: ClassValue;
27
+ className?: never;
28
+ } | {
29
+ class?: never;
30
+ className?: ClassValue;
31
+ };
32
+ export interface CVA {
33
+ <_ extends "cva's generic parameters are restricted to internal use only.", V>(config: V extends CVAVariantShape ? CVAConfigBase & {
34
+ variants?: V;
35
+ compoundVariants?: (V extends CVAVariantShape ? (CVAVariantSchema<V> | {
36
+ [Variant in keyof V]?: StringToBoolean<keyof V[Variant]> | StringToBoolean<keyof V[Variant]>[] | undefined;
37
+ }) & CVAClassProp : CVAClassProp)[];
38
+ defaultVariants?: CVAVariantSchema<V>;
39
+ } : CVAConfigBase & {
40
+ variants?: never;
41
+ compoundVariants?: never;
42
+ defaultVariants?: never;
43
+ }): (props?: V extends CVAVariantShape ? CVAVariantSchema<V> & CVAClassProp : CVAClassProp) => string;
44
+ }
45
+ export interface DefineConfigOptions {
46
+ hooks?: {
47
+ /**
48
+ * @deprecated please use `onComplete`
49
+ */
50
+ "cx:done"?: (className: string) => string;
51
+ /**
52
+ * Returns the completed string of concatenated classes/classNames.
53
+ */
54
+ onComplete?: (className: string) => string;
55
+ };
56
+ }
57
+ export interface DefineConfig {
58
+ (options?: DefineConfigOptions): {
59
+ compose: Compose;
60
+ cx: CX;
61
+ cva: CVA;
62
+ };
63
+ }
64
+ export declare const defineConfig: DefineConfig;
65
+ export declare const compose: Compose, cva: CVA, cx: CX;
66
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.cx=exports.cva=exports.compose=exports.defineConfig=void 0;var _clsx=require("clsx");const falsyToString=value=>typeof value==="boolean"?"".concat(value):value===0?"0":value;const defineConfig=options=>{const cx1=function(){for(var _len=arguments.length,inputs=new Array(_len),_key=0;_key<_len;_key++){inputs[_key]=arguments[_key]}var ref,ref1;if(typeof(options===null||options===void 0?void 0:(ref=options.hooks)===null||ref===void 0?void 0:ref["cx:done"])!=="undefined")return options===null||options===void 0?void 0:options.hooks["cx:done"]((0,_clsx).clsx(inputs));if(typeof(options===null||options===void 0?void 0:(ref1=options.hooks)===null||ref1===void 0?void 0:ref1.onComplete)!=="undefined")return options===null||options===void 0?void 0:options.hooks.onComplete((0,_clsx).clsx(inputs));return(0,_clsx).clsx(inputs)};const cva1=config=>{return props=>{var ref;if((config===null||config===void 0?void 0:config.variants)==null)return cx1(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);const{variants,defaultVariants}=config;const getVariantClassNames=Object.keys(variants).map(variant=>{const variantProp=props===null||props===void 0?void 0:props[variant];const defaultVariantProp=defaultVariants===null||defaultVariants===void 0?void 0:defaultVariants[variant];const variantKey=falsyToString(variantProp)||falsyToString(defaultVariantProp);return variants[variant][variantKey]});const defaultsAndProps={...defaultVariants,...props&&Object.entries(props).reduce((acc,param)=>{let[key,value]=param;return typeof value==="undefined"?acc:{...acc,[key]:value}},{})};const getCompoundVariantClassNames=config===null||config===void 0?void 0:(ref=config.compoundVariants)===null||ref===void 0?void 0:ref.reduce((acc,param1)=>{let{class:cvClass,className:cvClassName,...cvConfig}=param1;return Object.entries(cvConfig).every(param=>{let[cvKey,cvSelector]=param;const selector=defaultsAndProps[cvKey];return Array.isArray(cvSelector)?cvSelector.includes(selector):selector===cvSelector})?[...acc,cvClass,cvClassName]:acc},[]);return cx1(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)}};const compose1=function(){for(var _len=arguments.length,components=new Array(_len),_key=0;_key<_len;_key++){components[_key]=arguments[_key]}return props=>{const propsWithoutClass=Object.fromEntries(Object.entries(props||{}).filter(param=>{let[key]=param;return!["class","className"].includes(key)}));return cx1(components.map(component=>component(propsWithoutClass)),props===null||props===void 0?void 0:props.class,props===null||props===void 0?void 0:props.className)}};return{compose:compose1,cva:cva1,cx:cx1}};exports.defineConfig=defineConfig;const{compose,cva,cx}=defineConfig();exports.compose=compose;exports.cva=cva;exports.cx=cx
2
+
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"names":[],"mappings":"AAAA,wIAAqB,KAAA,KAAM,SAAN,MAAM,CAAA,AAiJ3B,OAAM,aAAa,CAAG,AAAoB,KAAQ,EAChD,OAAO,KAAK,GAAK,SAAS,CAAG,AAAC,E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,AAEF,QAAO,GAAE,CACP,UAAU,CAAC,GAAG,CAAC,AAAC,SAAS,EAAK,SAAS,CAAC,iBAAiB,CAAC,CAAC,CAC3D,KAAK,SAAL,KAAK,SAAO,CAAZ,KAAA,CAAY,CAAZ,KAAK,CAAE,KAAK,CACZ,KAAK,SAAL,KAAK,SAAW,CAAhB,KAAA,CAAgB,CAAhB,KAAK,CAAE,SAAS,CACjB,AAAC,CACH,CAAA,AAAC,AAEJ,OAAO,CACL,OAAO,CAAP,QAAO,CACP,GAAG,CAAH,IAAG,CACH,EAAE,CAAF,GAAE,CACH,AAAC,CACH,AAAC,SAvFW,YAAY,CAAZ,YAAY,AAyFlB,MAAM,CAAE,OAAO,CAAE,GAAG,CAAE,EAAE,CAAE,CAAG,YAAY,EAAE,AAAC,SAApC,OAAO,CAAP,OAAO,SAAE,GAAG,CAAH,GAAG,SAAE,EAAE,CAAF,EAAE,AAAoB","file":"index.js","sourcesContent":["import { clsx } from \"clsx\";\n\n/* Types\n ============================================ */\n\n/* clsx\n ---------------------------------- */\n\n// When compiling with `declaration: true`, many projects experience the dreaded\n// TS2742 error. To combat this, we copy clsx's types manually.\n// Should this project move to JSDoc, this workaround would no longer be needed.\n\ntype ClassValue =\n | ClassArray\n | ClassDictionary\n | string\n | number\n | null\n | boolean\n | undefined;\ntype ClassDictionary = Record<string, any>;\ntype ClassArray = ClassValue[];\n\n/* Utils\n ---------------------------------- */\n\ntype OmitUndefined<T> = T extends undefined ? never : T;\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\nexport type VariantProps<Component extends (...args: any) => any> = Omit<\n OmitUndefined<Parameters<Component>[0]>,\n \"class\" | \"className\"\n>;\n\n/* compose\n ---------------------------------- */\n\nexport interface Compose {\n <T extends ReturnType<CVA>[]>(...components: [...T]): (\n props?: (\n | UnionToIntersection<\n {\n [K in keyof T]: VariantProps<T[K]>;\n }[number]\n >\n | undefined\n ) &\n CVAClassProp\n ) => string;\n}\n\n/* cx\n ---------------------------------- */\n\nexport interface CX {\n (...inputs: ClassValue[]): string;\n}\n\nexport type CXOptions = Parameters<CX>;\nexport type CXReturn = ReturnType<CX>;\n\n/* cva\n ============================================ */\n\ntype CVAConfigBase = { base?: ClassValue };\ntype CVAVariantShape = Record<string, Record<string, ClassValue>>;\ntype CVAVariantSchema<V extends CVAVariantShape> = {\n [Variant in keyof V]?: StringToBoolean<keyof V[Variant]> | undefined;\n};\ntype CVAClassProp =\n | {\n class?: ClassValue;\n className?: never;\n }\n | {\n class?: never;\n className?: ClassValue;\n };\n\nexport interface CVA {\n <\n _ extends \"cva's generic parameters are restricted to internal use only.\",\n V\n >(\n config: V extends CVAVariantShape\n ? CVAConfigBase & {\n variants?: V;\n compoundVariants?: (V extends CVAVariantShape\n ? (\n | CVAVariantSchema<V>\n | {\n [Variant in keyof V]?:\n | StringToBoolean<keyof V[Variant]>\n | StringToBoolean<keyof V[Variant]>[]\n | undefined;\n }\n ) &\n CVAClassProp\n : CVAClassProp)[];\n defaultVariants?: CVAVariantSchema<V>;\n }\n : CVAConfigBase & {\n variants?: never;\n compoundVariants?: never;\n defaultVariants?: never;\n }\n ): (\n props?: V extends CVAVariantShape\n ? CVAVariantSchema<V> & CVAClassProp\n : CVAClassProp\n ) => string;\n}\n\n/* defineConfig\n ---------------------------------- */\n\nexport interface DefineConfigOptions {\n hooks?: {\n /**\n * @deprecated please use `onComplete`\n */\n \"cx:done\"?: (className: string) => string;\n /**\n * Returns the completed string of concatenated classes/classNames.\n */\n onComplete?: (className: string) => string;\n };\n}\n\nexport interface DefineConfig {\n (options?: DefineConfigOptions): {\n compose: Compose;\n cx: CX;\n cva: CVA;\n };\n}\n\n/* Exports\n ============================================ */\n\nconst falsyToString = <T extends unknown>(value: T) =>\n typeof value === \"boolean\" ? `${value}` : value === 0 ? \"0\" : value;\n\nexport const defineConfig: DefineConfig = (options) => {\n const cx: CX = (...inputs) => {\n if (typeof options?.hooks?.[\"cx:done\"] !== \"undefined\")\n return options?.hooks[\"cx:done\"](clsx(inputs));\n\n if (typeof options?.hooks?.onComplete !== \"undefined\")\n return options?.hooks.onComplete(clsx(inputs));\n\n return clsx(inputs);\n };\n\n const cva: CVA = (config) => (props) => {\n if (config?.variants == null)\n return cx(config?.base, props?.class, props?.className);\n\n const { variants, defaultVariants } = config;\n\n const getVariantClassNames = Object.keys(variants).map(\n (variant: keyof typeof variants) => {\n const variantProp = props?.[variant as keyof typeof props];\n const defaultVariantProp = defaultVariants?.[variant];\n\n const variantKey = (falsyToString(variantProp) ||\n falsyToString(\n defaultVariantProp\n )) as keyof typeof variants[typeof variant];\n\n return variants[variant][variantKey];\n }\n );\n\n const defaultsAndProps = {\n ...defaultVariants,\n // remove `undefined` props\n ...(props &&\n Object.entries(props).reduce<typeof props>(\n (acc, [key, value]) =>\n typeof value === \"undefined\" ? acc : { ...acc, [key]: value },\n {} as typeof props\n )),\n };\n\n const getCompoundVariantClassNames = config?.compoundVariants?.reduce(\n (acc, { class: cvClass, className: cvClassName, ...cvConfig }) =>\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 );\n\n return cx(\n config?.base,\n getVariantClassNames,\n getCompoundVariantClassNames,\n props?.class,\n props?.className\n );\n };\n\n const compose: Compose =\n (...components) =>\n (props) => {\n const propsWithoutClass = Object.fromEntries(\n Object.entries(props || {}).filter(\n ([key]) => ![\"class\", \"className\"].includes(key)\n )\n );\n\n return cx(\n components.map((component) => component(propsWithoutClass)),\n props?.class,\n props?.className\n );\n };\n\n return {\n compose,\n cva,\n cx,\n };\n};\n\nexport const { compose, cva, cx } = defineConfig();\n"]}
package/dist/index.mjs ADDED
@@ -0,0 +1,3 @@
1
+ import{clsx}from"clsx";const falsyToString=value=>typeof value==="boolean"?"".concat(value):value===0?"0":value;export const defineConfig=options=>{const cx1=function(){for(var _len=arguments.length,inputs=new Array(_len),_key=0;_key<_len;_key++){inputs[_key]=arguments[_key]}var ref,ref1;if(typeof(options===null||options===void 0?void 0:(ref=options.hooks)===null||ref===void 0?void 0:ref["cx:done"])!=="undefined")return options===null||options===void 0?void 0:options.hooks["cx:done"](clsx(inputs));if(typeof(options===null||options===void 0?void 0:(ref1=options.hooks)===null||ref1===void 0?void 0:ref1.onComplete)!=="undefined")return options===null||options===void 0?void 0:options.hooks.onComplete(clsx(inputs));return clsx(inputs)};const cva1=config=>{return props=>{var ref;if((config===null||config===void 0?void 0:config.variants)==null)return cx1(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);const{variants,defaultVariants}=config;const getVariantClassNames=Object.keys(variants).map(variant=>{const variantProp=props===null||props===void 0?void 0:props[variant];const defaultVariantProp=defaultVariants===null||defaultVariants===void 0?void 0:defaultVariants[variant];const variantKey=falsyToString(variantProp)||falsyToString(defaultVariantProp);return variants[variant][variantKey]});const defaultsAndProps={...defaultVariants,...props&&Object.entries(props).reduce((acc,param)=>{let[key,value]=param;return typeof value==="undefined"?acc:{...acc,[key]:value}},{})};const getCompoundVariantClassNames=config===null||config===void 0?void 0:(ref=config.compoundVariants)===null||ref===void 0?void 0:ref.reduce((acc,param1)=>{let{class:cvClass,className:cvClassName,...cvConfig}=param1;return Object.entries(cvConfig).every(param=>{let[cvKey,cvSelector]=param;const selector=defaultsAndProps[cvKey];return Array.isArray(cvSelector)?cvSelector.includes(selector):selector===cvSelector})?[...acc,cvClass,cvClassName]:acc},[]);return cx1(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)}};const compose1=function(){for(var _len=arguments.length,components=new Array(_len),_key=0;_key<_len;_key++){components[_key]=arguments[_key]}return props=>{const propsWithoutClass=Object.fromEntries(Object.entries(props||{}).filter(param=>{let[key]=param;return!["class","className"].includes(key)}));return cx1(components.map(component=>component(propsWithoutClass)),props===null||props===void 0?void 0:props.class,props===null||props===void 0?void 0:props.className)}};return{compose:compose1,cva:cva1,cx:cx1}};export const{compose,cva,cx}=defineConfig()
2
+
3
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAS,IAAI,KAAQ,MAAM,AAAC,AAiJ5B,OAAM,aAAa,CAAG,AAAoB,KAAQ,EAChD,OAAO,KAAK,GAAK,SAAS,CAAG,AAAC,EAAA,CAAQ,MAAA,CAAN,KAAK,CAAE,CAAG,KAAK,GAAK,CAAC,CAAG,GAAG,CAAG,KAAK,AAAC,AAEtE,QAAO,MAAM,YAAY,CAAiB,AAAC,OAAO,EAAK,CACrD,MAAM,GAAE,CAAO,UAAe,+BAAX,MAAM,0CAAN,MAAM,2BACZ,GAAc,CAGd,IAHX,AAGyB,IAHrB,MAAO,CAAA,OAAO,SAAP,OAAO,SAAO,CAAd,KAAA,CAAc,CAAd,CAAA,GAAc,CAAd,OAAO,CAAE,KAAK,UAAd,GAAc,SAAA,CAAd,KAAA,CAAc,CAAd,GAAc,AAAE,CAAC,SAAS,CAAZ,AAAa,CAAb,AAAa,GAAK,WAAW,CACpD,OAAO,OAAO,SAAP,OAAO,SAAO,CAAd,KAAA,CAAc,CAAd,OAAO,CAAE,KAAK,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,AAAC,AAEjD,IAAI,MAAO,CAAA,OAAO,SAAP,OAAO,SAAO,CAAd,KAAA,CAAc,CAAd,CAAA,IAAc,CAAd,OAAO,CAAE,KAAK,UAAd,IAAc,SAAA,CAAd,KAAA,CAAc,CAAd,IAAc,CAAE,UAAF,AAAY,CAAZ,AAAY,GAAK,WAAW,CACnD,OAAO,OAAO,SAAP,O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AAC,CACH,CAAA,AAAC,AAEF,OAAM,QAAO,CACX,yCAAI,UAAU,0CAAV,UAAU,uBACd,OAAA,AAAC,KAAK,EAAK,CACT,MAAM,iBAAiB,CAAG,MAAM,CAAC,WAAW,CAC1C,MAAM,CAAC,OAAO,CAAC,KAAK,EAAI,EAAE,CAAC,CAAC,MAAM,CAChC,OAAW,IAAV,CAAC,GAAG,CAAC,aAAK,CAAC,CAAC,OAAO,CAAE,WAAW,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAA,CACjD,CACF,AAAC,AAEF,QAAO,GAAE,CACP,UAAU,CAAC,GAAG,CAAC,AAAC,SAAS,EAAK,SAAS,CAAC,iBAAiB,CAAC,CAAC,CAC3D,KAAK,SAAL,KAAK,SAAO,CAAZ,KAAA,CAAY,CAAZ,KAAK,CAAE,KAAK,CACZ,KAAK,SAAL,KAAK,SAAW,CAAhB,KAAA,CAAgB,CAAhB,KAAK,CAAE,SAAS,CACjB,AAAC,CACH,CAAA,AAAC,AAEJ,OAAO,CACL,OAAO,CAAP,QAAO,CACP,GAAG,CAAH,IAAG,CACH,EAAE,CAAF,GAAE,CACH,AAAC,CACH,AAAC,AAEF,QAAO,KAAM,CAAE,OAAO,CAAE,GAAG,CAAE,EAAE,CAAE,CAAG,YAAY,EAAE,AAAC","file":"index.mjs","sourcesContent":["import { clsx } from \"clsx\";\n\n/* Types\n ============================================ */\n\n/* clsx\n ---------------------------------- */\n\n// When compiling with `declaration: true`, many projects experience the dreaded\n// TS2742 error. To combat this, we copy clsx's types manually.\n// Should this project move to JSDoc, this workaround would no longer be needed.\n\ntype ClassValue =\n | ClassArray\n | ClassDictionary\n | string\n | number\n | null\n | boolean\n | undefined;\ntype ClassDictionary = Record<string, any>;\ntype ClassArray = ClassValue[];\n\n/* Utils\n ---------------------------------- */\n\ntype OmitUndefined<T> = T extends undefined ? never : T;\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\nexport type VariantProps<Component extends (...args: any) => any> = Omit<\n OmitUndefined<Parameters<Component>[0]>,\n \"class\" | \"className\"\n>;\n\n/* compose\n ---------------------------------- */\n\nexport interface Compose {\n <T extends ReturnType<CVA>[]>(...components: [...T]): (\n props?: (\n | UnionToIntersection<\n {\n [K in keyof T]: VariantProps<T[K]>;\n }[number]\n >\n | undefined\n ) &\n CVAClassProp\n ) => string;\n}\n\n/* cx\n ---------------------------------- */\n\nexport interface CX {\n (...inputs: ClassValue[]): string;\n}\n\nexport type CXOptions = Parameters<CX>;\nexport type CXReturn = ReturnType<CX>;\n\n/* cva\n ============================================ */\n\ntype CVAConfigBase = { base?: ClassValue };\ntype CVAVariantShape = Record<string, Record<string, ClassValue>>;\ntype CVAVariantSchema<V extends CVAVariantShape> = {\n [Variant in keyof V]?: StringToBoolean<keyof V[Variant]> | undefined;\n};\ntype CVAClassProp =\n | {\n class?: ClassValue;\n className?: never;\n }\n | {\n class?: never;\n className?: ClassValue;\n };\n\nexport interface CVA {\n <\n _ extends \"cva's generic parameters are restricted to internal use only.\",\n V\n >(\n config: V extends CVAVariantShape\n ? CVAConfigBase & {\n variants?: V;\n compoundVariants?: (V extends CVAVariantShape\n ? (\n | CVAVariantSchema<V>\n | {\n [Variant in keyof V]?:\n | StringToBoolean<keyof V[Variant]>\n | StringToBoolean<keyof V[Variant]>[]\n | undefined;\n }\n ) &\n CVAClassProp\n : CVAClassProp)[];\n defaultVariants?: CVAVariantSchema<V>;\n }\n : CVAConfigBase & {\n variants?: never;\n compoundVariants?: never;\n defaultVariants?: never;\n }\n ): (\n props?: V extends CVAVariantShape\n ? CVAVariantSchema<V> & CVAClassProp\n : CVAClassProp\n ) => string;\n}\n\n/* defineConfig\n ---------------------------------- */\n\nexport interface DefineConfigOptions {\n hooks?: {\n /**\n * @deprecated please use `onComplete`\n */\n \"cx:done\"?: (className: string) => string;\n /**\n * Returns the completed string of concatenated classes/classNames.\n */\n onComplete?: (className: string) => string;\n };\n}\n\nexport interface DefineConfig {\n (options?: DefineConfigOptions): {\n compose: Compose;\n cx: CX;\n cva: CVA;\n };\n}\n\n/* Exports\n ============================================ */\n\nconst falsyToString = <T extends unknown>(value: T) =>\n typeof value === \"boolean\" ? `${value}` : value === 0 ? \"0\" : value;\n\nexport const defineConfig: DefineConfig = (options) => {\n const cx: CX = (...inputs) => {\n if (typeof options?.hooks?.[\"cx:done\"] !== \"undefined\")\n return options?.hooks[\"cx:done\"](clsx(inputs));\n\n if (typeof options?.hooks?.onComplete !== \"undefined\")\n return options?.hooks.onComplete(clsx(inputs));\n\n return clsx(inputs);\n };\n\n const cva: CVA = (config) => (props) => {\n if (config?.variants == null)\n return cx(config?.base, props?.class, props?.className);\n\n const { variants, defaultVariants } = config;\n\n const getVariantClassNames = Object.keys(variants).map(\n (variant: keyof typeof variants) => {\n const variantProp = props?.[variant as keyof typeof props];\n const defaultVariantProp = defaultVariants?.[variant];\n\n const variantKey = (falsyToString(variantProp) ||\n falsyToString(\n defaultVariantProp\n )) as keyof typeof variants[typeof variant];\n\n return variants[variant][variantKey];\n }\n );\n\n const defaultsAndProps = {\n ...defaultVariants,\n // remove `undefined` props\n ...(props &&\n Object.entries(props).reduce<typeof props>(\n (acc, [key, value]) =>\n typeof value === \"undefined\" ? acc : { ...acc, [key]: value },\n {} as typeof props\n )),\n };\n\n const getCompoundVariantClassNames = config?.compoundVariants?.reduce(\n (acc, { class: cvClass, className: cvClassName, ...cvConfig }) =>\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 );\n\n return cx(\n config?.base,\n getVariantClassNames,\n getCompoundVariantClassNames,\n props?.class,\n props?.className\n );\n };\n\n const compose: Compose =\n (...components) =>\n (props) => {\n const propsWithoutClass = Object.fromEntries(\n Object.entries(props || {}).filter(\n ([key]) => ![\"class\", \"className\"].includes(key)\n )\n );\n\n return cx(\n components.map((component) => component(propsWithoutClass)),\n props?.class,\n props?.className\n );\n };\n\n return {\n compose,\n cva,\n cx,\n };\n};\n\nexport const { compose, cva, cx } = defineConfig();\n"]}
package/package.json CHANGED
@@ -1,15 +1,67 @@
1
1
  {
2
2
  "name": "cva",
3
- "version": "0.0.0",
4
- "description": "Awesome node module",
3
+ "version": "1.0.0-beta.1",
4
+ "description": "Class Variance Authority 🧬",
5
5
  "keywords": [
6
- "placeholder",
7
- "zce"
6
+ "Class Variance Authority",
7
+ "class-variance-authority",
8
+ "classes",
9
+ "classname",
10
+ "classnames",
11
+ "css",
12
+ "cva",
13
+ "stitches",
14
+ "vanilla-extract",
15
+ "variants"
8
16
  ],
9
- "license": "MIT",
10
- "author": {
11
- "name": "zce",
12
- "email": "w@zce.me",
13
- "url": "https://zce.me"
17
+ "homepage": "https://github.com/joe-bell/cva#readme",
18
+ "bugs": "https://github.com/joe-bell/cva/issues",
19
+ "repository": "https://github.com/joe-bell/cva.git",
20
+ "funding": "https://joebell.co.uk/sponsors",
21
+ "license": "Apache-2.0",
22
+ "author": "Joe Bell (https://joebell.co.uk)",
23
+ "exports": {
24
+ "import": "./dist/index.mjs",
25
+ "require": "./dist/index.js",
26
+ "types": "./dist/index.d.ts"
27
+ },
28
+ "main": "dist/index.js",
29
+ "module": "dist/index.mjs",
30
+ "types": "dist/index.d.ts",
31
+ "files": [
32
+ "dist/"
33
+ ],
34
+ "dependencies": {
35
+ "clsx": "2.0.0"
36
+ },
37
+ "devDependencies": {
38
+ "@swc/cli": "0.1.57",
39
+ "@swc/core": "1.2.198",
40
+ "@types/node": "18.11.18",
41
+ "@types/react": "18.0.12",
42
+ "@types/react-dom": "18.0.5",
43
+ "bundlesize": "0.18.1",
44
+ "npm-run-all": "4.1.5",
45
+ "react": "18.2.0",
46
+ "react-dom": "18.2.0",
47
+ "ts-node": "10.8.1",
48
+ "typescript": "5.1.3"
49
+ },
50
+ "peerDependencies": {
51
+ "typescript": ">= 4.5.5 < 6"
52
+ },
53
+ "peerDependenciesMeta": {
54
+ "typescript": {
55
+ "optional": true
56
+ }
57
+ },
58
+ "scripts": {
59
+ "build": "run-p build:**",
60
+ "build:cjs": "swc ./src/index.ts --config-file ./.config/.swcrc -o dist/index.js -C module.type=commonjs",
61
+ "build:esm": "swc ./src/index.ts --config-file ./.config/.swcrc -o dist/index.mjs -C module.type=es6 ",
62
+ "build:tsc": "tsc --project .config/tsconfig.build.json",
63
+ "check": "tsc --project tsconfig.json --noEmit",
64
+ "dev": "jest --config .config/jest.config.ts --watch",
65
+ "bundlesize": "pnpm build && bundlesize -f 'dist/*.js' -s 1.2KB"
14
66
  }
15
- }
67
+ }
package/src/index.ts ADDED
@@ -0,0 +1,238 @@
1
+ import { clsx } from "clsx";
2
+
3
+ /* Types
4
+ ============================================ */
5
+
6
+ /* clsx
7
+ ---------------------------------- */
8
+
9
+ // When compiling with `declaration: true`, many projects experience the dreaded
10
+ // TS2742 error. To combat this, we copy clsx's types manually.
11
+ // Should this project move to JSDoc, this workaround would no longer be needed.
12
+
13
+ type ClassValue =
14
+ | ClassArray
15
+ | ClassDictionary
16
+ | string
17
+ | number
18
+ | null
19
+ | boolean
20
+ | undefined;
21
+ type ClassDictionary = Record<string, any>;
22
+ type ClassArray = ClassValue[];
23
+
24
+ /* Utils
25
+ ---------------------------------- */
26
+
27
+ type OmitUndefined<T> = T extends undefined ? never : T;
28
+ type StringToBoolean<T> = T extends "true" | "false" ? boolean : T;
29
+ type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (
30
+ k: infer I
31
+ ) => void
32
+ ? I
33
+ : never;
34
+
35
+ export type VariantProps<Component extends (...args: any) => any> = Omit<
36
+ OmitUndefined<Parameters<Component>[0]>,
37
+ "class" | "className"
38
+ >;
39
+
40
+ /* compose
41
+ ---------------------------------- */
42
+
43
+ export interface Compose {
44
+ <T extends ReturnType<CVA>[]>(...components: [...T]): (
45
+ props?: (
46
+ | UnionToIntersection<
47
+ {
48
+ [K in keyof T]: VariantProps<T[K]>;
49
+ }[number]
50
+ >
51
+ | undefined
52
+ ) &
53
+ CVAClassProp
54
+ ) => string;
55
+ }
56
+
57
+ /* cx
58
+ ---------------------------------- */
59
+
60
+ export interface CX {
61
+ (...inputs: ClassValue[]): string;
62
+ }
63
+
64
+ export type CXOptions = Parameters<CX>;
65
+ export type CXReturn = ReturnType<CX>;
66
+
67
+ /* cva
68
+ ============================================ */
69
+
70
+ type CVAConfigBase = { base?: ClassValue };
71
+ type CVAVariantShape = Record<string, Record<string, ClassValue>>;
72
+ type CVAVariantSchema<V extends CVAVariantShape> = {
73
+ [Variant in keyof V]?: StringToBoolean<keyof V[Variant]> | undefined;
74
+ };
75
+ type CVAClassProp =
76
+ | {
77
+ class?: ClassValue;
78
+ className?: never;
79
+ }
80
+ | {
81
+ class?: never;
82
+ className?: ClassValue;
83
+ };
84
+
85
+ export interface CVA {
86
+ <
87
+ _ extends "cva's generic parameters are restricted to internal use only.",
88
+ V
89
+ >(
90
+ config: V extends CVAVariantShape
91
+ ? CVAConfigBase & {
92
+ variants?: V;
93
+ compoundVariants?: (V extends CVAVariantShape
94
+ ? (
95
+ | CVAVariantSchema<V>
96
+ | {
97
+ [Variant in keyof V]?:
98
+ | StringToBoolean<keyof V[Variant]>
99
+ | StringToBoolean<keyof V[Variant]>[]
100
+ | undefined;
101
+ }
102
+ ) &
103
+ CVAClassProp
104
+ : CVAClassProp)[];
105
+ defaultVariants?: CVAVariantSchema<V>;
106
+ }
107
+ : CVAConfigBase & {
108
+ variants?: never;
109
+ compoundVariants?: never;
110
+ defaultVariants?: never;
111
+ }
112
+ ): (
113
+ props?: V extends CVAVariantShape
114
+ ? CVAVariantSchema<V> & CVAClassProp
115
+ : CVAClassProp
116
+ ) => string;
117
+ }
118
+
119
+ /* defineConfig
120
+ ---------------------------------- */
121
+
122
+ export interface DefineConfigOptions {
123
+ hooks?: {
124
+ /**
125
+ * @deprecated please use `onComplete`
126
+ */
127
+ "cx:done"?: (className: string) => string;
128
+ /**
129
+ * Returns the completed string of concatenated classes/classNames.
130
+ */
131
+ onComplete?: (className: string) => string;
132
+ };
133
+ }
134
+
135
+ export interface DefineConfig {
136
+ (options?: DefineConfigOptions): {
137
+ compose: Compose;
138
+ cx: CX;
139
+ cva: CVA;
140
+ };
141
+ }
142
+
143
+ /* Exports
144
+ ============================================ */
145
+
146
+ const falsyToString = <T extends unknown>(value: T) =>
147
+ typeof value === "boolean" ? `${value}` : value === 0 ? "0" : value;
148
+
149
+ export const defineConfig: DefineConfig = (options) => {
150
+ const cx: CX = (...inputs) => {
151
+ if (typeof options?.hooks?.["cx:done"] !== "undefined")
152
+ return options?.hooks["cx:done"](clsx(inputs));
153
+
154
+ if (typeof options?.hooks?.onComplete !== "undefined")
155
+ return options?.hooks.onComplete(clsx(inputs));
156
+
157
+ return clsx(inputs);
158
+ };
159
+
160
+ const cva: CVA = (config) => (props) => {
161
+ if (config?.variants == null)
162
+ return cx(config?.base, props?.class, props?.className);
163
+
164
+ const { variants, defaultVariants } = config;
165
+
166
+ const getVariantClassNames = Object.keys(variants).map(
167
+ (variant: keyof typeof variants) => {
168
+ const variantProp = props?.[variant as keyof typeof props];
169
+ const defaultVariantProp = defaultVariants?.[variant];
170
+
171
+ const variantKey = (falsyToString(variantProp) ||
172
+ falsyToString(
173
+ defaultVariantProp
174
+ )) as keyof typeof variants[typeof variant];
175
+
176
+ return variants[variant][variantKey];
177
+ }
178
+ );
179
+
180
+ const defaultsAndProps = {
181
+ ...defaultVariants,
182
+ // remove `undefined` props
183
+ ...(props &&
184
+ Object.entries(props).reduce<typeof props>(
185
+ (acc, [key, value]) =>
186
+ typeof value === "undefined" ? acc : { ...acc, [key]: value },
187
+ {} as typeof props
188
+ )),
189
+ };
190
+
191
+ const getCompoundVariantClassNames = config?.compoundVariants?.reduce(
192
+ (acc, { class: cvClass, className: cvClassName, ...cvConfig }) =>
193
+ Object.entries(cvConfig).every(([cvKey, cvSelector]) => {
194
+ const selector =
195
+ defaultsAndProps[cvKey as keyof typeof defaultsAndProps];
196
+
197
+ return Array.isArray(cvSelector)
198
+ ? cvSelector.includes(selector)
199
+ : selector === cvSelector;
200
+ })
201
+ ? [...acc, cvClass, cvClassName]
202
+ : acc,
203
+ [] as ClassValue[]
204
+ );
205
+
206
+ return cx(
207
+ config?.base,
208
+ getVariantClassNames,
209
+ getCompoundVariantClassNames,
210
+ props?.class,
211
+ props?.className
212
+ );
213
+ };
214
+
215
+ const compose: Compose =
216
+ (...components) =>
217
+ (props) => {
218
+ const propsWithoutClass = Object.fromEntries(
219
+ Object.entries(props || {}).filter(
220
+ ([key]) => !["class", "className"].includes(key)
221
+ )
222
+ );
223
+
224
+ return cx(
225
+ components.map((component) => component(propsWithoutClass)),
226
+ props?.class,
227
+ props?.className
228
+ );
229
+ };
230
+
231
+ return {
232
+ compose,
233
+ cva,
234
+ cx,
235
+ };
236
+ };
237
+
238
+ export const { compose, cva, cx } = defineConfig();