Making a Sass mixin with optional arguments

前端 未结 13 1239
眼角桃花
眼角桃花 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:12

    Old question, I know, but I think this is still relevant. Arguably, a clearer way of doing this is to use the unquote() function (which SASS has had since version 3.0.0):

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

    This is roughly equivalent to Josh's answer, but I think the explicitly named function is less obfuscated than the string interpolation syntax.

    0 讨论(0)
  • 2020-12-22 17:13
    @mixin box-shadow($left: 0, $top: 0, $blur: 6px, $color: hsla(0,0%,0%,0.25), $inset: false) {
        @if $inset {
            -webkit-box-shadow: inset $left $top $blur $color;
            -moz-box-shadow: inset $left $top $blur $color;
            box-shadow: inset $left $top $blur $color;
        } @else {
            -webkit-box-shadow: $left $top $blur $color;
            -moz-box-shadow: $left $top $blur $color;
            box-shadow: $left $top $blur $color;
        }
    }
    
    0 讨论(0)
  • 2020-12-22 17:17

    Super simple way

    Just add a default value of none to $inset - so

    @mixin box-shadow($top, $left, $blur, $color, $inset: none) { ....
    

    Now when no $inset is passed nothing will be displayed.

    0 讨论(0)
  • 2020-12-22 17:24

    A DRY'r Way of Doing It

    And, generally, a neat trick to remove the quotes.

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

    SASS Version 3+, you can use unquote():

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

    Picked this up over here: pass a list to a mixin as a single argument with SASS

    0 讨论(0)
  • 2020-12-22 17:25

    You can always assign null to your optional arguments. Here is a simple fix

    @mixin box-shadow($top, $left, $blur, $color, $inset:null) { //assigning null to inset value makes it optional
        -webkit-box-shadow: $top $left $blur $color $inset;
        -moz-box-shadow: $top $left $blur $color $inset;
        box-shadow: $top $left $blur $color $inset;
    }
    
    0 讨论(0)
  • 2020-12-22 17:27

    Sass supports @if statements. (See the documentation.)

    You could write your mixin like this:

    @mixin box-shadow($top, $left, $blur, $color, $inset:"") {
      @if $inset != "" {
        -webkit-box-shadow:$top $left $blur $color $inset;
        -moz-box-shadow:$top $left $blur $color $inset;
        box-shadow:$top $left $blur $color $inset;
      }
    }
    
    0 讨论(0)
提交回复
热议问题