klsx 0.2.1 → 0.2.2
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 +32 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
A simple replacement for [clsx](https://www.npmjs.com/package/clsx) **_you may not actually need_** (see the [benchmark](https://github.com/vuolter/klsx/blob/main/BENCHMARK.md))
|
|
4
4
|
|
|
5
|
-
Used to construct `
|
|
5
|
+
Used to construct `class` conditionally with strings, arrays and key-value objects.
|
|
6
6
|
|
|
7
7
|
Developed as a case study, [Bun](https://bun.com) friendly. 🌿
|
|
8
8
|
|
|
@@ -34,19 +34,42 @@ npm install klsx
|
|
|
34
34
|
```typescript
|
|
35
35
|
import { klsx } from 'klsx'
|
|
36
36
|
|
|
37
|
-
klsx('foo', 'bar')
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
klsx('foo', 'bar', 'baz')
|
|
38
|
+
// => "foo bar baz"
|
|
39
|
+
|
|
40
|
+
klsx({foo: 'string', bar: true, bux: null }, 'baz')
|
|
41
|
+
// => "foo bar baz"
|
|
42
|
+
|
|
43
|
+
klsx(['foo', ['bar', true && 'baz']])
|
|
44
|
+
// => "foo bar baz"
|
|
40
45
|
```
|
|
41
46
|
|
|
42
|
-
or
|
|
47
|
+
or importing the short alias `cn`:
|
|
43
48
|
|
|
44
|
-
|
|
49
|
+
### React
|
|
50
|
+
|
|
51
|
+
```jsx
|
|
52
|
+
import React, { useState } from 'react';
|
|
45
53
|
import { cn } from 'klsx'
|
|
46
54
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
55
|
+
const MyComponent = () => {
|
|
56
|
+
const [isActive, setIsActive] = useState(false);
|
|
57
|
+
return (
|
|
58
|
+
<div>
|
|
59
|
+
<div className={cn('base-class', isActive ? 'active' : 'inactive')}>
|
|
60
|
+
{/* ... */}
|
|
61
|
+
</div>
|
|
62
|
+
|
|
63
|
+
<div className={cn('base-class', {'active': isActive, 'inactive': !isActive})}>
|
|
64
|
+
{/* ... */}
|
|
65
|
+
</div>
|
|
66
|
+
|
|
67
|
+
<div className={cn('base-class', [isActive && 'active', !isActive && 'inactive'])}>
|
|
68
|
+
{/* ... */}
|
|
69
|
+
</div>
|
|
70
|
+
</div>
|
|
71
|
+
);
|
|
72
|
+
};
|
|
50
73
|
```
|
|
51
74
|
|
|
52
75
|
## Integrations
|