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

Commit b4ec2e2

Browse files
committed
updating self documenting selectors guide based on UI dev meeting discussion
1 parent e12a2f0 commit b4ec2e2

1 file changed

Lines changed: 63 additions & 4 deletions

File tree

css/class-naming-conventions/readme.md

Lines changed: 63 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 a few methods you can use to accomplish this. Try to pick one and stick to it throughout the project, adding comments as needed.
116116

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

126+
127+
#### Nested inside the component
128+
Nest the `c-component__sub-component` elements inside the `c-component` SCSS.
129+
126130
```scss
127131
.c-blog-post {
128132
&.c--featured {
129-
[STYLES]
133+
...
134+
135+
.c-blog-post__title {
136+
...
137+
}
130138
}
131139
}
140+
```
141+
142+
143+
#### Below the component as a selector chain
144+
Use a selector chain with `.c-component.c--modifier`, and nest the `c-component__sub-component` elements within it.
145+
146+
```scss
147+
.c-blog-post.c--featured {
148+
...
149+
150+
.c-blog-post__title {
151+
...
152+
}
153+
}
154+
```
155+
156+
When using the above methods in larger files, add a comment to the `c-component__sub-component` like so:
157+
```
158+
// Blog Post Title
159+
// ---
160+
//
161+
// Modifier styles can be found in:
162+
// .c-blog-post.c--featured
163+
164+
.c-blog-post__title {
165+
...
166+
}
167+
```
168+
169+
#### Nested inside the sub-component
170+
Nest the modifier code inside the subcomponent using `.c-component.c--modifier &`.
132171

172+
```scss
133173
.c-blog-post__title {
174+
...
175+
134176
.c-blog-post.c--featured & {
135-
[STYLES]
177+
...
136178
}
137179
}
138180
```
139181

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.
182+
When using the above method in larger files, add a comment to the `c-component` like so:
183+
```
184+
// Blog Post
185+
// ===
186+
.c-blog-post {
187+
...
188+
189+
// Featured Post
190+
// ---
191+
//
192+
// Also modifies:
193+
// .c-blog-post__title
194+
195+
.c--featured {
196+
...
197+
}
198+
}
199+
```
141200

142201
### State
143202

0 commit comments

Comments
 (0)