Real-world projects using CSS modules?
reactjs - How to apply global styles with CSS modules in a react app? - Stack Overflow
CSS modules in react
reactjs - How to use css modules with create-react-app? - Stack Overflow
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.
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.)
ยป npm install react-css-modules
I am confused between just creating a component_name.css file and importing it in the component Vs creating a component_name.module.css and importing it in the component. What are the differences? How should I decide what to choose?
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.
Hi, there is a deprecated library that I have been using for my personal projects called react-css-modules. The maintainer created a babel plugin that has less performance overhead called babel-plugin-react-css-modules which is also not being maintained currently. Luckily, someone created a fork of the babel plugin and is maintaining it under dr.pogodin/babel-plugin-react-css-modules.
I like this CSS solution more than css-modules because it is easy to separate between the classes inside of your scoped CSS module and global classes and because you can configure it to tell you whether any of your classes are unused. You can even enforce the use of a single CSS modules per ReactElement.
This is really my favorite solution to CSS, at least when using React, yet this library does not seem to be used often in projects. If you use css-modules, why would you choose css-modules over one of these libraries? Is it because css-modules is actively maintained and has more tooling around it with plugins like typescript-plugin-css-modulesand typed-css-modules? Would you ever consider using these libraries, at least for personal projects? Is it really that bad of an idea to use unmaintained packages (react-css-modules or babel-plugin-react-css-modules) or maintained packages with a tiny group of contributors (dr.pogodin/babel-plugin-react-css-modules)? I do plan on perhaps open-sourcing a project I am building soon so I would use css-modules because it would be more familiar to contributors for the project.
Would it make sense to use the unmaintained, yet stable (in my experience) react-css-modules or babel-plugin-react-css-modules ever to make sure no bugs creep up in my projects instead of using a regularly maintained yet unknown package in dr.pogodin/babel-plugin-react-css-modules?
Thanks.