Skip to content

Commit 16c1baf

Browse files
committed
docs: updates to README.md
1 parent a1e7377 commit 16c1baf

File tree

1 file changed

+59
-30
lines changed

1 file changed

+59
-30
lines changed

README.md

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,27 @@ It helps unclutter your markup when using utility-driven CSS principles or frame
66

77
### Installation
88

9-
```
9+
```shell
1010
npm i -D classgroup
1111
```
1212

1313
### Usage
1414

1515
To use ClassGroup, import it as you would any other utility:
1616

17-
```
17+
```js
1818
import ClassGroup from 'classgroup';
1919
```
2020

2121
If your project uses [CommonJS](https://en.wikipedia.org/wiki/CommonJS), then:
2222

23-
```
23+
```js
2424
import ClassGroup from 'classgroup/commonjs';
2525
```
2626

2727
We then use this simple function by passing in an object with our groupings.
2828

29-
```
29+
```js
3030
const classes = ClassGroup({
3131
identifier: value,
3232
});
@@ -39,7 +39,7 @@ The `value` can be a `string`, `array` or `object` with no limit on nesting dept
3939
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)
4040

4141

42-
```
42+
```html
4343
// Svelte, Vue
4444
<div class="{classes.identifier}">...</div>
4545

@@ -49,29 +49,50 @@ render() {
4949
}
5050
```
5151

52+
ClassGroup is written in Typescript and uses the following types internally:
53+
54+
```ts
55+
// ClassGroup.d.ts
56+
57+
export interface Output {
58+
[key: string]: string;
59+
}
60+
61+
export type OptionValue = boolean | string | Array<string> | Options;
62+
63+
export interface Options {
64+
[key: string]: OptionValue;
65+
}
66+
67+
export default function ClassGroup(collection: Options, ...overrides: Options[]): Output;
68+
```
69+
70+
5271
Let's take a look at a few examples so that this makes sense and see how we can apply this.
5372

5473

5574
### Basic Abstraction with Strings or Arrays
5675

57-
```
76+
```js
5877
const classes = ClassGroup({
5978
identifier1: 'class1 class2',
6079
identifier2: ['class3', 'class4'],
6180
});
6281

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+
// }
6889
```
6990

7091
### Grouping with Objects
7192

7293
The ability to use an object gives the developer the convenience of grouping classes semantically for better readability.
7394

74-
```
95+
```js
7596
const classes = ClassGroup({
7697
identifier: {
7798
layout: 'class1 class2',
@@ -80,16 +101,17 @@ const classes = ClassGroup({
80101
...
81102
});
82103

104+
console.log(classes);
105+
83106
// Results in:
84-
{
85-
identifier: 'class1 class2 class3 class4',
86-
...
87-
}
107+
// {
108+
// identifier: "class1 class2 class3 class4"
109+
// }
88110
```
89111

90112
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`).
91113

92-
```
114+
```js
93115
const classes = ClassGroup({
94116
identifier: {
95117
layout: {
@@ -100,10 +122,12 @@ const classes = ClassGroup({
100122
},
101123
});
102124

125+
console.log(classes);
126+
103127
// Results in:
104-
{
105-
identifier: 'class1 class2 class5 class3 class4 class6',
106-
}
128+
// {
129+
// identifier: "class1 class2 class5 class3 class4 class6"
130+
// }
107131
```
108132

109133
> 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**.
@@ -112,14 +136,17 @@ const classes = ClassGroup({
112136

113137
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.**
114138

115-
```
139+
```js
116140
const classes = ClassGroup({
117-
identifier: {
118-
variant: condition ? 'rounded' : '',
141+
btn: {
142+
variant: isRounded ? 'rounded' : '',
119143
animation: (() => {
120144
switch (arg) {
121145
case 'spin':
122146
return 'animation-spin';
147+
148+
...
149+
123150
default:
124151
return 'animation-none';
125152
}
@@ -136,7 +163,7 @@ ClassGroup accepts `n` number of subsequent parameters internally referenced as
136163
137164
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.
138165

139-
```
166+
```js
140167
const baseStyles = {
141168
identifier: {
142169
layout: 'class1 class2',
@@ -152,17 +179,19 @@ const styleOverrides = {
152179

153180
const classes = ClassGroup(baseStyles, styleOverrides);
154181

155-
// Effectively overriding the classes from 'baseStyles.identifier.presentation',
156-
// leaving 'baseStyles.identifier.layout' intact and resulting in:
182+
console.log(classes);
157183

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
161190
```
162191
---
163192
## VS Code Tailwind CSS IntelliSense
164193
In order to make the *Tailwind CSS IntelliSense* plugin work, make sure to use the `tailwindCSS.experimental.classRegex` setting with the following regex:
165-
```
194+
```json
166195
{
167196
...
168197
"tailwindCSS.experimental.classRegex":[

0 commit comments

Comments
 (0)