Skip to content

Commit 5b2363f

Browse files
committed
Update README.md
1 parent 3da59e5 commit 5b2363f

File tree

1 file changed

+14
-60
lines changed

1 file changed

+14
-60
lines changed

README.md

Lines changed: 14 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ npm install postcss-css-variables --save-dev
3232
- [Changelog](https://github.com/MadLittleMods/postcss-css-variables/blob/master/CHANGELOG.md)
3333

3434

35-
# Code Playground
35+
# [Code Playground](https://madlittlemods.github.io/postcss-css-variables/playground/)
3636

3737
[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.
3838

@@ -185,21 +185,21 @@ Will be transformed to:
185185

186186
### Nested Rules
187187

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:
189189

190190

191191
```js
192192
var postcss = require('postcss');
193193
var cssvariables = require('postcss-css-variables');
194-
var nestedcss = require('postcss-nested');
194+
var nested = require('postcss-nested');
195195

196196
var fs = require('fs');
197197

198198
var mycss = fs.readFileSync('input.css', 'utf8');
199199

200200
var output = postcss([
201201
// Flatten/unnest rules
202-
nestedcss,
202+
nested,
203203
// Then process any CSS variables
204204
cssvariables(/*options*/)
205205
])
@@ -256,7 +256,7 @@ For a more complex example with a media query:
256256
}
257257
```
258258

259-
This will be transformed to:
259+
Will be transformed to:
260260

261261
```css
262262
.box-foo {
@@ -278,64 +278,19 @@ This will be transformed to:
278278
}
279279
```
280280

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-
```
328281

329282

330283
# Why?
331284

332285
This plugin was spawned out of a [discussion on the `cssnext` repo](https://github.com/cssnext/cssnext/issues/99) and a personal need.
333286

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.
335288

336289
### Interoperability
337290

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.
339294

340295
### Differences from `postcss-custom-properties`
341296

@@ -346,10 +301,10 @@ Here's a quick overview of the differences:
346301

347302
- CSS variables may be declared in any selector like `.foo` or `.foo .bar:hover`, and is not limited to just `:root`
348303
- 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.
350305
- 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).
351306

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.
353308

354309

355310
# Caveats
@@ -372,8 +327,8 @@ Using the following CSS:
372327
color: var(--text-color);
373328
}
374329
```
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">`
377332

378333
```html
379334
<div class="component">
@@ -390,8 +345,7 @@ When nesting the markup like this, you may get incorrect behavior, because this
390345
</h1>
391346
</div>
392347
```
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.
395349

396350

397351
# Options

0 commit comments

Comments
 (0)