Parent referencing using ampersand (&) in Sass

Turns out reference selectors, and nesting in general, is a debatable paradigm to use. But for styles that are closely related and/or act on the same html elements, it can be a clever way to DRY up your code and keep things tidy. Hover effects are a great example. But I think the best one I've used so far is with visible and hidden transitions in my web apps.

Sass reference selector syntax


.hidden {
  animation-timing-function: cubic-bezier(0.1, 0.8, 0.1, 1.000);
  transition: opacity 500ms ease-in-out;
  &-true {
    @extend .hidden;
    opacity: 0;
  }
  &-false {
    @extend .hidden;
    opacity: 1;
  }
}

Compiles down to CSS

.hidden, .hidden-true, .hidden-false {
  animation-timing-function: cubic-bezier(0.1, 0.8, 0.1, 1);
  transition: opacity 500ms ease-in-out;
}
.hidden-true {
  opacity: 0;
}
.hidden-false {
  opacity: 1;
}

Comments

Popular posts from this blog

Contrasting var and ES6s new declarations: let and const

Modularity vs. Abstraction. Which is More Important?

Unit testable components pt.3: Testing components