Socket
Socket
Sign inDemoInstall

postcss-custom-properties

Package Overview
Dependencies
10
Maintainers
3
Versions
85
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    postcss-custom-properties

PostCSS plugin to polyfill W3C CSS Custom Properties for cascading variables


Version published
Weekly downloads
5.8M
decreased by-4.79%
Maintainers
3
Install size
1.52 MB
Created
Weekly downloads
 

Package description

What is postcss-custom-properties?

The postcss-custom-properties npm package is a PostCSS plugin that allows you to use CSS Custom Properties (also known as CSS variables) in environments that do not support them natively. It transforms CSS variables into static values based on your configurations, making it easier to maintain themes and styles dynamically across your project.

What are postcss-custom-properties's main functionalities?

Transform CSS Custom Properties

This feature allows the transformation of CSS custom properties into their corresponding static values. It is useful for supporting older browsers that do not understand CSS variables.

/* Input CSS */
:root {
  --main-color: red;
}

a {
  color: var(--main-color);
}

/* Output CSS */
a {
  color: red;
}

Preserve option

With the preserve option set to true, the plugin outputs both the transformed static value and the original variable. This is useful for progressive enhancement.

/* Input CSS */
:root {
  --main-color: red;
}

a {
  color: var(--main-color);
}

/* Output CSS with preserve: true */
a {
  color: red;
  color: var(--main-color);
}

Other packages similar to postcss-custom-properties

Readme

Source

PostCSS Custom Properties PostCSS Logo

NPM Version CSS Standard Status Build Status Gitter Chat

PostCSS Custom Properties lets you use CSS Custom Properties in CSS, following the CSS Custom Properties for Cascading Variables specification.

:root {
  --color: red;
}

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

/* becomes */

:root {
  --color: red;
}

div {
  color: red;
  color: var(--color);
}

Usage

Add PostCSS Custom Properties to your build tool:

npm install postcss-custom-properties --save-dev
Node

Use PostCSS Custom Properties to process your CSS:

import postCSSCustomProperties from 'postcss-custom-properties';

postCSSCustomProperties.process(YOUR_CSS);
PostCSS

Add PostCSS to your build tool:

npm install postcss --save-dev

Use PostCSS Custom Properties as a plugin:

import postcss from 'gulp-postcss';
import postCSSCustomProperties from 'postcss-custom-properties';

postcss([
  postCSSCustomProperties()
]).process(YOUR_CSS);
Gulp

Add Gulp PostCSS to your build tool:

npm install gulp-postcss --save-dev

Use PostCSS Custom Properties in your Gulpfile:

import postcss from 'gulp-postcss';
import postCSSCustomProperties from 'postcss-custom-properties';

gulp.task('css', () => gulp.src('./src/*.css').pipe(
  postcss([
    postCSSCustomProperties()
  ])
).pipe(
  gulp.dest('.')
));
Grunt

Add Grunt PostCSS to your build tool:

npm install grunt-postcss --save-dev

Use PostCSS Custom Properties in your Gruntfile:

import postCSSCustomProperties from 'postcss-custom-properties';

grunt.loadNpmTasks('grunt-postcss');

grunt.initConfig({
  postcss: {
    options: {
      use: [
       postCSSCustomProperties()
      ]
    },
    dist: {
      src: '*.css'
    }
  }
});

Options

strict

The strict option determines whether a var() function should transform into its specified fallback value. By default, the option is true because this plugin can not verify if the computed :root value is valid or not.

:root {
  --color: red;
}

div {
  color: var(--color, blue);
}

/* becomes */

:root {
  --color: red;
}

div {
  color: blue;
  color: var(--color, blue);
}

preserve

The preserve option determines how Custom Properties should be preserved. By default, this option is truthy and preserves declarations containing var().

:root {
  --color: red;
}

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

/* becomes */

:root {
  --color: red;
}

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

The option may also be set to false, where Custom Properties and declarations containing var() will be removed:

postCSSCustomProperties({
  variables: {
    preserve: false
  }
})
:root {
  --color: red;
}

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

/* becomes */

h1 {
  color: red;
}

The option may also be set to "preserve-computed", where Custom Properties will remain, but declarations containing var() will be removed:

postCSSCustomProperties({
  variables: {
    preserve: 'preserve-computed'
  }
})
:root {
  --color: red;
}

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

/* becomes */

:root {
  --color: red;
}

h1 {
  color: red;
}

variables

The variables option allows you to pass an object of variables into CSS, as if they had been specified on :root.

postCSSCustomProperties({
  variables: {
    color: 'red'
  }
})
h1 {
  color: var(--color);
}

/* becomes */

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

Note that these definitions will override any that exist in the CSS, and that the keys will be automatically prefixed (--) to make it easier to share variables in your codebase.

appendVariables

The appendVariables option determines whether Custom Properties will be appended to your CSS file. By default, this option is false.

If enabled when preserve is set to true or "computed", this option allows you to append your variables at the end of your CSS:

postCSSCustomProperties({
  appendVariables: true,
  variables: {
    color: 'red'
  }
})
h1 {
  color: var(--color);
}

/* becomes */

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

:root {
  --color: red;
}

warnings

The warnings option determines whether Custom Property related warnings should be logged by the plugin or not. By default, warnings are set to false and are not logged.

If enabled, the plugin will enable all warnings:

postCSSCustomProperties({
  warnings: true
})
h1 {
  color: var(--color);
}
variable '--color' is undefined and used without a fallback

noValueNotifications

When warnings are enabled, the noValueNotifications option determines whether undefined variables will throw a warning or an error. By default, it is set to warning.


Notes

As written in the specification, usage of var() is limited to property values. Do not expect the plugin to transform var() in media queries or in selectors.

The transformation of Custom Properties done by this plugin is not complete and cannot be because dynamic cascading variables based on custom properties relies on the DOM tree. Since we do not know the DOM in the context of this plugin, we cannot produce safe output. This plugin currently aims to provide a future-proof way of using a limited subset of the features provided by native CSS custom properties.

There is a separate plugin, PostCSS CSS Variables, that attempts to guess the context of Custom Properties without access to the DOM tree. This does not reflecting the specifications, so be sure you understand the risks before you decide to use it.

Contributing

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

$ git clone https://github.com/YOU/postcss-custom-properties.git
$ git checkout -b patch-1
$ npm install
$ npm test

Changelog

License

Keywords

FAQs

Last updated on 16 Feb 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc