Yogesh Chauhan's Blog

How to load a module with configuration in SCSS?

in SCSS on May 19, 2021

You can define variable with the !default flag to make them configurable. You can also load the same variables and assign them new values while loading them. This is really handy when you can configure variables rather than just simply importing the stylesheet.

Here’s the syntax for loading a module with configuration:


@use 'url' with (
  variable-name: new-value,
  variable-name: new-value,
  ...
);

Example: load a module with configuration


// _code-block.scss
$black: #000 !default;
$box-shadow: 0 0.5rem 1rem rgba($black, 0.15) !default;

code {
  color: $black;
  box-shadow: $box-shadow;
}


// main.scss
@use 'code-block' with (
  $black: #222,
  $border-radius: 0.1rem
);

Here’s the compiled CSS:


code {
  color: #222;
  box-shadow: 0 0.5rem 1rem rgba(34, 34, 34, 0.15);
}

How to configure modules with mixins?

If you want to configure multiple variable at once, SassScript recommends not to do it with “@use ‘url’ with”. Instead, you can use maps and pass it as configuration. Another way to do this is to just import the variables and then change them later on after the module is loaded.

You can also use @mixin rules. The trick is to set your variables by writing one mixin and then write another mixin to inject those variables/styles.


Simple example to configure modules with mixins


// _code-block.scss
$black: #000 !default;

@mixin configure($black: null) {
  @if $black {
    $-black: $black !global;
  }
}

@mixin styles {
  code {
    color: $black;
  }
}


// main.scss
@use 'code-block';

@include code-block.configure(
  $black: #222,
);

@include code-block.styles;

Here’s the compiled CSS:


code {
  color: #222;
}


Write a bit complex mixin


// _code-block.scss
$black: #000 !default;

@function -box-shadow() {
  @return $-box-shadow or (0 0.5rem 1rem rgba($-black, 0.15));
}

@mixin configure($black: null, $box-shadow: null) {
  @if $black {
    $-black: $black !global;
  }
  @if $box-shadow {
    $-box-shadow: $box-shadow !global;
  }
}

@mixin styles {
  code {
    color: $black;
    box-shadow: -box-shadow();
  }
}


// main.scss
@use 'code-block';

@include code-block.configure(
  $black: #222,
);

@include code-block.styles;


code {
  color: #222;
  box-shadow: 0 0.5rem 1rem rgba(34, 34, 34, 0.15);
}


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface #Jan 14 How to display a student’s individual transcript in Colleague? #Jan 11 How to install PuTTY on a MacOS?
You might also like these
How does @extend rule work in SCSS (Sass)?SCSSHow to Check If a Variable is an Integer, a Float, a Number, NaN, an Infinite or a Numeric in PHP?PHPWhat is ECMAScript?JavaScriptDISTINCT ON: The confusing, unique and useful feature in PostgresPostgresThe unique operator($) in Envision BasicEnvision BasicCSS Attribute SelectorsCSS