Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Breaking out of a Sass mixin

Tags:

sass

mixins

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.

like image 322
LandonSchropp Avatar asked Jan 11 '23 19:01

LandonSchropp


1 Answers

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.

like image 64
KatieK Avatar answered Jan 19 '23 05:01

KatieK