rework-vars
A Rework plugin to add support for the W3C-style CSS variables syntax.
N.B. This is not a polyfill. This plugin aims to provide a future-proof way of using a limited subset of the features provided by native CSS variables.
Installation
npm install rework-vars
Use
As a Rework plugin:
// dependenciesvar fs = ;var rework = ;var vars = ; // css to be processedvar css = fs; // process css using rework-varsvar options = {};var out = ;
Options
map
Optionally, you may pass an object of variables - map
- to the JavaScript
function.
var map = 'app-bg-color': 'white' var out = ;
preserve
(default: false
)
Setting preserve
to true
will preserve the variable definitions and
references in the output, so that they can be used by supporting browsers.
var out = ;
Supported features
Variables can be declared as custom CSS properties on the :root
element,
prefixed with --
:
Variables are applied using the var()
function, taking the name of a variable
as the first argument:
Fallback values are supported and are applied if a variable has not been declared:
Fallbacks can be "complex". Anything after the first comma in the var()
function will act as the fallback value – var(name, fallback)
. Nested
variables are also supported:
What to expect
Variables can only be declared for, and scoped to the :root
element. All
other variable declarations are left untouched. Any known variables used as
values are replaced.
yields:
Variables are not dynamic; they are replaced with normal CSS values. The value
of a defined variable is determined by the last declaration of that variable
for :root
.
yields:
Variables declared within @media
or @supports
are not currently supported
and will also be ignored.
{}
yields:
{}
License
MIT