Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting SCSS Variables to JS (auto looping variables)

UPDATE: If you plan to implement the export solution, you must place it in a separate file to prevent redundant exports in your compiled CSS code. See here.

I recently learned that you can export styles from SCSS into JS like so:

_variables.scss

:export {
    some-variable: 'some-value';
}

app.js

import styles from 'core-styles/brand/_variables.scss';

Based on this, my _variables.scss is formatted like so:

/* Define all colours */
$some-color:       #000;
$another-color:    #000;

// Export the color palette to make it accessible to JS
:export {
    some-color: $some-color;
    another-color: $another-color;
}

The issue with the above format is that I have to re-define each of my variables within export. Therefore, I am interested to know whether there is a way to loop though each of my variables automatically and export them to JS?

like image 701
Ben Carey Avatar asked Jun 10 '19 09:06

Ben Carey


People also ask

Can you import SCSS variables into JS?

To make Sass (or, specifically, SCSS in this case) variables available to JavaScript, we need to “export” them. The :export block is the magic sauce webpack uses to import the variables. What is nice about this approach is that we can rename the variables using camelCase syntax and choose what we expose.

Can you change SCSS variable value dynamically?

SCSS is compiled to CSS during compile time, and SCSS variables are replaced with resolved value during compile time, which means there is no way to change the variable during run time. However, CSS variables just sits there during run time, and you can dynamically CRUD them during run time with JavaScript (Web API).

How do you globally access Sass SCSS variables in react?

Each import of SCSS file in JS file is treated as an isolated SASS env, therefore, there is no such thing "global variables" while using SCSS in React. This behavior requires us to import the variables. scss file into each SCSS file that uses one of the variables. @import "variables.


2 Answers

Some improvements to the accepted answer:

  • Use camelcase so you will be able to individually export a variable.

  • Set the @each directive outside so it won't generate a new :export at-rule for each variable.


_variables.scss

$theme-colors: (
  'someColor': #000,
  'anotherColor': #FFF,
);

:export {
  @each $key, $value in $theme-colors {
    #{unquote($key)}: $value;
  }
}

app.js

import styles from './core-styles/brand/_variables.scss' // { anotherColor: "#FFF", someColor: "#000" }
import { someColor } from './core-styles/brand/_variables.scss' // #000

Side note: I prefer using quotes inside Sass Maps, but you can omit them.

like image 179
Quentin Veron Avatar answered Oct 17 '22 19:10

Quentin Veron


Taking a Cue from Bootstrap 4, you could combine a SASS map with a loop like below;

/* Define all colours */
$theme-colours: (
  some-color: #000,
  another-color: #000,
  third-color: #000,
  fourth-color: #000
)

@each $color, $value in $theme-colours {
  :export{
    $color: $value;
  }
}

Here's some examples from the Bootstrap 4 Docs

like image 26
Lewis Avatar answered Oct 17 '22 20:10

Lewis