Created
April 12, 2020 07:26
-
-
Save jkarttunen/dca226de50fbdb668f3a9a161675cc4b to your computer and use it in GitHub Desktop.
Simple Bem classname utility
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
/** | |
* Stupid simple BEM utility for class names. Useful for creating className strings in simple components | |
* @param {string} block - Block name | |
* @param {string} [element] - Element name | |
* @param {string} [modifier] - modifier name | |
* @return {string} A BEM string, | |
* | |
* @example | |
* bem('button', 'icon', 'red'); | |
* // returns 'button button--icon button--icon--red' | |
* | |
* @example | |
* bem('button', 'red'); | |
* // returns 'button button--red' | |
*/ | |
function bem (block, element, modifier) { | |
const b = `${block}`; | |
const e = element ? ` ${block}__${element}` : ''; | |
const m = modifier ? | |
element ? ` ${block}__${element}--${modifier}` : ` ${block}--${modifier}` | |
: ''; | |
return `${b}${e}${m}` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment