How to use the Modifier entity
How we use modifiers :
Some peoples will write .block-name--modifier-name__element-name
.
But in my experience it is overkill, and it prevent us to write modifiers in a Object oriented fashion.
We will prefer to write Element with a block modifier this way : .block-name--modifier-name .block-name__element-name {}
You will tell me, this create unnecessary specificity, and BEM state that your specificity should as flat as possible.
Yes absolutely, but in the first way, it's a pain in the *^%!" to use multiple modifiers. Let me explain with an exemple.
The button exemple
Assume I want two modifiers for their color scheme:
neutral
call to action
Also two modifiers for their sizes :
small
large
The button need to have two elements:
an icon
a text container
The first version
here what it could look like with the first version
First, we can see that our icon declarations are outside the icon
element declaration block, witch beat the purpose of our syntactic sugar. And let's face it, developer love syntactic sugar.
We would prefer to have all the declarations related to icon into the icon block, because it make more sense to colocate all styles related to icons.
It can be tempting to collocate the styles within the modifiers like so :
But because of the declaration order, the styles of the non-modified icon
and text
styles will be applied… Solving this would result in having to move all the modifier related entities at the bottom of our block, and I think this is a lot more confusing.
But there is worst, look at the markup :
We have our button block witch take his modifiers. But see how we need to re-apply all the modifiers into the all the required elements. This is a lot of classes. and there is only two elements that need to be modified, what if your block contain plenty of elements and have more than two types of modifiers (size and color). I think you see my point here.
Side note : We can also see that the icon came with its own selectors from another .ga-icon
component. and this is part of the magic of BEM : you know the relationship between classes by their names only.
What we do instead
The markup :
In this version, we can see that we have our syntactic sugar, as well as a way, way cleaner markup. if we need more modifier types, we just add it to the markup at the block level.
Its cost us a specificity of 20
instead of 10
but in my experience, as our components are isolated, this is rarely causing any problems as long as the SRP is applied with good (enough) care.
Last updated