Skip to content

Commit e9bec77

Browse files
author
Kenji Shimizu
committed
[WIP] Update TWP Style Guide
1 parent 254ac9b commit e9bec77

File tree

5 files changed

+770
-369
lines changed

5 files changed

+770
-369
lines changed

.prettierignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
README.md
1+
*.md

CSS-SASS.md

+217
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
# CSS
2+
3+
Work in progress
4+
5+
## Content
6+
7+
- [BEM Methodology](#bem-methodology)
8+
- [TWP Starter Theme common SASS usage](#twp-starter-theme-common-sass-usage)
9+
- [Additional notes](#additional-notes)
10+
- [Other style guides](#other-style-guides)
11+
12+
<br>
13+
<br>
14+
15+
---
16+
17+
## BEM methodology
18+
19+
By using the Block-Element-Modifier (BEM) CSS methodology, the chances of undesired and conflicting style overrides is virtually eliminated, and the need for nesting is diminished (which usually results in better performance, by eliminating excessive selector depth).
20+
21+
### Block, element, modifier basics
22+
23+
```scss
24+
.block {
25+
}
26+
27+
.block__element {
28+
}
29+
30+
.block__element--modifier {
31+
}
32+
33+
.block--modifier {
34+
}
35+
```
36+
37+
A good example is a simple card:
38+
39+
```scss
40+
// Product block
41+
.product {
42+
color: black;
43+
}
44+
45+
// Product elements
46+
.product__image {
47+
opacity: 1;
48+
}
49+
50+
// Product modifier
51+
.product--out-of-stock {
52+
color: red;
53+
54+
.product__image {
55+
opacity: 0.5;
56+
}
57+
}
58+
```
59+
60+
```html
61+
<!-- Normal product / color is black -->
62+
<div class="product">
63+
<img class="product__image" src="image.jpg" /> <!-- image is 100% opaque -->
64+
</div>
65+
66+
<!-- Out of stock product / color is red -->
67+
<div class="product product--out-of-stock">
68+
<img class="product__image" src="image.jpg" /> <!-- image is 50% opaque -->
69+
</div>
70+
```
71+
72+
### Avoid over-qualified selectors by (unnecessary) nesting
73+
74+
That means the following code should be avoided. Not only because it produces more code, it also makes it harder to keep track of the parent-children chain, and harder to debug in some cases. The longer the rules, the harder it gets to keep track of the parent when deep nesting is used.
75+
76+
```scss
77+
// Unnecessary nesting
78+
.product {
79+
color: black;
80+
81+
.product__image {
82+
opacity: 1;
83+
}
84+
85+
&.product--out-of-stock {
86+
color: red;
87+
88+
.product__image {
89+
opacity: 0.5;
90+
}
91+
}
92+
}
93+
94+
// Would result in:
95+
.product { color: black }
96+
.product .product__image { opacity: 1 }
97+
.product.product--out-of-stock { color: red }
98+
.product.product--out-of-stock .product__image { opacity: 0.5 }
99+
100+
// As opposed to how it should be (without the unnecessary nesting):
101+
.product { color: black }
102+
.product__image { opacity: 1 }
103+
.product--out-of-stock { color: red }
104+
.product--out-of-stock .product__image { opacity: 0.5 }
105+
```
106+
107+
In the above example, `.product .product__image` is an over-qualification, as `.product__image` itself is sufficient to target the correct element.
108+
109+
### Blocks can also be elements of other blocks
110+
111+
In slightly more complex situations, blocks can also be elements of other blocks:
112+
113+
```scss
114+
// Price block
115+
.price {
116+
font-weight: 700;
117+
}
118+
119+
// Product price element
120+
.product__price {
121+
font-weight: 400;
122+
}
123+
```
124+
125+
```html
126+
<!-- Price used isolated / 700 -->
127+
<p><span class="price">$100</span></p>
128+
129+
<!-- Price used as a product element / 400 -->
130+
<div class="product">
131+
<p><span class="price product__price">$100</span></p>
132+
</div>
133+
```
134+
135+
<br>
136+
<br>
137+
138+
---
139+
140+
## TWP Starter Theme common SASS usage
141+
142+
### Use `$spacing-x` variables for defining margins and paddings
143+
144+
```scss
145+
.block {
146+
padding: $spacing-md;
147+
}
148+
```
149+
150+
### Use `rem()` when defining custom pixel dimensions
151+
152+
```scss
153+
.block {
154+
font-size: rem(16px);
155+
}
156+
```
157+
158+
### Use `media-min` and `media-max` mixins for breakpoints
159+
160+
```scss
161+
.block {
162+
font-size: rem(12px);
163+
164+
@include media-max($large) {
165+
font-size: rem(16px);
166+
}
167+
168+
@include media-min($large) {
169+
font-size: rem(24px);
170+
}
171+
}
172+
```
173+
174+
<br>
175+
<br>
176+
177+
---
178+
179+
## Additional notes
180+
181+
### Prefer `data-attributes` as selectors in JS
182+
183+
Whenever possible and where it makes sense.
184+
185+
```html
186+
<!-- HTML -->
187+
<button class="button button--primary" data-toggle-button>
188+
Open menu
189+
</button>
190+
```
191+
192+
```css
193+
/* CSS */
194+
.button {
195+
}
196+
197+
.button--primary {
198+
}
199+
```
200+
201+
```js
202+
// JS
203+
const toggleButton = container.querySelector('[data-toggle-button]');
204+
```
205+
206+
<br>
207+
<br>
208+
<br>
209+
210+
---
211+
212+
## Other style guides
213+
214+
1. [Core Principles](/README.md)
215+
2. [Javascript](/JAVASCRIPT.md)
216+
3. CSS
217+
4. [Liquid & HTML](/LIQUID-HTML.md)

0 commit comments

Comments
 (0)