Making a Sass mixin with optional arguments

前端 未结 13 1368
眼角桃花
眼角桃花 2020-12-22 16:31

I am writing a mixin like this:

@mixin box-shadow($top, $left, $blur, $color, $inset:\"\") {
    -webkit-box-shadow:          


        
13条回答
  •  鱼传尺愫
    2020-12-22 17:00

    You can put the property with null as a default value and if you don't pass the parameter it will not be interpreted.

    @mixin box-shadow($top, $left, $blur, $color, $inset:null) {
      -webkit-box-shadow: $top $left $blur $color $inset;
      -moz-box-shadow:    $top $left $blur $color $inset;
      box-shadow:         $top $left $blur $color $inset;
    }
    

    This means you can write the include statement like this.

    @include box-shadow($top, $left, $blur, $color);
    

    Instead of writing it like this.

    @include box-shadow($top, $left, $blur, $color, null);
    

    As shown in the answer here

提交回复
热议问题