Generate theme files in SASS / SCSS - sass

I have theme.scss SCSS file which contains something like that
// global variables
#import "variables";
// SCSS placeholders
#import "placeholders";
// basic HTML styles - links etc.
#import "basic";
// default layout styles
#import "layout";
/* COMPONENTS */
#import "components/layout/header"; // top page header
#import "components/layout/heading"; // main page heading
//etc. etc. a lot of another components
And it generates one output CSS file. Now I need to generate 3 different color themes (differenet CSS files). So I've created 3 SCSS files theme1.scss, theme2.scss and theme3.scss. They have the same content with only one difference:
#import "variables";
Variables will be specific for each theme. Yeah, everything works. But everytime I need to create another theme, I'll need to create another SCSS file and copy-paste whole includes from previous theme file. Is there any way how can I create my theme1.scss, theme2.scss, theme3.scss without creating 3 duplicated files?

You couldn't do it with #mixin, currently Import directives not working inside mixin becuuse of this issue https://github.com/sass/sass/issues/451
#mixin apply-theme($theme) {
#if $theme == 'red' {
#import 'variables-red';
} #else if $theme == 'green'{
#import 'variables-green';
} #else{
#import 'variables-defaults';
}
}
So currently you can do with it
$theme:'red';
#if $theme == 'red' {
#import 'variables-red';
} #else if $theme == 'green'{
#import 'variables-green';
} #else{
#import 'variables-defaults';
}

Related

How to access a theme color in bootstrap 5?

I want to define my own theme color so the standard bootstrap elemets are overriden and also use the value later for my own components. Here is the code I use in a scss file:
$theme-colors: (
"primary": #611fe6,
"secondary": #ffbd58,
"dark": #000000
);
#import "node_modules/bootstrap/scss/bootstrap";
#import "node_modules/bootstrap/scss/functions";
#import "node_modules/bootstrap/scss/variables";
#import "node_modules/bootstrap/scss/mixins";
.fa-li {
color: $primary;
}
.fa-li then has the original primary color of bootstrap and not my own.
"How to access a theme color in bootstrap 5?"
Short answer, use map-get...
.fa-li {
color: map-get($theme-colors,"primary")
}
Demo
"I want to define my own theme color so the standard bootstrap elements
are overridden and also use the value later for my own components"
Full answer, For your theme changes it would be better to redefine the value of $primary. Then you won't have to use map-get. Simply reference $primary...
$primary: #611fe6;
$secondary: #ffbd58;
$dark: #000000;
#import "bootstrap";
.fa-li {
color: $primary
}
Demo
Also see: Overriding Bootstrap SCSS Variables

How to Modify Bootstrap 4 Mixins without Editing Core Files

So basically, I'm trying to modify the button-variant mixin in bootstrap 4. The core file where the code for this resides is bootstrap\scss\mixins, and the filename is _buttons.scss. In my custom.scss, I have the following code:
#import "../../node_modules/bootstrap/scss/bootstrap";
I would want to keep the mixin name the same and not override it using a different name because it's being used in the file node_modules\bootstrap\scss_buttons.scss in the following code that generates all buttons based on the colors available:
#each $color, $value in $theme-colors {
.btn-#{$color} {
#include button-variant($value, $value);
}
}
What happens is that when the new modified mixin code is added below importing bootstrap in custom.scss, it does not have any effect as the imported bootstrap gets compiled after that code and the default button css gets compiled. Whereas, when the modified mixin code is added after importing bootstrap in custom.scss, there is duplication of buttons in the compiled .css file.
How would one go about modifying the code in a mixin without editing the core bootstrap files?
You should import Bootstrap's SCSS files separately instead of the whole pack, and you should import your own mixins after Bootstrap's _mixins.scss. This way you can override them before Bootstrap's _buttons.scss would use them:
#import "bootstrap/scss/functions";
#import "bootstrap/scss/variables";
#import "bootstrap/scss/mixins";
#import "my-custom-mixins";
// rest of Bootstrap imports (see bootstrap.scss):
#import "bootstrap/scss/root";
#import "bootstrap/scss/reboot";
#import "bootstrap/scss/type";
#import "bootstrap/scss/images";
#import "bootstrap/scss/code";
#import "bootstrap/scss/grid";
#import "bootstrap/scss/tables";
#import "bootstrap/scss/forms";
#import "bootstrap/scss/buttons";
#import "my-custom-buttons";
#import "bootstrap/scss/transitions";
#import "bootstrap/scss/dropdown";
#import "bootstrap/scss/button-group";
#import "bootstrap/scss/input-group";
#import "bootstrap/scss/custom-forms";
#import "bootstrap/scss/nav";
#import "bootstrap/scss/navbar";
#import "bootstrap/scss/card";
#import "bootstrap/scss/breadcrumb";
#import "bootstrap/scss/pagination";
#import "bootstrap/scss/badge";
#import "bootstrap/scss/jumbotron";
#import "bootstrap/scss/alert";
#import "bootstrap/scss/progress";
#import "bootstrap/scss/media";
#import "bootstrap/scss/list-group";
#import "bootstrap/scss/close";
#import "bootstrap/scss/toasts";
#import "bootstrap/scss/modal";
#import "bootstrap/scss/tooltip";
#import "bootstrap/scss/popover";
#import "bootstrap/scss/carousel";
#import "bootstrap/scss/spinners";
#import "bootstrap/scss/utilities";
#import "bootstrap/scss/print";

