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 Overflow
🌐
Josh W. Comeau
joshwcomeau.com › css › css-variables-for-react-devs
How to use CSS variables with React • Josh W. Comeau
April 13, 2020 - You can specify a default value if the CSS variable isn't defined: var(--primary-color, pink) will fall back to pink if necessary. Let's see what this looks like in React.
Discussions

CSS Modules with variable class names
I'm gonna go against the grain here and say these are the kinds of patterns that make going back to a codebase more difficult. Keep it simple (KISS) and just map it function getClassName(level) { if (level === 0) return styles.levelOne; // etc } More on reddit.com
🌐 r/reactjs
9
2
February 19, 2022
Is it possible to import variables from CSS modules?
A community for discussing anything related to the React UI framework and its ecosystem. Join the Reactiflux Discord (reactiflux.com) for additional React discussion and help. ... If yes, how should I do it? I want to import variables such height, color etc from a CSS module. More on reddit.com
🌐 r/reactjs
5
5
February 12, 2021
Access SASS variables from css-module in a React component
I have a React component that is using css-modules. Is there a way for me to get access to the SASS variables used in the css-module? My styles.scss file is using a shared _colors.scss file. My fil... More on github.com
🌐 github.com
8
November 20, 2015
postcss - Update CSS Module variables from Javascript - Stack Overflow
I'm using a (now older) version of react-boilerplate which came with CSS Modules. What's nice about them is that you can create variables and import them in other CSS files. Here's my colors.css f... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
github.com › css-modules › css-modules › blob › master › docs › values-variables.md
css-modules/docs/values-variables.md at master · css-modules/css-modules
*/ @value colors: "./colors.css"; @value blue, red, green from colors; .button { color: blue; display: inline-block; } var path = require('path'); var webpack = require('webpack'); var ExtractTextPlugin = require('extract-text-webpack-plugin'); var values = require('postcss-modules-values'); module.exports = { entry: ['./src/index'], output: { filename: 'bundle.js', path: path.join(__dirname, 'public'), publicPath: '/public/', }, module: { loaders: [ { test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/ }, { test: /\.css$/, loader: ExtractTextPlugin.extract( 'style-loader', 'css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!postcss-loader' ), }, ], }, postcss: [values], plugins: [new ExtractTextPlugin('style.css', { allChunks: true })], };
Author   css-modules
🌐
DEV Community
dev.to › diballesteros › how-to-use-global-mixins-and-variables-with-css-modules-in-react-with-sass-37ie
How to use global mixins and variables with CSS Modules in React with SASS - DEV Community
March 21, 2022 - Create-react-app docs recommend using node-sass for the styling, however, this is deprecated so I would suggest using sass (Dart Sass) as it is still actively receiving support. This can be quickly done with: ... For variables it would be something very similar, let’s have a file with the name _variables.scss:
🌐
Reddit
reddit.com › r/reactjs › css modules with variable class names
r/reactjs on Reddit: CSS Modules with variable class names
February 19, 2022 -

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.

