Helper functions to create BEM-style classnames
npm install @dr/bem-helper
var bem = require("@dr/bem-helper");
var className = bem("block", "element", { modifier: true });
// className === "element block__element element--modifier block__element--modifier"
// ^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^
// _____/ _____________________/
// / /
// bem.scoped -> "block__element block__element--modifier"
// ^^^^^^^^^^^^^^^^^^^^^^^^
// ___________/
// /
// bem.single -> "block__element--modifier"
A function that creates all applicable combinations of classnames for an element in BEM-style.
block
(string) - The block element for the classname....args
(string|object) - Optional. Any number of the following arguments are allowed:element
(string) - Optional.modifier
(object) - Optional. Keys are used for modifier names. Values determine whether to turn the modifier off (false
,""
,null
orundefined
) or on - either throughtrue
or a value which will be appended to the modifier name;{modifier: true}
->"--modifier"
,{modifier: "value"}
->"--modifier-value"
. Should not follow a previous modifier argument.
var className = bem("block", "element");
// className === "element block__element"
var className = bem("block", "element", { modifier: true });
// className === "element block__element element--modifier block__element--modifier"
var className = bem("block", "element", { modifier: "value", modifier2: false });
// className === "element block__element element--modifier-value block__element--modifier-value"
var boundBem = bem.bind(null, "block");
var className = boundBem("element", { modifier: true });
// className === "element block__element element--modifier block__element--modifier"
Same as bem - but it returns a fully scoped classname.
var className = bem.scoped("block", "element");
// className === "block__element"
var className = bem.scoped("block", { modifier: true });
// className === "block block--modifier"
var className = bem.scoped("block", "element", { modifier: "value", modifier2: false });
// className === "block__element block__element--modifier-value"
var boundScoped = bem.scoped.bind(null, "block");
var className = boundScoped("element", { modifier: true });
// className === "block__element block__element--modifier"
Same as bem - but it only returns a single classname.
var className = bem.single("block", "element");
// className === "block__element"
var className = bem.single("block", "element", { modifier: true });
// className === "block__element--modifier"
var className = bem.single("block", "element", { modifier: "value" });
// className === "block__element--modifier-value"
var boundSingle = bem.single.bind(null, "block");
var className = boundSingle("element", { modifier: true });
// className === "block__element--modifier"