Skip to content

Instantly share code, notes, and snippets.

@jkarttunen
Created April 12, 2020 07:26
Show Gist options
  • Save jkarttunen/dca226de50fbdb668f3a9a161675cc4b to your computer and use it in GitHub Desktop.
Save jkarttunen/dca226de50fbdb668f3a9a161675cc4b to your computer and use it in GitHub Desktop.
Simple Bem classname utility
/**
* 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