Is it possible to include both optional and variable elements in a sass mixin?
I have attempted the following:
@mixin name($className, $centered: false, $dimension...)
however, the first dimension value is wrongly assigned to $centered
.
Switching the parameters order results in compilation errors:
@mixin name($className, $dimension..., $centered: false )
The error message reads:
SASSInvalid CSS after "..., $dimension...": expected ")", was ", $centered: fa..."
Given that the mixin without the optional parameter is extensively used (over 70 times), I am reluctant to modify it entirely to accommodate the new parameters. Is there a way to retain the optionality of this mixin? Or do I need to create a separate override with the new parameter?
For reference, here is the body of the mixin:
@for $i from 1 through length($dimension){
@if($centered) {
.#{$className}Td#{$i}, .#{$className}Th#{$i} {
width: nth($dimension, $i);
text-align:center;
}
}
@else{
.#{$className}Td#{$i}, .#{$className}Th#{$i} {width: nth($dimension, $i); }
}
}
EDIT with complete cooking example:
Essentially, I am defining CSS for column widths of my table more efficiently. When using the mixin like this:
@include nome("columnsName", 40%, 30%, 30%);
The output will be:
.columnsNameTd1, .columnsNameTh1{width: 40%;}
.columnsNameTd2, .columnsNameTh2{ width: 30%; }
.columnsNameTd3, .columnsNameTh3{ width: 30%;}
I am interested in finding a way to center align all columns or specific ones while keeping others left-aligned by default.