简体   繁体   English

SCSS / SASS-将BEM与SASS和归因一起使用

[英]SCSS/SASS - using BEM with SASS and atributes

I'm using BEM to name my class names, example: .accordion, .accordion__title, .accordion__item__content. 我正在使用BEM来命名我的班级名称,例如:.accordion,.accordion__title,.accordion__item__content。

For that I created the following mixin: 为此,我创建了以下mixin:

@mixin child_el($element) {

        &__#{$element} {
        @content;
      }
    }

It is working very well, but I have an issue when I need to add also attributes: 它工作得很好,但是当我还需要添加属性时遇到一个问题:

.accordion {
  display: block;
  @include child_el(item) {
    &[state=true] {
    @include child_el(content) {
      display: block;
    }
  }
}
}

What I'm trying to achieve: 我想要达到的目标:

.accordion[state=true] .accordion__item {
 display: block;
}

The parent is read as being &[state=true] instead of .accordion 父级被读取为&[state = true]而不是.accordion

You can rewrite this without the mixin: 您可以不使用mixin来重写它:

.accordion {
  display: block;

  &[state=true] &__item {
    display: block;
  }
}

The output CSS will be: 输出的CSS将是:

.accordion {
  display: block;
}

.accordion[state=true] .accordion__item {
  display: block;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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