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);
}
examples functions Modules sass variables