Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Commit 37a9e8a

Browse files
author
Sonja Farrell
committed
Merge pull request #57 from mobify/CSS__self-documenting-selectors
Updating self documenting selectors guide based on UI dev meeting discussion!
2 parents 93cf850 + 9be5c6b commit 37a9e8a

1 file changed

Lines changed: 64 additions & 4 deletions

File tree

css/class-naming-conventions/readme.md

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ These are used to modify components or subcomponents. They are always chained to
112112

113113
### Component modifiers that affect subcomponents
114114

115-
Sometimes you'll write a modifier for a component and you want that modifier to affect the subcomponents in that component. There's a standard way to write this that will ensure compiled styles are easy to find while maintaining consistent selector placement.
115+
Sometimes a component modifier will affect its sub-components. There are several methods you can use to accomplish this. As much as possible, stick to one method in your project.
116116

117117
```html
118118
<div class="c-blog-post c--featured">
@@ -123,21 +123,81 @@ Sometimes you'll write a modifier for a component and you want that modifier to
123123
</div>
124124
```
125125

126+
127+
#### 1. Styles grouped with modifier
128+
Nest the `.c-component__sub-component` elements inside the `.c-component` SCSS.
129+
130+
This method allows you to quickly update or edit the styles for all elements affected by a modifier.
131+
126132
```scss
127133
.c-blog-post {
128134
&.c--featured {
129-
[STYLES]
135+
...
136+
137+
.c-blog-post__title {
138+
...
139+
}
130140
}
131141
}
142+
```
143+
144+
*or*
132145

146+
147+
```scss
148+
.c-blog-post.c--featured {
149+
...
150+
151+
.c-blog-post__title {
152+
...
153+
}
154+
}
155+
```
156+
157+
In larger files, adding a comment in the `.c-component__sub-component` notes can be helpful:
158+
```scss
159+
// Blog Post Title
160+
// ---
161+
//
162+
// Modified by .c-blog-post.c--featured
163+
164+
.c-blog-post__title {
165+
...
166+
}
167+
```
168+
169+
#### 2. Styles grouped with sub-component
170+
Nest the modifier code inside the sub-component using `.c-component.c--modifier &`.
171+
172+
This method makes it easier to visualize the differences between a sub-component and its modified states.
173+
174+
```scss
133175
.c-blog-post__title {
176+
...
177+
134178
.c-blog-post.c--featured & {
135-
[STYLES]
179+
...
136180
}
137181
}
138182
```
139183

140-
This might look a little weird at the outset but it's the best way to ensure that all of a components styles stay in the same place. It also ensures that no modifier styles are accidentally inherited where they shouldn't be.
184+
In larger files, adding a comment in the `.c--modifier` notes can be helpful:
185+
```scss
186+
// Blog Post
187+
// ===
188+
.c-blog-post {
189+
...
190+
191+
// Featured Post
192+
// ---
193+
//
194+
// Also modifies .c-blog-post__title
195+
196+
.c--featured {
197+
...
198+
}
199+
}
200+
```
141201

142202
### State
143203

0 commit comments

Comments
 (0)