GithubHelp home page GithubHelp logo

stone-payments / postcss-custom-properties Goto Github PK

View Code? Open in Web Editor NEW

This project forked from postcss/postcss-custom-properties

1.0 3.0 0.0 115 KB

PostCSS plugin to transform W3C CSS Custom Properties for cascading variables

License: MIT License

JavaScript 100.00%

postcss-custom-properties's Introduction

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;
}

h1 {
  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({
  preserve: false
})
:root {
  --color: red;
}

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

/* becomes */

h1 {
  color: red;
}

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

postCSSCustomProperties({
  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

postcss-custom-properties's People

Contributors

moox avatar jonathantneal avatar semigradsky avatar bloodyowl avatar itaditya avatar shvaikalesh avatar bjankord avatar bundyo avatar davidtheclark avatar privatenumber avatar joeybaker avatar necolas avatar oleersoy avatar superol3g avatar ryanfitzer avatar everdimension avatar wtgtybhertgeghgtwtg avatar

Stargazers

Felipe Monteiro avatar

Watchers

James Cloos avatar Matheus Siqueira avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.