Socket
Socket
Sign inDemoInstall

@arshaw/postcss-custom-properties

Package Overview
Dependencies
2
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @arshaw/postcss-custom-properties

Use Custom Properties Queries in CSS


Version published
Weekly downloads
17
increased by112.5%
Maintainers
1
Created
Weekly downloads
 

Changelog

Source

9.1.1 (February 20, 2020)

  • Fixed: Preserve spaces in multi-part values (#203)

Readme

Source

Fork Info: Implements #169 by adding a preserveWithFallback flag.

PostCSS Custom Properties PostCSS

NPM Version CSS Standard Status Build Status Support Chat

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

'Can I use' table

:root {
  --color: red;
}

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

/* becomes */

:root {
  --color: red;
}

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

Usage

Add PostCSS Custom Properties to your project:

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

Use PostCSS Custom Properties to process your CSS:

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

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

Or use it as a PostCSS plugin:

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

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

PostCSS Custom Properties runs in all Node environments, with special instructions for:

NodePostCSS CLIWebpackCreate React AppGulpGrunt

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.

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.

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.

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, JS, and 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.

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.

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, JS, MJS, and JSON.

Keywords

FAQs

Last updated on 07 Jun 2020

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