This is how you do it with vanilla CSS Modules
// vars.css
:root {
--color-black: #222;
}
// myComponent.module.css
@import './vars.css';
.component {
color: var(--color-black);
}
Answer from Mantas on Stack OverflowThis is how you do it with vanilla CSS Modules
// vars.css
:root {
--color-black: #222;
}
// myComponent.module.css
@import './vars.css';
.component {
color: var(--color-black);
}
The CSS-Modules documentation mentions variables here: https://github.com/css-modules/css-modules/blob/master/docs/values-variables.md
With this you can import variables as so:
@value colors: "../../main/colors.css";
@value primary, secondary, tertiary from colors;
which can be used in your css:
.main {
background-color: tertiary;
border-top: 30px solid primary;
}
To make this work postcss-loader and postcss-modules-values need to be added to your webpack config. See below:
{
test: /\.css$/,
use: [{
loader: 'style-loader'
},
{
loader: 'css-loader',
options: {
modules: true,
localIdentName: '[name]_[local]_[hash:base64:5]'
}
},
{
loader: 'postcss-loader',
options: {
plugins: [postcssModulesValues]
}
}
]
}
CSS Modules with variable class names
Is it possible to import variables from CSS modules?
Access SASS variables from css-module in a React component
postcss - Update CSS Module variables from Javascript - Stack Overflow
Videos
Solved
say I have different "level" classes with names like:
level_1 level_2 level_3
and I want to programmatically assign those classes to items that are rendered inside a loop and the items get assigned a level based on a variable. How can I implement this with CSS Modules?
I've tried things like
{`${styles.level_}${itemLevel}`}and
{"styles.level_".concat(itemLevel)}but neither works.
To be clear, I need a formula that will result in something like "styles.level_1" when itemLevel === 1, for example.
I would just use the default color vars on the element/body/whatever, then put the alternate theme colors in another class, and toggle the theme class via JS. Here's a demo.
$("button").on("click", function() {
$("body").toggleClass("foo");
});
body {
--red: red;
--blue: blue;
--yellow: yellow;
background: #ccc;
text-align: center;
font-size: 5em;
}
.foo {
--red: #ce1126;
--blue: #68bfe5;
--yellow: #ffd100;
}
.red {
color: var(--red);
}
.blue {
color: var(--blue);
}
.yellow {
color: var(--yellow);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="red">RED</span> <span class="blue">BLUE</span> <span class="yellow">YELLOW</span>
<br>
<button>click me</button>
I would have 2 sheets and conditionally switch between the two:
colours.scss
:root {
/* Status colors */
--error: #842A2B;
--success: #657C59;
--pending: #666;
--warning: #7E6939;
}
otherColours.scss
:root {
/* Status colors */
--error: #FF0000;
--success: #00FF00;
--pending: #6666FF;
--warning: #FF00FF;
}
then in your react code import them and use them as you wish:
import styles from 'colours.scss';
import alternativeStyles from 'otherColours.scss';
...
{this.props.useNormalStyle ? styles.myClass : alternativeStyles.myClass}