Created
November 16, 2015 14:47
-
-
Save evenicoulddoit/594c488fd32148e33146 to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ---- | |
// Sass (v3.4.14) | |
// Compass (v1.0.3) | |
// ---- | |
$bem-element-separator: '__'; | |
$bem-modifier-separator: '--'; | |
@function selectorToString($selector) { | |
$selector: inspect($selector); //cast to string | |
$selector: str-slice($selector, 2, -2); //remove brackets | |
@return $selector; | |
} | |
@function lastIndex($str, $needle, $from: 0) { | |
$index: str-index($str, $needle); | |
@if(not $index) { | |
@return $from; | |
} | |
@else { | |
$str: str-slice($str, $index + 1); | |
@return lastIndex($str, $needle, $from + $index); | |
} | |
} | |
@function containsModifier($selector) { | |
$selector: selectorToString($selector); | |
@if(str-index($selector, $bem-modifier-separator)) { | |
@return true; | |
} | |
@else { | |
@return false; | |
} | |
} | |
@function lastElement($str) { | |
$last-index: lastIndex($str, " "); | |
$str: if($last-index, str-slice($str, $last-index + 1), $str); | |
@return $str; | |
} | |
@function getBlock($selector) { | |
$selector: selectorToString($selector); | |
$element: lastElement($selector); | |
@debug($element); | |
$modifierStart: str-index($element, $bem-modifier-separator) - 1; | |
@return str-slice($element, 0, $modifierStart); | |
} | |
@mixin block($block) { | |
.#{$block} { | |
@content; | |
} | |
} | |
@mixin element($element) { | |
$selector: &; | |
@if(containsModifier($selector)) { | |
$block: getBlock($selector); | |
@debug($block); | |
@at-root { | |
#{$selector} { | |
#{$block+$bem-element-separator+$element} { | |
@content; | |
} | |
} | |
} | |
} | |
@else { | |
@at-root { | |
#{$selector+$bem-element-separator+$element} { | |
@content; | |
} | |
} | |
} | |
} | |
@mixin modifier($modifier) { | |
@at-root { | |
#{&}#{$bem-modifier-separator+$modifier} { | |
@content; | |
} | |
} | |
} | |
@include block(test) { | |
background: red; | |
@include element(element){ | |
font-size: 14px; | |
@include modifier(big) { | |
font-size: 18px; | |
} | |
} | |
@include modifier(modifier) { | |
color: blue; | |
@include element(subelement) { | |
background: gray; | |
} | |
} | |
@include block(nested-block) { | |
@include modifier(modifier) { | |
@include element(element) { | |
color: blue; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.test { | |
background: red; | |
} | |
.test__element { | |
font-size: 14px; | |
} | |
.test__element--big { | |
font-size: 18px; | |
} | |
.test--modifier { | |
color: blue; | |
} | |
.test--modifier .test__subelement { | |
background: gray; | |
} | |
.test .nested-block--modifier .nested-block__element { | |
color: blue; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment