Skip to content

Instantly share code, notes, and snippets.

@tcdw
Last active February 23, 2023 17:18
Show Gist options
  • Save tcdw/2f6accd34a1a68e08a94e5cb31fdfb72 to your computer and use it in GitHub Desktop.
Save tcdw/2f6accd34a1a68e08a94e5cb31fdfb72 to your computer and use it in GitHub Desktop.
为单一元素生成符合 BEM 规则的 CSS Class 名称
/**
* 为单一元素生成符合 BEM 规则的 CSS Class 名称
* @param block
* @param element
* @param modifiers
* @returns classes 返回的 Class 名称数组,可直接供 Vue 使用
*/
export function getBEMClassName(
block: string,
element?: string | null,
modifiers: Record<string, any> = {}
) {
let baseName = block;
if (element) {
baseName += `__${element}`;
}
const classes = [baseName];
Object.entries(modifiers).forEach(([k, v]) => {
if (v) {
classes.push(`${baseName}--${k}`);
}
});
return classes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment