Created
July 17, 2023 09:18
-
-
Save jimjam88/ee950edd0f60a1d59de439725274a9a6 to your computer and use it in GitHub Desktop.
CSS BEM class names builder
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
type Modifiers = { | |
[key: string]: any, | |
} | |
export const toClassNames = (base: string, modifiers: Modifiers = {}): string => { | |
const mods = Object | |
.entries(modifiers) | |
.filter(([, value]) => !!value) | |
.map(([key, _]) => `${base}--${key}`); | |
return [base, ...mods].join(' '); | |
}; | |
// toClassNames("button", { | |
// red: true, | |
// "has-icon": undefined, | |
// disabled: "Y", | |
// }); | |
// | |
// Output: | |
// "button button--red button--disabled" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment