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
+64-4Lines changed: 64 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 several methods you can use to accomplish this. As much as possible, stick to one method in your project.
116
116
117
117
```html
118
118
<divclass="c-blog-post c--featured">
@@ -123,21 +123,81 @@ Sometimes you'll write a modifier for a component and you want that modifier to
123
123
</div>
124
124
```
125
125
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
+
126
132
```scss
127
133
.c-blog-post {
128
134
&.c--featured {
129
-
[STYLES]
135
+
...
136
+
137
+
.c-blog-post__title {
138
+
...
139
+
}
130
140
}
131
141
}
142
+
```
143
+
144
+
*or*
132
145
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
133
175
.c-blog-post__title {
176
+
...
177
+
134
178
.c-blog-post.c--featured& {
135
-
[STYLES]
179
+
...
136
180
}
137
181
}
138
182
```
139
183
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:
0 commit comments