# PostCSS Custom Properties

[![NPM Version](https://img.shields.io/npm/v/postcss-custom-properties.svg)](https://www.npmjs.com/package/postcss-custom-properties) [![CSS Standard Status](https://cssdb.org/badge/custom-properties.svg)](https://cssdb.org/#custom-properties) [![Build Status](https://img.shields.io/travis/postcss/postcss-custom-properties/master.svg)](https://travis-ci.org/postcss/postcss-custom-properties) [![Support Chat](https://img.shields.io/badge/support-chat-blue.svg)](https://gitter.im/postcss/postcss)

[PostCSS Custom Properties](https://github.com/postcss/postcss-custom-properties) lets you use Custom Properties in CSS, following the [CSS Custom Properties](https://www.w3.org/TR/css-variables-1/) specification.

```
:root {
  --color: red;
}

h1 {
  color: var(--color);
}

/* becomes */

:root {
  --color: red;
}

h1 {
  color: red;
  color: var(--color);
}
```

## Usage

Add [PostCSS Custom Properties](https://github.com/postcss/postcss-custom-properties) to your project:

```bash
npm install postcss-custom-properties --save-dev
```

Use [PostCSS Custom Properties](https://github.com/postcss/postcss-custom-properties) to process your CSS:

```js
const postcssCustomProperties = require('postcss-custom-properties');

postcssCustomProperties.process(YOUR_CSS /*, processOptions, pluginOptions */);
```

Or use it as a [PostCSS](https://github.com/postcss/postcss) plugin:

```js
const postcss = require('postcss');
const postcssCustomProperties = require('postcss-custom-properties');

postcss([
  postcssCustomProperties(/* pluginOptions */)
]).process(YOUR_CSS /*, processOptions */);
```

[PostCSS Custom Properties](https://github.com/postcss/postcss-custom-properties) runs in all Node environments, with special instructions for:

| [Node](https://github.com/bgoonz/Learning-Redux/blob/master/repos/examples/real-world/node_modules/postcss-custom-properties/INSTALL.md#node) | [PostCSS CLI](https://github.com/bgoonz/Learning-Redux/blob/master/repos/examples/real-world/node_modules/postcss-custom-properties/INSTALL.md#postcss-cli) | [Webpack](https://github.com/bgoonz/Learning-Redux/blob/master/repos/examples/real-world/node_modules/postcss-custom-properties/INSTALL.md#webpack) | [Create React App](https://github.com/bgoonz/Learning-Redux/blob/master/repos/examples/real-world/node_modules/postcss-custom-properties/INSTALL.md#create-react-app) | [Gulp](https://github.com/bgoonz/Learning-Redux/blob/master/repos/examples/real-world/node_modules/postcss-custom-properties/INSTALL.md#gulp) | [Grunt](https://github.com/bgoonz/Learning-Redux/blob/master/repos/examples/real-world/node_modules/postcss-custom-properties/INSTALL.md#grunt) |
| --------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |

## Options

### preserve

The `preserve` option determines whether Custom Properties and properties using custom properties should be preserved in their original form. By default, both of these are preserved.

```js
postcssCustomProperties({
  preserve: false
});
```

```
:root {
  --color: red;
}

h1 {
  color: var(--color);
}

/* becomes */

h1 {
  color: red;
}
```

### importFrom

The `importFrom` option specifies sources where Custom Properties can be imported from, which might be CSS, JS, and JSON files, functions, and directly passed objects.

```js
postcssCustomProperties({
  importFrom: 'path/to/file.css' // => :root { --color: red }
});
```

```
h1 {
  color: var(--color);
}

/* becomes */

h1 {
  color: red;
}
```

Multiple sources can be passed into this option, and they will be parsed in the order they are received. JavaScript files, JSON files, functions, and objects will need to namespace Custom Properties using the `customProperties` or `custom-properties` key.

```js
postcssCustomProperties({
  importFrom: [
    'path/to/file.css',   // :root { --color: red; }
    'and/then/this.js',   // module.exports = { customProperties: { '--color': 'red' } }
    'and/then/that.json', // { "custom-properties": { "--color": "red" } }
    {
      customProperties: { '--color': 'red' }
    },
    () => {
      const customProperties = { '--color': 'red' };

      return { customProperties };
    }
  ]
});
```

See example imports written in [CSS](https://github.com/bgoonz/Learning-Redux/blob/master/repos/examples/real-world/node_modules/postcss-custom-properties/test/import-properties.css), [JS](https://github.com/bgoonz/Learning-Redux/blob/master/repos/examples/real-world/node_modules/postcss-custom-properties/test/import-properties.js), and [JSON](https://github.com/bgoonz/Learning-Redux/blob/master/repos/examples/real-world/node_modules/postcss-custom-properties/test/import-properties.json).

### exportTo

The `exportTo` option specifies destinations where Custom Properties can be exported to, which might be CSS, JS, and JSON files, functions, and directly passed objects.

```js
postcssCustomProperties({
  exportTo: 'path/to/file.css' // :root { --color: red; }
});
```

Multiple destinations can be passed into this option, and they will be parsed in the order they are received. JavaScript files, JSON files, and objects will need to namespace Custom Properties using the `customProperties` or `custom-properties` key.

```js
const cachedObject = { customProperties: {} };

postcssCustomProperties({
  exportTo: [
    'path/to/file.css',   // :root { --color: red; }
    'and/then/this.js',   // module.exports = { customProperties: { '--color': 'red' } }
    'and/then/this.mjs',  // export const customProperties = { '--color': 'red' } }
    'and/then/that.json', // { "custom-properties": { "--color": "red" } }
    cachedObject,
    customProperties => {
      customProperties    // { '--color': 'red' }
    }
  ]
});
```

See example exports written to [CSS](https://github.com/bgoonz/Learning-Redux/blob/master/repos/examples/real-world/node_modules/postcss-custom-properties/test/export-properties.css), [JS](https://github.com/bgoonz/Learning-Redux/blob/master/repos/examples/real-world/node_modules/postcss-custom-properties/test/export-properties.js), [MJS](https://github.com/bgoonz/Learning-Redux/blob/master/repos/examples/real-world/node_modules/postcss-custom-properties/test/export-properties.mjs), and [JSON](https://github.com/bgoonz/Learning-Redux/blob/master/repos/examples/real-world/node_modules/postcss-custom-properties/test/export-properties.json).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bryan-guner.gitbook.io/my-docs/redux/repos/examples/real-world/node_modules/postcss-custom-properties.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