🌐
Veera
veerasundar.com › blog › setting-css-variables-from-react
Setting CSS variables from a React app - Blog | Veera
June 19, 2023 - /** my-custom-button.js **/ import ... is through CSS Variables. I can define a CSS variable as --my-var: 10px and then access it with margin-top: var(--my-var);. So for mt MyCustomButton component, I defined the required CSS ...
🌐
Falldowngoboone
falldowngoboone.com › blog › share-variables-between-javascript-and-css
Share variables between JavaScript and CSS | falldowngoboone
If you need a lightweight, “proper” way to share variables between JavaScript and CSS, look no further than Custom Properties. Custom Properties allow you to create arbitrary CSS properties and set them to any value you want. :root { --color-brand: #BADA55; --color-secondary: #005DAB; } import * as React from 'react'; function MyComponent() { const brandColor = getComputedStyle(document.documentElement) .getPropertyValue('--color-brand'); return <p style={{ color: brandColor }}>I'm brand color!</p> }
Find elsewhere
🌐
Epic React
epicreact.dev › css-variables
Use CSS Variables instead of React Context | Epic React by Kent C. Dodds
July 9, 2024 - With CSS Variables, you can get the values in your CSS with var(--css-variable-name) and in your JavaScript using getComputedStyle(element).getPropertyValue('--css-variable-name') (which you really don't need to do...)
🌐
Medium
medium.com › @toshvelaga › using-global-css-variables-in-react-js-216f03fcdc56
Using Global CSS variables in React JS | by Tosh Velaga | Medium
March 7, 2022 - Here’s how we do it. In our src directory we create a file called variables.css. Look at the screenshot below to see our folder structure and what we keep in the variables.css file. ... We then import the variables.css file into App.js so that all the components in our app have access to it.
🌐
Barbarian Meets Coding
barbarianmeetscoding.com › notes › css-modules
CSS modules | Barbarian Meets Coding
June 1, 2022 - There’s one case where it makes ... styles or themes. CSS modules are really good at this because at the end of the day they are a collection of classes and rules that can be passed to a component via props making it a really good fit for theming in React applicatio...
🌐
Reddit
reddit.com › r/reactjs › is it possible to import variables from css modules?
r/reactjs on Reddit: Is it possible to import variables from CSS modules?
February 12, 2021 - I believe there is a sass-to-json converter out there that can convert all your variables to a json and you can import it as a variable like that. Edit: here is the link to the npm package sass-to-json ... Otherway you can use attr from jquery or addAtributte from js but seeing your edit i thought you already solved it ... Just today I was working on v2 that fixes some bugs and makes the integration with webpack easier. So if it matches your use case I would love to know. ... A community for discussing anything related to the React UI framework and its ecosystem.
🌐
GitHub
github.com › css-modules › css-modules › issues › 86
Access SASS variables from css-module in a React component · Issue #86 · css-modules/css-modules
November 20, 2015 - import styles from './styles'; import React from 'react'; import SomeFooBarComponent from 'irrelevant'; export default class MyComponent extends React.Component { render() { return ( <div className={styles.root}> <h1>My amazing page</h1> {/* I want to use a variable here, rather than hard-code the color. I want that variable to come from my imported styles file (i.e.
Author   hdmchl
🌐
Atomizedobjects
atomizedobjects.com › blog › react › how-to-use-css-variables-with-react
How to use css variables with React | Atomized Objects
This principle can be used across the entire react application as long as your css with the variables has been imported into the application, this might be worth importing at the root level. Once these variables have been set you can use them in any style across the application, which can be inline-styles, css, css modules...
🌐
OpenReplay
blog.openreplay.com › using-css-modules-in-react
Using CSS Modules in React
September 2, 2022 - In React js, where classes function similarly to local variables in JavaScript, a CSS Module is just a .css file. It lessens React styling’s global scope. Additionally, it is a tool that prevents global scope and collisions by producing a random string as a className name and adding a unique hash to make each className unique.
🌐
CodeBurst
codeburst.io › a-complete-guide-to-css-modules-in-react-part-2-b7bad1f44463
A complete guide to CSS Modules in React (Part-2) | by Amandeep Singh | codeburst
July 13, 2020 - Carefully notice the syntax. The loader for CSS modules (the one you configure in webpack config and create react app configures for you) will replace the variables in the class name (styles.submitButton or styles.outlinedButton) with a randomized class name.
🌐
Medium
medium.com › trabe › react-component-variants-using-css-variables-e94a9f38004e
React Component Variants Using CSS Variables | by Lucas Andión | Trabe | Medium
May 13, 2021 - A rather boring component, but the button.module.css is where we use variables to create our Button variants.
🌐
Medium
medium.com › @diballesteros › how-to-use-global-mixins-and-variables-with-css-modules-in-react-with-sass-cfb064176f55
How to use global mixins and variables with CSS Modules in React with SASS | by Diego Ballesteros (Relatable Code) | Medium
November 7, 2021 - Create-react-app docs recommend using node-sass for the styling, however, this is deprecated so I would suggest using sass (Dart Sass) as it is still actively receiving support. This can be quickly done with: ... For variables it would be something very similar, let’s have a file with the name _variables.scss: ... This can be quite useful to reuse a lot of variables and similar styles throughout the entire project while still maintaining the module structure.