0

I have problem with root nesting

for better work with bem-modules in Sass we are use $root

.block
  $root: &
  background: gray
  &__elem
    background: red
    color: black
  &:hover
    #{$root}__elem
      color: white

this code compiled to:

.block {
  background: gray;
}
.block__elem {
  background: red;
  color: black;
}
.block:hover .block__elem {
  color: white;
}

How get this effect in stylus?

Eugene Balashov
  • 105
  • 1
  • 13

1 Answers1

4

Stylus have a special selectors for the same case.

stylus-lang.com/docs/selectors.html#initial-reference

You can use ~/ or ^[0]

.block
    &:hover
        ~/__elem
            color: white
Fortael
  • 322
  • 2
  • 15
  • 1
    @ЕвгенийБалашов Probably this is an old version. Try this - http://stylus-lang.com/try.html#?code=.block%0A%20%20background%20gray%0A%20%20%26__elem%0A%20%20%20%20background%3A%20white%0A%20%20%20%20~%2F%3Ahover%20%26%0A%20%20%20%20%20%20color%3A%20blue – Fortael Jun 09 '16 at 03:30
  • what if we use @extend? – Eugene Balashov Jun 28 '16 at 10:37
  • `.element` > `&--modifier` > `@extend ^[0]` — not working – Eugene Balashov Jun 28 '16 at 10:38
  • `.element` > `&--modifier` > `@extend ~/` — and this not work – Eugene Balashov Jun 28 '16 at 10:40
  • in this ^ examples — we need to get styles of `element` in `.element--modifier` – Eugene Balashov Jun 28 '16 at 10:41
  • @ЕвгенийБалашов is that contrary to the methodology of BEM. – Fortael Jun 30 '16 at 06:04
  • block may have modifier, with same properties. i confused and write `element` instead `block` – Eugene Balashov Jun 30 '16 at 12:18
  • and so we need: `.block` > `&--modifier` > `@extend ^[0]` (.block style has modifier with root style, and their special style) – Eugene Balashov Jun 30 '16 at 12:21
  • this alternative for sass: `.block` > `$root: &` + `&--modifier` > `@extend #{$root}` – Eugene Balashov Jun 30 '16 at 12:25
  • @ЕвгенийБалашов still contrary to the methodology of BEM.The modifier should not contain block style. Only overwrites. You may use mixins – Fortael Jun 30 '16 at 12:56
  • Oo )) ... [whats wrong?](http://codepen.io/AliveDD/pen/oLWzoZ?editors=0100) — it is very clean and clear – Eugene Balashov Jun 30 '16 at 14:04
  • mixins working too, but have the worst readability and the number of code lines in css is [increased from 30 to 56](http://codepen.io/AliveDD/pen/YWVGva?editors=0100) – Eugene Balashov Jun 30 '16 at 14:18
  • @ЕвгенийБалашов That's wrong way. It must be — `class="status status--ok"` This is true according to the methodology. What if you want to use several modifiers? Like `class="status status--err status--big status--withoutborder"` – Fortael Jul 01 '16 at 04:37