Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I declare a new variable inside a SASS mixin? [duplicate]

I have the following SASS mixin:

@mixin gradient($start, $end, $fallback: $end, $direction: bottom) {
@if $direction == top {
    $directionOld: bottom;
} @else if $direction == right {
    $directionOld: left;
} @elseif $direction == bottom {
    $directionOld: top;
} @elseif $direction == left {
    $directionOld: right;
}

background: $fallback;
background: -webkit-linear-gradient($directionOld, $start, $end);
background:         linear-gradient(to $direction, $start, $end);
}

This mixin throws an error because $directionOld is not defined. I can fix it adding this variable by default to the mixin params:

@mixin gradient($start, $end, $fallback: $end, $direction: bottom, $directionOld: top) {
@if $direction == top {
    $directionOld: bottom;
} @else if $direction == right {
    $directionOld: left;
} @elseif $direction == bottom {
    $directionOld: top;
} @elseif $direction == left {
    $directionOld: right;
}

background: $fallback;
background: -webkit-linear-gradient($directionOld, $start, $end);
background:         linear-gradient(to $direction, $start, $end);
}

But this is not as clean as I want, is there any error in the first code?

Thank you so much!

like image 545
scerrutti Avatar asked Oct 24 '13 15:10

scerrutti


People also ask

What is the difference between mixin and variable?

Mixin do some kind of styling which can be used again again in your whole styling and same you can use variable as much as you can. Functions and Mixins are different. One returns value while the other is used like a macro to substitute.

What is the difference between mixin and extend in Sass?

@mixin is used to group css code that has to be reused a no of times. Whereas the @extend is used in SASS to inherit(share) the properties from another css selector. @extend is most useful when the elements are almost same or identical.

What is difference between mixin and function Sass?

Functions are useful specifically because they return values. Mixins are nothing like functions--they usually just provide valuable blocks of code. Usually, there are cases where you might have to use both. Which provides us with the actual code.


1 Answers

Yes you can define new variable in Mixin but you have to define it before use it in an if statement.

I write your code again:

@mixin gradient($start, $end, $fallback: $end, $direction: bottom) {
    $directionOld:top !default;

    @if $direction == top {
        $directionOld: bottom;
    } @else if $direction == right {
        $directionOld: left;
    } @elseif $direction == bottom {
        $directionOld: top;
    } @elseif $direction == left {
        $directionOld: right;
    }

    background: $fallback;
    background: -webkit-linear-gradient($directionOld, $start, $end);
    background:         linear-gradient(to $direction, $start, $end);
}
like image 161
Parhum Avatar answered Oct 07 '22 00:10

Parhum