NOTE I now use the conventions detailed in the SUIT framework
Used to provide structural templates.
Pattern
t-template-name
t-template-name--modifier-name
t-template-name__subcomponent-name--subcomponent-modifier-name
Examples
t-icon
t-icon--large
t-btn
t-btn--large
t-media
t-media__img
t-media__img--large
t-media__opt
t-media__body
Used to indicate the state of a component
Pattern
is-state-type
Examples
is-hidden
is-collapsed
is-expanded
is-selected
Used to provide JS-only hooks for a component. Can be used to provide a JS-enhanced UI or to abstract other JS behaviours.
Pattern
js-action-name
Examples
js-submit
js-action-save
js-ui-collapsible
js-ui-dropdown
js-ui-dropdown--control
js-ui-dropdown--menu
js-ui-carousel
Could reuse the Template Component naming convention, for example:
specific-name
specific-name--modifier-name
specific-name__subcomponent-name
specific-name__subcomponent-name--subcomponent-modifier-name
Or just let anything go.
Andrew, I believe the logic is that name spacing your selectors allows devs that come along behind you to understand that .product-rating__panel is meant to be a sub-component of .product-rating. Thus, it should always be used inside that DOM node. It isn't meant to stand alone or outside that node.
The other benefit I see is, avoiding descendent selectors where possible allows you to utilize the cascade more fully. You can modify the .panel rule with another single class rule that comes later in the cascade. You don't have to use another descendent selector or !important to override the original class like you would if you used:
Adding the modifier class .foo to the original .panel would not override the original rule unless you wrote the new rule as:
Or
(Chaining selectors, as above, can give you a performance hit and so should be avoided where possible.)
If you had used the .product-rating__panel style of naming, a second single class would modify the necessary properties. For example:
Yes, it does get long... and yes, I've struggled with this as well (in fact, I'm reevaluating naming conventions now for an extremely large project). But for keeping the code maintainable on a site with a large number of developers, this seems logical. You'd be left with these rules (all with the same specificity):
(Harry Roberts suggests indenting the rules in your CSS, so that they mirror the DOM instead of nesting rules so that specificity is increased.)