1

my idea is to add optionally a CSS-Selector to already existing selector stack inside a mixin.

My wish

@mixin myMixin(mobileSelector:true) {
  @if(mobileSelector) {
    .foo .mobile:before,
  }
  .classXY .subclass:before{
    color: red;
  }
}

.awesomeClass-A {
  @include myMixin();
}

@media (min-width: 1025px){
  .awesomeClass-B {
    @include myMixin(false);
  }
}

Should compile to:

 
.awesomeClass-A .foo .mobile:before,
.awesomeClass-A .classXY .subclass:before {
    color: red;
}

@media (min-width: 1025px) {
  .awesomeClass-B .classXY .subclass:before {
    color: red;
  }
}

How to get this work? :)

TylerD
  • 181
  • 1
  • 1
  • 8

2 Answers2

1

Just call variables with $ before and add whole class to @if condition:


@mixin myMixin($mobileSelector: true) {
  @if ($mobileSelector) {
    .foo .mobile:before,
    .classXY subclass:before {
      color: red;
    }
  } @else {
    .classXY subclass:before {
      color: red;
    }
  }
}

.awesomeClass-A {
  @include myMixin();
}

@media (min-width: 1025px) {
  .awesomeClass-B {
    @include myMixin(false);
  }
}

Brebber
  • 2,986
  • 2
  • 9
  • 19
1

You could use a variable and interpolation syntax:

@mixin myMixin($mobileSelector: true) {
  $mobile: if($mobileSelector, ".foo .mobile:before,","");
  #{$mobile} .classXY .subclass:before{
    color: red;
  }
}

.awesomeClass-A {
  @include myMixin();
}

@media (min-width: 1025px){
  .awesomeClass-B {
    @include myMixin(false);
  }
}
ReSedano
  • 4,970
  • 2
  • 18
  • 31
  • 1
    Yeah, the if-Function in the second line is what I was searching for. :D Great! Thanks you! – TylerD Mar 08 '21 at 09:56