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 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #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
You might also like these
The difference between Class declarations and Class expressions in JavaScriptJavaScriptList of WordPress directories functionsWordPressGeneral concepts in DrupalDrupalFive common features of Angular template syntax (with examples)Angularsubstring() Method in JavaScriptJavaScriptWordPress: How to create a folder if it doesn’t already exist?WordPress