[6a3a178] | 1 | # PostCSS 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 | [![CSS Standard Status][css-img]][css-url]
|
---|
| 5 | [![Build Status][cli-img]][cli-url]
|
---|
| 6 | [![Support Chat][git-img]][git-url]
|
---|
| 7 |
|
---|
| 8 | [PostCSS Custom Properties] lets you use Custom Properties in CSS, following
|
---|
| 9 | the [CSS Custom Properties] specification.
|
---|
| 10 |
|
---|
| 11 | ```pcss
|
---|
| 12 | :root {
|
---|
| 13 | --color: red;
|
---|
| 14 | }
|
---|
| 15 |
|
---|
| 16 | h1 {
|
---|
| 17 | color: var(--color);
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | /* becomes */
|
---|
| 21 |
|
---|
| 22 | :root {
|
---|
| 23 | --color: red;
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | h1 {
|
---|
| 27 | color: red;
|
---|
| 28 | color: var(--color);
|
---|
| 29 | }
|
---|
| 30 | ```
|
---|
| 31 |
|
---|
| 32 | ## Usage
|
---|
| 33 |
|
---|
| 34 | Add [PostCSS Custom Properties] to your project:
|
---|
| 35 |
|
---|
| 36 | ```bash
|
---|
| 37 | npm install postcss-custom-properties --save-dev
|
---|
| 38 | ```
|
---|
| 39 |
|
---|
| 40 | Use [PostCSS Custom Properties] to process your CSS:
|
---|
| 41 |
|
---|
| 42 | ```js
|
---|
| 43 | const postcssCustomProperties = require('postcss-custom-properties');
|
---|
| 44 |
|
---|
| 45 | postcssCustomProperties.process(YOUR_CSS /*, processOptions, pluginOptions */);
|
---|
| 46 | ```
|
---|
| 47 |
|
---|
| 48 | Or use it as a [PostCSS] plugin:
|
---|
| 49 |
|
---|
| 50 | ```js
|
---|
| 51 | const postcss = require('postcss');
|
---|
| 52 | const postcssCustomProperties = require('postcss-custom-properties');
|
---|
| 53 |
|
---|
| 54 | postcss([
|
---|
| 55 | postcssCustomProperties(/* pluginOptions */)
|
---|
| 56 | ]).process(YOUR_CSS /*, processOptions */);
|
---|
| 57 | ```
|
---|
| 58 |
|
---|
| 59 | [PostCSS Custom Properties] runs in all Node environments, with special instructions for:
|
---|
| 60 |
|
---|
| 61 | | [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) |
|
---|
| 62 | | --- | --- | --- | --- | --- | --- |
|
---|
| 63 |
|
---|
| 64 | ## Options
|
---|
| 65 |
|
---|
| 66 | ### preserve
|
---|
| 67 |
|
---|
| 68 | The `preserve` option determines whether Custom Properties and properties using
|
---|
| 69 | custom properties should be preserved in their original form. By default, both
|
---|
| 70 | of these are preserved.
|
---|
| 71 |
|
---|
| 72 | ```js
|
---|
| 73 | postcssCustomProperties({
|
---|
| 74 | preserve: false
|
---|
| 75 | });
|
---|
| 76 | ```
|
---|
| 77 |
|
---|
| 78 | ```pcss
|
---|
| 79 | :root {
|
---|
| 80 | --color: red;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | h1 {
|
---|
| 84 | color: var(--color);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | /* becomes */
|
---|
| 88 |
|
---|
| 89 | h1 {
|
---|
| 90 | color: red;
|
---|
| 91 | }
|
---|
| 92 | ```
|
---|
| 93 |
|
---|
| 94 | ### importFrom
|
---|
| 95 |
|
---|
| 96 | The `importFrom` option specifies sources where Custom Properties can be imported
|
---|
| 97 | from, which might be CSS, JS, and JSON files, functions, and directly passed
|
---|
| 98 | objects.
|
---|
| 99 |
|
---|
| 100 | ```js
|
---|
| 101 | postcssCustomProperties({
|
---|
| 102 | importFrom: 'path/to/file.css' // => :root { --color: red }
|
---|
| 103 | });
|
---|
| 104 | ```
|
---|
| 105 |
|
---|
| 106 | ```pcss
|
---|
| 107 | h1 {
|
---|
| 108 | color: var(--color);
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | /* becomes */
|
---|
| 112 |
|
---|
| 113 | h1 {
|
---|
| 114 | color: red;
|
---|
| 115 | }
|
---|
| 116 | ```
|
---|
| 117 |
|
---|
| 118 | Multiple sources can be passed into this option, and they will be parsed in the
|
---|
| 119 | order they are received. JavaScript files, JSON files, functions, and objects
|
---|
| 120 | will need to namespace Custom Properties using the `customProperties` or
|
---|
| 121 | `custom-properties` key.
|
---|
| 122 |
|
---|
| 123 | ```js
|
---|
| 124 | postcssCustomProperties({
|
---|
| 125 | importFrom: [
|
---|
| 126 | 'path/to/file.css', // :root { --color: red; }
|
---|
| 127 | 'and/then/this.js', // module.exports = { customProperties: { '--color': 'red' } }
|
---|
| 128 | 'and/then/that.json', // { "custom-properties": { "--color": "red" } }
|
---|
| 129 | {
|
---|
| 130 | customProperties: { '--color': 'red' }
|
---|
| 131 | },
|
---|
| 132 | () => {
|
---|
| 133 | const customProperties = { '--color': 'red' };
|
---|
| 134 |
|
---|
| 135 | return { customProperties };
|
---|
| 136 | }
|
---|
| 137 | ]
|
---|
| 138 | });
|
---|
| 139 | ```
|
---|
| 140 |
|
---|
| 141 | See example imports written in [CSS](test/import-properties.css),
|
---|
| 142 | [JS](test/import-properties.js), and [JSON](test/import-properties.json).
|
---|
| 143 |
|
---|
| 144 | ### exportTo
|
---|
| 145 |
|
---|
| 146 | The `exportTo` option specifies destinations where Custom Properties can be exported
|
---|
| 147 | to, which might be CSS, JS, and JSON files, functions, and directly passed
|
---|
| 148 | objects.
|
---|
| 149 |
|
---|
| 150 | ```js
|
---|
| 151 | postcssCustomProperties({
|
---|
| 152 | exportTo: 'path/to/file.css' // :root { --color: red; }
|
---|
| 153 | });
|
---|
| 154 | ```
|
---|
| 155 |
|
---|
| 156 | Multiple destinations can be passed into this option, and they will be parsed
|
---|
| 157 | in the order they are received. JavaScript files, JSON files, and objects will
|
---|
| 158 | need to namespace Custom Properties using the `customProperties` or
|
---|
| 159 | `custom-properties` key.
|
---|
| 160 |
|
---|
| 161 | ```js
|
---|
| 162 | const cachedObject = { customProperties: {} };
|
---|
| 163 |
|
---|
| 164 | postcssCustomProperties({
|
---|
| 165 | exportTo: [
|
---|
| 166 | 'path/to/file.css', // :root { --color: red; }
|
---|
| 167 | 'and/then/this.js', // module.exports = { customProperties: { '--color': 'red' } }
|
---|
| 168 | 'and/then/this.mjs', // export const customProperties = { '--color': 'red' } }
|
---|
| 169 | 'and/then/that.json', // { "custom-properties": { "--color": "red" } }
|
---|
| 170 | cachedObject,
|
---|
| 171 | customProperties => {
|
---|
| 172 | customProperties // { '--color': 'red' }
|
---|
| 173 | }
|
---|
| 174 | ]
|
---|
| 175 | });
|
---|
| 176 | ```
|
---|
| 177 |
|
---|
| 178 | See example exports written to [CSS](test/export-properties.css),
|
---|
| 179 | [JS](test/export-properties.js), [MJS](test/export-properties.mjs), and
|
---|
| 180 | [JSON](test/export-properties.json).
|
---|
| 181 |
|
---|
| 182 | [cli-img]: https://img.shields.io/travis/postcss/postcss-custom-properties/master.svg
|
---|
| 183 | [cli-url]: https://travis-ci.org/postcss/postcss-custom-properties
|
---|
| 184 | [css-img]: https://cssdb.org/badge/custom-properties.svg
|
---|
| 185 | [css-url]: https://cssdb.org/#custom-properties
|
---|
| 186 | [git-img]: https://img.shields.io/badge/support-chat-blue.svg
|
---|
| 187 | [git-url]: https://gitter.im/postcss/postcss
|
---|
| 188 | [npm-img]: https://img.shields.io/npm/v/postcss-custom-properties.svg
|
---|
| 189 | [npm-url]: https://www.npmjs.com/package/postcss-custom-properties
|
---|
| 190 |
|
---|
| 191 | [CSS Custom Properties]: https://www.w3.org/TR/css-variables-1/
|
---|
| 192 | [PostCSS]: https://github.com/postcss/postcss
|
---|
| 193 | [PostCSS Custom Properties]: https://github.com/postcss/postcss-custom-properties
|
---|