css-variants 1.0.0

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,19 @@
1
+ MIT License
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to
5
+ deal in the Software without restriction, including without limitation the
6
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7
+ sell copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19
+ DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,407 @@
1
+ ![Logo](.github/assets/logo.png)
2
+
3
+ [![test](https://github.com/timphandev/css-variants/actions/workflows/ci.yml/badge.svg)](https://github.com/timphandev/css-variants/actions/workflows/ci.yml)
4
+ [![license](https://img.shields.io/github/license/timphandev/css-variants)](https://github.com/timphandev/css-variants/blob/main/LICENSE)
5
+ [![npm](https://img.shields.io/npm/dm/css-variants)](https://npmjs.com/package/css-variants)
6
+ ![npm](https://img.shields.io/npm/v/css-variants)
7
+
8
+ # A powerful and flexible package for managing style-based variants
9
+
10
+ Creating variants with the "traditional" CSS approach can become an arduous task; manually matching classes to props and manually adding types.
11
+
12
+ `css-variants` aims to take those pain points away, allowing you to focus on the more fun aspects of UI development.
13
+
14
+ `css-variants` is heavily inspired by [CVA](https://github.com/joe-bell/cva), [Tailwind Variants](https://github.com/nextui-org/tailwind-variants) and [Panda CSS](https://github.com/chakra-ui/panda).
15
+
16
+ ## Features
17
+ - First-class variant API
18
+ - Inline-style support
19
+ - Slots support
20
+ - Fully type-safe
21
+ - Framework agnostic
22
+ - Supports custom strategies like `tailwind-merge`
23
+ - Light-weight without any dependencies
24
+
25
+ ## Table of Contents
26
+ * [Installation](#installation)
27
+ * [Variants](#variants)
28
+ * [Adding variants](#adding-variants)
29
+ * [Multiple variants](#multiple-variants)
30
+ * [Boolean variants](#boolean-variants)
31
+ * [Compound variants](#compound-variants)
32
+ * [Default variants](#default-variants)
33
+ * [Slots](#events)
34
+ * [Basic Usage](#basic-usage)
35
+ * [Slots with variants](#slots-with-variants)
36
+ * [Handling Style Conflicts](#handling-style-conflicts)
37
+ * [TypeScript](#typeScript)
38
+ * [Contribute](#contribute)
39
+ * [License](#license)
40
+
41
+
42
+ ## Installation
43
+
44
+ To use `css-variants` in your project, you can install it as a dependency:
45
+
46
+ ```sh
47
+ yarn add css-variants
48
+ ```
49
+
50
+ ## Variants
51
+
52
+ Variants allows you to create multiple versions of the same component.
53
+
54
+ ### Adding variants
55
+
56
+ You can add variants by using the `variants` key. There's no limit to how many variants you can add.
57
+
58
+ ```ts
59
+ import { cv } from 'css-variants'
60
+
61
+ const button = cv({
62
+ base: 'font-bold rounded-sm',
63
+ variants: {
64
+ color: {
65
+ primary: 'bg-blue-500 hover:bg-blue-700',
66
+ secondary: 'bg-purple-500 hover:bg-purple-700',
67
+ success: {
68
+ className: 'hover:bg-green-700',
69
+ style: { color: 'green' }, // You can also use inline style
70
+ },
71
+ },
72
+ },
73
+ })
74
+
75
+ button({ color: 'secondary' })
76
+ /**
77
+ * Result:
78
+ * {
79
+ * className: 'font-bold rounded-sm bg-purple-500 hover:bg-purple-700',
80
+ * style: {},
81
+ * }
82
+ */
83
+
84
+ button({ color: 'success' })
85
+ /**
86
+ * Result:
87
+ * {
88
+ * className: 'font-bold rounded-sm hover:bg-green-700',
89
+ * style: { color: 'green' },
90
+ * }
91
+ */
92
+ ```
93
+
94
+ ### Multiple variants
95
+
96
+ You can add multiple variants to a single component.
97
+
98
+ ```ts
99
+ import { cv } from 'css-variants'
100
+
101
+ const button = cv({
102
+ base: {
103
+ className: 'font-bold',
104
+ style: { borderRadius: 16 },
105
+ },
106
+ variants: {
107
+ color: {
108
+ primary: 'bg-blue-500 hover:bg-blue-700',
109
+ secondary: 'bg-purple-500 hover:bg-purple-700',
110
+ success: 'bg-green-500 hover:bg-green-700'
111
+ },
112
+ size: {
113
+ sm: 'text-sm p-2',
114
+ md: 'text-md p-4',
115
+ lg: 'text-lg p-6',
116
+ },
117
+ },
118
+ })
119
+
120
+ button({ color: 'success', size: 'lg' })
121
+ /**
122
+ * Result:
123
+ * {
124
+ * className: 'font-bold bg-green-500 hover:bg-green-700 text-lg p-6',
125
+ * style: { borderRadius: 16 },
126
+ * }
127
+ */
128
+ ```
129
+
130
+ ### Boolean variants
131
+
132
+ You can also add boolean variants to a component. This is useful when you want to add a state variant e.g. `disabled`.
133
+
134
+ ```ts
135
+ import { cv } from 'css-variants'
136
+
137
+ const button = cv({
138
+ base: {
139
+ style: { fontWeight: 'bold' },
140
+ },
141
+ variants: {
142
+ color: {
143
+ primary: 'bg-blue-500 hover:bg-blue-700',
144
+ secondary: 'bg-purple-500 hover:bg-purple-700',
145
+ success: 'bg-green-500 hover:bg-green-700'
146
+ },
147
+ disabled: {
148
+ true: 'opacity-50 pointer-events-none',
149
+ },
150
+ },
151
+ })
152
+
153
+ button({ disabled: true })
154
+ /**
155
+ * Result:
156
+ * {
157
+ * className: 'opacity-50 pointer-events-none',
158
+ * style: { fontWeight: 'bold' },
159
+ * }
160
+ */
161
+ ```
162
+
163
+ ### Compound variants
164
+
165
+ Sometimes you might want to add a variant that depends on another variant. This is possible by using the `compoundVariants` key.
166
+
167
+ ```ts
168
+ import { cv } from 'css-variants'
169
+
170
+ const button = cv({
171
+ base: {
172
+ style: { fontWeight: 'bold' },
173
+ },
174
+ variants: {
175
+ size: {
176
+ sm: 'text-sm p-2',
177
+ md: 'text-md p-4',
178
+ lg: {
179
+ className: 'text-lg',
180
+ style: { padding: 6 },
181
+ },
182
+ },
183
+ disabled: {
184
+ true: 'opacity-50 pointer-events-none',
185
+ },
186
+ },
187
+ compoundVariants: [
188
+ {
189
+ size: 'lg', // You can also use the values as an array
190
+ disabled: true,
191
+ className: 'uppercase',
192
+ style: { padding: 5 },
193
+ }
194
+ ],
195
+ })
196
+
197
+ button({ size: 'lg', disabled: true })
198
+ /**
199
+ * Result:
200
+ * {
201
+ * className: 'text-lg p-6 opacity-50 pointer-events-none uppercase',
202
+ * style: { fontWeight: 'bold', padding: 5 },
203
+ * }
204
+ */
205
+ ```
206
+
207
+ ### Default variants
208
+
209
+ You can also add a default variant to a component. This is useful when you want to add a predefined variants values to a component.
210
+
211
+ ```ts
212
+ import { cv } from 'css-variants'
213
+
214
+ const button = cv({
215
+ base: 'font-bold rounded-sm',
216
+ variants: {
217
+ color: {
218
+ primary: 'bg-blue-500 hover:bg-blue-700',
219
+ secondary: 'bg-purple-500 hover:bg-purple-700',
220
+ success: 'bg-green-500 hover:bg-green-700'
221
+ },
222
+ },
223
+ defaultVariants: {
224
+ color: 'primary',
225
+ },
226
+ })
227
+
228
+ button()
229
+ /**
230
+ * Result:
231
+ * {
232
+ * className: 'font-bold rounded-sm bg-blue-500 hover:bg-blue-700',
233
+ * style: {},
234
+ * }
235
+ */
236
+ ```
237
+
238
+ ## Slots
239
+
240
+ Slots allows you to separate a component into multiple parts.
241
+
242
+ ### Basic Usage
243
+
244
+ You can add `slots` by using the slots key. There's no limit to how many slots you can add.
245
+
246
+ ```ts
247
+ import { csv } from 'css-variants'
248
+
249
+ const notification = cv({
250
+ slots: ['root', 'title'],
251
+ base: {
252
+ root: 'root',
253
+ title: {
254
+ className: 'title',
255
+ style: { fontSize: 16 },
256
+ },
257
+ },
258
+ });
259
+
260
+ notification()
261
+ /**
262
+ * Result:
263
+ * {
264
+ * root: {
265
+ * className: 'root',
266
+ * style: {},
267
+ * },
268
+ * title: {
269
+ * className: 'title',
270
+ * style: { fontSize: 16 },
271
+ * },
272
+ * }
273
+ */
274
+
275
+ ```
276
+
277
+ ### Slots with variants
278
+
279
+ You can also change the entire component and its slots by using the variants.
280
+
281
+ ```ts
282
+ import { csv } from 'css-variants'
283
+
284
+ const notification = cv({
285
+ slots: ['root', 'title', 'content'],
286
+ base: {
287
+ root: 'root',
288
+ title: 'title',
289
+ content: {
290
+ className: 'content',
291
+ style: { fontSize: 16 },
292
+ },
293
+ },
294
+ variants: {
295
+ color: {
296
+ primary: {
297
+ root: 'root-primary',
298
+ title: 'title-primary',
299
+ content: 'content-primary',
300
+ },
301
+ secondary: {
302
+ title: 'title-secondary',
303
+ content: 'content-secondary',
304
+ },
305
+ }
306
+ },
307
+ });
308
+
309
+ notification({ color: 'primary' })
310
+ /**
311
+ * Result:
312
+ * {
313
+ * root: {
314
+ * className: 'root root-primary',
315
+ * style: {},
316
+ * },
317
+ * title: {
318
+ * className: 'title title-primary',
319
+ * style: {},
320
+ * },
321
+ * content: {
322
+ * className: 'content content-primary',
323
+ * style: { fontSize: 16 },
324
+ * },
325
+ * }
326
+ */
327
+
328
+ notification({ color: 'secondary' })
329
+ /**
330
+ * Result:
331
+ * {
332
+ * root: {
333
+ * className: 'root',
334
+ * style: {},
335
+ * },
336
+ * title: {
337
+ * className: 'title title-secondary',
338
+ * style: {},
339
+ * },
340
+ * content: {
341
+ * className: 'content content-secondary',
342
+ * style: { fontSize: 16 },
343
+ * },
344
+ * }
345
+ */
346
+ ```
347
+
348
+ ## Handling Style Conflicts
349
+
350
+ Although `css-variants` is designed to help you avoid styling conflicts, there's still a small margin of error.
351
+
352
+ If you're keen to lift that burden altogether, check out the wonderful `tailwind-merge` package.
353
+
354
+ For bulletproof components, wrap your `cx`, `cv`, `csv` component with `twMerge`.
355
+
356
+ ```ts
357
+ import { twMerge } from 'tailwind-merge'
358
+ import { cx as baseCx, cv as baseCv } from 'css-variants'
359
+
360
+ export const cx: typeof baseCx = (...args) => twMerge(baseCx(...args))
361
+
362
+ export const cv: typeof baseCv = (config) => {
363
+ return baseCv({
364
+ ...config,
365
+ onDone: ({ className, style }) => {
366
+ const css = {
367
+ style,
368
+ className: twMerge(className),
369
+ }
370
+
371
+ return config.onDone ? config.onDone(css) : css
372
+ },
373
+ })
374
+ }
375
+ ```
376
+
377
+ ## TypeScript
378
+
379
+ ### Extracting Variant Types
380
+
381
+ You can use the `VariantProps` utility to extract variants from a component.
382
+
383
+ ```tsx
384
+ import { VariantProps } from 'css-variants'
385
+ import { cv } from 'class-variance-authority'
386
+
387
+ export const button = cv({
388
+ variants: {
389
+ color: {
390
+ primary: 'bg-blue-500 text-white',
391
+ secondary: 'bg-purple-500 text-white',
392
+ },
393
+ },
394
+ })
395
+
396
+ export type ButtonProps = VariantProps<typeof button>
397
+ ```
398
+
399
+ ## Contribute
400
+
401
+ If you would like to contribute to the project, please read how to contribute here [CONTRIBUTING.md](./CONTRIBUTING.md).
402
+
403
+ ## License
404
+
405
+ Licensed under the MIT License.
406
+
407
+ See [MIT license](./LICENSE) for more information.
@@ -0,0 +1,3 @@
1
+ import { SlotVariantCreatorFn } from './types';
2
+ export declare const csv: SlotVariantCreatorFn;
3
+ export default csv;
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.csv = void 0;
4
+ const cx_1 = require("./cx");
5
+ const utils_1 = require("./utils");
6
+ const push = (data, key, value) => {
7
+ if (value) {
8
+ if (data[key] === undefined) {
9
+ data[key] = { classNames: [], style: {} };
10
+ }
11
+ if (typeof value === 'string') {
12
+ data[key].classNames.push(value);
13
+ }
14
+ else {
15
+ if (value.className) {
16
+ data[key].classNames.push(value.className);
17
+ }
18
+ if (value.style) {
19
+ data[key].style = { ...data[key].style, ...value.style };
20
+ }
21
+ }
22
+ }
23
+ };
24
+ const csv = ({ slots, base, variants, compoundVariants, defaultVariants, onDone }) => (props) => {
25
+ const { classNames: propClassNames, styles: propStyles, ...rest } = props ?? {};
26
+ const data = {};
27
+ const variantProps = { ...defaultVariants, ...(0, utils_1.compact)(rest) };
28
+ for (const [key, value] of (0, utils_1.entries)(base)) {
29
+ push(data, key, value);
30
+ }
31
+ for (const [propKey, propValue] of (0, utils_1.entries)(variantProps)) {
32
+ for (const [key, value] of (0, utils_1.entries)(variants?.[propKey]?.[propValue])) {
33
+ push(data, key, value);
34
+ }
35
+ }
36
+ for (const { classNames: cvClassNames, styles: cvStyles, ...compoundVariant } of compoundVariants ?? []) {
37
+ if ((0, utils_1.match)(compoundVariant, variantProps)) {
38
+ for (const [key, value] of (0, utils_1.entries)(cvClassNames))
39
+ push(data, key, { className: value });
40
+ for (const [key, value] of (0, utils_1.entries)(cvStyles))
41
+ push(data, key, { style: value });
42
+ }
43
+ }
44
+ for (const [key, value] of (0, utils_1.entries)(propClassNames))
45
+ push(data, key, { className: value });
46
+ for (const [key, value] of (0, utils_1.entries)(propStyles))
47
+ push(data, key, { style: value });
48
+ const css = (0, utils_1.fromEntries)(slots.map((slot) => [
49
+ slot,
50
+ {
51
+ className: (0, cx_1.cx)(data[slot]?.classNames),
52
+ style: data[slot]?.style ?? {},
53
+ },
54
+ ]));
55
+ if (onDone)
56
+ return onDone(css);
57
+ return css;
58
+ };
59
+ exports.csv = csv;
60
+ exports.default = exports.csv;
61
+ //# sourceMappingURL=csv.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"csv.js","sourceRoot":"","sources":["../../src/csv.ts"],"names":[],"mappings":";;;AAAA,6BAAyB;AACzB,mCAA8D;AAG9D,MAAM,IAAI,GAAG,CACX,IAAsE,EACtE,GAAM,EACN,KAAsC,EACtC,EAAE;IACF,IAAI,KAAK,EAAE,CAAC;QACV,IAAI,IAAI,CAAC,GAAG,CA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F,MAAM,GAAG,GAAG,IAAA,mBAAW,EACrB,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC;QAClB,IAAI;QACJ;YACE,SAAS,EAAE,IAAA,OAAE,EAAC,IAAI,CAAC,IAAI,CAAC,EAAE,UAAU,CAAC;YACrC,KAAK,EAAE,IAAI,CAAC,IAAI,CAAC,EAAE,KAAK,IAAI,EAAE;SAC/B;KACF,CAAC,CACH,CAAA;IAED,IAAI,MAAM;QAAE,OAAO,MAAM,CAAC,GAAG,CAAC,CAAA;IAE9B,OAAO,GAAG,CAAA;AACZ,CAAC,CAAA;AA1CU,QAAA,GAAG,OA0Cb;AAEH,kBAAe,WAAG,CAAA"}
@@ -0,0 +1,3 @@
1
+ import { VariantCreatorFn } from './types';
2
+ export declare const cv: VariantCreatorFn;
3
+ export default cv;
package/dist/cjs/cv.js ADDED
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.cv = void 0;
4
+ const cx_1 = require("./cx");
5
+ const utils_1 = require("./utils");
6
+ const push = (data, value) => {
7
+ if (typeof value === 'string') {
8
+ data.classNames.push(value);
9
+ }
10
+ else {
11
+ if (value?.className) {
12
+ data.classNames.push(value.className);
13
+ }
14
+ if (value?.style) {
15
+ data.style = { ...data.style, ...value.style };
16
+ }
17
+ }
18
+ };
19
+ const cv = ({ base, variants, compoundVariants, defaultVariants, onDone }) => (props) => {
20
+ const { className: propClassName, style: propStyle, ...rest } = props ?? {};
21
+ const data = { classNames: [], style: {} };
22
+ const variantProps = { ...defaultVariants, ...(0, utils_1.compact)(rest) };
23
+ if (base) {
24
+ push(data, base);
25
+ }
26
+ for (const [propKey, propValue] of (0, utils_1.entries)(variantProps)) {
27
+ push(data, variants?.[propKey]?.[propValue]);
28
+ }
29
+ for (const { className: cvClassName, style: cvStyle, ...compoundVariant } of compoundVariants ?? []) {
30
+ if ((0, utils_1.match)(compoundVariant, variantProps)) {
31
+ push(data, { className: cvClassName, style: cvStyle });
32
+ }
33
+ }
34
+ if (propClassName || propStyle) {
35
+ push(data, { className: propClassName, style: propStyle });
36
+ }
37
+ const css = { className: (0, cx_1.cx)(data.classNames), style: data.style };
38
+ if (onDone)
39
+ return onDone(css);
40
+ return css;
41
+ };
42
+ exports.cv = cv;
43
+ exports.default = exports.cv;
44
+ //# sourceMappingURL=cv.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cv.js","sourceRoot":"","sources":["../../src/cv.ts"],"names":[],"mappings":";;;AAAA,6BAAyB;AACzB,mCAAiD;AAGjD,MAAM,IAAI,GAAG,CAAC,IAAoD,EAAE,KAAsC,EAAE,EAAE;IAC5G,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;IAC7B,CAAC;SAAM,CAAC;QACN,IAAI,KAAK,EAAE,SAAS,EAAE,CAAC;YACrB,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAA;QACvC,CAAC;QAED,IAAI,KAAK,EAAE,KAAK,EAAE,CAAC;YACjB,IAAI,CAAC,KAAK,GAAG,EAAE,GAAG,IAAI,CAAC,KAAK,EAAE,GAAG,KAAK,CAAC,KAAK,EAAE,CAAA;QAChD,CAAC;IACH,CAAC;AACH,CAAC,CAAA;AAEM,MAAM,EAAE,GACb,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,EAAE,EAAE,EAAE,CAClE,CAAC,KAAK,EAAE,EAAE;IACR,MAAM,EAAE,SAAS,EAAE,aAAa,EAAE,KAAK,EAAE,SAAS,EAAE,GAAG,IAAI,EAAE,GAAG,KAAK,IAAI,EAAE,CAAA;IAE3E,MAAM,IAAI,GAAmD,EAAE,UAAU,EAAE,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE,CAAA;IAE1F,MAAM,YAAY,GAAG,EAAE,GAAG,eAAe,EAAE,GAAG,IAAA,eAAO,EAAC,IAAI,CAAC,EAAE,CAAA;IAE7D,IAAI,IAAI,EAAE,CAAC;QACT,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAA;IAClB,CAAC;IAED,KAAK,MAAM,CAAC,OAAO,EAAE,SAAS,CAAC,IAAI,IAAA,eAAO,EAAC,YAAY,CAAC,EAAE,CAAC;QACzD,IAAI,CAAC,IAAI,EAAE,QAAQ,EAAE,CAAC,OAAO,CAAC,EAAE,CAAC,SAAmB,CAAC,CAAC,CAAA;IACxD,CAAC;IAED,KAAK,MAAM,EAAE,SAAS,EAAE,WAAW,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,eAAe,EAAE,IAAI,gBAAgB,IAAI,EAAE,EAAE,CAAC;QACpG,IAAI,IAAA,aAAK,EAAC,eAAe,EAAE,YAAY,CAAC,EAAE,CAAC;YACzC,IAAI,CAAC,IAAI,EAAE,EAAE,SAAS,EAAE,WAAW,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAA;QACxD,CAAC;IACH,CAAC;IAED,IAAI,aAAa,IAAI,SAAS,EAAE,CAAC;QAC/B,IAAI,CAAC,IAAI,EAAE,EAAE,SAAS,EAAE,aAAa,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC,CAAA;IAC5D,CAAC;IAED,MAAM,GAAG,GAAG,EAAE,SAAS,EAAE,IAAA,OAAE,EAAC,IAAI,CAAC,UAAU,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,CAAA;IAEjE,IAAI,MAAM;QAAE,OAAO,MAAM,CAAC,GAAG,CAAC,CAAA;IAE9B,OAAO,GAAG,CAAA;AACZ,CAAC,CAAA;AAhCU,QAAA,EAAE,MAgCZ;AAEH,kBAAe,UAAE,CAAA"}
@@ -0,0 +1,3 @@
1
+ type Arg = string | number | null | undefined | Record<string, boolean>;
2
+ export declare const cx: (...args: (Arg | Arg[])[]) => string;
3
+ export default cx;
package/dist/cjs/cx.js ADDED
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.cx = void 0;
4
+ const concat = (str1, str2) => {
5
+ const str = typeof str2 === 'number' ? str2.toString() : str2.trim();
6
+ if (str && str1)
7
+ return str1 + ' ' + str;
8
+ return str1 || str;
9
+ };
10
+ const cx = (...args) => {
11
+ let className = '';
12
+ for (const arg of args) {
13
+ if (typeof arg === 'string') {
14
+ className = concat(className, arg);
15
+ continue;
16
+ }
17
+ if (typeof arg === 'number') {
18
+ className = concat(className, arg);
19
+ continue;
20
+ }
21
+ if (arg && typeof arg === 'object') {
22
+ if (Array.isArray(arg)) {
23
+ className = concat(className, (0, exports.cx)(...arg));
24
+ continue;
25
+ }
26
+ for (const key in arg) {
27
+ if (arg[key]) {
28
+ className = concat(className, key);
29
+ }
30
+ }
31
+ continue;
32
+ }
33
+ }
34
+ return className;
35
+ };
36
+ exports.cx = cx;
37
+ exports.default = exports.cx;
38
+ //# sourceMappingURL=cx.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cx.js","sourceRoot":"","sources":["../../src/cx.ts"],"names":[],"mappings":";;;AAEA,MAAM,MAAM,GAAG,CAAC,IAAY,EAAE,IAAqB,EAAE,EAAE;IACrD,MAAM,GAAG,GAAG,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,CAAA;IACpE,IAAI,GAAG,IAAI,IAAI;QAAE,OAAO,IAAI,GAAG,GAAG,GAAG,GAAG,CAAA;IACxC,OAAO,IAAI,IAAI,GAAG,CAAA;AACpB,CAAC,CAAA;AAEM,MAAM,EAAE,GAAG,CAAC,GAAG,IAAqB,EAAE,EAAE;IAC7C,IAAI,SAAS,GAAG,EAAE,CAAA;IAElB,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;QACvB,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE,CAAC;YAC5B,SAAS,GAAG,MAAM,CAAC,SAAS,EAAE,GAAG,CAAC,CAAA;YAClC,SAAQ;QACV,CAAC;QAED,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE,CAAC;YAC5B,SAAS,GAAG,MAAM,CAAC,SAAS,EAAE,GAAG,CAAC,CAAA;YAClC,SAAQ;QACV,CAAC;QAED,IAAI,GAAG,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE,CAAC;YACnC,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;gBACvB,SAAS,GAAG,MAAM,CAAC,SAAS,EAAE,IAAA,UAAE,EAAC,GAAG,GAAG,CAAC,CAAC,CAAA;gBACzC,SAAQ;YACV,CAAC;YAED,KAAK,MAAM,GAAG,IAAI,GAAG,EAAE,CAAC;gBACtB,IAAI,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;oBACb,SAAS,GAAG,MAAM,CAAC,SAAS,EAAE,GAAG,CAAC,CAAA;gBACpC,CAAC;YACH,CAAC;YAED,SAAQ;QACV,CAAC;IACH,CAAC;IAED,OAAO,SAAS,CAAA;AAClB,CAAC,CAAA;AA/BY,QAAA,EAAE,MA+Bd;AAED,kBAAe,UAAE,CAAA"}
@@ -0,0 +1,4 @@
1
+ export * from './cx';
2
+ export * from './cv';
3
+ export * from './csv';
4
+ export { VariantProps } from './types';
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./cx"), exports);
18
+ __exportStar(require("./cv"), exports);
19
+ __exportStar(require("./csv"), exports);
20
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,uCAAoB;AACpB,uCAAoB;AACpB,wCAAqB"}
@@ -0,0 +1,44 @@
1
+ import { Properties } from 'csstype';
2
+ export type StringToBoolean<T> = T extends 'true' | 'false' ? boolean : T;
3
+ export type OneOrMore<T> = T | T[];
4
+ export type PartialRecord<S extends string, T> = Partial<Record<S, T>>;
5
+ export type RequireAtLeastOne<T> = {
6
+ [K in keyof T]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<keyof T, K>>>;
7
+ }[keyof T];
8
+ export type CssProperties = Properties<string | number>;
9
+ export type VariantStyle = {
10
+ className: string;
11
+ style: CssProperties;
12
+ };
13
+ export type VariantRecord = Record<string, Record<string, string | RequireAtLeastOne<VariantStyle>>>;
14
+ export type VariantSelection<T> = T extends undefined ? Record<string, unknown> : {
15
+ [K in keyof T]?: StringToBoolean<keyof T[K]> | undefined;
16
+ };
17
+ export type VariantCompoundSelection<T> = T extends undefined ? Record<string, unknown> : {
18
+ [K in keyof T]?: OneOrMore<StringToBoolean<keyof T[K]>> | undefined;
19
+ };
20
+ export interface VariantDefinition<T extends VariantRecord | undefined> {
21
+ base?: string | RequireAtLeastOne<VariantStyle>;
22
+ variants?: T;
23
+ compoundVariants?: (VariantCompoundSelection<T> & RequireAtLeastOne<VariantStyle>)[];
24
+ defaultVariants?: VariantSelection<T>;
25
+ onDone?: (css: VariantStyle) => VariantStyle;
26
+ }
27
+ export type VariantFn<T extends VariantRecord | undefined> = (props?: VariantSelection<T> & Partial<VariantStyle>) => VariantStyle;
28
+ export type VariantCreatorFn = <T extends VariantRecord | undefined>(config: VariantDefinition<T>) => VariantFn<T>;
29
+ export type SlotVariantStyle<S extends string> = {
30
+ classNames: PartialRecord<S, string>;
31
+ styles: PartialRecord<S, CssProperties>;
32
+ };
33
+ export type SlotVariantRecord<S extends string> = Record<string, Record<string, PartialRecord<S, string | RequireAtLeastOne<VariantStyle>>>>;
34
+ export interface SlotVariantDefinition<S extends string, T extends SlotVariantRecord<S> | undefined> {
35
+ slots: S[] | Readonly<S[]>;
36
+ base?: PartialRecord<S, string | RequireAtLeastOne<VariantStyle>>;
37
+ variants?: T;
38
+ compoundVariants?: (VariantCompoundSelection<T> & RequireAtLeastOne<SlotVariantStyle<S>>)[];
39
+ defaultVariants?: VariantSelection<T>;
40
+ onDone?: (css: Record<S, VariantStyle>) => Record<S, VariantStyle>;
41
+ }
42
+ export type SlotVariantFn<S extends string, T extends SlotVariantRecord<S> | undefined> = (props?: VariantSelection<T> & Partial<SlotVariantStyle<S>>) => Record<S, VariantStyle>;
43
+ export type SlotVariantCreatorFn = <S extends string, T extends SlotVariantRecord<S> | undefined>(config: SlotVariantDefinition<S, T>) => SlotVariantFn<S, T>;
44
+ export type VariantProps<T extends VariantFn<VariantRecord> | SlotVariantFn<string, SlotVariantRecord<string>>> = Parameters<T>[0];
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,6 @@
1
+ export declare const entries: <T extends object, TK extends Extract<keyof T, string>, TV extends T[TK], TEntry extends [TK, TV]>(obj?: T) => TEntry[];
2
+ export declare function fromEntries<A extends symbol | string | number, B>(entries: [A, B][]): {
3
+ [key in A]: B;
4
+ };
5
+ export declare const compact: <T extends Record<string, unknown>>(obj?: T) => T;
6
+ export declare const match: <T1 extends object, T2 extends object>(obj1: T1, obj2: T2) => boolean;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.match = exports.compact = exports.entries = void 0;
4
+ exports.fromEntries = fromEntries;
5
+ const entries = (obj) => (obj ? Object.entries(obj) : []);
6
+ exports.entries = entries;
7
+ function fromEntries(entries) {
8
+ const result = {};
9
+ for (const [key, value] of entries) {
10
+ result[key] = value;
11
+ }
12
+ return result;
13
+ }
14
+ const compact = (obj) => {
15
+ const newObj = {};
16
+ if (obj) {
17
+ for (const k in obj) {
18
+ if (obj[k] !== undefined) {
19
+ newObj[k] = obj[k];
20
+ }
21
+ }
22
+ }
23
+ return newObj;
24
+ };
25
+ exports.compact = compact;
26
+ const match = (obj1, obj2) => {
27
+ for (const k in obj1) {
28
+ const val1 = obj1[k];
29
+ const val2 = obj2[k];
30
+ if (Array.isArray(val1)) {
31
+ if (!val1.includes(val2))
32
+ return false;
33
+ }
34
+ else {
35
+ if (val1 !== val2)
36
+ return false;
37
+ }
38
+ }
39
+ return true;
40
+ };
41
+ exports.match = match;
42
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":";;;AASA,kCAMC;AAfM,MAAM,OAAO,GAAG,CAMrB,GAAO,EACG,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAa,CAAA;AAP9C,QAAA,OAAO,WAOuC;AAE3D,SAAgB,WAAW,CAAwC,OAAiB;IAClF,MAAM,MAAM,GAAsB,EAAuB,CAAA;IACzD,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,OAAO,EAAE,CAAC;QACnC,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAA;IACrB,CAAC;IACD,OAAO,MAAM,CAAA;AACf,CAAC;AAEM,MAAM,OAAO,GAAG,CAAoC,GAAO,EAAE,EAAE;IACpE,MAAM,MAAM,GAAG,EAAO,CAAA;IAEtB,IAAI,GAAG,EAAE,CAAC;QACR,KAAK,MAAM,CAAC,IAAI,GAAG,EAAE,CAAC;YACpB,IAAI,GAAG,CAAC,CAAC,CAAC,KAAK,SAAS,EAAE,CAAC;gBACzB,MAAM,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAA;YACpB,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAA;AACf,CAAC,CAAA;AAZY,QAAA,OAAO,WAYnB;AAEM,MAAM,KAAK,GAAG,CAAuC,IAAQ,EAAE,IAAQ,EAAE,EAAE;IAChF,KAAK,MAAM,CAAC,IAAI,IAAI,EAAE,CAAC;QACrB,MAAM,IAAI,GAAG,IAAI,CAAC,CAAC,CAAC,CAAA;QACpB,MAAM,IAAI,GAAG,IAAI,CAAC,CAAwB,CAAC,CAAA;QAC3C,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;YACxB,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC;gBAAE,OAAO,KAAK,CAAA;QACxC,CAAC;aAAM,CAAC;YACN,IAAK,IAAgB,KAAK,IAAI;gBAAE,OAAO,KAAK,CAAA;QAC9C,CAAC;IACH,CAAC;IACD,OAAO,IAAI,CAAA;AACb,CAAC,CAAA;AAXY,QAAA,KAAK,SAWjB"}
@@ -0,0 +1,3 @@
1
+ import { SlotVariantCreatorFn } from './types';
2
+ export declare const csv: SlotVariantCreatorFn;
3
+ export default csv;
@@ -0,0 +1,57 @@
1
+ import { cx } from './cx';
2
+ import { compact, entries, fromEntries, match } from './utils';
3
+ const push = (data, key, value) => {
4
+ if (value) {
5
+ if (data[key] === undefined) {
6
+ data[key] = { classNames: [], style: {} };
7
+ }
8
+ if (typeof value === 'string') {
9
+ data[key].classNames.push(value);
10
+ }
11
+ else {
12
+ if (value.className) {
13
+ data[key].classNames.push(value.className);
14
+ }
15
+ if (value.style) {
16
+ data[key].style = { ...data[key].style, ...value.style };
17
+ }
18
+ }
19
+ }
20
+ };
21
+ export const csv = ({ slots, base, variants, compoundVariants, defaultVariants, onDone }) => (props) => {
22
+ const { classNames: propClassNames, styles: propStyles, ...rest } = props ?? {};
23
+ const data = {};
24
+ const variantProps = { ...defaultVariants, ...compact(rest) };
25
+ for (const [key, value] of entries(base)) {
26
+ push(data, key, value);
27
+ }
28
+ for (const [propKey, propValue] of entries(variantProps)) {
29
+ for (const [key, value] of entries(variants?.[propKey]?.[propValue])) {
30
+ push(data, key, value);
31
+ }
32
+ }
33
+ for (const { classNames: cvClassNames, styles: cvStyles, ...compoundVariant } of compoundVariants ?? []) {
34
+ if (match(compoundVariant, variantProps)) {
35
+ for (const [key, value] of entries(cvClassNames))
36
+ push(data, key, { className: value });
37
+ for (const [key, value] of entries(cvStyles))
38
+ push(data, key, { style: value });
39
+ }
40
+ }
41
+ for (const [key, value] of entries(propClassNames))
42
+ push(data, key, { className: value });
43
+ for (const [key, value] of entries(propStyles))
44
+ push(data, key, { style: value });
45
+ const css = fromEntries(slots.map((slot) => [
46
+ slot,
47
+ {
48
+ className: cx(data[slot]?.classNames),
49
+ style: data[slot]?.style ?? {},
50
+ },
51
+ ]));
52
+ if (onDone)
53
+ return onDone(css);
54
+ return css;
55
+ };
56
+ export default csv;
57
+ //# sourceMappingURL=csv.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"csv.js","sourceRoot":"","sources":["../../src/csv.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,MAAM,CAAA;AACzB,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAG9D,MAAM,IAAI,GAAG,CACX,IAAsE,EACtE,GAAM,EACN,KAAsC,EACtC,EAAE;IACF,IAAI,KAAK,EAAE,CAA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AAG,GAAG,WAAW,CACrB,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC;QAClB,IAAI;QACJ;YACE,SAAS,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,UAAU,CAAC;YACrC,KAAK,EAAE,IAAI,CAAC,IAAI,CAAC,EAAE,KAAK,IAAI,EAAE;SAC/B;KACF,CAAC,CACH,CAAA;IAED,IAAI,MAAM;QAAE,OAAO,MAAM,CAAC,GAAG,CAAC,CAAA;IAE9B,OAAO,GAAG,CAAA;AACZ,CAAC,CAAA;AAEH,eAAe,GAAG,CAAA"}
@@ -0,0 +1,3 @@
1
+ import { VariantCreatorFn } from './types';
2
+ export declare const cv: VariantCreatorFn;
3
+ export default cv;
package/dist/esm/cv.js ADDED
@@ -0,0 +1,40 @@
1
+ import { cx } from './cx';
2
+ import { compact, entries, match } from './utils';
3
+ const push = (data, value) => {
4
+ if (typeof value === 'string') {
5
+ data.classNames.push(value);
6
+ }
7
+ else {
8
+ if (value?.className) {
9
+ data.classNames.push(value.className);
10
+ }
11
+ if (value?.style) {
12
+ data.style = { ...data.style, ...value.style };
13
+ }
14
+ }
15
+ };
16
+ export const cv = ({ base, variants, compoundVariants, defaultVariants, onDone }) => (props) => {
17
+ const { className: propClassName, style: propStyle, ...rest } = props ?? {};
18
+ const data = { classNames: [], style: {} };
19
+ const variantProps = { ...defaultVariants, ...compact(rest) };
20
+ if (base) {
21
+ push(data, base);
22
+ }
23
+ for (const [propKey, propValue] of entries(variantProps)) {
24
+ push(data, variants?.[propKey]?.[propValue]);
25
+ }
26
+ for (const { className: cvClassName, style: cvStyle, ...compoundVariant } of compoundVariants ?? []) {
27
+ if (match(compoundVariant, variantProps)) {
28
+ push(data, { className: cvClassName, style: cvStyle });
29
+ }
30
+ }
31
+ if (propClassName || propStyle) {
32
+ push(data, { className: propClassName, style: propStyle });
33
+ }
34
+ const css = { className: cx(data.classNames), style: data.style };
35
+ if (onDone)
36
+ return onDone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,EAAE,CAAC;QACT,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAA;IAClB,CAAC;IAED,KAAK,MAAM,CAAC,OAAO,EAAE,SAAS,CAAC,IAAI,OAAO,CAAC,YAAY,CAAC,EAAE,CAAC;QACzD,IAAI,CAAC,IAAI,EAAE,QAAQ,EAAE,CAAC,OAAO,CAAC,EAAE,CAAC,SAAmB,CAAC,CAAC,CAAA;IACxD,CAAC;IAED,KAAK,MAAM,EAAE,SAAS,EAAE,WAAW,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,eAAe,EAAE,IAAI,gBAAgB,IAAI,EAAE,EAAE,CAAC;QACpG,IAAI,KAAK,CAAC,eAAe,EAAE,YAAY,CAAC,EAAE,CAAC;YACzC,IAAI,CAAC,IAAI,EAAE,EAAE,SAAS,EAAE,WAAW,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAA;QACxD,CAAC;IACH,CAAC;IAED,IAAI,aAAa,IAAI,SAAS,EAAE,CAAC;QAC/B,IAAI,CAAC,IAAI,EAAE,EAAE,SAAS,EAAE,aAAa,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC,CAAA;IAC5D,CAAC;IAED,MAAM,GAAG,GAAG,EAAE,SAAS,EAAE,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,CAAA;IAEjE,IAAI,MAAM;QAAE,OAAO,MAAM,CAAC,GAAG,CAAC,CAAA;IAE9B,OAAO,GAAG,CAAA;AACZ,CAAC,CAAA;AAEH,eAAe,EAAE,CAAA"}
@@ -0,0 +1,3 @@
1
+ type Arg = string | number | null | undefined | Record<string, boolean>;
2
+ export declare const cx: (...args: (Arg | Arg[])[]) => string;
3
+ export default cx;
package/dist/esm/cx.js ADDED
@@ -0,0 +1,34 @@
1
+ const concat = (str1, str2) => {
2
+ const str = typeof str2 === 'number' ? str2.toString() : str2.trim();
3
+ if (str && str1)
4
+ return str1 + ' ' + str;
5
+ return str1 || str;
6
+ };
7
+ export const cx = (...args) => {
8
+ let className = '';
9
+ for (const arg of args) {
10
+ if (typeof arg === 'string') {
11
+ className = concat(className, arg);
12
+ continue;
13
+ }
14
+ if (typeof arg === 'number') {
15
+ className = concat(className, arg);
16
+ continue;
17
+ }
18
+ if (arg && typeof arg === 'object') {
19
+ if (Array.isArray(arg)) {
20
+ className = concat(className, cx(...arg));
21
+ continue;
22
+ }
23
+ for (const key in arg) {
24
+ if (arg[key]) {
25
+ className = concat(className, key);
26
+ }
27
+ }
28
+ continue;
29
+ }
30
+ }
31
+ return className;
32
+ };
33
+ export default cx;
34
+ //# sourceMappingURL=cx.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cx.js","sourceRoot":"","sources":["../../src/cx.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,GAAG,CAAC,IAAY,EAAE,IAAqB,EAAE,EAAE;IACrD,MAAM,GAAG,GAAG,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,CAAA;IACpE,IAAI,GAAG,IAAI,IAAI;QAAE,OAAO,IAAI,GAAG,GAAG,GAAG,GAAG,CAAA;IACxC,OAAO,IAAI,IAAI,GAAG,CAAA;AACpB,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,IAAqB,EAAE,EAAE;IAC7C,IAAI,SAAS,GAAG,EAAE,CAAA;IAElB,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;QACvB,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE,CAAC;YAC5B,SAAS,GAAG,MAAM,CAAC,SAAS,EAAE,GAAG,CAAC,CAAA;YAClC,SAAQ;QACV,CAAC;QAED,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE,CAAC;YAC5B,SAAS,GAAG,MAAM,CAAC,SAAS,EAAE,GAAG,CAAC,CAAA;YAClC,SAAQ;QACV,CAAC;QAED,IAAI,GAAG,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE,CAAC;YACnC,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;gBACvB,SAAS,GAAG,MAAM,CAAC,SAAS,EAAE,EAAE,CAAC,GAAG,GAAG,CAAC,CAAC,CAAA;gBACzC,SAAQ;YACV,CAAC;YAED,KAAK,MAAM,GAAG,IAAI,GAAG,EAAE,CAAC;gBACtB,IAAI,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;oBACb,SAAS,GAAG,MAAM,CAAC,SAAS,EAAE,GAAG,CAAC,CAAA;gBACpC,CAAC;YACH,CAAC;YAED,SAAQ;QACV,CAAC;IACH,CAAC;IAED,OAAO,SAAS,CAAA;AAClB,CAAC,CAAA;AAED,eAAe,EAAE,CAAA"}
@@ -0,0 +1,4 @@
1
+ export * from './cx';
2
+ export * from './cv';
3
+ export * from './csv';
4
+ export { VariantProps } from './types';
@@ -0,0 +1,4 @@
1
+ export * from './cx';
2
+ export * from './cv';
3
+ export * from './csv';
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,MAAM,CAAA;AACpB,cAAc,MAAM,CAAA;AACpB,cAAc,OAAO,CAAA"}
@@ -0,0 +1,44 @@
1
+ import { Properties } from 'csstype';
2
+ export type StringToBoolean<T> = T extends 'true' | 'false' ? boolean : T;
3
+ export type OneOrMore<T> = T | T[];
4
+ export type PartialRecord<S extends string, T> = Partial<Record<S, T>>;
5
+ export type RequireAtLeastOne<T> = {
6
+ [K in keyof T]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<keyof T, K>>>;
7
+ }[keyof T];
8
+ export type CssProperties = Properties<string | number>;
9
+ export type VariantStyle = {
10
+ className: string;
11
+ style: CssProperties;
12
+ };
13
+ export type VariantRecord = Record<string, Record<string, string | RequireAtLeastOne<VariantStyle>>>;
14
+ export type VariantSelection<T> = T extends undefined ? Record<string, unknown> : {
15
+ [K in keyof T]?: StringToBoolean<keyof T[K]> | undefined;
16
+ };
17
+ export type VariantCompoundSelection<T> = T extends undefined ? Record<string, unknown> : {
18
+ [K in keyof T]?: OneOrMore<StringToBoolean<keyof T[K]>> | undefined;
19
+ };
20
+ export interface VariantDefinition<T extends VariantRecord | undefined> {
21
+ base?: string | RequireAtLeastOne<VariantStyle>;
22
+ variants?: T;
23
+ compoundVariants?: (VariantCompoundSelection<T> & RequireAtLeastOne<VariantStyle>)[];
24
+ defaultVariants?: VariantSelection<T>;
25
+ onDone?: (css: VariantStyle) => VariantStyle;
26
+ }
27
+ export type VariantFn<T extends VariantRecord | undefined> = (props?: VariantSelection<T> & Partial<VariantStyle>) => VariantStyle;
28
+ export type VariantCreatorFn = <T extends VariantRecord | undefined>(config: VariantDefinition<T>) => VariantFn<T>;
29
+ export type SlotVariantStyle<S extends string> = {
30
+ classNames: PartialRecord<S, string>;
31
+ styles: PartialRecord<S, CssProperties>;
32
+ };
33
+ export type SlotVariantRecord<S extends string> = Record<string, Record<string, PartialRecord<S, string | RequireAtLeastOne<VariantStyle>>>>;
34
+ export interface SlotVariantDefinition<S extends string, T extends SlotVariantRecord<S> | undefined> {
35
+ slots: S[] | Readonly<S[]>;
36
+ base?: PartialRecord<S, string | RequireAtLeastOne<VariantStyle>>;
37
+ variants?: T;
38
+ compoundVariants?: (VariantCompoundSelection<T> & RequireAtLeastOne<SlotVariantStyle<S>>)[];
39
+ defaultVariants?: VariantSelection<T>;
40
+ onDone?: (css: Record<S, VariantStyle>) => Record<S, VariantStyle>;
41
+ }
42
+ export type SlotVariantFn<S extends string, T extends SlotVariantRecord<S> | undefined> = (props?: VariantSelection<T> & Partial<SlotVariantStyle<S>>) => Record<S, VariantStyle>;
43
+ export type SlotVariantCreatorFn = <S extends string, T extends SlotVariantRecord<S> | undefined>(config: SlotVariantDefinition<S, T>) => SlotVariantFn<S, T>;
44
+ export type VariantProps<T extends VariantFn<VariantRecord> | SlotVariantFn<string, SlotVariantRecord<string>>> = Parameters<T>[0];
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,6 @@
1
+ export declare const entries: <T extends object, TK extends Extract<keyof T, string>, TV extends T[TK], TEntry extends [TK, TV]>(obj?: T) => TEntry[];
2
+ export declare function fromEntries<A extends symbol | string | number, B>(entries: [A, B][]): {
3
+ [key in A]: B;
4
+ };
5
+ export declare const compact: <T extends Record<string, unknown>>(obj?: T) => T;
6
+ export declare const match: <T1 extends object, T2 extends object>(obj1: T1, obj2: T2) => boolean;
@@ -0,0 +1,35 @@
1
+ export const entries = (obj) => (obj ? Object.entries(obj) : []);
2
+ export function fromEntries(entries) {
3
+ const result = {};
4
+ for (const [key, value] of entries) {
5
+ result[key] = value;
6
+ }
7
+ return result;
8
+ }
9
+ export const compact = (obj) => {
10
+ const newObj = {};
11
+ if (obj) {
12
+ for (const k in obj) {
13
+ if (obj[k] !== undefined) {
14
+ newObj[k] = obj[k];
15
+ }
16
+ }
17
+ }
18
+ return newObj;
19
+ };
20
+ export const match = (obj1, obj2) => {
21
+ for (const k in obj1) {
22
+ const val1 = obj1[k];
23
+ const val2 = obj2[k];
24
+ if (Array.isArray(val1)) {
25
+ if (!val1.includes(val2))
26
+ return false;
27
+ }
28
+ else {
29
+ if (val1 !== val2)
30
+ return false;
31
+ }
32
+ }
33
+ return true;
34
+ };
35
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,OAAO,GAAG,CAMrB,GAAO,EACG,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,GAA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css-variants",
3
+ "version": "1.0.0",
4
+ "description": "Variant API for plain class names",
5
+ "type": "module",
6
+ "main": "dist/cjs/index.js",
7
+ "module": "dist/esm/index.js",
8
+ "types": "dist/esm/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "require": "./dist/cjs/index.js",
12
+ "import": "./dist/esm/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "scripts": {
19
+ "lint": "eslint .",
20
+ "test": "vitest run --coverage",
21
+ "build:cjs": "tsc --project tsconfig.cjs.json",
22
+ "build:esm": "tsc --project tsconfig.esm.json",
23
+ "build": "rimraf ./dist && yarn build:cjs && yarn build:esm"
24
+ },
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "git+https://github.com/timphandev/css-variants.git"
28
+ },
29
+ "keywords": [
30
+ "javascript",
31
+ "typescript",
32
+ "js",
33
+ "ts",
34
+ "css variants",
35
+ "class variants",
36
+ "classname variants",
37
+ "style variants",
38
+ "cva",
39
+ "class variance authority"
40
+ ],
41
+ "authors": [
42
+ {
43
+ "name": "Tim Phan",
44
+ "email": "timphan.dev@gmail.com"
45
+ }
46
+ ],
47
+ "license": "MIT",
48
+ "devDependencies": {
49
+ "@eslint/js": "^9.7.0",
50
+ "@types/eslint__js": "^8.42.3",
51
+ "@types/node": "^20.12.12",
52
+ "@vitest/coverage-v8": "^1.6.0",
53
+ "eslint": "^9.7.0",
54
+ "eslint-config-prettier": "^9.1.0",
55
+ "eslint-plugin-prettier": "^5.2.1",
56
+ "prettier": "^3.3.0",
57
+ "rimraf": "^5.0.7",
58
+ "typescript": "^5.4.5",
59
+ "typescript-eslint": "^7.16.1",
60
+ "vitest": "^1.6.0"
61
+ },
62
+ "dependencies": {
63
+ "csstype": "^3.1.3"
64
+ }
65
+ }