Cannot get Bootstrap 4 custom.css

Here is my _custom.scss:
// Bootstrap overrides
//
// Copy variables from `_variables.scss` to this file to override default values
// without modifying source files.
//$body-bg: $gray-dark;
//$body-color: $gray-light;
$blue: #363636;
$component-active-bg: #484545;
$dropdown-bg: $brand-primary;
$dropdown-link-color: #c3c3c3;
$dropdown-link-hover-color: darken(#c3c3c3, 5%);
and bootstrap.scss:
// Core variables and mixins
#import "variables";
#import "mixins";
#import "_custom";
I am compiling it with the provided Gruntfile.js.
The resultant css file has no effect. Everything looks just like with the original css.
How can I test whether I am getting compilation process actually working?
Thanks
From the format of boostrap.scss I think you should have:
#import "custom";
Next, when you make your changes in _custom.scss, leave off the "!default" tag. For example:
// _variables.css
$blue: #0275d8 !default;
/// _custom.css
$blue: #002b5b;

How do I share variables between different imported files?

I want to use SASS in a modular way. In the code segment below you can see a way I consider organizing some of the layouts of a page.
What I have in mind is the external variables in languages like C.
// file: some_page.scss
//
// I want some variables from the fonts, colors partials
// to be visible to the buttons partial
// Is it possible?
// error: _buttons.scss (Line X: Undefined variable: "$color_blue")
#import "colors"
#import "fonts"
#import "buttons"
// in file: _colors.scss
$color_blue: blue;
// in file: _buttons.scss
.button {
background-color: $color_blue;
}
Yes, that's how it works.
As long as _colors.scss is imported before the other files.
You can check out the port of Twitter Bootstrap to Sass here: https://github.com/thomas-mcdonald/bootstrap-sass it uses variables in a similar fashion.
You need to add a ; at the end of the #import line.

avoid duplication using placeholder selectors in Sass 3.2

The problem is that my Sass code is generating duplicate declarations in the compiled CSS file.
My Sass code is organized in multiple partials, and I #import them into a final screen.scss file
I have a _placeholders.scss which contains %alignright and %alignleft.
I have a _content.scss file which uses these, so I #import "_placeholder.scss" at the top of that file and I do the same in _footer.scss. So I guess #import "_placeholders.scss" in 2 places is causing the duplication?
If I just #import "_placeholders.scss" at the beginning of screen.scss to make them globally accessible, then it messes with the order of the CSS declarations.
The first CSS selector to use a placeholder selector will be inserted at the order where I #import "_placeholders.scss", instead of where I #import "_conntent.scss".
For example, in screen.scss:
#import "placeholders";
#import "reset";
#import "typography"
#import "content" // uses placeholder %alignleft
#import "footer" // uses placeholder alignleft
Generated CSS:
/* Content styles that use placeholders */
/* reset styles */
/* typography styles */
/* expected order of content styles */
/* footer styles */
How do I avoid duplication and get the styles to be output in the correct place in the compiled CSS?
This is a place where you'd need to #include a #mixin rather than #extending a placeholder.
// in _placeholders.scss
#mixin alignleft {
text-align: left;
}
// in _content.scss
div.whatever {
#include alignleft;
}
// in _footer.scss
div.whatever-footer {
#include alignleft;
}

Resources