Skip to content

StetHD/postcss-at-rules-variables

 
 

Repository files navigation

postcss-at-rules-variables

PostCSS plugin to transform W3C CSS Custom Properties for at-rule’s parameters.

Travis Build StatusAppVeyor Build Statustesten badgenodenpm versionDependency StatusXO code styleCoveralls status

npm downloadsnpmPackage QualityBADGINATOR

Why?

Adds the ability to use a custom property in the options, at-rules.
Used in conjunction with the plugin postcss-each, postcss-conditionals, postcss-for and more at-rules plugins.

Install

$ npm install postcss-at-rules-variables

Note: This project is compatible with node v4+

Usage

// dependencies
var fs = require('fs');
var postcss = require('postcss');
var atImport = require('postcss-import');
var atEach = require('postcss-each');
var atVariables = require('postcss-at-rules-variables');
var atIf = require('postcss-conditionals');
var atFor = require('postcss-for');
var customProperties = require('postcss-custom-properties');
var nested = require('postcss-nested');

// css to be processed
var css = fs.readFileSync('css/input.css', 'utf8');

// process css
var output = postcss()
    .use(atVariables({ /* atRules: ['media'] */ }))
    .use(atEach())
    .use(atImport({
        plugins: [
            require('postcss-at-rules-variables')({ /* options */ }),
            require('postcss-import')
        ]
    }))
    .use(atFor())
    .use(atIf())
    .use(customProperties())
    .use(nested())
    .process(css, {
        from: 'css/input.css'
    })
    .css;

console.log(output);

Use postcss-at-rules-variables before you at-rules plugins

Example

/* input.css */
:root {
    --array: foo, bar, baz;
    --from: 1;
    --to: 3;
    --icon-exclude: 2;
    --color-danger: red;
}

@each $val in var(--array) {
    @import "$val.css";
}
/* foo.css */
html {
    background-color: var(--color-danger);
}
/* bar.css */
.some-class {
    color: #fff;

    @for $val from var(--from) to var(--to) {
        @if $val != var(--icon-exclude) {
            .icon-$val {
                background-position: 0 $(val)px;
            }
        }
    }
}
/* baz.css */
h1 {
    font-size: 24px;
}

@import "biz.css";
/* biz.css */
h2 {
    color: olive;
}
/* Output example */
html {
    background-color: red;
}

.some-class {
    color: #fff;
}

.some-class .icon-1 {
    background-position: 0 1px;
}

.some-class .icon-3 {
    background-position: 0 3px;
}

h1 {
    font-size: 24px;
}

h2 {
    color: olive;
}

Options

atRules

Type: Array
Default: ['for', 'if', 'else', 'each']
Description: The array used in all at-rules in your project

LICENSE

MIT License (MIT)

Copyright (c) Ivan Demidov [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

About

PostCss plugin to use CSS Custom Properties in at-rule @each, @for, @if, @else and more...

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%