Bootstrap SASS variable override challenge - css

EDIT: This question was marked as a duplicate of this one, but see the addendum near the end of this answer to see what that question doesn't ask, and what the answer doesn't answer.
I'm working on a web app that uses Bootstrap 3. I have a basic 3-layer override architecture, where 1) Bootstrap's _variables.scss contains the core variables, 2) _app-variables.scss contains the base app variables that override Bootstrap's _variables.scss, and 3) _client-variables.scss contains client-specific customizations that override _app-variables.scss. Either #2 or #3 (or both) can be blank files. So here's the override order:
_variables.scss // Bootstrap's core
_app-variables.scss // App base
_client-variables.scss // Client-specific
Simple enough in theory, but a problem arises because of what I'll call "variable dependencies" -- where variables are defined as other variables. For example:
$brand: blue;
$text: $brand;
Now, let's say the above variables are defined in _variables.scss. Then let's say in _app-variables.scss, I override only the $brand variable to make it red: $brand: red. Since SASS interprets the code line by line sequentially, it will first set $brand to blue, then it will set $text to blue (because $brand is blue at that point), and finally it will set $brand to red. So the end result is that changing $brand afterwards does not affect any variables that were based on the old value of $brand:
_variables.scss
---------------------
$brand: blue;
$text: $brand; // $text = blue
.
.
.
_app-variables.scss
---------------------
$brand: red; // this does not affect $text, b/c $text was already set to blue above.
But obviously that's not what I want - I want my change of $brand to affect everything that depends on it. In order to properly override variables, I'm currently just making a full copy of _variables.scss into _app-variables.scss, and then making modifications within _app-variables from that point. And similarly I'm making a full copy of _app-variables.scss into _client-variables.scss and then making modifications within _client-variables.scss at that point. Obviously this is less than ideal (understatement) from a maintenance point of view - everytime I make a modification to _variables.scss (in the case of a Bootstrap upgrade) or _app-variables.scss, I have to manual trickle the changes down the file override stack. And plus I'm having to redeclare a ton of variables that I may not even be overriding.
I found out that LESS has what they call "lazy loading" (http://lesscss.org/features/#variables-feature-lazy-loading), where the last definition of a variable is used everywhere, even before the last definition. I believe this would solve my problem. But does anyone know a proper variable-override solution using SASS?
ADDENDUM:
Here's one technique I've already thought through: include the files in reverse order, using !default for all variables (this technique was also suggested in the answer to this question). So here's how this would play out:
_app-variables.scss
---------------------
$brand: red !default; // $brand is set to red here, overriding _variables.scss's blue.
.
.
.
_variables.scss
---------------------
$brand: blue !default; // brand already set in _app-variables.scss, so not overridden here.
$text: $brand !default; // $text = red (desired behavior)
So that solution is almost perfect. However, now in my override files, I don't have access to variables defined in Bootstrap's _variables.scss, which I would need if I wanted to define my variable overrides (or my own additional custom variables) using other Bootstrap variables. For example, I might want to do: $custom-var: $grid-gutter-width / 2;

Solved, but I don’t know from which version this works. I believe the solution could have always been available. Tested on:
> sassc --version
sassc: 3.2.1
libsass: 3.2.5
sass2scss: 1.0.3
We are going to use a simplified environment, so filenames do not match with Bootstrap’s.
Challenge
Given a framework we do not control (for example installed only on the Continuous Integration environment and not available in our machines) that expresses SCSS variables in the following manner:
// bootstrap/_variables.scss
$brand-primary: #f00 !default;
$brand-warning: #f50 !default;
$link-color: $brand-primary !default;
And given a file in that same framework that uses the variables:
// bootstrap/main.scss
a:link, a:visited {
color: $link-color;
}
The challenge is:
Include the framework in your own application’s SCSS in such a way that
variables’ dependencies in the framework are preserved and honors;
you can depend in on the default values but still be able to change the results on the framework dependencies.
More precisely:
Include the framework in your application’s SCSS in such a way that $brand-color will always be the inverse of $brand-warning, whatever its value is in the framework.
Solution
The main file would look like this:
// application.scss
#import "variables";
#import "bootstrap/variables";
#import "bootstrap/main";
And your variables file would look like this:
// _variables.scss
%scope {
#import "bootstrap/variables";
$brand-primary: invert($brand-warning) !global;
}
Results:
> sassc main.scss
a {
color: blue; }
Explanation
The %scope part is not something magic of SCSS, it’s simply a hidden class with the name scope, available exclusively for later extensions with #extend. We are using it just to create a variable scope (hence the name).
Inside the scope we #import the framework’s variables. Because at this moment there’s no value for each variable every variable is created and assigned its !default value.
But here’s the gimmick. The variables are not global, but local. We can access them but they are not going to pollute the global scope, the one that will be later used to derive variables inside the framework.
In fact, when we want to define our variables, we want them global, and indeed we use the !global keyword to signal SCSS to store them in the global scope.
Caveats
There’s one major caveat: you cannot use your own variables while you define them.
That means that in this file
%scope {
#import "bootstrap/variables";
$brand-primary: black !global;
#debug $brand-primary;
}
The #debug statement will print the default value defined in bootstrap/_variables.scss, not black.
Solution
Split variables in two parts:
%scope {
#import "bootstrap/variables";
$brand-primary: black !global;
#debug $brand-primary;
}
#debug $brand-primary;
The second #debug will indeed correctly print black.

With Bootstrap 4 or bootstrap-sass all variables set in the _variables.scss with the !default flag.
Therefore, if you set a variable before bootstrap's _variables.scss is included, when it is included, the value from _variables.scss will be ignored.
So my sass entry file might look like this ...
#import "bootstrap-overrides";
#import "bootstrap/scss/bootstrap-flex";
#import "mixins/module";

In alpha 6 of Bootstrap 4, all variables in _variables.scss can be overridden in _custom.scss, in the way that mryarbles describes.
However, the overrides do not cascade to other elements, because the inclusion order is:
#import "variables";
#import "mixins";
#import "custom";
When I change this to
#import "custom";
#import "variables";
#import "mixins";
it works as expected.

The _custom.scss file in BS4 dev branch has been removed. Try to reorder your imports with this order:
Setup
#import "client-variables";
#import "app-variables";
#import "boostrap";
#import "app-mixins";
#import "client-mixins";
Make sure to copy the content of boostrap variable file _variables.scss to app-variables and client-variables. Leave the !default beside each variable to allow further override.
Explanation
All bootstrap variables are declared with !default. From Sass reference:
You can assign to variables if they aren’t already assigned by adding the !default flag to the end of the value. This means that if the variable has already been assigned to, it won’t be re-assigned, but if it doesn’t have a value yet, it will be given one.
Bootstrap will respect all variables already defined on top making the app_variables with higher priority and client_variables with highest priority.
You need to copy all the variable declarations from bootstrap _variables into app-variables and client-variables so you can have a custom variable as you wanted. (Disadvantage is that it is harder to maintain on every bootstrap update)
All variables are now available in your app-mixins and client-mixins

Related

How do I load both ag-grid themes using SCSS and toggle between them?

I've tried using in my main.scss
#import '~ag-grid-community/src/styles/ag-grid';
#import '~ag-grid-community/src/styles/ag-theme-balham/sass/ag-theme-balham';
#import '~ag-grid-community/src/styles/ag-theme-balham-dark/sass/ag-theme-balham-dark';
and then conditionally, I want to apply ag-theme-balham or ag-theme-balham-dark to the div containing ag-grid.
However, if both themes are loaded via scss, since they both build on some common theme files with variables it seems that one overrides the other?? My grid is ending up light even when I can verify through chrome inspector that the surrounding div has class="ag-theme-balham-dark" (even though dark is imported last).
Removing the import of the light theme first succesfully makes for a dark grid.
If I switch to use ag-grid's CSS instead of SCSS that does work, but the whole point of SCSS is so that I could override some theme variables?
To solve this, I ended up not importing their pre-built themes, and created custom themes. Creating the custom themes is still simple to do, since you can use the default variables as a base.
#import '~ag-grid-community/src/styles/ag-grid';
#import '~ag-grid-community/src/styles/ag-theme-balham/vars/ag-theme-balham-vars';
#import '~ag-grid-community/src/styles/ag-theme-balham/sass/ag-theme-balham-common';
.ag-theme-balham {
// $ag-params contains all values for the light theme
#include ag-theme-balham($ag-params);
}
.ag-theme-balham-dark {
$background: #2d3436;
$foreground: #F5F5F5;
// override the default params with the dark values
$dark-params: map-merge($ag-params, (
background-color: $background,
foreground-color: $foreground,
secondary-foreground-color: $foreground,
odd-row-background-color: darken($background, 3),
header-background-color: darken(#636e72, 30),
header-foreground-color: $foreground,
header-cell-hover-background-color: lighten($background, 5),
header-cell-moving-background-color: lighten($background, 5),
border-color: #424242,
hover-color: lighten($background, 7)
));
#include ag-theme-balham($dark-params);
}
https://stackblitz.com/edit/ag-grid-scss-themes?embed=1&file=src/styles.scss
Depending on what features you use in ag-grid, you may have to override more of the $ag-params values. I only overrode the ones that were needed for the example.

Darken function for css theming - Error: argument `$color` of `darken($color, $amount)` must be a color

I'm trying to add an option to change the site colour. So, I have a color field which is working fine but the problem is I need to change the colour to 10% darker when the mouse is hover. The function darken says the first argument must be a color.
Error: argument `$color` of `darken($color, $amount)` must be a color
My code is the following:
:root {
--main-colour: #f06d06;
}
$colour-primary: var(--main-colour);
.btn {
background-color: $colour-primary;
&:hover {
background-color: darken($colour-primary, 10%);
}
}
I need the variable --main-colour because this will be used to change the colour in real time.
Any ideas?
Many thanks
var(--main-colour) is an CSS function that is interpolated at runtime (so it will be resolved AFTER the compilation of SCSS).
SCSS is compiled, therefore all its functions are calculated before and doesnt change run-time.
The problem in your code happens because darken function requires a valid color to perform calculations on, and during compilation time all it gets is var(--main-colour) and not the color itself. (darken is an SCSS function, and not a CSS function, so it cannot be changed runtime).
That problem comes from Bootstrap.
If you are working with a framework like React or angular, remember to import the function before the variables.
#import 'functions';
#import 'variables';
As already answered, not possible. However, in some cases filter: brightness(90%); could be a workaround. More on CSS filters here.
In new version You have to import bootstrap variables. if Still error check file patch, it solves after importing bootstrap variables , I was not importing bootstrap variables ,...
#import "~bootstrap/scss/functions";
#import '~bootstrap/scss/variables';
You need to import the 3 files
#import "../node_modules/bootstrap/scss/functions";
#import "../node_modules/bootstrap/scss/variables";
#import "../node_modules/bootstrap/scss/mixins";

update sass user defined color variables

I have created an angular component MyComponent that has a sass and the theme file.
mycomponent.component.scss
#import "../styles/common";
#import 'mycomponent.theme';
.element {
color:$primary
}
in the common.scss, I have imported other files likes variables.scss that contain $primary with predefined or default colour and other theme variables.
and in the mycomponent.theme.scss, I defined the #mixin to update the primary variable
#mixin mycomponent-theme() {
$primary: green ;
}
Now the theme file that creates the final theme.css call this #mixin
#include mycomponent-theme();
but the element class's color is not getting updated to the green color as mixin is updating the $primary variable.
Am I making any mistake? Or what is the other way to achieve this?
Update
Here is the codepen https://codepen.io/ermarkar/pen/wygwYp?page=1&
Your issue has to do with scoping.
The $primary variable that you set to the value green in the mixin mycomponent-theme() is a local variable, therefore, it is only visible to the code that is inside that mixin.
Even though it has the same name as another global variable (I assume it is global), they are different variables.
When you wrote:
.element {
color:$primary
}
Sass used the global variable called $primary, whose value your mixin did not change, the mixin created a local variable with the same name.
There is a very good resource that I recommend you to read that helps understand variable scoping in Sass, much better than I can explain here.
UPDATE:
One thing you could do that I just thought of, is use !global. This keyword lets you manipulate variables in the global scope from within function or mixin blocks.
In your case, if you change your mixin to this:
#mixin mycomponent-theme() {
$primary: green !global;
}
Then the mycomponent-theme() mixin will change the global $primary variable.

