I have a Sass file which is generating a CSS file. I have used many variables in my sass file for background color, font-size, now I want to control my all variables through JavaScript.
For example, in style.sass we have
$bg : #000;
$font-size: 12px;
How can I change these values from JavaScript?
Share data between Sass and JavaScript using JSON.
See related question https://stackoverflow.com/a/26507880/4127132
I find sass-extract-loader especially helpful for using with Create-React-App.
You can use it like:
_variables.scss
component.tsx
CSS
JS or TS
If you're looking to do what bootstrap do (ie editing fields to download a theme).
You'd have to:
.replace()
to search and replace variablesI'd personally do this with php.
I also needed this functionality, here's what worked for me:
With JS you can set a class to body, ex.
.blue
or.default
Now create a set of rules to extend from, which you'd like to set:Now instead of having
color: $someColor
in your scss file, use it like this:Now, if your body will have the
default
class, the color insidesomeClass
will be red, and if body will have theblue
class the color will be blue.You can't. SASS is a CSS preprocessor, which means that all SASS specific information disapears when you compile it to CSS.