BEM composer

A easy BEM classname tool for React, styled-components, linaria.
Recommend using with classnames.
Install
npm
npm i bem-composer
yarn
yarn add bem-composer
API
Output
import { bem } from 'bem-composer';
const b = bem('button');
console.log(b.toString());
console.log(b());
console.log(b.toSelector());
const e = b('icon');
console.log(e.toString());
console.log(e());
console.log(e.toSelector());
console.log(e.toSuffix());
console.log(e.toParentSelector());
const m = e('active');
console.log(m.toString());
console.log(m().toString());
console.log(m.toSelector().toString());
console.log(m.toSuffix().toString());
console.log(m.toParentSelector().toString());
console.log(m.toSuffix(true).toString());
console.log(m.toParentSelector(true).toString());
console.log(m('large')({
name: 'rounded',
value: true
}).toString());
const mv = e({
name: 'active',
value: 'hover'
});
console.log(mv.toString());
console.log(e({
'active',
value: false
}).toString())
console.log(e({
'active',
value: true
}).toString())
console.log(b('', 'active').toString());
Configure delimiters
import { configure, DefaultBEMDelimiters } from 'bem-composer';
const cbem = configure({
prefix: 'bem-',
element: '__',
modifier: '~~',
modifierValue: '~'
});
console.log(
cbem('block', 'icon', {
name: 'active',
value: 'hover'
}).toString()
);
Curried API
Provide a curried API to easily create elements and modifiers.
import { bem } from 'bem-composer';
console.log(bem('button', 'icon').toString());
console.log(bem('button')('icon').toString());
console.log(bem('button', 'icon', 'active').toString());
console.log(bem('button')('icon', 'active').toString());
console.log(bem('button', 'icon')('active').toString());
BEM Constructor type definition
function bem(blockName: string): BEMBlock;
function bem(blockName: string, elementName: string): BEMElement;
function bem(
blockName: string,
elementName: string,
modifierValue: BEMModifierValue
): BEMModifier;
export type BEMModifierValue =
| {
name: string;
value?: string | boolean;
}
| string;
export interface BEMBlock {
(): string;
(elementName: string): BEMElement;
(
elementName: undefined | string,
modifierValue: BEMModifierValue
): BEMModifier;
toString: () => string;
toSelector: () => string;
}
export interface BEMElement {
(): string;
(modifierValue: BEMModifierValue): BEMModifier;
toString: () => string;
toSelector: () => string;
toSuffix: () => string;
toParentSelector: () => string;
}
export interface BEMModifier {
(): string;
(modifierValue: BEMModifierValue): BEMModifier;
toString: () => string;
toSelector: () => string;
toSuffix: (withElement?: boolean) => string;
toParentSelector: (withElement?: boolean) => string;
}
React
import React from 'react';
import { bem } from 'bem-composer';
import classNames from 'classnames';
const b = bem('button');
const e = b('icon');
const m = e('active');
export default function Button(props) {
const { active } = props;
return (
<button
classname={classNames([
b(), // 'button'
b('', {
name: 'active',
value: active
}) // button__active
])}
>
<img
classname={classNames([
e(), // 'button-icon'
e({
name: 'active',
value: active
}), // active === true ? 'button--icon__active' : 'button--icon'
e('primary') // 'button--icon__primary'
])}
/>
</button>
);
}
Styled-components & Linaria
import styled from 'styled-components';
const b = bem('button');
const e = b('icon');
const m = e('active');
export const StyledButton = styled.button`
/* .button */
${b.toSelector()} {
border: 1px solid red;
outline: none;
}
/* &--icon */
${e.toParentSelector()} {
width: 20px;
height: 20px;
/* &__active */
${m.toParentSelector()} {
opacity: 0.5;
}
}
`;
Trouble shooting
Multiple elements support
As we don't want BEM classname to be too complicated, multiple elements is not provided by this library, but you can do some workaround like this:
bem('block', 'e1--e2--e3').toString();