Skip to content

Commit 12e7b58

Browse files
committed
Add note about PascalCasing BEM blocks with React
BEM wasn't designed with React in mind, and I think that this deviation from the convention is useful to more closely connect the concepts of BEM to your React components. Granted, with CSS Modules or inline CSS (CSS in JS), this is pretty irrelevant, but it is nice to provide the guidance for folks who are not yet living in that world.
1 parent e6a32ac commit 12e7b58

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

README.md

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ Finally, properties are what give the selected elements of a rule declaration th
6767
### Formatting
6868

6969
* Use soft tabs (2 spaces) for indentation
70-
* Prefer dashes over camelCasing in class names. Underscores are OK if you're using BEM (see [OOCSS and BEM](#oocss-and-bem) below).
70+
* Prefer dashes over camelCasing in class names.
71+
- Underscores and PascalCasing are okay if you are using BEM (see [OOCSS and BEM](#oocss-and-bem) below).
7172
* Do not use ID selectors
7273
* When using multiple selectors in a rule declaration, give each selector its own line.
7374
* Put a space before the opening brace `{` in rule declarations
@@ -131,30 +132,38 @@ We encourage some combination of OOCSS and BEM for these reasons:
131132
* CSS Trick's [BEM 101](https://css-tricks.com/bem-101/)
132133
* Harry Roberts' [introduction to BEM](http://csswizardry.com/2013/01/mindbemding-getting-your-head-round-bem-syntax/)
133134

135+
We recommend a variant of BEM with PascalCased “blocks”, which works particularly well when combined with components (e.g. React). Underscores and dashes are still used for modifiers and children.
136+
134137
**Example**
135138

136-
```html
137-
<article class="listing-card listing-card--featured">
139+
```jsx
140+
// ListingCard.jsx
141+
function ListingCard() {
142+
return (
143+
<article class="ListingCard ListingCard--featured">
138144

139-
<h1 class="listing-card__title">Adorable 2BR in the sunny Mission</h1>
145+
<h1 class="ListingCard__title">Adorable 2BR in the sunny Mission</h1>
140146

141-
<div class="listing-card__content">
142-
<p>Vestibulum id ligula porta felis euismod semper.</p>
143-
</div>
147+
<div class="ListingCard__content">
148+
<p>Vestibulum id ligula porta felis euismod semper.</p>
149+
</div>
144150

145-
</article>
151+
</article>
152+
);
153+
}
146154
```
147155

148156
```css
149-
.listing-card { }
150-
.listing-card--featured { }
151-
.listing-card__title { }
152-
.listing-card__content { }
157+
/* ListingCard.css */
158+
.ListingCard { }
159+
.ListingCard--featured { }
160+
.ListingCard__title { }
161+
.ListingCard__content { }
153162
```
154163

155-
* `.listing-card` is the “block” and represents the higher-level component
156-
* `.listing-card__title` is an “element” and represents a descendant of `.listing-card` that helps compose the block as a whole.
157-
* `.listing-card--featured` is a “modifier” and represents a different state or variation on the `.listing-card` block.
164+
* `.ListingCard` is the “block” and represents the higher-level component
165+
* `.ListingCard__title` is an “element” and represents a descendant of `.ListingCard` that helps compose the block as a whole.
166+
* `.ListingCard--featured` is a “modifier” and represents a different state or variation on the `.ListingCard` block.
158167

159168
### ID selectors
160169

@@ -282,5 +291,5 @@ If you must use an ID selector in the first place (and you should really try not
282291
## Translation
283292
284293
This style guide is also available in other languages:
285-
294+
286295
- ![cn](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/China.png) **Chinese (Simplified)**: [Zhangjd/css-style-guide](https://github.com/Zhangjd/css-style-guide)

0 commit comments

Comments
 (0)