Last active
September 26, 2016 11:39
-
-
Save remi/1fc4fc2f3cf126867c908dde3ac6ac73 to your computer and use it in GitHub Desktop.
BEM-style class name builder
This file contains hidden or 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
// @example | |
// build('foo', {bar: true, baz: false, omg: true}); | |
// => 'foo foo--bar foo--omg' | |
const build = (base, modifiers = {}) => { | |
const reduceModifier = (memo, modifier) => { | |
return modifiers[modifier] | |
? memo.concat([`${base}--${modifier.replace(/([A-Z])/g, (part) => `-${part.toLowerCase()}`)}`]) | |
: memo; | |
}; | |
return Object.keys(modifiers).reduce(reduceModifier, [base]).join(' '); | |
}; | |
export default {build}; |
This file contains hidden or 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
import ClassNameBuilder from 'ClassNameBuilder'; | |
ClassNameBuilder.build('component-name__sub-component-name', { | |
foo: true, | |
bar: false, | |
fromBaz: true | |
}); | |
// => "component-name__sub-component-name component-name__sub-component-name--foo component-name__sub-component-name--from-baz" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment