I am writing a mixin like this:
@mixin box-shadow($top, $left, $blur, $color, $inset:"") {
-webkit-box-shadow: $top $left $blur $color $inset;
-moz-box-shadow: $top $left $blur $color $inset;
box-shadow: $top $left $blur $color $inset;
}
When called what I really want is that if no $inset
value is passed, nothing is output, rather than it compiling to something like this:
-webkit-box-shadow: 2px 2px 5px #555555 "";
-moz-box-shadow: 2px 2px 5px #555555 "";
box-shadow: 2px 2px 5px #555555 "";
How do I rewrite the mixin so that if there is no value of $inset
passed, nothing is output?
You can always assign null to your optional arguments. Here is a simple fix
A DRY'r Way of Doing It
And, generally, a neat trick to remove the quotes.
SASS Version 3+, you can use
unquote()
:Picked this up over here: pass a list to a mixin as a single argument with SASS
Old question, I know, but I think this is still relevant. Arguably, a clearer way of doing this is to use the unquote() function (which SASS has had since version 3.0.0):
This is roughly equivalent to Josh's answer, but I think the explicitly named function is less obfuscated than the string interpolation syntax.
Super simple way
Just add a default value of
none
to $inset - soNow when no $inset is passed nothing will be displayed.
You can put the property with null as a default value and if you don't pass the parameter it will not be interpreted.
This means you can write the include statement like this.
Instead of writing it like this.
As shown in the answer here
here is the solution i use, with notes below: