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?
Sass supports
@if
statements. (See the documentation.)You could write your mixin like this:
With sass@3.4.9 :
and use without value, button will be blue
and with value, button will be red
works with hexa too
I know its not exactly the answer you were searching for but you could pass
"null"
as last argument when@include box-shadow
mixin, like this@include box-shadow(12px, 14px, 2px, green, null);
Now, if that argument is only one in that property than that property (and its (default) value) won't get compiled. If there are two or more args on that "line" only ones that you nulled won't get compiled (your case).CSS output is exactly as you wanted it, but you have to write your
null
s :)Even DRYer way!
And now you can reuse your box-shadow even smarter:
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:And now you can reuse your box-shadow in every class you want by passing all needed args: