V
V
Vladimir Vasiliev2022-02-12 02:52:24
Sass
Vladimir Vasiliev, 2022-02-12 02:52:24

How to make friends BEM, BEM modifiers, SCSS and cascading?

Very often you need to change the state of the external unit.
For example, when we focus an element, disable it, make it read-only, etc.
If you apply the BEM methodology, you need to enter a modifier. So we add a modifier to the main element. But here's the problem, we have several nested elements. And they all need to change styles, not just the parent element.

Here, please tell me how to reconcile these BEM modifiers with cascading (with nested elements) and SCSS. Duplicate the parent's name is not the best solution.

<div class="my-select my-select_disabled">
    <div class="my-select__header">...</div>
    <div class="my-select__list">...</div>
</div>


.my-select {
    &__header {
        //...
    }

    &__list {
        //...
    }

    &_disabled {
        // Когда отключен весь селект
        // поменять цвет для my-select__header
        // поменять цвет для my-select__list
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey delphinpro, 2022-02-12
@bobahvas

The problem is writing to scss, right?
There are two options:

.my-select {
    &__header { }
    &__list { }

    &_disabled { }
    &_disabled &__header { }
    &_disabled &__list { }
}

.my-select {
    $this: &;
    &__header { }
    &__list { }

    &_disabled {
        #{$this}__header { }
        #{$this}__list { }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question