@@ -12,6 +12,7 @@ Compiles CSS packages with:
12
12
* [ postcss-easy-import] ( https://github.com/TrySound/postcss-easy-import )
13
13
* [ postcss-custom-properties] ( https://github.com/postcss/postcss-custom-properties )
14
14
* [ postcss-calc] ( https://github.com/postcss/postcss-calc )
15
+ * [ postcss-autoreset] ( https://github.com/maximkoretskiy/postcss-autoreset )
15
16
* [ postcss-color-function] ( https://github.com/postcss/postcss-color-function )
16
17
* [ postcss-apply] ( https://github.com/pascalduez/postcss-apply )
17
18
* [ postcss-custom-media] ( https://github.com/postcss/postcss-custom-media )
@@ -37,6 +38,8 @@ suitcss input.css output.css
37
38
38
39
### Command Line
39
40
41
+ Options are [ documented below] ( #options )
42
+
40
43
```
41
44
Usage: suitcss [<input>] [<output>]
42
45
@@ -45,6 +48,7 @@ Options:
45
48
-h, --help output usage information
46
49
-c, --config [path] a custom PostCSS config file
47
50
-i, --import-root [path] the root directory for imported css files
51
+ -s, --encapsulate encapsulate component styles
48
52
-w, --watch watch the input file and any imports for changes
49
53
-m, --minify minify output with cssnano
50
54
-e, --throw-error throw an error when any warnings are found
@@ -99,16 +103,16 @@ preprocessor(css, {
99
103
});
100
104
```
101
105
102
- #### Options
106
+ ### Options
103
107
104
- ##### ` root `
108
+ #### ` root `
105
109
106
110
* Type: ` String `
107
111
* Default: ` process.cwd() `
108
112
109
113
Where to resolve imports from. Passed to [ ` postcss-import ` ] ( https://github.com/postcss/postcss-import/blob/master/README.md#root ) .
110
114
111
- ##### ` debug `
115
+ #### ` debug `
112
116
113
117
* Type: ` Function `
114
118
* Default: identity (it does nothing)
@@ -137,7 +141,130 @@ function debug(plugins) {
137
141
}
138
142
```
139
143
140
- ##### ` lint `
144
+ #### ` encapsulate `
145
+
146
+ _ (experimental)_
147
+
148
+ * Type: ` Boolean `
149
+ * Default: ` false `
150
+
151
+ Resets CSS properties to their [ initial values] ( https://developer.mozilla.org/en-US/docs/Web/CSS/initial_value )
152
+ to effectively allow a component to opt out of CSS inheritance and be
153
+ encapsulated from the rest of the application similar to [ the Shadow DOM] ( https://www.w3.org/TR/shadow-dom/ ) .
154
+ There are two types of CSS properties that affect components, inherited (e.g.
155
+ ` font-size, ` ` color ` ) and non-inherited (e.g. ` margin ` , ` background ` ). This
156
+ option works so that:
157
+
158
+ * Root elements (e.g. ` .Component ` ) have both inherited and non-inherited
159
+ properties reset to default values.
160
+ * Descendants (e.g. ` .Component-item ` ) only have non-inherited properties reset
161
+ as this allows properties set on the root element to be inherited by its
162
+ descendants.
163
+
164
+ This means that components are isolated from styles outside the component root
165
+ element but should an inheritable property such as ` font-size ` be applied on the
166
+ component root element it will be inherited by the component descendants as
167
+ normal. This prevents the need to redeclare properties on every descendant in a
168
+ component.
169
+
170
+ The same rules also apply to nested components.
171
+
172
+ ** Rationale**
173
+
174
+ One of the difficulties with CSS components is predictably. Unwanted styles
175
+ can be inherited from parent components and this can make it difficult to
176
+ reuse components in different contexts.
177
+
178
+ Methodologies such as SUIT and BEM exist to solve problems around the cascade
179
+ and specificity but they cannot protect components from inheriting unwanted
180
+ styles. What would really help is to allow inheritance to be 'opt-in' and let
181
+ component authors decide what properties are inherited. This creates a more
182
+ predictable baseline for styling components and promoting easier
183
+ reuse.
184
+
185
+ * [ Component Based Style Reuse] ( https://youtu.be/_70Yp8KPXH8?t=27m45s )
186
+ * [ React: CSS in JS] ( http://blog.vjeux.com/2014/javascript/react-css-in-js-nationjs.html )
187
+
188
+ ** Examples**
189
+
190
+ * [ CodePen encapsulate] ( http://codepen.io/simonsmith/pen/BLOyAX ) - Demonstrates
191
+ how components are encapsulated from global and parent styles.
192
+ * [ CodePen encapsulate inheritance] ( http://codepen.io/simonsmith/pen/LRgxdp ) -
193
+ Similar to above but shows how components can opt-in to inheritance.
194
+
195
+ ** What about ` all: initial ` ?**
196
+
197
+ The ` all: initial ` declaration will reset both inherited and non-inherited
198
+ properties but this can be too forceful. For example ` display ` is reset to
199
+ ` inline ` on block elements and as mentioned earlier, descendants of a component
200
+ should only have non-inherited properties reset to allow declarations to be
201
+ inherited from the root element.
202
+
203
+ > For example, if an author specifies ` all: initial ` on an element it will block
204
+ all inheritance and reset all properties, as if no rules appeared in the
205
+ author, user, or user-agent levels of the cascade.
206
+
207
+ https://www.w3.org/TR/css3-cascade/#all-shorthand
208
+
209
+ Instead a subset of properties are reset to allow more
210
+ granular control over what parts of a component use inheritance.
211
+
212
+ To achieve this the preprocessor uses
213
+ [ postcss-autoreset] ( https://github.com/maximkoretskiy/postcss-autoreset ) with
214
+ the SUIT preset and a [ custom set of CSS properties] ( lib/encapsulation.js ) that
215
+ are reset to their initial values. ** Only selectors conforming** to the SUIT naming
216
+ conventions are affected.
217
+
218
+ ** Caveats**
219
+
220
+ ##### Selectors must be present in the component CSS
221
+
222
+ If an element is present in the HTML but not styled in the component CSS
223
+ (perhaps relying on utility classes) it will not be reset. In
224
+ this instance an empty ruleset can be added to ensure it is correctly reset:
225
+
226
+ ``` html
227
+ <div class =" Component u-posRelative u-textCenter" >
228
+ <div class =" Component-item" ></div >
229
+ </div >
230
+ ```
231
+ ``` css
232
+ /* Empty ruleset required */
233
+ .Component {}
234
+
235
+ .Component-item {
236
+ color : red ;
237
+ }
238
+ ```
239
+
240
+ ##### Global styles can still override descendants
241
+
242
+ Because component descendants only have non-inheritable properties reset it can
243
+ lead to specific global rules still applying:
244
+
245
+ ``` css
246
+ /* global.css */
247
+ span {
248
+ color : red ;
249
+ }
250
+
251
+ /* component.css */
252
+ .Component-text {
253
+ font-style : bold ;
254
+ }
255
+ ```
256
+ ``` html
257
+ <div class =" Component" >
258
+ <span class =" Component-text" >
259
+ <!-- this text is red -->
260
+ <span >
261
+ </div >
262
+ ```
263
+
264
+ The solution to this is to minimise or avoid entirely the use of global styles
265
+ which is the recommended approach in a SUIT CSS application.
266
+
267
+ #### ` lint `
141
268
142
269
* Type: ` Boolean `
143
270
* Default: ` true `
@@ -161,14 +288,14 @@ locally in your package.
161
288
}
162
289
```
163
290
164
- ##### ` minify `
291
+ #### ` minify `
165
292
166
293
* Type: ` Boolean `
167
294
* Default: ` false `
168
295
169
296
If set to ` true ` then the output is minified by [ ` cssnano ` ] ( http://cssnano.co/ ) .
170
297
171
- ##### ` postcss `
298
+ #### ` postcss `
172
299
173
300
* Type: ` Object `
174
301
* Default: ` undefined `
@@ -194,7 +321,7 @@ A list of plugins that are passed to PostCSS. This can be used to add new plugin
194
321
}
195
322
```
196
323
197
- ##### ` <plugin-name> `
324
+ #### ` <plugin-name> `
198
325
199
326
* Type: ` Object `
200
327
* Default: ` undefined `
0 commit comments