YogeshChauhan . com

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);
}

amazon

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 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

What is a component helper function in React?ReactThe 8 Golden Rules of Programming I Have Learned in My CareerMiscHow to add AppRoutingModule in Angular application using command line?AngularHow to Remove PHP File Extensions From Your Website URLs?PHPHow to create flickering text using CSS?CSSWhat are keys in React?React