Reputation: 622
I want to use a variable value in @each: to create classes for different states. But SASS don't use value, it's put in variable name.
Have this SASS syntax:
$success: green;
$warning: yellow;
$danger: red;
$information: steelblue;
@mixin theme ($color) {
&:checked {
background: $color;
}
@each $theme in success, warning, danger, information {
.checkbox--#{$theme} {
@include theme($theme);
}
}
Instead of using $warning value, mixin will create
color: warning
How can I fix it?
Upvotes: 2
Views: 52
Reputation: 21181
There are no dynamic variable names in SCSS, but you could use a Map instead:
$success: green;
$warning: yellow;
$danger: red;
$information: steelblue;
@mixin theme ($color) {
&:checked {
background: $color;
}
}
$themes: (
success $success,
warning $warning,
danger $danger,
information $information
);
@each $theme, $color in $themes {
.checkbox-#{$theme} {
@include theme($color);
}
}
Just for the sake of completeness, if you are using a really old version of SCSS that doesn't support them, you can use a nested Lists and the nth
function.
...
$themes: (
(success $success),
(warning $warning),
(danger $danger),
(information $information)
);
@each $pair in $themes {
$theme: nth($pair, 1);
$color: nth($pair, 2);
.checkbox-#{$theme} {
@include theme($color);
}
}
Both will produce the same output:
.checkbox-success:checked {
background: green;
}
.checkbox-warning:checked {
background: yellow;
}
.checkbox-danger:checked {
background: red;
}
.checkbox-information:checked {
background: steelblue;
}
Upvotes: 3