You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+59-30Lines changed: 59 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,27 +6,27 @@ It helps unclutter your markup when using utility-driven CSS principles or frame
6
6
7
7
### Installation
8
8
9
-
```
9
+
```shell
10
10
npm i -D classgroup
11
11
```
12
12
13
13
### Usage
14
14
15
15
To use ClassGroup, import it as you would any other utility:
16
16
17
-
```
17
+
```js
18
18
importClassGroupfrom'classgroup';
19
19
```
20
20
21
21
If your project uses [CommonJS](https://en.wikipedia.org/wiki/CommonJS), then:
22
22
23
-
```
23
+
```js
24
24
importClassGroupfrom'classgroup/commonjs';
25
25
```
26
26
27
27
We then use this simple function by passing in an object with our groupings.
28
28
29
-
```
29
+
```js
30
30
constclasses=ClassGroup({
31
31
identifier: value,
32
32
});
@@ -39,7 +39,7 @@ The `value` can be a `string`, `array` or `object` with no limit on nesting dept
39
39
It will return a flattened object that for convention we store in a variable called `classes`. You can then access the resultant string referencing by [dot notation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors#dot_notation)
40
40
41
41
42
-
```
42
+
```html
43
43
// Svelte, Vue
44
44
<divclass="{classes.identifier}">...</div>
45
45
@@ -49,29 +49,50 @@ render() {
49
49
}
50
50
```
51
51
52
+
ClassGroup is written in Typescript and uses the following types internally:
Let's take a look at a few examples so that this makes sense and see how we can apply this.
53
72
54
73
55
74
### Basic Abstraction with Strings or Arrays
56
75
57
-
```
76
+
```js
58
77
constclasses=ClassGroup({
59
78
identifier1:'class1 class2',
60
79
identifier2: ['class3', 'class4'],
61
80
});
62
81
63
-
// Both result in:
64
-
{
65
-
identifier1: 'class1 class2',
66
-
identifier2: 'class3 class4',
67
-
}
82
+
console.log(classes);
83
+
84
+
// Results in:
85
+
// {
86
+
// identifier1: "class1 class2",
87
+
// identifier2: "class3 class4",
88
+
// }
68
89
```
69
90
70
91
### Grouping with Objects
71
92
72
93
The ability to use an object gives the developer the convenience of grouping classes semantically for better readability.
73
94
74
-
```
95
+
```js
75
96
constclasses=ClassGroup({
76
97
identifier: {
77
98
layout:'class1 class2',
@@ -80,16 +101,17 @@ const classes = ClassGroup({
80
101
...
81
102
});
82
103
104
+
console.log(classes);
105
+
83
106
// Results in:
84
-
{
85
-
identifier: 'class1 class2 class3 class4',
86
-
...
87
-
}
107
+
// {
108
+
// identifier: "class1 class2 class3 class4"
109
+
// }
88
110
```
89
111
90
112
It is advised that each subsequent key represents a breakpoint (e.g. `sm`, `md`, `lg`, `xl`), a state (e.g. `hover`, `focus`, `disabled`), or a semantic group (e.g. `layout`, `presentation`).
> There is no limit on nesting depth and you can mix and match types. Other than the primary root key(s) i.e. `identifier`, **all other nested key names are discarded**.
By keeping our data in an object, it opens up quite a few patterns. You can for example use functions and ternary operators, or pre-process and combine multiple objects. As long as they return one of the expected types (object, string, array), it'll work. **Any other types are ignored.**
114
138
115
-
```
139
+
```js
116
140
constclasses=ClassGroup({
117
-
identifier: {
118
-
variant: condition ? 'rounded' : '',
141
+
btn: {
142
+
variant:isRounded?'rounded':'',
119
143
animation: (() => {
120
144
switch (arg) {
121
145
case'spin':
122
146
return'animation-spin';
147
+
148
+
...
149
+
123
150
default:
124
151
return'animation-none';
125
152
}
@@ -136,7 +163,7 @@ ClassGroup accepts `n` number of subsequent parameters internally referenced as
136
163
137
164
These subsequent parameters intention is to provide an interface to override key values when recycling styling objects (for instance, in a component library) where default values are already present.
// Effectively overriding the classes from 'baseStyles.identifier.presentation',
156
-
// leaving 'baseStyles.identifier.layout' intact and resulting in:
182
+
console.log(classes);
157
183
158
-
{
159
-
identifier: 'class1 class2 bg-blue',
160
-
}
184
+
// Results in:
185
+
// {
186
+
// identifier: 'class1 class2 bg-blue',
187
+
// }
188
+
189
+
// Effectively overriding the classes from 'baseStyles.identifier.presentation' and leaving 'baseStyles.identifier.layout' intact
161
190
```
162
191
---
163
192
## VS Code Tailwind CSS IntelliSense
164
193
In order to make the *Tailwind CSS IntelliSense* plugin work, make sure to use the `tailwindCSS.experimental.classRegex` setting with the following regex:
0 commit comments