Created
August 30, 2018 08:30
-
-
Save szkrd/ff4e9eb4e1a0f102fe60e28c85d7bf27 to your computer and use it in GitHub Desktop.
quick bem helper hack
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
import * as log from 'loglevel' | |
// # create bem | |
// | |
// ``` | |
// const bem = createBem('my-favorite-foods') | |
// | |
// bem.b() === 'my-favorite-foods' | |
// bem.b('ordered') === 'my-favorite-foods--ordered' | |
// bem.b(['ordered', 'waiting-for']) === 'my-favorite-foods--ordered my-favorite-foods--waiting-for' | |
// | |
// bem.e('cheese') === 'my-favorite-foods__cheese' | |
// bem.e('cheese', 'cheddar') === 'my-favorite-foods__cheese--cheddar' | |
// bem.e('cheese', ['cheddar', 'camembert']) === | |
// 'my-favorite-foods__cheese--cheddar my-favorite-foods__cheese--camembert' | |
// ``` | |
const logged = [] | |
const logClassName = (combined) => { | |
// just copy this section from the console and paste into | |
// a new scss file, then do a bit of a search and replace | |
if (logged.indexOf(combined) === -1) { | |
log.info(combined) | |
logged.push(combined) | |
} | |
} | |
const createBem = (block: string, debug = false) => ({ | |
// block helper | |
b: (mod?: string | string[]): string => { | |
let combined = '' | |
if (!mod) { | |
combined = block | |
} else { | |
const modArr = typeof mod === 'string' ? [mod] : mod | |
combined = modArr.map(modItem => `${block}--${modItem}`).join(' ') | |
} | |
if (debug) { | |
logClassName(combined) | |
} | |
return combined | |
}, | |
// block__element helper | |
e: (element = '', mod?: string | string[]): string => { | |
const blockElement = `${block}__${element}` | |
let combined = '' | |
if (!mod) { | |
combined = blockElement | |
} else { | |
const modArr = typeof mod === 'string' ? [mod] : mod | |
combined = modArr.map(modItem => `${blockElement}--${modItem}`).join(' ') | |
} | |
if (debug) { | |
logClassName(combined) | |
} | |
return combined | |
} | |
}) | |
export default createBem |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment