Last active
February 23, 2023 17:18
-
-
Save tcdw/2f6accd34a1a68e08a94e5cb31fdfb72 to your computer and use it in GitHub Desktop.
为单一元素生成符合 BEM 规则的 CSS Class 名称
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
/** | |
* 为单一元素生成符合 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