css-variants 1.1.3 → 2.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/README.md +113 -153
- package/dist/cjs/cv.d.ts +48 -2
- package/dist/cjs/cv.js +49 -37
- package/dist/cjs/cv.js.map +1 -1
- package/dist/cjs/cx.d.ts +4 -2
- package/dist/cjs/cx.js +49 -34
- package/dist/cjs/cx.js.map +1 -1
- package/dist/cjs/index.d.ts +3 -2
- package/dist/cjs/index.js +3 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/scv.d.ts +56 -0
- package/dist/cjs/scv.js +90 -0
- package/dist/cjs/scv.js.map +1 -0
- package/dist/cjs/ssv.d.ts +51 -0
- package/dist/cjs/ssv.js +77 -0
- package/dist/cjs/ssv.js.map +1 -0
- package/dist/cjs/sv.d.ts +44 -0
- package/dist/cjs/sv.js +63 -0
- package/dist/cjs/sv.js.map +1 -0
- package/dist/cjs/utils/compact.d.ts +1 -0
- package/dist/cjs/utils/compact.js +13 -0
- package/dist/cjs/utils/compact.js.map +1 -0
- package/dist/cjs/utils/entries.d.ts +1 -0
- package/dist/cjs/utils/entries.js +7 -0
- package/dist/cjs/utils/entries.js.map +1 -0
- package/dist/cjs/utils/types.d.ts +14 -0
- package/dist/cjs/{types.js.map → utils/types.js.map} +1 -1
- package/dist/esm/cv.d.ts +48 -2
- package/dist/esm/cv.js +49 -37
- package/dist/esm/cv.js.map +1 -1
- package/dist/esm/cx.d.ts +4 -2
- package/dist/esm/cx.js +47 -31
- package/dist/esm/cx.js.map +1 -1
- package/dist/esm/index.d.ts +3 -2
- package/dist/esm/index.js +3 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/scv.d.ts +56 -0
- package/dist/esm/scv.js +86 -0
- package/dist/esm/scv.js.map +1 -0
- package/dist/esm/ssv.d.ts +51 -0
- package/dist/esm/ssv.js +73 -0
- package/dist/esm/ssv.js.map +1 -0
- package/dist/esm/sv.d.ts +44 -0
- package/dist/esm/sv.js +59 -0
- package/dist/esm/sv.js.map +1 -0
- package/dist/esm/utils/compact.d.ts +1 -0
- package/dist/esm/utils/compact.js +10 -0
- package/dist/esm/utils/compact.js.map +1 -0
- package/dist/esm/utils/entries.d.ts +1 -0
- package/dist/esm/utils/entries.js +4 -0
- package/dist/esm/utils/entries.js.map +1 -0
- package/dist/esm/utils/types.d.ts +14 -0
- package/dist/esm/{types.js.map → utils/types.js.map} +1 -1
- package/package.json +13 -10
- package/dist/cjs/csv.d.ts +0 -3
- package/dist/cjs/csv.js +0 -83
- package/dist/cjs/csv.js.map +0 -1
- package/dist/cjs/types.d.ts +0 -46
- package/dist/cjs/utils.d.ts +0 -6
- package/dist/cjs/utils.js +0 -42
- package/dist/cjs/utils.js.map +0 -1
- package/dist/esm/csv.d.ts +0 -3
- package/dist/esm/csv.js +0 -79
- package/dist/esm/csv.js.map +0 -1
- package/dist/esm/types.d.ts +0 -46
- package/dist/esm/utils.d.ts +0 -6
- package/dist/esm/utils.js +0 -35
- package/dist/esm/utils.js.map +0 -1
- /package/dist/cjs/{types.js → utils/types.js} +0 -0
- /package/dist/esm/{types.js → utils/types.js} +0 -0
package/README.md
CHANGED
|
@@ -44,7 +44,7 @@ Thank you to the authors and contributors of these projects for their innovative
|
|
|
44
44
|
* [Overriding styles](#overriding-styles)
|
|
45
45
|
* [Overriding styles on a single component](#overriding-styles-on-a-single-component)
|
|
46
46
|
* [Overriding styles on a component with slots](#overriding-styles-on-a-component-with-slots)
|
|
47
|
-
* [
|
|
47
|
+
* [Custom function to resolve class names](#custom-function-to-resolve-class-names)
|
|
48
48
|
* [TypeScript](#typeScript)
|
|
49
49
|
* [Contribute](#contribute)
|
|
50
50
|
* [License](#license)
|
|
@@ -75,31 +75,39 @@ const button = cv({
|
|
|
75
75
|
color: {
|
|
76
76
|
primary: 'bg-blue-500 hover:bg-blue-700',
|
|
77
77
|
secondary: 'bg-purple-500 hover:bg-purple-700',
|
|
78
|
+
success: 'hover:bg-green-700',
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
button({ color: 'secondary' })
|
|
84
|
+
// => 'font-bold rounded-sm bg-purple-500 hover:bg-purple-700'
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
import { sv } from 'css-variants'
|
|
89
|
+
|
|
90
|
+
const button = sv({
|
|
91
|
+
base: {
|
|
92
|
+
fontWeight: 700,
|
|
93
|
+
},
|
|
94
|
+
variants: {
|
|
95
|
+
color: {
|
|
96
|
+
primary: {
|
|
97
|
+
color: 'blue',
|
|
98
|
+
},
|
|
99
|
+
secondary: {
|
|
100
|
+
color: 'red',
|
|
101
|
+
},
|
|
78
102
|
success: {
|
|
79
|
-
|
|
80
|
-
style: { color: 'green' }, // You can also use inline style
|
|
103
|
+
color: 'green',
|
|
81
104
|
},
|
|
82
105
|
},
|
|
83
106
|
},
|
|
84
107
|
})
|
|
85
108
|
|
|
86
109
|
button({ color: 'secondary' })
|
|
87
|
-
|
|
88
|
-
* Result:
|
|
89
|
-
* {
|
|
90
|
-
* className: 'font-bold rounded-sm bg-purple-500 hover:bg-purple-700',
|
|
91
|
-
* style: {},
|
|
92
|
-
* }
|
|
93
|
-
*/
|
|
94
|
-
|
|
95
|
-
button({ color: 'success' })
|
|
96
|
-
/**
|
|
97
|
-
* Result:
|
|
98
|
-
* {
|
|
99
|
-
* className: 'font-bold rounded-sm hover:bg-green-700',
|
|
100
|
-
* style: { color: 'green' },
|
|
101
|
-
* }
|
|
102
|
-
*/
|
|
110
|
+
// => { fontWeight: 700, color: 'red' }
|
|
103
111
|
```
|
|
104
112
|
|
|
105
113
|
### Multiple variants
|
|
@@ -110,10 +118,7 @@ You can add multiple variants to a single component.
|
|
|
110
118
|
import { cv } from 'css-variants'
|
|
111
119
|
|
|
112
120
|
const button = cv({
|
|
113
|
-
base:
|
|
114
|
-
className: 'font-bold',
|
|
115
|
-
style: { borderRadius: 16 },
|
|
116
|
-
},
|
|
121
|
+
base: 'font-bold',
|
|
117
122
|
variants: {
|
|
118
123
|
color: {
|
|
119
124
|
primary: 'bg-blue-500 hover:bg-blue-700',
|
|
@@ -129,13 +134,7 @@ const button = cv({
|
|
|
129
134
|
})
|
|
130
135
|
|
|
131
136
|
button({ color: 'success', size: 'lg' })
|
|
132
|
-
|
|
133
|
-
* Result:
|
|
134
|
-
* {
|
|
135
|
-
* className: 'font-bold bg-green-500 hover:bg-green-700 text-lg p-6',
|
|
136
|
-
* style: { borderRadius: 16 },
|
|
137
|
-
* }
|
|
138
|
-
*/
|
|
137
|
+
// => 'font-bold bg-green-500 hover:bg-green-700 text-lg p-6'
|
|
139
138
|
```
|
|
140
139
|
|
|
141
140
|
### Boolean variants
|
|
@@ -146,9 +145,6 @@ You can also add boolean variants to a component. This is useful when you want t
|
|
|
146
145
|
import { cv } from 'css-variants'
|
|
147
146
|
|
|
148
147
|
const button = cv({
|
|
149
|
-
base: {
|
|
150
|
-
style: { fontWeight: 'bold' },
|
|
151
|
-
},
|
|
152
148
|
variants: {
|
|
153
149
|
color: {
|
|
154
150
|
primary: 'bg-blue-500 hover:bg-blue-700',
|
|
@@ -162,13 +158,7 @@ const button = cv({
|
|
|
162
158
|
})
|
|
163
159
|
|
|
164
160
|
button({ disabled: true })
|
|
165
|
-
|
|
166
|
-
* Result:
|
|
167
|
-
* {
|
|
168
|
-
* className: 'opacity-50 pointer-events-none',
|
|
169
|
-
* style: { fontWeight: 'bold' },
|
|
170
|
-
* }
|
|
171
|
-
*/
|
|
161
|
+
// => 'opacity-50 pointer-events-none'
|
|
172
162
|
```
|
|
173
163
|
|
|
174
164
|
### Compound variants
|
|
@@ -179,17 +169,11 @@ Sometimes you might want to add a variant that depends on another variant. This
|
|
|
179
169
|
import { cv } from 'css-variants'
|
|
180
170
|
|
|
181
171
|
const button = cv({
|
|
182
|
-
base: {
|
|
183
|
-
style: { fontWeight: 'bold' },
|
|
184
|
-
},
|
|
185
172
|
variants: {
|
|
186
173
|
size: {
|
|
187
174
|
sm: 'text-sm p-2',
|
|
188
175
|
md: 'text-md p-4',
|
|
189
|
-
lg:
|
|
190
|
-
className: 'text-lg',
|
|
191
|
-
style: { padding: 6 },
|
|
192
|
-
},
|
|
176
|
+
lg: 'text-lg',
|
|
193
177
|
},
|
|
194
178
|
disabled: {
|
|
195
179
|
true: 'opacity-50 pointer-events-none',
|
|
@@ -200,19 +184,12 @@ const button = cv({
|
|
|
200
184
|
size: 'lg', // You can also use the values as an array
|
|
201
185
|
disabled: true,
|
|
202
186
|
className: 'uppercase',
|
|
203
|
-
style: { padding: 5 },
|
|
204
187
|
}
|
|
205
188
|
],
|
|
206
189
|
})
|
|
207
190
|
|
|
208
191
|
button({ size: 'lg', disabled: true })
|
|
209
|
-
|
|
210
|
-
* Result:
|
|
211
|
-
* {
|
|
212
|
-
* className: 'text-lg p-6 opacity-50 pointer-events-none uppercase',
|
|
213
|
-
* style: { fontWeight: 'bold', padding: 5 },
|
|
214
|
-
* }
|
|
215
|
-
*/
|
|
192
|
+
// => 'text-lg p-6 opacity-50 pointer-events-none uppercase'
|
|
216
193
|
```
|
|
217
194
|
|
|
218
195
|
### Default variants
|
|
@@ -237,13 +214,7 @@ const button = cv({
|
|
|
237
214
|
})
|
|
238
215
|
|
|
239
216
|
button()
|
|
240
|
-
|
|
241
|
-
* Result:
|
|
242
|
-
* {
|
|
243
|
-
* className: 'font-bold rounded-sm bg-blue-500 hover:bg-blue-700',
|
|
244
|
-
* style: {},
|
|
245
|
-
* }
|
|
246
|
-
*/
|
|
217
|
+
// => 'font-bold rounded-sm bg-blue-500 hover:bg-blue-700'
|
|
247
218
|
```
|
|
248
219
|
|
|
249
220
|
## Slots
|
|
@@ -255,16 +226,13 @@ Slots allows you to separate a component into multiple parts.
|
|
|
255
226
|
You can add `slots` by using the slots key. There's no limit to how many slots you can add.
|
|
256
227
|
|
|
257
228
|
```ts
|
|
258
|
-
import {
|
|
229
|
+
import { scv } from 'css-variants'
|
|
259
230
|
|
|
260
|
-
const notification =
|
|
231
|
+
const notification = scv({
|
|
261
232
|
slots: ['root', 'title'],
|
|
262
233
|
base: {
|
|
263
234
|
root: 'root',
|
|
264
|
-
title:
|
|
265
|
-
className: 'title',
|
|
266
|
-
style: { fontSize: 16 },
|
|
267
|
-
},
|
|
235
|
+
title: 'title',
|
|
268
236
|
},
|
|
269
237
|
})
|
|
270
238
|
|
|
@@ -272,17 +240,35 @@ notification()
|
|
|
272
240
|
/**
|
|
273
241
|
* Result:
|
|
274
242
|
* {
|
|
275
|
-
* root:
|
|
276
|
-
*
|
|
277
|
-
* style: {},
|
|
278
|
-
* },
|
|
279
|
-
* title: {
|
|
280
|
-
* className: 'title',
|
|
281
|
-
* style: { fontSize: 16 },
|
|
282
|
-
* },
|
|
243
|
+
* root: 'root',
|
|
244
|
+
* title: 'title',
|
|
283
245
|
* }
|
|
284
246
|
*/
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
```ts
|
|
250
|
+
import { ssv } from 'css-variants'
|
|
285
251
|
|
|
252
|
+
const notification = ssv({
|
|
253
|
+
slots: ['root', 'title'],
|
|
254
|
+
base: {
|
|
255
|
+
root: {
|
|
256
|
+
fontWeight: 'bold',
|
|
257
|
+
},
|
|
258
|
+
title: {
|
|
259
|
+
fontSize: '20px',
|
|
260
|
+
}
|
|
261
|
+
},
|
|
262
|
+
})
|
|
263
|
+
|
|
264
|
+
notification()
|
|
265
|
+
/**
|
|
266
|
+
* Result:
|
|
267
|
+
* {
|
|
268
|
+
* root: { fontWeight: 'bold' },
|
|
269
|
+
* title: { fontSize: '20px' },
|
|
270
|
+
* }
|
|
271
|
+
*/
|
|
286
272
|
```
|
|
287
273
|
|
|
288
274
|
### Slots with variants
|
|
@@ -297,10 +283,7 @@ const notification = cv({
|
|
|
297
283
|
base: {
|
|
298
284
|
root: 'root',
|
|
299
285
|
title: 'title',
|
|
300
|
-
content:
|
|
301
|
-
className: 'content',
|
|
302
|
-
style: { fontSize: 16 },
|
|
303
|
-
},
|
|
286
|
+
content: 'content',
|
|
304
287
|
},
|
|
305
288
|
variants: {
|
|
306
289
|
color: {
|
|
@@ -321,18 +304,9 @@ notification({ color: 'primary' })
|
|
|
321
304
|
/**
|
|
322
305
|
* Result:
|
|
323
306
|
* {
|
|
324
|
-
* root:
|
|
325
|
-
*
|
|
326
|
-
*
|
|
327
|
-
* },
|
|
328
|
-
* title: {
|
|
329
|
-
* className: 'title title-primary',
|
|
330
|
-
* style: {},
|
|
331
|
-
* },
|
|
332
|
-
* content: {
|
|
333
|
-
* className: 'content content-primary',
|
|
334
|
-
* style: { fontSize: 16 },
|
|
335
|
-
* },
|
|
307
|
+
* root: 'root root-primary',
|
|
308
|
+
* title: 'title title-primary',
|
|
309
|
+
* content: 'content content-primary',
|
|
336
310
|
* }
|
|
337
311
|
*/
|
|
338
312
|
|
|
@@ -340,18 +314,9 @@ notification({ color: 'secondary' })
|
|
|
340
314
|
/**
|
|
341
315
|
* Result:
|
|
342
316
|
* {
|
|
343
|
-
* root:
|
|
344
|
-
*
|
|
345
|
-
*
|
|
346
|
-
* },
|
|
347
|
-
* title: {
|
|
348
|
-
* className: 'title title-secondary',
|
|
349
|
-
* style: {},
|
|
350
|
-
* },
|
|
351
|
-
* content: {
|
|
352
|
-
* className: 'content content-secondary',
|
|
353
|
-
* style: { fontSize: 16 },
|
|
354
|
-
* },
|
|
317
|
+
* root: 'root',
|
|
318
|
+
* title: 'title title-secondary',
|
|
319
|
+
* content: 'content content-secondary',
|
|
355
320
|
* }
|
|
356
321
|
*/
|
|
357
322
|
```
|
|
@@ -380,18 +345,34 @@ const button = cv({
|
|
|
380
345
|
button({
|
|
381
346
|
color: 'secondary',
|
|
382
347
|
className: 'border-purple-600',
|
|
383
|
-
|
|
384
|
-
|
|
348
|
+
})
|
|
349
|
+
// => 'bg-purple-500 hover:bg-purple-700 border-purple-600'
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
```ts
|
|
353
|
+
import { sv } from 'css-variants'
|
|
354
|
+
|
|
355
|
+
const button = cv({
|
|
356
|
+
base: {
|
|
357
|
+
fontWeight: 700,
|
|
385
358
|
},
|
|
359
|
+
variants: {
|
|
360
|
+
color: {
|
|
361
|
+
primary: {
|
|
362
|
+
color: 'blue',
|
|
363
|
+
},
|
|
364
|
+
secondary: {
|
|
365
|
+
color: 'purple',
|
|
366
|
+
},
|
|
367
|
+
}
|
|
368
|
+
}
|
|
386
369
|
})
|
|
387
370
|
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
* }
|
|
394
|
-
*/
|
|
371
|
+
button({
|
|
372
|
+
color: 'secondary',
|
|
373
|
+
style: { color: 'red' },
|
|
374
|
+
})
|
|
375
|
+
// => { fontWeight: 700, color: 'red' }
|
|
395
376
|
```
|
|
396
377
|
|
|
397
378
|
### Overriding styles on a component with slots
|
|
@@ -405,10 +386,7 @@ const notification = cv({
|
|
|
405
386
|
slots: ['root', 'title'],
|
|
406
387
|
base: {
|
|
407
388
|
root: 'root',
|
|
408
|
-
title:
|
|
409
|
-
className: 'title',
|
|
410
|
-
style: { fontSize: 16 },
|
|
411
|
-
},
|
|
389
|
+
title: 'title',
|
|
412
390
|
},
|
|
413
391
|
})
|
|
414
392
|
|
|
@@ -416,53 +394,38 @@ notification({
|
|
|
416
394
|
classNames: {
|
|
417
395
|
root: 'root-custom-class',
|
|
418
396
|
},
|
|
419
|
-
styles: {
|
|
420
|
-
title: {
|
|
421
|
-
fontSize: 20,
|
|
422
|
-
}
|
|
423
|
-
},
|
|
424
397
|
})
|
|
425
398
|
/**
|
|
426
399
|
* Result:
|
|
427
400
|
* {
|
|
428
|
-
* root:
|
|
429
|
-
*
|
|
430
|
-
* style: {},
|
|
431
|
-
* },
|
|
432
|
-
* title: {
|
|
433
|
-
* className: 'title',
|
|
434
|
-
* style: { fontSize: 20 },
|
|
435
|
-
* },
|
|
401
|
+
* root: 'root root-custom-class',
|
|
402
|
+
* title: 'title',
|
|
436
403
|
* }
|
|
437
404
|
*/
|
|
438
405
|
|
|
439
406
|
```
|
|
440
407
|
|
|
441
|
-
##
|
|
442
|
-
|
|
443
|
-
Although `css-variants` is designed to help you avoid styling conflicts, there's still a small margin of error when combining multiple classes or variants. To further minimize these conflicts and ensure consistent styling, you can integrate `tailwind-merge` into your project.
|
|
408
|
+
## Custom function to resolve class names
|
|
444
409
|
|
|
445
|
-
|
|
410
|
+
Default is the internal 'cx' function. The resolver function should accept multiple class name arguments and return a single concatenated string.
|
|
446
411
|
|
|
447
|
-
|
|
412
|
+
You can use this to integrate with class name utilities like clsx, classnames, or your own custom class name resolution logic.
|
|
448
413
|
|
|
449
414
|
```ts
|
|
450
|
-
import {
|
|
451
|
-
import {
|
|
415
|
+
import { clsx } from 'clsx'
|
|
416
|
+
import { cv as _cv, scv as _scv } from 'css-variants'
|
|
452
417
|
|
|
453
|
-
export const
|
|
454
|
-
|
|
455
|
-
export const cv: typeof baseCv = (config) => {
|
|
456
|
-
return baseCv({
|
|
418
|
+
export const cv: typeof _cv = (config) => {
|
|
419
|
+
return _cv({
|
|
457
420
|
...config,
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
className: twMerge(className),
|
|
462
|
-
}
|
|
421
|
+
classNameResolver: clsx,
|
|
422
|
+
})
|
|
423
|
+
}
|
|
463
424
|
|
|
464
|
-
|
|
465
|
-
|
|
425
|
+
export const scv: typeof _scv = (config) => {
|
|
426
|
+
return _scv({
|
|
427
|
+
...config,
|
|
428
|
+
classNameResolver: clsx,
|
|
466
429
|
})
|
|
467
430
|
}
|
|
468
431
|
```
|
|
@@ -471,11 +434,8 @@ export const cv: typeof baseCv = (config) => {
|
|
|
471
434
|
|
|
472
435
|
### Extracting Variant Types
|
|
473
436
|
|
|
474
|
-
You can use the `VariantProps` utility to extract variants from a component.
|
|
475
|
-
|
|
476
437
|
```tsx
|
|
477
|
-
import {
|
|
478
|
-
import { cv } from 'class-variance-authority'
|
|
438
|
+
import { cv } from 'css-variants'
|
|
479
439
|
|
|
480
440
|
export const button = cv({
|
|
481
441
|
variants: {
|
|
@@ -486,7 +446,7 @@ export const button = cv({
|
|
|
486
446
|
},
|
|
487
447
|
})
|
|
488
448
|
|
|
489
|
-
export type ButtonProps =
|
|
449
|
+
export type ButtonProps = Parameters<typeof button>[0]
|
|
490
450
|
```
|
|
491
451
|
|
|
492
452
|
## Contribute
|
package/dist/cjs/cv.d.ts
CHANGED
|
@@ -1,3 +1,49 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { ObjectKeyPicker, ObjectKeyArrayPicker } from './utils/types';
|
|
2
|
+
import { cx, ClassValue } from './cx';
|
|
3
|
+
export type ClassVariantRecord = Record<string, Record<string, ClassValue>>;
|
|
4
|
+
export interface ClassVariantDefinition<T extends ClassVariantRecord | undefined> {
|
|
5
|
+
base?: ClassValue;
|
|
6
|
+
variants?: T;
|
|
7
|
+
compoundVariants?: (ObjectKeyArrayPicker<T> & {
|
|
8
|
+
className: ClassValue;
|
|
9
|
+
})[];
|
|
10
|
+
defaultVariants?: ObjectKeyPicker<T>;
|
|
11
|
+
classNameResolver?: typeof cx;
|
|
12
|
+
}
|
|
13
|
+
export type ClassVariantFn<T extends ClassVariantRecord | undefined> = (props?: ObjectKeyPicker<T> & {
|
|
14
|
+
className?: ClassValue;
|
|
15
|
+
}) => string;
|
|
16
|
+
export type ClassVariantCreatorFn = <T extends ClassVariantRecord | undefined>(config: ClassVariantDefinition<T>) => ClassVariantFn<T>;
|
|
17
|
+
/**
|
|
18
|
+
* Creates a class variant function that combines base classes, variants, compound variants, and default variants.
|
|
19
|
+
*
|
|
20
|
+
* @template T - Type of the variant record
|
|
21
|
+
* @param config - Configuration object for creating class variants
|
|
22
|
+
* @returns A function that accepts variant props and returns a combined class string
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* const button = cv({
|
|
27
|
+
* base: 'px-4 py-2 rounded',
|
|
28
|
+
* variants: {
|
|
29
|
+
* color: {
|
|
30
|
+
* primary: 'bg-blue-500 text-white',
|
|
31
|
+
* secondary: 'bg-gray-500 text-white'
|
|
32
|
+
* },
|
|
33
|
+
* size: {
|
|
34
|
+
* sm: 'text-sm',
|
|
35
|
+
* lg: 'text-lg'
|
|
36
|
+
* }
|
|
37
|
+
* },
|
|
38
|
+
* defaultVariants: {
|
|
39
|
+
* color: 'primary',
|
|
40
|
+
* size: 'sm'
|
|
41
|
+
* }
|
|
42
|
+
* });
|
|
43
|
+
*
|
|
44
|
+
* button(); // => 'px-4 py-2 rounded bg-blue-500 text-white text-sm'
|
|
45
|
+
* button({ color: 'secondary' }); // => 'px-4 py-2 rounded bg-gray-500 text-white text-sm'
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export declare const cv: ClassVariantCreatorFn;
|
|
3
49
|
export default cv;
|
package/dist/cjs/cv.js
CHANGED
|
@@ -1,56 +1,68 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.cv = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
4
|
+
const compact_1 = require("./utils/compact");
|
|
5
|
+
const entries_1 = require("./utils/entries");
|
|
6
|
+
const cx_1 = require("./cx");
|
|
7
|
+
/**
|
|
8
|
+
* Creates a class variant function that combines base classes, variants, compound variants, and default variants.
|
|
9
|
+
*
|
|
10
|
+
* @template T - Type of the variant record
|
|
11
|
+
* @param config - Configuration object for creating class variants
|
|
12
|
+
* @returns A function that accepts variant props and returns a combined class string
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const button = cv({
|
|
17
|
+
* base: 'px-4 py-2 rounded',
|
|
18
|
+
* variants: {
|
|
19
|
+
* color: {
|
|
20
|
+
* primary: 'bg-blue-500 text-white',
|
|
21
|
+
* secondary: 'bg-gray-500 text-white'
|
|
22
|
+
* },
|
|
23
|
+
* size: {
|
|
24
|
+
* sm: 'text-sm',
|
|
25
|
+
* lg: 'text-lg'
|
|
26
|
+
* }
|
|
27
|
+
* },
|
|
28
|
+
* defaultVariants: {
|
|
29
|
+
* color: 'primary',
|
|
30
|
+
* size: 'sm'
|
|
31
|
+
* }
|
|
32
|
+
* });
|
|
33
|
+
*
|
|
34
|
+
* button(); // => 'px-4 py-2 rounded bg-blue-500 text-white text-sm'
|
|
35
|
+
* button({ color: 'secondary' }); // => 'px-4 py-2 rounded bg-gray-500 text-white text-sm'
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
20
38
|
const cv = (config) => {
|
|
21
|
-
const { base, variants, compoundVariants, defaultVariants,
|
|
39
|
+
const { base, variants, compoundVariants, defaultVariants, classNameResolver = cx_1.cx } = config;
|
|
22
40
|
return (props) => {
|
|
23
|
-
const { className
|
|
24
|
-
const
|
|
25
|
-
const
|
|
26
|
-
let tmp;
|
|
41
|
+
const { className, ...rest } = props ?? {};
|
|
42
|
+
const mergedProps = { ...defaultVariants, ...(0, compact_1.compact)(rest) };
|
|
43
|
+
const classValues = [];
|
|
27
44
|
if (base) {
|
|
28
|
-
push(
|
|
45
|
+
classValues.push(base);
|
|
29
46
|
}
|
|
30
47
|
if (variants) {
|
|
31
|
-
for (const [key,
|
|
32
|
-
|
|
33
|
-
|
|
48
|
+
for (const [key, value] of (0, entries_1.entries)(mergedProps)) {
|
|
49
|
+
const classValue = variants[key][value];
|
|
50
|
+
if (classValue) {
|
|
51
|
+
classValues.push(classValue);
|
|
34
52
|
}
|
|
35
53
|
}
|
|
36
54
|
}
|
|
37
55
|
if (compoundVariants) {
|
|
38
|
-
for (const { className:
|
|
39
|
-
if ((0,
|
|
40
|
-
push(
|
|
56
|
+
for (const { className: classValue, ...compoundVariant } of compoundVariants) {
|
|
57
|
+
if ((0, entries_1.entries)(compoundVariant).every(([key, value]) => Array.isArray(value) ? value.includes(mergedProps[key]) : value === mergedProps[key])) {
|
|
58
|
+
classValues.push(classValue);
|
|
41
59
|
}
|
|
42
60
|
}
|
|
43
61
|
}
|
|
44
|
-
if (
|
|
45
|
-
|
|
46
|
-
css.className += propClassName;
|
|
47
|
-
}
|
|
48
|
-
if (propStyle) {
|
|
49
|
-
css.style = { ...css.style, ...propStyle };
|
|
62
|
+
if (className) {
|
|
63
|
+
classValues.push(className);
|
|
50
64
|
}
|
|
51
|
-
|
|
52
|
-
return onDone(css);
|
|
53
|
-
return css;
|
|
65
|
+
return classNameResolver(...classValues);
|
|
54
66
|
};
|
|
55
67
|
};
|
|
56
68
|
exports.cv = cv;
|
package/dist/cjs/cv.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cv.js","sourceRoot":"","sources":["../../src/cv.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"cv.js","sourceRoot":"","sources":["../../src/cv.ts"],"names":[],"mappings":";;;AACA,6CAAyC;AACzC,6CAAyC;AACzC,6BAAqC;AAoBrC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACI,MAAM,EAAE,GAA0B,CAAC,MAAM,EAAE,EAAE;IAClD,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,gBAAgB,EAAE,eAAe,EAAE,iBAAiB,GAAG,OAAE,EAAE,GAAG,MAAM,CAAA;IAC5F,OAAO,CAAC,KAAK,EAAE,EAAE;QACf,MAAM,EAAE,SAAS,EAAE,GAAG,IAAI,EAAE,GAAG,KAAK,IAAI,EAAE,CAAA;QAE1C,MAAM,WAAW,GAAG,EAAE,GAAG,eAAe,EAAE,GAAG,IAAA,iBAAO,EAAC,IAAI,CAAC,EAAE,CAAA;QAE5D,MAAM,WAAW,GAAiB,EAAE,CAAA;QAEpC,IAAI,IAAI,EAAE,CAAC;YACT,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QACxB,CAAC;QAED,IAAI,QAAQ,EAAE,CAAC;YACb,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,IAAA,iBAAO,EAAC,WAAW,CAAC,EAAE,CAAC;gBAChD,MAAM,UAAU,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC,KAAe,CAAC,CAAA;gBACjD,IAAI,UAAU,EAAE,CAAC;oBACf,WAAW,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;gBAC9B,CAAC;YACH,CAAC;QACH,CAAC;QAED,IAAI,gBAAgB,EAAE,CAAC;YACrB,KAAK,MAAM,EAAE,SAAS,EAAE,UAAU,EAAE,GAAG,eAAe,EAAE,IAAI,gBAAgB,EAAE,CAAC;gBAC7E,IACE,IAAA,iBAAO,EAAC,eAAe,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE,CAC9C,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,KAAK,WAAW,CAAC,GAAG,CAAC,CACrF,EACD,CAAC;oBACD,WAAW,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;gBAC9B,CAAC;YACH,CAAC;QACH,CAAC;QAED,IAAI,SAAS,EAAE,CAAC;YACd,WAAW,CAAC,IAAI,CAAC,SAAS,CAAC,CAAA;QAC7B,CAAC;QAED,OAAO,iBAAiB,CAAC,GAAG,WAAW,CAAC,CAAA;IAC1C,CAAC,CAAA;AACH,CAAC,CAAA;AAxCY,QAAA,EAAE,MAwCd;AAED,kBAAe,UAAE,CAAA"}
|
package/dist/cjs/cx.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
type
|
|
2
|
-
export
|
|
1
|
+
export type ClassDictionary = Record<string, boolean | string | number>;
|
|
2
|
+
export type ClassValue = ClassValue[] | string | number | bigint | ClassDictionary | null | boolean | undefined;
|
|
3
|
+
export type ClassArray = ClassValue[];
|
|
4
|
+
export declare function cx(...args: ClassValue[]): string;
|
|
3
5
|
export default cx;
|