Skip to content

Commit 4249b3f

Browse files
committed
Move adding a css modules stylesheet to its own file
1 parent a6ce4c2 commit 4249b3f

File tree

3 files changed

+56
-54
lines changed

3 files changed

+56
-54
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
id: adding-a-css-modules-stylesheet
3+
title: Adding a CSS Modules Stylesheet
4+
---
5+
6+
> Note: this feature is available with `[email protected]` and higher.
7+
8+
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 `[filename]\_[classname]\_\_[hash]`.
9+
10+
> **Tip:** Should you want to preprocess a stylesheet with Sass then make sure to [follow the installation instructions](#adding-a-sass-stylesheet) and then change the stylesheet file extension as follows: `[name].module.scss` or `[name].module.sass`.
11+
12+
CSS Modules let you use the same CSS class name in different files without worrying about naming clashes. Learn more about CSS Modules [here](https://css-tricks.com/css-modules-part-1-need/).
13+
14+
### `Button.module.css`
15+
16+
```css
17+
.error {
18+
background-color: red;
19+
}
20+
```
21+
22+
### `another-stylesheet.css`
23+
24+
```css
25+
.error {
26+
color: red;
27+
}
28+
```
29+
30+
### `Button.js`
31+
32+
```js
33+
import React, { Component } from 'react';
34+
import styles from './Button.module.css'; // Import css modules stylesheet as styles
35+
import './another-stylesheet.css'; // Import regular stylesheet
36+
37+
class Button extends Component {
38+
render() {
39+
// reference as a js object
40+
return <button className={styles.error}>Error Button</button>;
41+
}
42+
}
43+
```
44+
45+
### Result
46+
47+
No clashes from other `.error` class names
48+
49+
```html
50+
<!-- This button has red background but not red text -->
51+
<button class="Button_error_ax7yz"></div>
52+
```
53+
54+
**This is an optional feature.** Regular `<link>` stylesheets and CSS files are fully supported. CSS Modules are turned on for files ending with the `.module.css` extension.

docusaurus/docs/user-guide.md

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ You can find the most recent version of this guide [here](https://github.com/fac
1111

1212
## Table of Contents
1313

14-
- [Adding a CSS Modules Stylesheet](#adding-a-css-modules-stylesheet)
1514
- [Adding a Sass Stylesheet](#adding-a-sass-stylesheet)
1615
- [Post-Processing CSS](#post-processing-css)
1716
- [Adding Images, Fonts, and Files](#adding-images-fonts-and-files)
@@ -46,58 +45,6 @@ You can find the most recent version of this guide [here](https://github.com/fac
4645
- [Advanced Configuration](#advanced-configuration)
4746
- [Alternatives to Ejecting](#alternatives-to-ejecting)
4847

49-
## Adding a CSS Modules Stylesheet
50-
51-
> Note: this feature is available with `[email protected]` and higher.
52-
53-
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 `[filename]\_[classname]\_\_[hash]`.
54-
55-
> **Tip:** Should you want to preprocess a stylesheet with Sass then make sure to [follow the installation instructions](#adding-a-sass-stylesheet) and then change the stylesheet file extension as follows: `[name].module.scss` or `[name].module.sass`.
56-
57-
CSS Modules let you use the same CSS class name in different files without worrying about naming clashes. Learn more about CSS Modules [here](https://css-tricks.com/css-modules-part-1-need/).
58-
59-
### `Button.module.css`
60-
61-
```css
62-
.error {
63-
background-color: red;
64-
}
65-
```
66-
67-
### `another-stylesheet.css`
68-
69-
```css
70-
.error {
71-
color: red;
72-
}
73-
```
74-
75-
### `Button.js`
76-
77-
```js
78-
import React, { Component } from 'react';
79-
import styles from './Button.module.css'; // Import css modules stylesheet as styles
80-
import './another-stylesheet.css'; // Import regular stylesheet
81-
82-
class Button extends Component {
83-
render() {
84-
// reference as a js object
85-
return <button className={styles.error}>Error Button</button>;
86-
}
87-
}
88-
```
89-
90-
### Result
91-
92-
No clashes from other `.error` class names
93-
94-
```html
95-
<!-- This button has red background but not red text -->
96-
<button class="Button_error_ax7yz"></div>
97-
```
98-
99-
**This is an optional feature.** Regular `<link>` stylesheets and CSS files are fully supported. CSS Modules are turned on for files ending with the `.module.css` extension.
100-
10148
## Adding a Sass Stylesheet
10249

10350
> Note: this feature is available with `[email protected]` and higher.

docusaurus/website/sidebars.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"installing-a-dependency",
2222
"importing-a-component",
2323
"code-splitting",
24-
"adding-a-stylesheet"
24+
"adding-a-stylesheet",
25+
"adding-a-css-modules-stylesheet"
2526
],
2627
"Testing": ["running-tests", "debugging-tests"],
2728
"Deployment": ["publishing-components-to-npm", "deployment"],

0 commit comments

Comments
 (0)