Making a Sass mixin with optional arguments

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

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

    Even DRYer way!

    @mixin box-shadow($args...) {
      @each $pre in -webkit-, -moz-, -ms-, -o- {
        #{$pre + box-shadow}: $args;
      } 
      #{box-shadow}: #{$args};
    }
    

    And now you can reuse your box-shadow even smarter:

    .myShadow {
      @include box-shadow(2px 2px 5px #555, inset 0 0 0);
    }
    
    0 讨论(0)
  • 2020-12-22 17:04

    A so much better DRY way

    is to pass $args... to the @mixin. That way, no matter how many $args you will pass. In the @input call, you can pass all args needed. Like so:

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

    And now you can reuse your box-shadow in every class you want by passing all needed args:

    .my-box-shadow {
      @include box-shadow(2px 2px 5px #555, inset 0 0 0);
    }
    
    0 讨论(0)
  • 2020-12-22 17:04

    here is the solution i use, with notes below:

    @mixin transition($trans...) {
      @if length($trans) < 1 {
        $trans: all .15s ease-out;
      }
      -moz-transition: $trans;
      -ms-transition: $trans;
      -webkit-transition: $trans;
      transition: $trans;
    }
    
    .use-this {
      @include transition;
    }
    
    .use-this-2 {
      @include transition(all 1s ease-out, color 2s ease-in);
    }
    
    • prefer passing property values as native css to stay close to "the tongue"
    • allow passing variable number of arguments
    • define a default value for laziness
    0 讨论(0)
  • 2020-12-22 17:10

    With sass@3.4.9 :

    // declare
    @mixin button( $bgcolor:blue ){
        background:$bgcolor;
    }
    

    and use without value, button will be blue

    //use
    .my_button{
        @include button();
    }
    

    and with value, button will be red

    //use
    .my_button{
        @include button( red );
    }
    

    works with hexa too

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

    I am new to css compilers, hope this helps,

            @mixin positionStyle($params...){
    
                $temp:nth($params,1);
                @if $temp != null{
                position:$temp;
                }
    
                 $temp:nth($params,2);
                @if $temp != null{
                top:$temp;
                }
    
                 $temp:nth($params,3);
                @if $temp != null{
                right:$temp;
                }
    
                 $temp:nth($params,4);
                @if $temp != null{
                bottom:$temp;
                }
    
                $temp:nth($params,5);
                @if $temp != null{
                left:$temp;
                }
    
        .someClass{
        @include positionStyle(absolute,30px,5px,null,null);
        }
    
    //output
    
    .someClass{
    position:absolute;
     top: 30px;
     right: 5px;
    }
    
    0 讨论(0)
提交回复
热议问题