Skip to content

Commit ceae302

Browse files
committed
merge docs-patch-1
1 parent 874e8b8 commit ceae302

File tree

1 file changed

+74
-56
lines changed

1 file changed

+74
-56
lines changed

README.md

Lines changed: 74 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
2-
31
# Introduction
42

5-
This is a babel plugin to transform all your string classes into css-module classes automatically.
3+
This is a babel plugin to transform all your string classes into css-module classes automatically.
4+
5+
Its lets you use css-module classes without style object.
6+
It is faster to write, improves code readability and supports multiple css-module using [named-module](#introducing-named-css-modules)
67

7-
Its lets you write css-module classes just like normal classes (without using style objects). Its faster to write and improves code readability.
8-
You can import multiple css-modules with different names and then use it as [named-module](#introducing-named-css-modules). supports \*sass/scss modules also.
8+
Also supports \*sass/scss modules.
99

10-
*\*you may need to use sass-loader with webpack*
10+
_\*you may need to use sass-loader with webpack_
1111

1212
# Installation
1313

14-
Install the plugin using npm:
14+
- Install the plugin with npm:
15+
1516
```sh
16-
npm install --save-dev jsx-css-module-transforms
17+
npm install --save-dev jsx-css-module-transforms
1718
```
1819

19-
after installing, If you are using babel, add this to your plugins:
20+
- If you are using babel, add this to your plugins:
21+
2022
```jsonc
2123
// .babelrc
2224

2325
{
24-
"plugins": [
25-
"module:jsx-css-module-transforms",
26-
]
26+
"plugins": ["module:jsx-css-module-transforms"]
2727
}
2828
```
2929

30-
Or, for Webpack, modify the babel-loader options to include the plugin:
30+
- For Webpack, modify the babel-loader options to include the plugin:
3131

3232
```js
3333
// webpack.config.js
@@ -46,8 +46,9 @@ module.exports = {
4646
}
4747
```
4848

49-
> Note: *The plugin expects source code with **JSX**, plugins that executes before might transform JSX even before it reaches to the plugin.
50-
> If plugin isn't working or throwing unexpected errors, try keeping it at the beginning of the list.*
49+
> Note: _The plugin relies on the **JSX** syntax. Other plugins that executes early might transform JSX
50+
> before it reaches to the plugin which might cause some problems. jsx-css-module-transforms plugin needs
51+
> to be placed before(ideally, at the beginning) other plugins that transform JSX._
5152
5253
# Usage
5354

@@ -56,36 +57,43 @@ We can import the css-module like normal css import without any import variable.
5657
```jsx
5758
import "./m1.module.css"
5859
```
59-
The plugin will automatically change this import statement to use style object,
60-
which will then be used to access the css-module classes.
60+
61+
The plugin will automatically change the import statement to include style object,
62+
which will be used to access the css-module classes.
6163

6264
```jsx
6365
import _style from "./m1.module.css" // modified
6466
```
6567

66-
If we now want to use the css-module, we have to write all our css classes using style object that we just imported:
68+
If we want to use the css-module, we need to write all our css classes using
69+
style object that we just imported:
6770

6871
```jsx
6972
import _style from "./m1.module.css"
7073

7174
function Component() {
72-
return <h1 className={`${_style.foo} ${_style.bar}`}> ..... </h1>
75+
return <h1 className={`${_style.foo} ${_style.bar}`}> ... </h1>
7376
}
7477
```
75-
This sometimes can get too verbose and hurts **code readibility**. It would be nice if we could write our classes as strings and not having to deal with any style objects.
7678

77-
*remember, because of style objects we need to introduce template strings and object notations within our `className` attribute.*
79+
This sometimes can get too verbose and hurts **code readibility**.
80+
It would be nice if we could write classnames within a string and not
81+
having to deal with any style objects.
82+
83+
84+
With the help of plugin, we don't have to use style object anymore, instead we can specify our classes by just using strings:
7885

79-
With the help of the plugin, we don't have to use style object anymore, instead we can specify our classes just using strings:
8086
```jsx
8187
import "./m1.module.css"
8288

8389
function Component() {
84-
return <h1 className="foo bar"> ..... </h1>
90+
return <h1 className="foo bar"> ... </h1>
8591
}
8692
```
87-
This looks more readable and is faster to write as well.
88-
The plugin will modify our code to use css-module and its style object automatically without us having to do anything:
93+
94+
This looks more readable and is faster to write as well.
95+
__jsx-css-modules-transforms__ will modify the code to use css-module and its style object automatically without
96+
us having to do anything:
8997

9098
```jsx
9199
// modified
@@ -96,15 +104,20 @@ function Component() {
96104
}
97105
```
98106

99-
**By default, If plugin found any `'*.module.css'` import, it will transform all our css classes to use style objects.**
107+
The transformed code uses object bracket-notation instead of dot-notation as this allows
108+
us to use `-` (dash) within our classnames (eg. `className="foo-bar"`).
100109

101-
If we want to use global css classes, we need to add `':g'` at the end of the class. This will tell plugin not to transform these classes and keep them as is:
110+
## Global Styles
111+
112+
By default, If plugin finds **any** `'*.module.css'` import, it will transform **all** the css classes
113+
to use style objects. To use global css classnames, we need to add `':g'` at the end of the classname.
114+
This tells plugin not to transform these classes and keep them as is:
102115

103116
```jsx
104117
import "./m1.module.css"
105118

106119
function Component() {
107-
return <h1 className="foo bar:g baz"> .... </h1>
120+
return <h1 className="foo bar:g baz"> ... </h1>
108121
}
109122
```
110123

@@ -113,25 +126,26 @@ function Component() {
113126
import _style from "./m1.module.css"
114127

115128
function Component() {
116-
return <h1 className={`${_style["foo"]} bar ${_style["baz"]}`}> .... </h1>
129+
return <h1 className={`${_style["foo"]} bar ${_style["baz"]}`}> ... </h1>
117130
}
118131
```
119132

120-
In this example, `'bar'` might be declared in the global stylesheet while `'foo'` and `'baz'` are scoped to the imported module.
133+
In this example, `'bar'` might be declared in the global style-sheet, while `'foo'` and `'baz'` are
134+
scoped to the imported css module.
135+
121136

122-
*The transformed code will use object indexing instead of dot-notation, this helps us to use dashes within our class names (eg. `className="foo-bar baz"`) or else, we would have to use camel-case pattern while using css classes.*
137+
## Usage With Already Imported CSS-Module
123138

124-
## Usage With Already Imported CSS-Module
139+
If you are already using CSS-Modules, the plugin will transform string (containing classnames)
140+
that is given to any `className` attr. For example:
125141

126-
If you were already been using css-module with style objects, the plugin will see it and transform other css-classes accordingly.
127-
for example:
128142
```jsx
129143
import style from "./component.module.css"
130144

131145
function Component() {
132146
return (
133-
<div className={style.foo}>
134-
<h1 className="bar baz"> .... </h1>
147+
<div className={style.foo}>
148+
<h1 className="bar baz"> ... </h1>
135149
</div>
136150
)
137151
}
@@ -143,47 +157,51 @@ import style from "./component.module.css"
143157

144158
function Component() {
145159
return (
146-
<div className={style.foo}>
147-
<h1 className={`${style["bar"]} ${style["baz"]}`}> .... </h1>
160+
<div className={style.foo}>
161+
<h1 className={`${style["bar"]} ${style["baz"]}`}> ... </h1>
148162
</div>
149163
)
150164
}
151165
```
152166

153-
154167
# Introducing Named CSS-Modules
155168

156-
In most cases, we would be using single css-module inside our components but sometimes
157-
it might make sense to import bunch of css-modules and use them together.
169+
In most cases, we would be using single CSS-Module inside a component but sometimes,
170+
It might make sense to create reusable CSS-Modules and apply them in multiple different components.
158171

159-
We can give names to our css-module imports, and then use it to apply specific css classes from different modules
160-
161-
eg. we can import two module and use them like:
172+
In order to work with named CSS-Modules, we need to give names to each css-module import
173+
by adding `:<module-name>` at the end of the path:
162174

163175
```jsx
164-
// original
165-
166176
import "./layout.module.css:layout"
167-
import "./component.module.css:comp"
177+
import "./component.module.css:com"
178+
```
168179

180+
To access CSS-Module class, simply add `:<module-name>` at the end of the classname that specifies which CSS-Module
181+
to use for the class:
182+
183+
```jsx
169184
function Component() {
170185
return (
171-
<div className="foo:layout bar:comp baz:layout">
172-
<h1 className="grid-1:layout"> ... </h1>
173-
</div>
186+
<ul className="food-items:layout">
187+
<li className="food-item:com"> ... </li>
188+
<li className="food-item:com"> ... </li>
189+
</ul>
174190
)
175191
}
192+
```
193+
Transformed code would look like this:
176194

177-
// modified
178-
195+
```jsx
179196
import _layout from "./layout.module.css"
180-
import _comp from "./component.module.css"
197+
import _com from "./component.module.css"
181198

182199
function Component() {
183200
return (
184-
<div className={`${_layout["foo"]} ${_comp["bar"]} ${_layout["baz"]}`}>
185-
<h1 className={`${_layout["grid-1"]}`}></h1>
186-
</div>
201+
<ul className={`${_layout["food-items"]}`}>
202+
<li className={`${_com["food-item"]}`}> ... </li>
203+
<li className={`${_com["food-item"]}`}> ... </li>
204+
</ul>
187205
)
188206
}
189207
```

0 commit comments

Comments
 (0)