Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a sass @mixin accept an undefined number of arguments?

I'm trying to create a sass mixin for transitions. This is what I have so far.

@mixin transition($var)   -webkit-transition: $var   transition: $var 

I want to be able to pass it multiple arguments like this

@include transition(color .5s linear, width .5s linear) 

Unfortunately, I get the following error

Syntax error: Mixin transition takes 1 argument but 2 were passed. 

Is there a way to do this so it produces the following output in css while still accepting an undefined number of arguments?

-webkit-transition: color .5s linear, width .5s linear; transition: color .5s linear, width .5s linear; 
like image 591
Ryan Avatar asked Oct 25 '11 20:10

Ryan


People also ask

What does @mixin do in SCSS?

The @mixin directive lets you create CSS code that is to be reused throughout the website.

How do you pass arguments to 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.

Which arguments are used to include arguments in the mixins in Sass?

The keyword arguments are used to include in mixins. It specifies that the named arguments can be passed in any order and the default value of arguments can be omitted.


1 Answers

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); } 

is compiled to:

.shadows {   -moz-box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;   -webkit-box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;   box-shadow: 0px 4px 5px #666, 2px 6px 10px #999; } 

From : SASS official Documentation

So you basically just need to change your mixins declaration to look like this :

@mixin transition($var...)   -webkit-transition: $var   transition: $var 
like image 135
Jérémie Parker Avatar answered Sep 24 '22 14:09

Jérémie Parker