Variables in parameters in SASS
There are situations where a mixin or function needs to take an unknown number of parameters. That's why SASS has the ability to pass "variable parameters," or parameters that are specified last in a function or mixin and pack all other passed parameters into a list.
After such parameters, you need to put an ellipsis. Consider the following example:
@mixin box-content-padding($padding...) {
-moz-box-content-padding: $padding;
-webkit-box-content-padding: $padding;
}
div {
@include box-content-padding(0px 4px 5px 2px);
}
Compilation result:
div {
-moz-box-content-padding: 0px 4px 5px 2px;
-webkit-box-content-padding: 0px 4px 5px 2px;
}
You can also pass named parameters to variables of a function or mixin. To access them, use the keywords($args) function, which returns them as a mapping of names (without the $ sign) to values.
@mixin common-colors($text-color, $background, $shadow) {
color: $text-color;
background-color: $background;
box-shadow: $shadow;
}
$values: white, green, grey;
.primary {
@include common-colors($values...);
}
$value-map: (text-color: black, background: grey, shadow: green);
.secondary {
@include common-colors($value-map...);
}
After compilation we will see:
.primary {
color: white;
background-color: green;
box-shadow: grey;
}
.secondary {
color: black;
background-color: grey;
box-shadow: green;
}