| 1 | # PostCSS Progressive Custom Properties [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS" width="90" height="90" align="right">][postcss]
|
|---|
| 2 |
|
|---|
| 3 | [![NPM Version][npm-img]][npm-url]
|
|---|
| 4 | [![Build Status][cli-img]][cli-url]
|
|---|
| 5 | [<img alt="Discord" src="https://shields.io/badge/Discord-5865F2?logo=discord&logoColor=white">][discord]
|
|---|
| 6 |
|
|---|
| 7 | [PostCSS Progressive Custom Properties] is a utility plugin to correctly declare Custom Property fallbacks and enhancements.
|
|---|
| 8 |
|
|---|
| 9 | ⚠️ It is not intended to be used directly by stylesheet authors.
|
|---|
| 10 | Meant to be included in other PostCSS plugins that provide CSS value transforms as fallbacks.
|
|---|
| 11 |
|
|---|
| 12 | [Custom Properties are not discarded like regular declarations when invalid.](https://www.w3.org/TR/css-variables-1/#invalid-variables)
|
|---|
| 13 | This makes it tricky to provide fallback values for older browsers.
|
|---|
| 14 |
|
|---|
| 15 | The solution is to wrap Custom Property declarations in an `@supports` rule.
|
|---|
| 16 |
|
|---|
| 17 | ```pcss
|
|---|
| 18 | :root {
|
|---|
| 19 | /* fallback */
|
|---|
| 20 | --a-color: red;
|
|---|
| 21 | /* progressive enhancement */
|
|---|
| 22 | --a-color: oklch(40% 0.234 0.39 / var(--opacity-50));
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | /* becomes */
|
|---|
| 26 |
|
|---|
| 27 | :root {
|
|---|
| 28 | --a-color: red;
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | @supports (color: oklch(0% 0 0)) {
|
|---|
| 32 | :root {
|
|---|
| 33 | --a-color: oklch(40% 0.234 0.39 / var(--opacity-50));
|
|---|
| 34 | }
|
|---|
| 35 | }
|
|---|
| 36 | ```
|
|---|
| 37 |
|
|---|
| 38 | ## Ignored values
|
|---|
| 39 |
|
|---|
| 40 | `initial` and `<white space>` are ignored.
|
|---|
| 41 |
|
|---|
| 42 | ```pcss
|
|---|
| 43 | .initial {
|
|---|
| 44 | --prop-1: red;
|
|---|
| 45 | --prop-1: initial;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | .white-space {
|
|---|
| 49 | --prop-1: red;
|
|---|
| 50 | --prop-1:;
|
|---|
| 51 |
|
|---|
| 52 | --prop-2: red;
|
|---|
| 53 | --prop-2: ;
|
|---|
| 54 |
|
|---|
| 55 | --prop-3: red;
|
|---|
| 56 | --prop-3: ;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | /* remains */
|
|---|
| 60 |
|
|---|
| 61 | .initial {
|
|---|
| 62 | --prop-1: red;
|
|---|
| 63 | --prop-1: initial;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | .white-space {
|
|---|
| 67 | --prop-1: red;
|
|---|
| 68 | --prop-1:;
|
|---|
| 69 |
|
|---|
| 70 | --prop-2: red;
|
|---|
| 71 | --prop-2: ;
|
|---|
| 72 |
|
|---|
| 73 | --prop-3: red;
|
|---|
| 74 | --prop-3: ;
|
|---|
| 75 | }
|
|---|
| 76 | ```
|
|---|
| 77 |
|
|---|
| 78 | ## Usage
|
|---|
| 79 |
|
|---|
| 80 | Add [PostCSS Progressive Custom Properties] to your project:
|
|---|
| 81 |
|
|---|
| 82 | ```bash
|
|---|
| 83 | npm install @csstools/postcss-progressive-custom-properties --save-dev
|
|---|
| 84 | ```
|
|---|
| 85 |
|
|---|
| 86 | Use [PostCSS Progressive Custom Properties] as a [PostCSS] plugin:
|
|---|
| 87 |
|
|---|
| 88 | ```js
|
|---|
| 89 | const postcss = require('postcss');
|
|---|
| 90 | const postcssCustomProperties = require('@csstools/postcss-progressive-custom-properties');
|
|---|
| 91 |
|
|---|
| 92 | postcss([
|
|---|
| 93 | postcssProgressiveCustomProperties()
|
|---|
| 94 | ]).process(YOUR_CSS /*, processOptions */);
|
|---|
| 95 | ```
|
|---|
| 96 |
|
|---|
| 97 | ## @supports
|
|---|
| 98 |
|
|---|
| 99 | This plugin wraps Custom Property override declarations in an `@supports` rule.
|
|---|
| 100 | With PostCSS 8 this trigger declaration visitors to run again.
|
|---|
| 101 |
|
|---|
| 102 | Make sure your plugin detects and ignores values inside relevant `@supports` rules.
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 | [PostCSS Progressive Custom Properties] runs in all Node environments, with special instructions for:
|
|---|
| 106 |
|
|---|
| 107 | | [Node](INSTALL.md#node) | [PostCSS CLI](INSTALL.md#postcss-cli) | [Webpack](INSTALL.md#webpack) | [Create React App](INSTALL.md#create-react-app) | [Gulp](INSTALL.md#gulp) | [Grunt](INSTALL.md#grunt) |
|
|---|
| 108 | | --- | --- | --- | --- | --- | --- |
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 | [cli-img]: https://github.com/csstools/postcss-plugins/actions/workflows/test.yml/badge.svg
|
|---|
| 112 | [cli-url]: https://github.com/csstools/postcss-plugins/actions/workflows/test.yml?query=workflow/test
|
|---|
| 113 | [discord]: https://discord.gg/bUadyRwkJS
|
|---|
| 114 | [npm-img]: https://img.shields.io/npm/v/@csstools/postcss-progressive-custom-properties.svg
|
|---|
| 115 | [npm-url]: https://www.npmjs.com/package/@csstools/postcss-progressive-custom-properties
|
|---|
| 116 |
|
|---|
| 117 | [PostCSS]: https://github.com/postcss/postcss
|
|---|
| 118 | [PostCSS Progressive Custom Properties]: https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-progressive-custom-properties
|
|---|