Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a mixin as a parameter of another mixin in SASS

I have a mixin that converts px to rem PX TO REM, I have this code:


    .button {
        @include rem(font-size, 24px);
        @include rem(padding, 10px);
        @include rem(border-radius, 5px);
    }

This would produce this CSS:


    .button {
        font-size: 1.5rem;
        padding: 0.625rem;
        border-radius: 0.3125rem; }

But I'd like to use some mixins from compass and for example I want to use border-radius from compass


    .box {
        @include border-radius(10px);
    }

And it would generate this CSS:


    .box {
        -moz-border-radius: 10px;
        -webkit-border-radius: 10px;
        border-radius: 10px; }

Is there a way to do something like this:


    .box {
        @include rem(@include border-radius(10));
    }

like image 688
freeman76 Avatar asked Mar 05 '15 21:03

freeman76


People also ask

How do you pass parameters in mixin?

Variable argument is used to pass any number of arguments to mixin. It contains keyword arguments passed to the function or mixin. Keyword arguments passed to the mixin can be accessed using keywords($args) function which return values mapped to String.

How do I call Sass mixin?

A mixin is defined with the @mixin directive. Tip: A tip on hyphens and underscore in Sass: Hyphens and underscores are considered to be the same. This means that @mixin important-text { } and @mixin important_text { } are considered as the same mixin!

Which arguments are used to include arguments in the mixin?

Keyword arguments: The arguments are used to include in mixins. These types of arguments, if named, can be passed in any order and their default values can be skipped.


1 Answers

You can't add two mixins together the way you'd like. So you just have to make the rem mixin do what you want it to do. So I wrote new code to handle that for you.

@function parseInt($n) {
    @return $n / ($n * 0 + 1);
}

@mixin rem($property, $values, $prefix: false) {
    $px: ();
    $rem: ();

    @each $value in $values {

        @if $value == 0 or $value == auto or unit($value) == "%" {
            $px: append($px, $value);
            $rem: append($rem, $value);
        } @else {
            $unit: unit($value);
            $val: parseInt($value);

            @if $unit == "px" {
                $px: append($px, $value);
                $rem: append($rem, ($val / 16 + rem));
            }

            @if $unit == "rem" {
                $px: append($px, ($val * 16 + px));
                $rem: append($rem, $value);
            }
        }
    }

    @if $px == $rem {
        #{$property}: $px;
    } @else if $prefix == true {
        #{-moz- + $property}: $px;
        #{-moz- +$property}: $rem;
        #{-webkit- +$property}: $px;
        #{-webkit- +$property}: $rem;
        #{$property}: $px;
        #{$property}: $rem;
    } @else {
        #{$property}: $px;
        #{$property}: $rem;
    }
}

Now all you have to do add prefixes to any property is add the value true to the end of the mixin like so...

@include rem(border-radius, 10px, true);

Otherwise if you don't want any prefixs on property like fon-size or something you just don't add a last value like so...

@include rem(font-size, 10px);

I have a working demo here...

*Also on a side note I modified this mixin to handle percentages too.

like image 161
Richard Dell Donatoni Avatar answered Nov 03 '22 03:11

Richard Dell Donatoni