npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

postcss-export-custom-variables

v1.0.0

Published

Export custom media queries, custom properties, custom property sets, and custom selectors from CSS as JavaScript variables

Downloads

1,447

Readme

PostCSS Export Custom Variables

NPM Version Build Status Windows Build Status Gitter Chat

PostCSS Export Custom Variables lets you export custom media queries, custom properties, custom property sets, and custom selectors from CSS to JavaScript.

:root {
  --custom-size: 960px;
  --custom-styles: {
    color: black;
    background-color: white;
  }
}

@custom-media --custom-viewport (max-width: 30em);

@custom-selector :--custom-selector :hover, :focus;

By default, CSS is transformed into JSON:

{
  "customSize": "960px",
  "customStyles": {
    "color": "black",
    "backgroundColor": "white"
  },
  "customViewport": "(max-width: 30em)",
  "customSelector": ":hover, :focus"
}

With a small adjustment, it may be transformed into JavaScript exports:

require('postcss-export-custom-variables')({
  exporter: 'js',
  destination: 'css-vars-exports.js'
});
export const customSize = "960px";
export const customStyles = { color: "black", backgroundColor: "white" };
export const customViewport = "(max-width: 30em)";
export const customSelector = ":hover, :focus";

With these variables synchronized to JavaScript, they may be used later by something like window.matchMedia(), document.querySelectorAll(), element.style, or element.animate().

Usage

Add PostCSS Export Custom Variables to your build tool:

npm install postcss-export-custom-variables --save-dev

Node

Use PostCSS Export Custom Variables to process your CSS:

require('postcss-export-custom-variables').process(YOUR_CSS, { /* postcss options */ }, { /* plugin options */ });

PostCSS

Add PostCSS to your build tool:

npm install postcss --save-dev

Use PostCSS Export Custom Variables as a plugin:

postcss([
  require('postcss-export-custom-variables')({ /* plugin options */ })
]).process(YOUR_CSS, /* postcss options */);

Gulp

Add Gulp PostCSS to your build tool:

npm install gulp-postcss --save-dev

Use PostCSS Export Custom Variables in your Gulpfile:

gulp.task('css', () => gulp.src('./src/*.css').pipe(
  require('gulp-postcss')([
    require('postcss-export-custom-variables')({ /* plugin options */ })
  ])
).pipe(
  gulp.dest('.')
));

Grunt

Add Grunt PostCSS to your build tool:

npm install grunt-postcss --save-dev

Use PostCSS Export Custom Variables in your Gruntfile:

grunt.loadNpmTasks('grunt-postcss');

grunt.initConfig({
  postcss: {
    options: {
      use: [
        require('postcss-export-custom-variables')({ /* options */ })
      ]
    },
    dist: {
      src: '*.css'
    }
  }
});

Advanced Options

These options may be passed directly into the plugin.

require('postcss-export-custom-variables')({ /* options */ });

destination

destination is the path where your JSON or JS Exports will be saved. By default, it is the CSS source with an additional .js or .json extension.

variables

variables is the object your CSS variables are assigned to. This might be used to pre-populate some unique set of custom properties. By default, it is a new object.

exporter

exporter is the function used to export the whole object of custom variables. The plugin will return this function, so Promises should be returned if performing an asynchronous operation, such as writing to a file.

{
  exporter: (variables, options, root) => {
    // variables: an object of all the variables collected
    // options: options passed into the plugin
    // root: the AST of CSS parsed by PostCSS

    // return new Promise();
  }
}
  • If a js string is passed, the default JavaScript stringifier will be used.
  • If a json string is passed, the default JSON stringifier will be used.
{
  exporter: 'json'
}

Custom Assigners

Use these custom assigners to determine how you would like to organize your custom variables.

customMediaQueryAssigner

customMediaQueryAssigner is the function used to create an object from the query and value of custom media queries.

{
  customMediaQueryAssigner: (name, queries, node) => {
    // name: name of the custom media query
    // queries: queries for the custom media query
    // node: PostCSS atrule for the custom media query

    // returns { name: queries }
  }
}

customPropertyAssigner

customPropertyAssigner is the function used to create an object from the property and value of custom properties.

{
  customPropertyAssigner: (property, value, node) => {
    // property: name of the custom property
    // value: value of the custom property
    // node: PostCSS declaration for the custom property

    // returns { property: value };
  }
}

customPropertySetAssigner

customPropertySetAssigner is the function used to create an object from the property and value of custom property sets.

{
  customPropertySetAssigner: (property, nodes, node) => {
    // property: name of the custom property set
    // nodes: array of all the children of the property set
    // node: PostCSS rule for the custom property set

    // returns { property: object_from_nodes };
  }
}

customSelectorAssigner

customSelectorAssigner is the function used to create an object from the property and value of custom selectors.

{
  customSelectorAssigner: (property, selectors, node) => {
    // property: name of the custom selector
    // selectors: selectors for the custom selector
    // node: PostCSS atrule for the custom selector

    // returns { property: selectors };
  }
}