A list of utilities for creating PostCSS plugins
npm install postcss-plugin-utilitiesA list of utilities for creating [PostCSS] plugins
[PostCSS]: https://github.com/postcss/postcss
[Gulp]: https://github.com/gulpjs/gulp
[exact-regex]: https://github.com/arpadHegedus/exact-regex.git
[overwrite-object]: https://github.com/arpadHegedus/overwrite-object.git
[mathjs]: http://mathjs.org/
npm install postcss-plugin-utilities
`
Utilities
$3
Do calculations on css (uses [mathjs])
Example:
`js
let util = require('postcss-plugin-utilities'),
width = '1600px',
height = util.calc('x/16*9', width);
// height will be 900px
`
$3
Returns either black or white depending on contrasting a color. Useful for calculating overlay text color for an unknown background-color.
$3
Updates a selector with SASS like syntax
Example:
`js
let util = require('postcss-plugin-utilities'),
selector = 'p, ul, ol',
newSelector = util.eachSelector(selector, '&:before');
// newSelector will be 'p:before, ul:before, ol:before'
`
$3
Go through an array and filter the values as per set rules and falling back to defaults.
Example:
`js
let util = require('postcss-plugin-utilities'),
values = ['20px', 'center', 'black'],
theObject = util.filterObject(values, {
// rules are set up using an object,
// the values being an array of correct values
// or a validation function
align: ['left', 'right', 'center'], // left, right or center are accepted values
blackOrWhite: [(testingValue) => { // a function can be passedin to validate
if(['black', 'white'].indexOf(testingValue) >= 0) { return true; }
return false;
}],
size: [util.isSize], // we can also link in functions for validation
shadow: [util.isBoxShadow]
}, {
// a set of defaults can be passed in as well
size: '30px',
shadow: '1px 1px 1px black'
});
// theObject will result in
// {
// align: 'center',
// blackOrWhite: 'black',
// size: '20px',
// shadow: '1px 1px 1px black'
// }
`
$3
Get an object of RGB and possibly A values from a color string
$3
Wrap filter object and return sides values the same way as margin and padding works in CSS
Example:
`js
let util = require('postcss-plugin-utilities'),
values = ['20px', '10px'],
sides = util.getSides(values, {
'border-top': [util.isSize],
'border-right': [util.isSize],
'border-bottom': [util.isSize],
'border-left': [util.isSize]
});
// sides will equal to
// {
// 'border-top': '20px',
// 'border-right': '10px',
// 'border-bottom': '20px',
// 'border-left': '10px',
// }
``