You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on May 4, 2019. It is now read-only.
Copy file name to clipboardExpand all lines: _posts/2015-10-19-creating-bem-stylesheets-for-react-components.markdown
+34-51Lines changed: 34 additions & 51 deletions
Original file line number
Diff line number
Diff line change
@@ -44,50 +44,34 @@ A few things to remember:
44
44
45
45
The BEM methodology has been very useful within our React components. Seeing as we're already creating objects that are wholey reuseable across the site, it only seems natural to make sure that our CSS is as concise.
46
46
47
-
I was recently creating a new React component for our "Book a Tour" modal, called LocationCmInfo ("Cm" standing for Community Manager, the person giving the tour.)
<h2className="apercu">42nd Street - 205 E 42nd St</h2>
62
+
<p>Jacquie Dershowitz is a Community Manager at 42nd Street and will give you a tour of the building.</p>
63
+
</div>
64
+
</div>
65
+
</div>
66
+
</div>
83
67
```
84
68
85
-
The markup is incredibly readable and easy to comprehend. We have a block (`.location-cm-info`) that contains an element (`.location-cm-info__content`) containing 2 other elements (`.location-cm-info__image` and `.location-cm-info__text`.) There are no modifiers yet.
69
+
The markup is incredibly readable and easy to comprehend. We have a block (`.location-info`) that contains an element (`.location-info__content`) containing 2 other elements (`.location-info__image` and `.location-info__text`.) There are no modifiers yet.
86
70
87
71
I was taking a look at this the other day and realized that **this CSS could use a little refactoring**:
88
72
89
73
```sass
90
-
.location-cm-info {
74
+
.location-info {
91
75
&__content {
92
76
position: relative;
93
77
background-color: $syracuse;
@@ -134,20 +118,19 @@ I was taking a look at this the other day and realized that **this CSS could use
134
118
}
135
119
```
136
120
137
-
I've been breaking my own rule: "Do not use HTML elements in selectors." Let's first refactor the markup and change the stylesheet accordingly. First of all, I would like to rename this component `.location-info` and add a modifier `--with-cm` to clarify that this module can be used for more than just highlighting a Community Manager.
138
-
121
+
I've been breaking my own rule: "Do not use HTML elements in selectors." Let's first refactor the markup and change the stylesheet accordingly. First of all, I would like to add a modifier `--with-image` to clarify that this module can be used without an image as well.
<pclassName="location-info__paragraph">{selectedLocation.community_manager.name} is a Community Manager at {selectedLocation.name} and will give you a tour of the building.</p>
132
+
<h2className="location-info__subheader apercu">42nd Street - 205 E 42nd St</h2>
133
+
<pclassName="location-info__paragraph">Jacquie Dershowitz is a Community Manager at 42nd Street and will give you a tour of the building.</p>
- Brought in the useful aspect of SASS that allows us to define and interpolate variables (by using: `$module: 'location-info'`). (see an example of this implementation in [Support for BEM modules in Sass 3.3](http://mikefowler.me/2013/10/17/support-for-bem-modules-sass-3.3/)) This came in handy when styling elements within my modifier.
242
-
- Moved `.location-info__image` into the modifier `.location-info--with-cm`, **assuming** that in the future, we will not be using an image in our location-info components unless we are displaying the CM's information along with it. Who knows, maybe we *will* want to refactor this in the future, to allow for other types of images to be displayed in our location-info components. But that would just be a simple addition of `.location-info .location-info_image-container` with its own properties. And best of all, that **won't affect** the styles for location-info components with the class `.location-info--with-cm`.
243
-
- Moved specific styles for `.location-info__content` to nest under `.location-info--with-cm`, as I doubt that these styles will be used for full-width page view of a LocationInfo component without a CM image. (ie. We probably won't want a gray (`$syracuse`) background when we use LocationInfo without the CM image.)
225
+
- Moved `.location-info__image` into the modifier `.location-info--with-image`, **assuming** that in the future, we will not be using an image in our location-info components.
226
+
- Moved specific styles for `.location-info__content` to nest under `.location-info--with-image`, as I doubt that these styles will be used for full-width page view of a LocationInfo component without an image. (ie. We probably won't want a gray (`$syracuse`) background when we use LocationInfo without the image.)
244
227
- Added `.location-info__text-container`, `.location-info__header`, `.location-info__subheader`, and `.location-info__paragraph`.
245
228
246
229
@@ -252,8 +235,8 @@ Immediately, my CSS is more readable, not relying on HTML elements, and context-
<pclassName="location-info__paragraph">{selectedLocation.name} is a great place to work. We have amenities such as {selectedLocation.amenities}.</p>
238
+
<h2className="location-info__subheader apercu">42nd Street - 205 E 42nd St</h2>
239
+
<pclassName="location-info__paragraph">42nd Street is a great place to work. We have amenities such as community managers, high speed internet, and printing services.</p>
0 commit comments