"lessify" a CSS color theme

I've inherited a project with a ton of CSS and been assigned the task of modifying it so the color palette can easily be changed.
I've immediately thought of using a CSS preprocessor, tried less and easily switched the colors for variables, so I just have to define a base color and can switch the color theme.
The problem is, every time I switch the color theme I have to either overwrite colors.less with the new color settings or modify the colors.less import in a ton of files.
What I want is to end up with a single file with a lot of imports (basically one per component or set of components), and on that file when I import colors-red.less instead of colors-blue.less all the components imported right after use the red palette so the theme compiled is red instead of blue, for example.
The problem I am having is that the component files do not get the "globals" with the color definitions so I can't compile the base file that imports those files.
I've read there is the possibility of using "partials" (files starting with _ that won't get compiled independently but imported and then compiled), but my compiler seems to be ignoring this feature, and the eclipse plugin I use for editing and verifying less files also complains about the color variables not being defined on those partials.
How can I can get the partials to work? Is there a better approach to do this task?
Stil, they won't be defined on the imported files, just on the main file, so >compilation will break on the imported files. You see what I mean?
Nope? example:
mixins.less:
.mixin()
{
color: #color;
}
variables.less:
#color: orange;
project.less:
#import "mixins";
#import "variables";
p {
.mixin();
}
Now running lessc project.less outputs:
p {
color:orange;
}
Now i change to content of project.less as follows:
#import "mixins";
#import "variables";
p {
.mixin();
}
#color: red;
Then running lessc project.less outputs:
p {
color:red;
}

override less variable property/value with inline variable in jsp/html

I have a situation where I have to override the less variable property/value with inline variable written in jsp/html. I define few variable in main file (abc.less) as follows:
#bodyColor: rgb(88,90,91); // (#585a5b) grayish
#brandColor1: rgb(23,59,107); // (#173b6b) dark
and I write following ones in jsp/html for changing the color, I am using the same variables name as main file.
#bodyColor: rgb(255,0,0); // (#ff0000) redish
#brandColor1: rgb(204,204,204); // (#cccccc) grayish
but it is overriding the main file property, kindly let me know how it will works. Thanks in advance.
You can create a file with all the colors defined in it as colors.less
#bodyColor: rgb(255,0,0); // (#ff0000) redish
#brandColor1: rgb(204,204,204); // (#cccccc) grayish
then add the following statement at the top in all the other less files,
#import "colors.less";
This way there is not need to change the colors in all the files, instead you can just change it in the colors.less file and it will get updated in all the other places.
You can use the same method for defining and reusing styles, mixins and other variables.

Resources