简体   繁体   中英

Extending a class that its name is generated in a mixin

This question applies to both SASS and LESS.

I have a bunch of classes that are generated with mixins. Inside my SASS or LESS code I want to extend those classes. In LESS it doesn't work complaining that no such class exists (the classes show up in CSS, but obviously they are not in LESS code). I found this feature request so I guess LESS doesn't support it. How about SASS?

Example (LESS but I'll switch to SASS if it can do that):

.errorLevels(@x){
  .level-@{x} {
    font-size: unit(8 + @x,px);
  }
}

.errorLevels(1);
.errorLevels(2);
.errorLevels(3);

.seriousError {
  &:extend(.level-1);
}

I expect:

.level-1 {
  font-size: 9px;
}
.level-2 {
  font-size: 10px;
}
.level-3 {
  font-size: 11px;
}

//this part is missing from output
.seriousError {
  font-size: 1px;
}

You could do the following to extend a class that was generated in a mixin:

@mixin classGenerator {
    .error {
        color: red;
        font-size: 9px;
    }
}

@include classGenerator;

.seriousError {
    @extend .error;
    font-size: 20px;
}

EDIT

According to the changes in your question:

@mixin errorLevels($x){
  .level-#{$x} {
    font-size: #{12 + $x}px
  }
}

@include errorLevels(1);

.seriousError {
  @extend .level-1;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM