Reputation: 876
I am new to sass and I wrote some sass code but it is not compiling.
$classes : primary secondary success warning danger;
$colors : (primary:#007bff,secondary : #6c757d,success: #28a745,warning: #ffc107,dangaer: #dc3545);
@each $class in $classes{
.btn-#{$class}{
$currentColor: map-get($colors,#{$class});
background:linear-gradient(to right,$currentColor,lighten($currentColor,10%));
}
}
The error is :
$color: null is not a color.
stdin 14:55 root stylesheet on line 14 at column 55
But when I replace linear-gradient with variable it is working fine i.e
$classes : primary secondary success warning danger;
$colors : (primary:#007bff,secondary : #6c757d,success: #28a745,warning: #ffc107,dangaer: #dc3545);
@each $class in $classes{
.btn-#{$class}{
$currentColor: map-get($colors,#{$class});
background:$currentColor;
//background:linear-gradient(to right,$currentColor,lighten($currentColor,10%));
}
}
This is code is compiled successfully.
What is the for nor working of $currentColor variable inside linear-gradient() function
Upvotes: 7
Views: 6044
Reputation: 199
Indeed there is something with passing variables from map-get to other sass functions.
But you can slightly modify your code and it will work:
$classes: primary secondary success warning danger;
$colors: (
primary: ( normal: #007bff, light: lighten(#007bff,10%) ),
secondary: ( normal: #6c757d, light: lighten(#6c757d,10%) ),
success: ( normal: #28a745, light: lighten(#28a745,10%) ),
warning: ( normal: #ffc107, light: lighten(#ffc107,10%) ),
danger: ( normal: #dc3545, light: lighten(#dc3545,10%) )
);
@each $class in $classes{
.btn-#{$class}{
$currentColor: map-get(map-get($colors,#{$class}), normal);
$currentColorLighten: map-get(map-get($colors,#{$class}), light);
background: linear-gradient(to right, $currentColor, $currentColorLighten);
}
}
You define two colors for each class (normal and lighten version) and just use it via double map-get.
Upvotes: 3