Is it possible to break out/return early of a Sass mixin? I'd like to do something like this:
@mixin foo($bar: false) {
@if $bar {
// return early without applying any of the styles below
}
color: red;
}
Edit: Please keep in mind that this example is the simplest thing I could come up with that illustrates my problem. In the real world, my code is much more complex and the use case for this is clear.
Sass doesn't really have the concept of "break", but an @if/@else
will get you pretty close:
@mixin foo($bar: false) {
@if $bar {
color: green;
}
@else {
color: red;
}
}
From the Lead Sass developer at https://github.com/nex3/sass/issues/378:
The issue is that the more seldom-used control structures exist in Sass, the harder it is for something with only passing familiarity with the language to read stylesheets that use those control structures. That's why it started out with the bare minimum set of structures needed to do anything: because in many cases it makes sense to skew towards a smaller surface area of the language rather than optimal semantics for writing complex code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With