postcss/postcss-custom-properties

Name: postcss-custom-properties

Owner: PostCSS

Description: PostCSS plugin to transform W3C CSS Custom Properties for cascading variables

Created: 2014-07-29 05:48:21.0

Updated: 2018-01-17 04:27:39.0

Pushed: 2017-10-26 06:27:25.0

Homepage:

Size: 100

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

postcss-custom-properties CSS Standard Status Build Status

PostCSS plugin to transform W3C CSS Custom Properties for ~~cascading~~ variables syntax to more compatible CSS.

Per w3c specifications, the usage of var() is limited to property values. Do not expect the plugin to transform var() in media queries or in selectors.

N.B. The transformation is not complete and cannot be (dynamic cascading variables based on custom properties relies on the DOM tree). It currently just aims to provide a future-proof way of using a limited subset (to :root selector) of the features provided by native CSS custom properties. Since we do not know the DOM in the context of this plugin, we cannot produce safe output. Read #1 & #9 to know why this limitation exists.

If you are looking for a full support of CSS custom properties, please follow the opened issue for runtime support.

N.B.² If you are wondering why there is a different plugin (postcss-css-variables) that claims to do more than this plugin, be sure to understand the explanation above about limitation. This plugins have a behavior that is not reflecting the specifications.

This plugin works great with postcss-calc.

Installation
m install postcss-custom-properties
Usage
ependencies
fs = require("fs")
postcss = require("postcss")
customProperties = require("postcss-custom-properties")

ss to be processed
css = fs.readFileSync("input.css", "utf8")

rocess css using postcss-custom-properties
output = postcss()
se(customProperties())
rocess(css)
ss

Using this input.css:

t {
color: red;


{
lor: var(--color);

you will get:

{
lor: red;

You can also compile CSS custom properties with their fallback value.

Using this input.css:

{
lor: var(--color, #f00);

you will get:

{
lor: #f00;

Note that plugin returns itself in order to expose a setVariables function that allow you to programmatically change the variables.

variables = {
-a": "b",

plugin = customProperties()
in.setVariables(variables)
result = postcss()
se(plugin)
rocess(input)

This might help for dynamic live/hot reloading.

Checkout tests for more.

Options
strict

Default: true

Per specifications, all fallbacks should be added since we can't verify if a computed value is valid or not. This option allows you to avoid adding too many fallback values in your CSS.

preserve

Default: false

Allows you to preserve custom properties & var() usage in output.

out = postcss()
se(customProperties({preserve: true}))
rocess(css)
ss

You can also set preserve: "computed" to get computed resolved custom properties in the final output. Handy to make them available to your JavaScript.

variables

Default: {}

Allows you to pass an object of variables for :root. These definitions will override any that exist in the CSS. The keys are automatically prefixed with the CSS -- to make it easier to share variables in your codebase.

appendVariables

Default: false

If preserve is set to true (or "computed"), allows you to append your variables at the end of your CSS.

warnings

Default: true Type: Boolean|Object

Allows you to enable/disable warnings. If true, will enable all warnings. For now, it only allow to disable messages about custom properties definition not scoped in a :root selector.

noValueNotifications

Default: 'warning' Values: 'warning'|'error'

If it is set to 'error', using of undefined variable will throw an error.


Contributing

Fork, work on a branch, install dependencies & run tests before submitting a PR.

t clone https://github.com/YOU/postcss-custom-properties.git
t checkout -b patch-1
m install
m test
Changelog
License

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.