Sass 1
Sass (Syntactically Awesome StyleSheet) is a CSS-compatible pre-processor extension, developed in 2006.
In React, we npm install sass to transpile a sass file into CSS, due to the browser being unable to read sass.
Global and local variables are declared outside/inside selectors.
Sass variables are imperative, any change will only affect the following variables, while CSS variables are declarative, and any change in value will affect the previous uses too.
//Sass variables can store strings/numbers/booleans/colors/lists
//Any variables will affect only the selectors below it
$primo: red;
.coral{
color: $primo;
}
.coral1{
$primo: green; //use !global to reset the global variable
color: $primo ;
}

Sass can nest properties names if they start with the same word.
//for font-family, font-size and font-weight
//remember the space before {}
.nested{
font: {
family: Helvetica, sans-serif;
size: 18px;
weight: bold;
}
}
We can @import external sass files, usually to separate the sass variables file from the sass selectors file.
//We add a _partial to the file's name to keep it from transpiring
//@import on the top of the sass file for it to have global scope
_Global.scss
$bicolor: yellow;
App.scss
@import "Global";
.exported{
background-color: $bicolor;
}
SCSS mixin, include and extend
The @mixin and @include directives create and set blocks of sass properties to use on selectors.
Both - and _ are considered the same digit on mixin names.
//@mixin are not selectors, they can be used only with @include
@mixin bordi-y{
border-bottom: 2px solid blue;
border-top: 2px solid red;
}
@mixin testo{
color: purple;
font: {
size: 25px;
}
}
.paper{
@include bordi-y;
@include testo;
}
We can also use it to shorten vendor prefixes.
@mixin transform($property) {
-webkit-transform: $property;
-ms-transform: $property;
transform: $property;
}
.myBox {
@include transform(rotate(20deg));
}

The @extend directive passes a set of properties between selectors.
.primo{
border-top: 3px solid green;
border-bottom: 3px solid red;
}
//gets the .primo orders plus text
.secondo{
@extend .primo;
color: lightseagreen;
font-size: 15px;
}
Export Sass to javascript and CSS color-mix()
We :export sass variables to javascript:
app.scss
$giallo: yellow;
:export{
greeno: $giallo
}
//But we can't modify it on js
App.js
import extra from'./App.scss';
console.log( extra.greeno ) //yellow
The Sass file doesn't exist during runtime, so we need CSS :root variables to change the Sass variables dynamically.
App.css
:root{
--verde: green;
}
App.scss
$greeno: var(--verde);
.verde{
color: $greeno;
}
//We access and edit the :root CSS variables
App.js
let r = document.querySelector(':root');
var rs = getComputedStyle(r);
console.log( rs.getPropertyValue('--verde') ) //green
r.style.setProperty('--verde', 'red'); //Or ternary theme ? "#262833" : "#fff"
The color-mix(in colorspace, color1 %, color2 %) is a CSS functional notation to mix color values
The default percentage values are 50% and its return value can be passed as var().
App.css
:root{
--base: color-mix(in srgb, green 70%, red 20%);
--sopra: color-mix(in lch, green 20%, red 70%);
}
App.scss
$base: var(--base);
$sopra: var(--sopra);
.mac{
background-color: $base;
color: $sopra;
}

Set bootstrap variables with Scss
We use Sass to modify npm install bootstrap variables.
//The scss file has to go AFTER the bootstrap import
App.js
import 'bootstrap/dist/css/bootstrap.css';
import "./Global.scss"
Global.scss
$primary: darkred;
$secondary: lightcoral;
$success: lightgreen;
@import '~bootstrap/scss/bootstrap.scss';
//Also we can't export the global.scss to another scss to avoid slowing down the app

Sass variables are used as fallback values, and are converted to CSS during compilation, without the need for CSS root{} properties.
We can only edit sass variables when used in a selector, so we use them to overwrite bootstrap props (after removing the css bootstrap import from App.js).
//We remove !important from the bootstrap properties (works only on scss)
//we also added a ID on the parent tag
$enable-important-utilities: false;
$primo: brown;
#overboot .text-primary{
color: var(--primo, $primo);
}
#overboot .bg-primary{
background-color: var(--primo, $primo);
}
#overboot .border-primary{
border-color: var(--primo, $primo);
}
@import '~bootstrap/scss/bootstrap.scss';
//The scss --primo variable won't be immediately available, to use it we first:
document.documentElement.style.setProperty("--primo", "brown")
//Then in ReactJs, we can edit it.
document.documentElement.style.getPropertyValue("--primo") == "brown" ?
document.documentElement.style.setProperty("--primo", "lightblue") :
document.documentElement.style.setProperty("--primo", "brown")

1
1
Last updated
Was this helpful?