You are not allowed to generate mixins dynamically in Sass
but CSS
classes can be created dynamically.
reference - Read this thread
Method 1
In first method, I have used "key": value
pair to define a Sass map and from that we can easily set the name and value you prefer to mention as class name and it's property value.
$spacing-extra-small: 0.25rem;
$spacing-small: 0.5rem;
$spacing-medium: 1rem;
$spacing-large: 2rem;
$spacing-extra-large: 4rem;
$paddings: (
"pr0": 0,
"pr1": $spacing-extra-small,
"pr2": $spacing-small,
"pr3": $spacing-medium,
"pr4": $spacing-large,
"pr5": $spacing-extra-large
);
@each $padding, $value in $paddings {
.#{$padding} {
padding-right: #{$value};
}
}
Note - Instead of variables I have added in $padding
map, you can apply absolute pixel values directly as shown below.
$paddings: (
"pr0": 0,
"pr1": 0.25rem,
"pr2": 0.5rem,
"pr3": 1rem,
"pr4": 2rem,
"pr5": 4rem
);
Method 2
Here in second method, I have used only value
s to define a Sass map and it's class name is generated dynamically using index
of map value
.
Not like in JavaScript
array
object, initial Index
of map is started from number 1. So I have perform a calculation by subtracting 1
value from the current index of map value
and match to your individual mixin names mentioned above.
$spacing-extra-small: 0.25rem;
$spacing-small: 0.5rem;
$spacing-medium: 1rem;
$spacing-large: 2rem;
$spacing-extra-large: 4rem;
$paddings: ( 0, $spacing-extra-small, $spacing-small, $spacing-medium, $spacing-large, $spacing-extra-large );
@each $padding in $paddings {
.pr#{index(($paddings), ($padding)) - 1} {
padding-right: #{$padding};
}
}
Method 3
Here I have made it bit simplify by reducing to 1 mixin
and 5 variables. This mixin
can be included inside any css
class with a preferred padding variable.
HTML
<div class="box">
set a padding value here
</div>
SCSS
$spacing-extra-small: 0.25rem;
$spacing-small: 0.5rem;
$spacing-medium: 1rem;
$spacing-large: 2rem;
$spacing-extra-large: 4rem;
$pr0 : 0px;
$pr1 : $spacing-extra-small;
$pr2 : $spacing-small;
$pr3 : $spacing-medium;
$pr4 : $spacing-large;
$pr5 : $spacing-extra-large;
@mixin set-padding ($pr){
@if($pr){
padding-right: $pr;
}
}
/*Apply set-padding mixin to .box class*/
.box {
background-color: #ccc;
@include set-padding($pr2);
}
All three methods will be helpful to solve your problem. Thanks :)