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
[Try it in the playground](https://madlittlemods.github.io/postcss-css-variables/playground/) and see what you think! Just add some CSS and see to see the final transformed/compiled CSS. You can try anything here in the playground, too.
38
38
@@ -185,21 +185,21 @@ Will be transformed to:
185
185
186
186
### Nested Rules
187
187
188
-
This pairs very well with [`postcss-nested`](https://github.com/postcss/postcss-nested) or [`postcss-nesting`](https://github.com/jonathantneal/postcss-nesting), adding support for nested rules. For either one, you must use that plugin before `postcss-css-variables` in your setup so the `&` references can be expanded first (`postcss-css-variables` doesn't understand them). For example, with `postcss-nested`, your PostCSS setup can look like this:
188
+
This pairs very well with [`postcss-nested`](https://github.com/postcss/postcss-nested) or [`postcss-nesting`](https://github.com/jonathantneal/postcss-nesting), adding support for nested rules. For either, you must put the plugin before `postcss-css-variables` in the plugin stack so that the `&` references are expanded first (`postcss-css-variables` doesn't understand them). For example, with `postcss-nested`, your PostCSS setup would look like this:
189
189
190
190
191
191
```js
192
192
var postcss =require('postcss');
193
193
var cssvariables =require('postcss-css-variables');
194
-
varnestedcss=require('postcss-nested');
194
+
varnested=require('postcss-nested');
195
195
196
196
var fs =require('fs');
197
197
198
198
var mycss =fs.readFileSync('input.css', 'utf8');
199
199
200
200
var output =postcss([
201
201
// Flatten/unnest rules
202
-
nestedcss,
202
+
nested,
203
203
// Then process any CSS variables
204
204
cssvariables(/*options*/)
205
205
])
@@ -256,7 +256,7 @@ For a more complex example with a media query:
256
256
}
257
257
```
258
258
259
-
This will be transformed to:
259
+
Will be transformed to:
260
260
261
261
```css
262
262
.box-foo {
@@ -278,64 +278,19 @@ This will be transformed to:
278
278
}
279
279
```
280
280
281
-
### `calc()`
282
-
283
-
It also pairs well with [`postcss-calc`](https://github.com/postcss/postcss-calc) to reduce `calc()` expressions arising from using variables.
284
-
285
-
```js
286
-
var postcss =require('postcss');
287
-
var cssvariables =require('postcss-css-variables');
288
-
var calc =require('postcss-calc');
289
-
290
-
var fs =require('fs');
291
-
292
-
var mycss =fs.readFileSync('input.css', 'utf8');
293
-
294
-
var output =postcss([
295
-
// Process any CSS variables
296
-
cssvariables(/*options*/)
297
-
// Then reduce `calc()` expressions
298
-
calc()
299
-
])
300
-
.process(mycss)
301
-
.css;
302
-
303
-
console.log(output);
304
-
```
305
-
306
-
```css
307
-
:root {
308
-
--page-margin: 1em;
309
-
}
310
-
311
-
/* Easy centering */
312
-
.wrapper {
313
-
width: calc(100%-2*var(--page-margin));
314
-
margin-left: var(--page-margin);
315
-
margin-right: var(--page-margin);
316
-
}
317
-
```
318
-
319
-
This will be transformed to:
320
-
321
-
```css
322
-
.wrapper {
323
-
width: calc(100%-2em);
324
-
margin-left: 1em;
325
-
margin-right: 1em;
326
-
}
327
-
```
328
281
329
282
330
283
# Why?
331
284
332
285
This plugin was spawned out of a [discussion on the `cssnext` repo](https://github.com/cssnext/cssnext/issues/99) and a personal need.
333
286
334
-
There is another similar plugin available, [`postcss-custom-properties`](https://github.com/postcss/postcss-custom-properties), although it restricts itself much more than this, preferring spec conformance over imperfect feature support.
287
+
There is another similar plugin available, [`postcss-custom-properties`](https://github.com/postcss/postcss-custom-properties), although it restricts itself much more than this plugin, preferring partial spec conformance. This plugin has the same capabilities but also adds imperfect feature support which stem from not being to know what the DOM will look like when you compile your CSS. We instead look at the explicit structure of your CSS selectors.
335
288
336
289
### Interoperability
337
290
338
-
If you are/were already using [`postcss-custom-properties`](https://github.com/postcss/postcss-custom-properties), this should work out of the box, without issue, and other than differences in the JavaScript API, it's a drop-in replacement. The only difference in CSS handling is that this attempts much broader support, and you can just start taking advantage immediately. Note the [caveats](#caveats), in that although this does correctly support what `postcss-custom-properties` does, there are certain edge cases it doesn't get perfectly.
291
+
Putting `postcss-css-variables` in place of `postcss-custom-properties` should work out of the box.
292
+
293
+
In `postcss-custom-properties`, CSS variable declarations are specifically restricted to the `:root` selector. In `postcss-css-variables`, this is not the case and they may be declared inside any rule with whatever selector. The variables are substituted based on statically known CSS selector inheritance.
339
294
340
295
### Differences from `postcss-custom-properties`
341
296
@@ -346,10 +301,10 @@ Here's a quick overview of the differences:
346
301
347
302
- CSS variables may be declared in any selector like `.foo` or `.foo .bar:hover`, and is not limited to just `:root`
348
303
- CSS variables may be declared in `@media`, `@support`, and other at-rules.
349
-
- CSS variables may be declared in `:hover` and other psuedo-classes, and they are evaluated properly.
304
+
- CSS variables may be declared in `:hover` and other psuedo-classes, which get expanded properly.
350
305
- Variables in nested rules can be deduced with the help of [`postcss-nested`](https://github.com/postcss/postcss-nested) or [`postcss-nesting`](https://github.com/jonathantneal/postcss-nesting).
351
306
352
-
Continue to the next section to see where some of these might be unsafe to do. There are reasons behind the ethos of why the other plugin, `postcss-custom-properties`, is very limited in what it supports, due to differing opinions on whether broader, yet potentially incorrect, support is okay.
307
+
Continue to the next section to see where some of these might be unsafe to do. There are reasons behind the ethos of why the other plugin, [`postcss-custom-properties`](https://github.com/postcss/postcss-custom-properties), is very limited in what it supports, due to differing opinions on what is okay to support.
353
308
354
309
355
310
# Caveats
@@ -372,8 +327,8 @@ Using the following CSS:
372
327
color: var(--text-color);
373
328
}
374
329
```
375
-
376
-
When nesting the markup like this, you may get incorrect behavior, because this only knows the CSS inheritance, not the HTML structure. (Note the innermost `<h1 class="Title">`.)
330
+
331
+
Note the nested markup below. We only know about the DOM's inheritance from your CSS selectors. If you want nest multiple times, you need to be explicit about it in your CSS which isn't necessary with browser that natively support CSS variables. See the innermost `<h1 class="title">`
377
332
378
333
```html
379
334
<divclass="component">
@@ -390,8 +345,7 @@ When nesting the markup like this, you may get incorrect behavior, because this
390
345
</h1>
391
346
</div>
392
347
```
393
-
394
-
This spec deviation is intentional, as there's nothing this tool can do about that. [`postcss-custom-properties`](https://github.com/postcss/postcss-custom-properties) avoids this problem entirely by restricting itself to just the `:root` selector. This is because the developers there would prefer to not support a feature instead of supporting it almost-correctly like what this plugin does.
348
+
[`postcss-custom-properties`](https://github.com/postcss/postcss-custom-properties) avoids this problem entirely by restricting itself to just the `:root` selector. This is because the developers there would prefer to not support a feature instead of something almost-spec-compliant like what `postcss-css-variables` does.
0 commit comments