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
Add support for CSS Modules with explicit filename (#2285)
* Add css modules with [name].modules.css file convention
* Add e2e for CSS Modules
* Updated based on feedback
* Change css modules class name to be deterministic and fix licences
* Update css modules class naming convention
Copy file name to clipboardExpand all lines: packages/react-scripts/template/README.md
+46
Original file line number
Diff line number
Diff line change
@@ -24,6 +24,7 @@ You can find the most recent version of this guide [here](https://github.com/fac
24
24
-[Importing a Component](#importing-a-component)
25
25
-[Code Splitting](#code-splitting)
26
26
-[Adding a Stylesheet](#adding-a-stylesheet)
27
+
-[Adding a CSS Modules stylesheet](#adding-a-css-modules-stylesheet)
27
28
-[Post-Processing CSS](#post-processing-css)
28
29
-[Adding a CSS Preprocessor (Sass, Less etc.)](#adding-a-css-preprocessor-sass-less-etc)
29
30
-[Adding Images, Fonts, and Files](#adding-images-fonts-and-files)
@@ -513,6 +514,51 @@ In development, expressing dependencies this way allows your styles to be reload
513
514
514
515
If you are concerned about using Webpack-specific semantics, you can put all your CSS right into `src/index.css`. It would still be imported from `src/index.js`, but you could always remove that import if you later migrate to a different build tool.
515
516
517
+
## Adding a CSS Modules stylesheet
518
+
519
+
This project supports [CSS Modules](https://github.com/css-modules/css-modules) alongside regular stylesheets using the **[name].module.css** file naming convention. CSS Modules allows the scoping of CSS by automatically creating a unique classname of the format **[dir]\_\_[filename]___[classname]**.
520
+
521
+
An advantage of this is the ability to repeat the same classname within many CSS files without worrying about a clash.
importstylesfrom'./Button.module.css'; // Import css modules stylesheet as styles
545
+
546
+
classButtonextendsComponent {
547
+
render() {
548
+
// You can use them as regular CSS styles
549
+
return<div className={styles.button} />;
550
+
}
551
+
}
552
+
```
553
+
### `exported HTML`
554
+
No clashes from other `.button` classnames
555
+
556
+
```html
557
+
<divclass="src__Button-module___button"></div>
558
+
```
559
+
560
+
**This is an optional feature.** Regular html stylesheets and js imported stylesheets are fully supported. CSS Modules are only added when explictly named as a css module stylesheet using the extension `.module.css`.
561
+
516
562
## Post-Processing CSS
517
563
518
564
This project setup minifies your CSS and adds vendor prefixes to it automatically through [Autoprefixer](https://github.com/postcss/autoprefixer) so you don’t need to worry about it.
0 commit comments