Skip to content

Instantly share code, notes, and snippets.

@timknight
Last active August 29, 2015 14:24
Show Gist options
  • Save timknight/da1749a06075cc2c29a1 to your computer and use it in GitHub Desktop.
Save timknight/da1749a06075cc2c29a1 to your computer and use it in GitHub Desktop.
Improved BEM Mixins

I've previously used a mixin format for creating BEM-based selectors that would allow me to do something like this:

.block {
  @include e(element) {
    @include m(modifier) {...}
  }
}

This worked for me until there were instances when I needed multiple elements with similar attributes and didn't want to go through the process of creating a placeholder class to @extend.

This new style would allow for creating multiple elements or modifiers from the same declaration.

.article { 
  @include e(title, description) {
    font-size: 1.5em;
    margin-bottom: 0;
    margin-top: 0;
  }
  ...
}

Creates:

.article--title, .article--description {
  font-size: 1.5em;
  margin-bottom: 0;
  margin-top: 0;
}
@mixin e($elements...) {
$element-list: ();
@each $element in $elements {
$element-list: append($element-list, "&__#{$element}", comma)
}
#{$element-list} {
@content;
}
}
@mixin m($modifiers...) {
$modifier-list: ();
@each $modifier in $modifiers {
$modifier-list: append($modifier-list, "&--#{$modifier}", comma)
}
#{$modifier-list} {
@content;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment