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 Aug 8, 2023. It is now read-only.
Copy file name to clipboardExpand all lines: css/class-naming-conventions/readme.md
+63-4Lines changed: 63 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -112,7 +112,7 @@ These are used to modify components or subcomponents. They are always chained to
112
112
113
113
### Component modifiers that affect subcomponents
114
114
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.
116
116
117
117
```html
118
118
<divclass="c-blog-post c--featured">
@@ -123,21 +123,80 @@ Sometimes you'll write a modifier for a component and you want that modifier to
123
123
</div>
124
124
```
125
125
126
+
127
+
#### Nested inside the component
128
+
Nest the `c-component__sub-component` elements inside the `c-component` SCSS.
129
+
126
130
```scss
127
131
.c-blog-post {
128
132
&.c--featured {
129
-
[STYLES]
133
+
...
134
+
135
+
.c-blog-post__title {
136
+
...
137
+
}
130
138
}
131
139
}
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 &`.
132
171
172
+
```scss
133
173
.c-blog-post__title {
174
+
...
175
+
134
176
.c-blog-post.c--featured& {
135
-
[STYLES]
177
+
...
136
178
}
137
179
}
138
180
```
139
181
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:
0 commit comments