V
V
Vladimir Golub2020-10-23 09:25:02
Sass
Vladimir Golub, 2020-10-23 09:25:02

How to go up a level in sass?

There is a style structure like this:

.block {
  &__text {
    ...
  }

  &__title {
    ...
  }
}


But for the block I want to have a modifier that affects children:
.block {

  &--open {
    .block--text {
     ...
    }
  }

  &__text {
    ...
  }

  &__title {
    ...
  }
}


How can I go up a level above &--open and not write the entire selector ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Emil Rakhmatullin, 2020-10-23
@RazerVG

The variable $this.
A typical problem when creating self-controlled components (especially when using the BEM methodology) is creating selectors inside modifiers.
Instead of:

.filter-block {
    &__title {
        color: black;
    }
        
    &--expandable {
        .filter-block__title {
            color: blue;
        }
    }
}

Can:
.filter-block {
    $this: &;
    &__title {
        color: black;
    }
    &--expandable {
        #{$this}__title {
            color: blue;
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question