# css-vars-ponyfill
[](https://www.npmjs.com/package/css-vars-ponyfill)
[](https://travis-ci.org/jhildenbiddle/css-vars-ponyfill)
[](https://www.codacy.com/app/jhildenbiddle/css-vars-ponyfill?utm_source=github.com&utm_medium=referral&utm_content=jhildenbiddle/css-vars-ponyfill&utm_campaign=Badge_Grade)
[](https://codecov.io/gh/jhildenbiddle/css-vars-ponyfill)
[](https://github.com/jhildenbiddle/css-vars-ponyfill/blob/master/LICENSE)
[](https://twitter.com/intent/tweet?url=https%3A%2F%2Fgithub.com%2Fjhildenbiddle%2Fcss-vars-ponyfill&hashtags=css,developers,frontend,javascript)
A [ponyfill](https://ponyfill.com/) that provides client-side support for [CSS custom properties](https://developer.mozilla.org/en-US/docs/Web/CSS/--*) (aka "CSS variables") in legacy and modern browsers.
- [Demo](https://codepen.io/jhildenbiddle/pen/ZxYJrR/) (CodePen)
------
- [Features](#features)
- [Installation](#installation)
- [Examples](#examples)
- [Options](#options)
- [Attribution](#attribution)
- [Contact](#contact)
- [License](#license)
------
## Features
- Client-side transformation of CSS custom properties to static values
- Live updates of runtime values in both modern and legacy browsers
- Auto-updates on `` and `
```
```css
/* style.css */
:root {
/* Chained references */
--a: var(--b);
--b: var(--c);
--c: 10px;
}
div {
/* External value (from
```
To update values, call `cssVars()` with [options.variables](#optionsvariables):
```javascript
cssVars({
variables: {
color: 'red',
unknown: '5px'
}
});
```
Values will be updated in both legacy and modern browsers:
- In legacy browsers, the ponyfill will get, parse, transform, and append
legacy-compatible CSS to the DOM once again.
```html
```
- In modern browsers with native support for CSS custom properties, the ponyfill will update values using the [style.setProperty()](https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration/setProperty) interface.
```javascript
document.documentElement.style.setProperty('--color', 'red');
document.documentElement.style.setProperty('--unknown', '5px');
```
## Options
- [rootElement](#optionsrootelement)
- [include](#optionsinclude)
- [exclude](#optionsexclude)
- [fixNestedCalc](#optionsfixnestedcalc)
- [onlyLegacy](#optionsonlylegacy)
- [onlyVars](#optionsonlyvars)
- [preserve](#optionspreserve)
- [shadowDOM](#optionsshadowdom)
- [silent](#optionssilent)
- [updateDOM](#optionsupdatedom)
- [updateURLs](#optionsupdateurls)
- [variables](#optionsvariables)
- [watch](#optionswatch)
- [onBeforeSend](#optionsonbeforesend)
- [onSuccess](#optionsonsuccess)
- [onWarning](#optionsonwarning)
- [onError](#optionsonerror)
- [onComplete](#optionsoncomplete)
**Example**
```javascript
// All options (default values shown)
cssVars({
rootElement : document,
include : 'link[rel=stylesheet],style',
exclude : '',
fixNestedCalc: true,
onlyLegacy : true,
onlyVars : false,
preserve : false,
shadowDOM : false,
silent : false,
updateDOM : true,
updateURLs : true,
variables : {
// ...
},
watch : false,
onBeforeSend(xhr, node, url) {
// ...
},
onSuccess(cssText, node, url) {
// ...
},
onWarning(message) {
// ...
},
onError(message, node, xhr, url) {
// ...
},
onComplete(cssText, styleNode, cssVariables) {
// ...
}
});
```
### options.rootElement
- Type: `object`
- Default: `document`
Root element containing `` and `
include: '[data-cssvarsponyfill="true"]'
});
```
### options.exclude
- Type: `string`
- Default: *none*
CSS selector matching `` and `
include: '[data-cssvarsponyfill="false"]'
});
```
### options.fixNestedCalc
- Type: `boolean`
- Default: `true`
Determines if nested `calc` keywords will be removed for compatibility with legacy browsers.
**Example**
CSS:
```css
:root {
--a: calc(1px + var(--b));
--b: calc(2px + var(--c));
--c: calc(3px + var(--d));
--d: 4px;
}
p {
margin: var(--a);
}
```
JavaScript:
```javascript
cssVars({
fixNestedCalc: true // default
});
```
Output when `fixNestedCalc: true`
```css
p {
/* Works in legacy browsers */
margin: calc(1px + (2px + (3px + 4)));
}
```
Output when `fixNestedCalc: false`
```css
p {
/* Does not work in legacy browsers */
margin: calc(1px + calc(2px + calc(3px + 4)));
}
```
### options.onlyLegacy
- Type: `boolean`
- Default: `true`
Determines if the ponyfill will ignore modern browsers with native CSS custom property support.
When `true`, the ponyfill will only transform custom properties, generate CSS, and trigger callbacks in legacy browsers that lack native support. When `false`, the ponyfill will treat all browsers as legacy, regardless of their support for CSS custom properties.
**Example**
```javascript
cssVars({
onlyLegacy: true // default
});
cssVars({
// Treat all browsers as legacy
onlyLegacy: false
});
cssVars({
// Treat Edge 15/16 as legacy
onlyLegacy: !(/Edge\/1[56]\./i.test(navigator.userAgent))
});
```
### options.onlyVars
- Type: `boolean`
- Default: `false`
Determines if CSS rulesets and declarations without a custom property value should be removed from the transformed CSS.
When `true`, rulesets and declarations without a custom property value will be removed from the generated CSS, reducing CSS output size. This can significantly reduce the amount of CSS processed and output by the ponyfill, but runs the risk of breaking the original cascade order once the transformed values are appended to the DOM. When `false`, all rulesets and declarations will be retained in the generated CSS. This means the ponyfill will process and output more CSS, but it ensures that the original cascade order is maintained after the transformed styles are appended to the DOM.
**Note:** `@font-face` and `@keyframes` require all declarations to be retained if a CSS custom property is used anywhere within the ruleset.
**Example**
CSS:
```css
:root {
--color: red;
}
h1 {
font-weight: bold;
}
p {
margin: 20px;
padding: 10px;
color: var(--color);
}
```
JavaScript:
```javascript
cssVars({
onlyVars: true // default
});
```
Output when `onlyVars: true`
```css
p {
color: red;
}
```
Output when `onlyVars: false`
```css
h1 {
font-weight: bold;
}
p {
margin: 20px;
padding: 10px;
color: red;
}
```
### options.preserve
- Type: `boolean`
- Default: `false`
Determines if the original CSS custom property declaration will be retained in the transformed CSS.
When `true`, the original custom property declarations are available in the transformed CSS along with their static values. When `false`, only static values are available in the transformed CSS.
**Example**
CSS:
```css
:root {
--color: red;
}
p {
color: var(--color);
}
```
JavaScript:
```javascript
cssVars({
preserve: false // default
});
```
Output when `preserve: false`
```css
p {
color: red;
}
```
Output when `preserve: true`
```css
:root {
--color: red;
}
p {
color: red;
color: var(--color);
}
```
### options.shadowDOM
- Type: `boolean`
- Default: `false`
Determines if shadow DOM tree(s) nested within the [options.rootElement](#optionsrootelement) will be processed.
**Example**
```javascript
// Do no process shadow DOM trees
cssVars({
shadowDOM: false // default
});
// Process all shadow DOM trees in document
cssVars({
shadowDOM: true
});
// Process all shadow DOM trees in custom element
cssVars({
rootElement: document.querySelector('my-element'),
shadowDOM : true
});
```
### options.silent
- Type: `boolean`
- Default: `false`
Determines if warning and error messages will be displayed on the console.
When `true`, messages will be displayed on the console for each warning and error encountered while processing CSS. When `false`, messages will not be displayed on the console but will still be available using the [options.onWarning](#optionsonwarning) and [options.onSuccess](#optionsonsuccess) callbacks.
**Example**
CSS:
```css
@import "fail.css"
p {
color: var(--fail);
}
p {
color: red;
```
JavaScript:
```javascript
cssVars({
silent: false // default
});
```
Console:
```bash
> CSS XHR error: "fail.css" 404 (Not Found)
> CSS transform warning: variable "--fail" is undefined
> CSS parse error: missing "}"
```
### options.updateDOM
- Type: `boolean`
- Default: `true`
Determines if the ponyfill will update the DOM after processing CSS custom properties.
When `true`, the ponyfill updates will be applied to the DOM. For legacy browsers, this is accomplished by appending a `
```
### options.updateURLs
- Type: `boolean`
- Default: `true`
Determines if the ponyfill will convert relative `url()` paths to absolute urls.
When `true`, the ponyfill will parse each block of external CSS for relative `url()` paths and convert them to absolute URLs. This allows resources (images, fonts, etc.) referenced using paths relative to an external stylesheet to load properly when legacy-compatible CSS is generated and appended to the DOM by the ponyfill. When `false`, the ponyfill will not modify relative `url()` paths.
**Example**
CSS:
```css
/* http://mydomain.com/path/to/style.css */
div {
background-image: url(image.jpg);
}
```
JavaScript:
```javascript
cssVars({
updateURLs: true // default
});
```
Output when `updateURLs: true`
```css
div {
background-image: url(http://mydomain.com/path/to/image.jpg);
}
```
Output when `updateURLs: false`
```css
div {
background-image: url(image.jpg);
}
```
### options.variables
- Type: `object`
- Default: `{}`
A map of custom property name/value pairs to apply to both legacy and modern browsers. Property names can include or omit the leading double-hyphen (`--`). Values specified will override previous values.
Legacy browsers will process these values while generating legacy-compatible CSS. Modern browsers with native support for CSS custom properties will add/update these values using the [setProperty()](https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration/setProperty) method when [options.updateDOM](#optionsupdatedom) is `true`.
**Note:** Although this option affects both legacy and modern browsers, ponyfill callbacks like (e.g. [onComplete](#oncomplete)) will only be triggered in legacy browsers (or in modern browsers when [onlyLegacy](#optionsonlylegacy) is `false`).
**Example**
```javascript
cssVars({
variables: {
'--color1': 'red', // Leading -- included
'color2' : 'green' // Leading -- omitted
}
});
```
### options.watch
- Type: `boolean`
- Default: `null`
Determines if a [MutationObserver](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) will be created to watch for `` and `