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-js-mixins

v2.5.3

Published

PostCSS plugin for custom mixin syntax

Downloads

162

Readme

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.