pass a list to a mixin as a single argument with SASS

后端 未结 6 1036
悲哀的现实
悲哀的现实 2021-01-30 06:35

I like to make mixins with SASS that help me make good cross-browser compatibility. I want to make a mixin that looks like this:

@mixin box-shadow($value) {
             


        
相关标签:
6条回答
  • 2021-01-30 07:13

    There's many ways to do this, the best way is using a mixin like so:

    @mixin box-shadow($value...) {
      -webkit-box-shadow: $value;               
      -moz-box-shadow: $value;                  
      box-shadow: $value;
    }
    

    And include it like this:

    @include box-shadow(inset 0 1px 0 #FFD265, 0 0 0 1px #912E01, 0 0 0 7px rgba(0, 0, 0, .1), 0 1px 4px rgba(0, 0, 0, .6));

    or

    @mixin box-shadow($value) {
          -webkit-box-shadow: #{$value};               
          -moz-box-shadow: #{$value};          
          box-shadow: #{$value};
    }
    

    And include it like this:

    @include box-shadow("inset 0 1px 0 #FFD265, 0 0 0 1px #912E01, 0 0 0 7px rgba(0, 0, 0, .1), 0 1px 4px rgba(0, 0, 0, .6)");
    

    or:

    @mixin box-shadow($value) {
          $value: unquote($value);
          -webkit-box-shadow: $value;               
          -moz-box-shadow: $value;          
          box-shadow: $value;
    }
    

    or:

    @mixin box-shadow($value) {
      -webkit-box-shadow: $value;               
      -moz-box-shadow: $value;                  
      box-shadow: $value;
    }
    

    And include it like this:

    @include box-shadow((inset 0 1px 0 #FFD265, 0 0 0 1px #912E01, 0 0 0 7px rgba(0, 0, 0, .1), 0 1px 4px rgba(0, 0, 0, .6)));
    

    Sass is very powerful :)

    0 讨论(0)
  • 2021-01-30 07:22

    i want to point out that you can also pass a value using the argument name as you call the mixin:

    @mixin shadow($shadow: 0 0 2px #000) {
        box-shadow: $shadow;
        -webkit-box-shadow: $shadow; 
        -moz-box-shadow: $shadow; 
    }
    
    .my-shadow {
      @include shadow($shadow: 0 0 5px #900, 0 2px 2px #000);
    }
    

    note that scss is scoped so $shadow will still retain its mixin value if used again later. less i believe, suffers from reassignment in this scenario

    0 讨论(0)
  • 2021-01-30 07:25

    SASS 3.1 or less

    Note: If you're using SASS 3.2+ then use the Variable Arguments feature as rzar suggests.

    Just use string interpolation in the mixin itself, like so:

    @mixin box-shadow($value) {
      -webkit-box-shadow: #{$value};               // #{} removes the quotation marks that
      -moz-box-shadow: #{$value};                  // cause the CSS to render incorrectly.
      box-shadow: #{$value};
    }
    
    // ... calling it with quotations works great ...
    @include box-shadow("inset -2px -2px 2px rgba(0,0,0,0.5), inset 1px 1px 2px rgba(255,255,255,0.5), inset 0px 0px 0px 1px #ff800f");
    

    Thanks for the tip Ryan.

    0 讨论(0)
  • 2021-01-30 07:26

    This doesn't compile:

    +box-shadow(inset 0 1px 0 #FFD265, 0 0 0 1px #912E01, 0 0 0 7px rgba(0, 0, 0, .1), 0 1px 4px rgba(0, 0, 0, .6))
    

    this compiles:

    +box-shadow((inset 0 1px 0 #FFD265, 0 0 0 1px #912E01, 0 0 0 7px rgba(0, 0, 0, .1), 0 1px 4px rgba(0, 0, 0, .6)))
    

    That is, add a parenthesis around the comma-separated list of shadows and it should work:

    +box-shadow( (myshadow1, myshadow2, ...) )
    
    0 讨论(0)
  • 2021-01-30 07:27

    Use string interpolation

    @include box-shadow(#{"inset -2px -2px 2px rgba(0,0,0,0.5), inset 1px 1px 2px rgba(255,255,255,0.5), inset 0px 0px 0px 1px #ff800f"});
    
    0 讨论(0)
  • 2021-01-30 07:31

    Variable Arguments

    Sometimes it makes sense for a mixin to take an unknown number of arguments. For example, a mixin for creating box shadows might take any number of shadows as arguments. For these situations, Sass supports “variable arguments,” which are arguments at the end of a mixin declaration that take all leftover arguments and package them up as a list. These arguments look just like normal arguments, but are followed by .... For example:

    @mixin box-shadow($shadows...) {
      -moz-box-shadow: $shadows;
      -webkit-box-shadow: $shadows;
      box-shadow: $shadows;
    }
    
    .shadows {
      @include box-shadow(0px 4px 5px #666, 2px 6px 10px #999);
    }
    

    via: http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#variable_arguments

    0 讨论(0)
提交回复
热议问题