In the past, I’ve normally used the Rails asset helpers to load a global CSS file, and I’ve used CSS modules (and the sass-resources-loader for bits of CSS that are local to the React components within one directory.
CSS Modules
For CSS modules the syntax looks like this:
import styles from './style.css';
...
<button className=`${styles.primaryButton}`>Confirm</button>
In the webpack config, the key is to set the modules query option to true
for the css-loader
module, per the instructions: css-loader
The query parameter modules enables the CSS Modules spec.
This enables local scoped CSS by default. (You can switch it off
with :global(...)
or:global
for selectors and rules.)
Another issue that applies to CSS module includes is that you don’t have access to the global variables, unless you use the sass-resources-loader
Here’s an interesting idea to wrap a library CSS file inside of a class. You can find more details here.
// Select.scss (SASS + CSS Modules)
.wrap {
:global {
@import "node_modules/react-select/scss/default.scss";
}
}
With that CSS, you’d wrap the library component in a div with the css module wrap
class:
const Select = (props) => {
return (
<div className={styles.wrap}><ReactSelect {...props} /></div>
)
};
Global CSS
If you are using the CSS modules option in your webpack config and you want to include a CSS or SASS file without the CSS module changes to the class names, you have two options:
- You can configure your webpack config with a test that matches files you want to globally include, and have the test for files to use CSS modules include a look-ahead regexp to exclude the file. Here’s an example. Notice the fancy regexp in the second test.
module: {
rules: [
{
test: /(\.global\.css$|react-select.css)/,
use: [
{
loader: 'style-loader',
},
{
loader: 'css-loader',
},
],
},
{
test: /^((?!\.global|react-select).)*\.css$/,
use: [
{
loader: 'style-loader',
},
{
loader: 'css-loader',
options: {
modules: true,
importLoaders: 1,
sourceMap: true,
},
},
],
},
- You can create a wrapper file and import the CSS or SASS:
:global {
@import "file1.scss";
@import "file2.scss";
}
An advantage of the second option is that you have better control over the ordering of the sass files, which matters for the inclusion of variables, computations based on variables, and overriding selectors.