Skip to content

Instantly share code, notes, and snippets.

@CyberAP
Last active December 28, 2020 07:52
Show Gist options
  • Select an option

  • Save CyberAP/dd7b78ba05cbe9ad4f01130604612599 to your computer and use it in GitHub Desktop.

Select an option

Save CyberAP/dd7b78ba05cbe9ad4f01130604612599 to your computer and use it in GitHub Desktop.
Generate bem classes in runtime
// 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