Real-world projects using CSS modules?
reactjs - How to apply global styles with CSS modules in a react app? - Stack Overflow
reactjs - How to use css modules with create-react-app? - Stack Overflow
[Discussion] CSS Modules
This is an excellent talk on CSS modules that gives solid context on why you may want to use them in a historical context.
As for how to use them in CRA v2, you can do the following:
import styles from './styles.module.css'
export default () => (
<div className={styles.example} />
)
and styles.module.css:
.example {
width: 20px;
height: 20px;
background: red;
}
More on reddit.com Videos
Does anyone know any good open-source React projects that uses CSS modules? Preferably Next.js-based projects, but anything's fine.
I'm curious what's the idiomatic way to structure CSS modules in a real-world project. For example, putting css module files next to component files vs. in a separate directory, etc.
» npm install react-css-modules
Since you're using the ES6 import syntax you may use the same syntax to import your stylesheet
import './App.css'
Also, you can wrap your class with :global to switch to the global scope (this mean CSS Module won't modulify it, eg: adding a random id next to it)
:global(.myclass) {
background-color: red;
}
This can be done by simply adding:
require('./App.css');
(thanks @elmeister who correctly answered this question.)
You do not need to eject.
Create React App supports CSS Modules right out of the box as of version 2.
Upgrade to the latest (react-scripts@latest) version.
If you use yarn:
Copyyarn upgrade react-scripts@latest
If you use npm:
Copynpm install --save react-scripts@latest
Then you just have to create a file with the extension .module.css
For example:
Copy.myStyle {
color: #fff
}
Then you can use it like so:
Copyimport React from 'react'
import styles from './mycssmodule.module.css'
export default () => <div className={styles.myStyle}>We are styled!</div>
To enable CSS module in to your app, you don't need to eject create-react-app. You can follow these simple steps described in this link to use CSS module in your project.
But in case if you ejected your create-react-app, then you must find file named
"webpack.config.js"
open this file and find this {test:cssRegex....etc} in line no. 391 and replace to this changes:
Copy {
test: cssRegex,
exclude: cssModuleRegex,
use: getStyleLoaders({
importLoaders: 1,
modules: true,
localIdentName: '[name]__[local]__[hash:base64:5]'
}),
},
Open .js file and import statement like this
Copyimport cssStyleClassName from './MyApp.css';
Then this import statement allow you to do following changes in component tags like this
Copy<div className={cssStyleClassName.styleName}></div>
styleName is what you defined in your MyAppStyle.css file
Copy.styleName{
your properties here...
}
After eject your app, you must restart your local server, sometime changes don't get reflect.
Note In earlier version there were two generated file for production and dev separately. Now in current version one file named "webpack.config.js" file for which you need to concerned for changes. Thank you.