css-variants 1.1.1 → 1.1.3
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 +97 -6
- package/dist/cjs/types.d.ts +3 -1
- package/dist/cjs/utils.js +1 -1
- package/dist/cjs/utils.js.map +1 -1
- package/dist/esm/types.d.ts +3 -1
- package/dist/esm/utils.js +1 -1
- package/dist/esm/utils.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,7 +13,13 @@ A lightweight, flexible API for managing CSS class variants in JavaScript and Ty
|
|
|
13
13
|
|
|
14
14
|
`css-variants` provides a simple yet powerful way to handle dynamic class names and inline styles based on component props or state. It's designed to work seamlessly with modern JavaScript frameworks and CSS methodologies, offering a type-safe approach to styling your UI components.
|
|
15
15
|
|
|
16
|
-
`css-variants` is heavily inspired by
|
|
16
|
+
`css-variants` is heavily inspired by the following excellent packages:
|
|
17
|
+
|
|
18
|
+
- [CVA (Class Variance Authority)](https://github.com/joe-bell/cva): A powerful utility for creating dynamic class names with variants.
|
|
19
|
+
- [Tailwind Variants](https://github.com/nextui-org/tailwind-variants): A flexible and reusable variant system for Tailwind CSS.
|
|
20
|
+
- [Panda CSS](https://github.com/chakra-ui/panda): A CSS-in-JS solution with a focus on developer experience and performance.
|
|
21
|
+
|
|
22
|
+
Thank you to the authors and contributors of these projects for their innovative work.
|
|
17
23
|
|
|
18
24
|
## Features
|
|
19
25
|
|
|
@@ -35,6 +41,9 @@ A lightweight, flexible API for managing CSS class variants in JavaScript and Ty
|
|
|
35
41
|
* [Slots](#events)
|
|
36
42
|
* [Basic Usage](#basic-usage)
|
|
37
43
|
* [Slots with variants](#slots-with-variants)
|
|
44
|
+
* [Overriding styles](#overriding-styles)
|
|
45
|
+
* [Overriding styles on a single component](#overriding-styles-on-a-single-component)
|
|
46
|
+
* [Overriding styles on a component with slots](#overriding-styles-on-a-component-with-slots)
|
|
38
47
|
* [Handling Style Conflicts](#handling-style-conflicts)
|
|
39
48
|
* [TypeScript](#typeScript)
|
|
40
49
|
* [Contribute](#contribute)
|
|
@@ -257,7 +266,7 @@ const notification = cv({
|
|
|
257
266
|
style: { fontSize: 16 },
|
|
258
267
|
},
|
|
259
268
|
},
|
|
260
|
-
})
|
|
269
|
+
})
|
|
261
270
|
|
|
262
271
|
notification()
|
|
263
272
|
/**
|
|
@@ -306,7 +315,7 @@ const notification = cv({
|
|
|
306
315
|
},
|
|
307
316
|
}
|
|
308
317
|
},
|
|
309
|
-
})
|
|
318
|
+
})
|
|
310
319
|
|
|
311
320
|
notification({ color: 'primary' })
|
|
312
321
|
/**
|
|
@@ -347,13 +356,95 @@ notification({ color: 'secondary' })
|
|
|
347
356
|
*/
|
|
348
357
|
```
|
|
349
358
|
|
|
359
|
+
## Overriding styles
|
|
360
|
+
|
|
361
|
+
`css-variants` allows you to override or extend the styles of your components. This feature is useful when you need to add custom styles or modify existing ones without changing the original component definition.
|
|
362
|
+
|
|
363
|
+
### Overriding styles on a single component
|
|
364
|
+
|
|
365
|
+
You can override or extend styles for a single component by passing additional `className` and `style` properties when calling the component function. These will be merged with the existing styles.
|
|
366
|
+
|
|
367
|
+
```ts
|
|
368
|
+
import { cv } from 'css-variants'
|
|
369
|
+
|
|
370
|
+
const button = cv({
|
|
371
|
+
base: 'font-semibold',
|
|
372
|
+
variants: {
|
|
373
|
+
color: {
|
|
374
|
+
primary: 'bg-blue-500 hover:bg-blue-700',
|
|
375
|
+
secondary: 'bg-purple-500 hover:bg-purple-700',
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
})
|
|
379
|
+
|
|
380
|
+
button({
|
|
381
|
+
color: 'secondary',
|
|
382
|
+
className: 'border-purple-600',
|
|
383
|
+
style: {
|
|
384
|
+
color: 'purple',
|
|
385
|
+
},
|
|
386
|
+
})
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* Result:
|
|
390
|
+
* {
|
|
391
|
+
* className: 'bg-purple-500 hover:bg-purple-700 border-purple-600',
|
|
392
|
+
* style: { color: 'purple' },
|
|
393
|
+
* }
|
|
394
|
+
*/
|
|
395
|
+
```
|
|
396
|
+
|
|
397
|
+
### Overriding styles on a component with slots
|
|
398
|
+
|
|
399
|
+
For components with slots, you can override styles using the `classNames` and `styles` properties. These allow you to target specific slots and apply custom classes or inline styles.
|
|
400
|
+
|
|
401
|
+
```ts
|
|
402
|
+
import { csv } from 'css-variants'
|
|
403
|
+
|
|
404
|
+
const notification = cv({
|
|
405
|
+
slots: ['root', 'title'],
|
|
406
|
+
base: {
|
|
407
|
+
root: 'root',
|
|
408
|
+
title: {
|
|
409
|
+
className: 'title',
|
|
410
|
+
style: { fontSize: 16 },
|
|
411
|
+
},
|
|
412
|
+
},
|
|
413
|
+
})
|
|
414
|
+
|
|
415
|
+
notification({
|
|
416
|
+
classNames: {
|
|
417
|
+
root: 'root-custom-class',
|
|
418
|
+
},
|
|
419
|
+
styles: {
|
|
420
|
+
title: {
|
|
421
|
+
fontSize: 20,
|
|
422
|
+
}
|
|
423
|
+
},
|
|
424
|
+
})
|
|
425
|
+
/**
|
|
426
|
+
* Result:
|
|
427
|
+
* {
|
|
428
|
+
* root: {
|
|
429
|
+
* className: 'root-custom-class',
|
|
430
|
+
* style: {},
|
|
431
|
+
* },
|
|
432
|
+
* title: {
|
|
433
|
+
* className: 'title',
|
|
434
|
+
* style: { fontSize: 20 },
|
|
435
|
+
* },
|
|
436
|
+
* }
|
|
437
|
+
*/
|
|
438
|
+
|
|
439
|
+
```
|
|
440
|
+
|
|
350
441
|
## Handling Style Conflicts
|
|
351
442
|
|
|
352
|
-
Although `css-variants` is designed to help you avoid styling conflicts, there's still a small margin of error.
|
|
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.
|
|
353
444
|
|
|
354
|
-
|
|
445
|
+
`tailwind-merge` is a utility that intelligently combines Tailwind CSS classes, resolving conflicts by giving precedence to the latter class when two classes target the same style property. By incorporating `tailwind-merge` with `css-variants`, you can create more robust components that automatically handle class conflicts.
|
|
355
446
|
|
|
356
|
-
|
|
447
|
+
The following example demonstrates how to extend functions from `css-variants` to use `tailwind-merge`. This integration ensures that your components will have consistent styling, even when multiple classes or variants are applied.
|
|
357
448
|
|
|
358
449
|
```ts
|
|
359
450
|
import { twMerge } from 'tailwind-merge'
|
package/dist/cjs/types.d.ts
CHANGED
|
@@ -5,7 +5,9 @@ export type PartialRecord<S extends string, T> = Partial<Record<S, T>>;
|
|
|
5
5
|
export type RequireAtLeastOne<T> = {
|
|
6
6
|
[K in keyof T]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<keyof T, K>>>;
|
|
7
7
|
}[keyof T];
|
|
8
|
-
export type CssProperties = Properties<string | number
|
|
8
|
+
export type CssProperties = Properties<string | number> & {
|
|
9
|
+
[key: `--${string}`]: string | number;
|
|
10
|
+
};
|
|
9
11
|
export type VariantStyle = {
|
|
10
12
|
className: string;
|
|
11
13
|
style: CssProperties;
|
package/dist/cjs/utils.js
CHANGED
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.match = exports.compact = exports.entries = void 0;
|
|
4
4
|
exports.fromEntries = fromEntries;
|
|
5
5
|
const entries = (obj) => {
|
|
6
|
-
return Object.
|
|
6
|
+
return Object.keys(obj).map((key) => [key, obj[key]]);
|
|
7
7
|
};
|
|
8
8
|
exports.entries = entries;
|
|
9
9
|
function fromEntries(entries) {
|
package/dist/cjs/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":";;;AAWA,kCAMC;AAjBM,MAAM,OAAO,GAAG,CAMrB,GAAM,EACI,EAAE;IACZ,OAAO,MAAM,CAAC,
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":";;;A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xB,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"}
|
package/dist/esm/types.d.ts
CHANGED
|
@@ -5,7 +5,9 @@ export type PartialRecord<S extends string, T> = Partial<Record<S, T>>;
|
|
|
5
5
|
export type RequireAtLeastOne<T> = {
|
|
6
6
|
[K in keyof T]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<keyof T, K>>>;
|
|
7
7
|
}[keyof T];
|
|
8
|
-
export type CssProperties = Properties<string | number
|
|
8
|
+
export type CssProperties = Properties<string | number> & {
|
|
9
|
+
[key: `--${string}`]: string | number;
|
|
10
|
+
};
|
|
9
11
|
export type VariantStyle = {
|
|
10
12
|
className: string;
|
|
11
13
|
style: CssProperties;
|
package/dist/esm/utils.js
CHANGED
package/dist/esm/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,OAAO,GAAG,CAMrB,GAAM,EACI,EAAE;IACZ,OAAO,MAAM,CAAC,
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,OAAO,GAAG,CAMrB,GAAM,EACI,EAAE;IACZ,OAAO,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,GAAG,EAAE,GAAG,CAAC,GAAS,CAAC,CAAC,CAAa,CAAA;AACzE,CAAC,CAAA;AAED,MAAM,UAAU,WAAW,CAAwC,OAAiB;IAClF,MAAM,MAAM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