Socket
Socket
Sign inDemoInstall

postcss-js-mixins

Package Overview
Dependencies
10
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    postcss-js-mixins

PostCSS plugin for custom mixin syntax


Version published
Weekly downloads
143
increased by32.41%
Maintainers
1
Install size
2.09 MB
Created
Weekly downloads
 

Readme

Source

PostCSS JS Mixins

Build Status codecov npm

PostCSS plugin for custom mixin syntax

/* before */
.block {
	column(spaced, 1, 2);
}

/* after */
.block {
	float: left;
	width: 50%;
	margin-left: 5%;
}

/* before */
.block {
	spacedBlock(width: 10px);
}

/* after */
.block {
	margin-bottom: 2rem;
	width: 10px;
	display: block;
}

Usage

const syntax = require('postcss-wee-syntax');
const mixins = require('postcss-js-mixins');

postcss([ mixins({ /* options */ }) ]).process(input, {
			syntax: syntax
		})
		.then(result => {
			// Do something with result
		});

See PostCSS docs for examples for your environment.

Options

mixins

Type: Object
Default: {}

Register mixins that you want to reference in your style sheets.

const decl = require('postcss-js-mixins/lib/declaration');
const { isEmpty } = require('postcss-js-mixins/lib/helpers');

require('postcss-js-mixins')({
	mixins: {
		/**
		* Example of creating a shorthand with default value
		*/
		spaced(value = '20px') {
			return decl('margin-bottom', value);
		}
	}
});

units

Type: Object
Default: { default: 'rem', lineHeight: 'em' }

These units will be appended intelligently when number values are passed without a unit. For example, the font-size property will have the unit appended, but opacity will not.

Writing Mixins

Mixins are written solely in JavaScript. They can return a declaration, a rule, or an array of declarations/rules.

Declaration

Declarations take a CSS property and it's value as arguments.

const decl = require('postcss-js-mixins/lib/declaration');

// Create single declaration
decl(prop, value);

Rule

Rules take a selector and an array of Declaration objects.

const rule = require('postcss-js-mixins/lib/rule');

// Create single declaration
rule('.block:after', [
	decl(prop, value),
	decl(prop, value)
]);
Methods
createMany

Matches indexes from two arrays to produce declarations for each. This is used when order matters for your mixin arguments.

// Create multiple declarations
function position(...args) {
	return decl.createMany(['top', 'right', 'left', 'bottom'], args);
}
position(10%, 0, false, 0);
createManyFromObj

Takes an object with property: value pairs and an optional prefix to prepend to each property value.

// Create multiple declarations from an object
function padding(top = 0, right = 0, bottom = 0, left = 0) {
	return decl.createManyFromObj({
		top: top,
		right: right,
		bottom: bottom,
		left: left
	}, 'padding');
}
padding(top: '10px', left: '12px');

/* Output */
padding-top: 10px;
padding-right: 0;
padding-bottom: 0;
padding-left: 12px;

Helper Methods

Helper methods have been provided in order to make writing mixins easier.

const helpers = require('postcss-js-mixins/lib/helpers');
const { darken, lighten } = require('postcss-js-mixins/lib/colorHelpers');
List of Helper Methods
  • darken
  • lighten
  • calcOpacity
  • hexToRgba
  • isColor
  • isEmpty
  • isNumber
  • isObject
  • isPercentage
  • isProvided
  • isString
  • isUnit
  • prefix
  • setDefaultUnits
  • toDashCase
  • toDegrees
  • type
  • unit

Note: This plugin uses TinyColor which has a large number of other color helper methods that can easily be exposed if requested.

Keywords

FAQs

Last updated on 18 Aug 2017

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