Last active
December 28, 2020 07:52
-
-
Save CyberAP/dd7b78ba05cbe9ad4f01130604612599 to your computer and use it in GitHub Desktop.
Generate bem classes in runtime
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
| // Usage: | |
| // bem('class-name', { foo: 'bar', baz: true }) → ['class-name--foo-bar', 'class-name--baz'] | |
| export const bem = (baseClass, mods) => { | |
| const keys = Object.keys(mods); | |
| const res = []; | |
| keys.forEach(key => { | |
| const param = mods[key]; | |
| if (!param) return; | |
| if (Array.isArray(param)) { | |
| param.forEach(item => { | |
| res.push(`${baseClass}--${key}-${item}`); | |
| }); | |
| } else { | |
| const mod = typeof param === 'boolean' ? key : `${key}-${param}`; | |
| res.push(`${baseClass}--${mod}`); | |
| } | |
| }); | |
| return res; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment