BEM entities can be defined with a help of JS object with the following fields:
block — a block name. The field is required because only a block exists as an independent BEM entity
elem — an element name.
modName — a modifier name.
modVal — a modifier value.
The modifier consists of a pair of fields modName and modVal. This means that the field modVal without modName has no meaning.
Example:
// The block modifier{ block: 'block', modName: 'mod', modVal: 'val'}// Not valid BEM-notation{ block: 'block', modVal: 'val'}
To describe the boolean modifier field modVal must be specified as true.
Example:
// Boolean modifier of a block{ block: 'block', modName: 'mod', modVal: true}// Shorthand for the boolean modifier of a block{ block: 'block', modName: 'mod'}// Not valid BEM-notation{ block: 'block', modName: 'mod', modVal: false}
String to separate modifiers from blocks and elements.
modValDelim
String to separate value of modifier from name of modifier.
Common misconceptions
BEM methodology involves the use of flat structure inside a block. It means that BEM entity can not be represented as an element of the other element and the following string representation will be invalid:
'block__some-elem__sub-elem'
Also there is no such BEM entity as a modifier and an element modifier simultaneously so the following string representation will be invalid:
Use bemNaming function to create instance to manage naming of your own naming convention.
Example:
var myNaming = bemNaming({ elem: '-', mod: { name: '--', val: '_' } wordPattern: '[a-zA-Z0-9]+' // because element and modifier's separators include}); // hyphen in it, we need to exclude it from block, // element and modifier's namemyNaming.parse('block--mod_val'); // { block: 'block', // modName: 'mod', modVal: 'val' }myNaming.stringify({ // 'blockName-elemName--boolElemMod' block: 'blockName', elem: 'elemName', modName: 'boolElemMod'});
bemNaming({ elem, mod, wordPattern })
elem
Type: String
Default: __
Separates element's name from block
mod
Type: String, { name: String, val: String }
Default: _
Separates modifiers from blocks and elements.
This option can take object with following fields:
name — separates name of modifier from blocks and elements.
Default as _.
val — separates value of modifier from name of modifier.
Default as the value of the name.
wordPattern
Type: String
Default: [a-z0-9]+(?:-[a-z0-9]+)*
Defines which symbols can be used for block, element and modifier's names.
Convention by Harry Roberts
According to this convention elements are delimited with two underscores (__), modifiers are delimited by two hyphens (--), and values of modifiers are delimited by one underscore (_).