Skip to content

Instantly share code, notes, and snippets.

@jimjam88
Created July 17, 2023 09:18
Show Gist options
  • Save jimjam88/ee950edd0f60a1d59de439725274a9a6 to your computer and use it in GitHub Desktop.
Save jimjam88/ee950edd0f60a1d59de439725274a9a6 to your computer and use it in GitHub Desktop.
CSS BEM class names builder
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