Sass @mixin 的默认参数
Default parameter for Sass @mixin
我似乎无法在 Stack Overflow 上的任何地方找到这个特定问题的答案。
我正在使用 Compass 并正在为 box-shadow
/text-shadow
构建一个 @mixin
。
我想知道是否可以在 Sass/SCSS 中设置默认参数?
这是我当前的代码:
@mixin outer-glow($color, $type) {
@if $type == 'text' {
@include text-shadow(0 0 2px #{$color});
@include text-shadow(0 0 .125rem #{$color}); // 2px
} @else {
@include box-shadow(0 0 2px #{$color});
@include box-shadow(0 0 .125rem #{$color}); // 2px
}
}
我想使用这个 @mixin
,如果没有声明 $type
,它默认为 box-shadow
:
// declaration
@include outer-glow($my-color);
// output
would compile to box-shadow
// declaration
@include outer-glow($my-color, text);
// output
would compile to text-shadow
我发现这个 helpful post 回答了我的问题。
所以我编辑了我的 @mixin
,现在我可以按预期使用它,如果现在分配了参数,它默认为 box-shadow
:
@mixin outer-glow($color, $type: box) {
@if $type == 'text' {
@include text-shadow(0 0 2px #{$color});
@include text-shadow(0 0 .125rem #{$color}); // 2px
} @else {
@include box-shadow(0 0 2px #{$color});
@include box-shadow(0 0 .125rem #{$color}); // 2px
}
}
我似乎无法在 Stack Overflow 上的任何地方找到这个特定问题的答案。
我正在使用 Compass 并正在为 box-shadow
/text-shadow
构建一个 @mixin
。
我想知道是否可以在 Sass/SCSS 中设置默认参数?
这是我当前的代码:
@mixin outer-glow($color, $type) {
@if $type == 'text' {
@include text-shadow(0 0 2px #{$color});
@include text-shadow(0 0 .125rem #{$color}); // 2px
} @else {
@include box-shadow(0 0 2px #{$color});
@include box-shadow(0 0 .125rem #{$color}); // 2px
}
}
我想使用这个 @mixin
,如果没有声明 $type
,它默认为 box-shadow
:
// declaration
@include outer-glow($my-color);
// output
would compile to box-shadow
// declaration
@include outer-glow($my-color, text);
// output
would compile to text-shadow
我发现这个 helpful post 回答了我的问题。
所以我编辑了我的 @mixin
,现在我可以按预期使用它,如果现在分配了参数,它默认为 box-shadow
:
@mixin outer-glow($color, $type: box) {
@if $type == 'text' {
@include text-shadow(0 0 2px #{$color});
@include text-shadow(0 0 .125rem #{$color}); // 2px
} @else {
@include box-shadow(0 0 2px #{$color});
@include box-shadow(0 0 .125rem #{$color}); // 2px
}
}