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 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #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

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
Some EASY-to-Understand CSS media query examplesCSSINNER JOIN in PostgresPostgresHow to create a circle that follows a cursor using JavaScript and CSS?CSSWordPress get_posts ExamplesWordPressHow to deploy Angular App on Dreamhost or Amazon S3 or firebase?AngularKilling A Project Part 3: How can an organization ensure that a doomed project is killed as early as possible?Miscellaneous