npm install @csstools/postcss-mixins --save-devPostCSS Mixins lets you use @mixin and @apply following CSS Mixins 1.
Several specification aspects of CSS Mixins still need to be settled.
This plugin is only a partial implementation to avoid conflicts with the final specification.
Unsupported:
- typed mixin arguments
- layered
@mixindeclarations - conditional
@mixindeclarations - mixin overrides
Run @csstools/postcss-private-rule after this plugin to transpile @private rules in style rules that do not apply a mixin.
A mixin with an unsupported definition is left unchanged: the definition and every @apply of that name stay as they are.
Rules that apply such a mixin are left unchanged as well, so native support keeps working for them.
The same happens when private properties in a rule can not be transpiled completely, for example when a private property registration is inside an anonymous cascade layer.
Private properties in the applying rule and in @contents blocks are transpiled together with the mixin.
An @apply with a CSS-wide keyword (inherit, initial, unset, revert, revert-layer) as an argument is left unchanged:
browsers substitute these keywords into the property that uses the parameter, while the specification resolves them in the argument phase.
CSS-wide keywords in parameter defaults are resolved in the argument phase.
@mixin --foo() {
color: green;
}
.foo {
@apply --foo;
}
@mixin --bar(--baz) {
color: rgb(var(--baz) 127 0);
}
.bar {
@apply --bar(200);
}
@mixin --a {
font-weight: 500;
}
@mixin --b {
@apply --a;
font-size: 2rem;
}
.c {
@apply --b;
}
/* becomes */
.foo {
color: green;
}
.bar {
--_csstools-mix-1gice1e-arg-0: 200;
--_csstools-m-1gice1e-0--baz: var(--_csstools-mix-1gice1e-arg-0);
color: rgb(var(--_csstools-m-1gice1e-0--baz) 127 0);
}
.c {
font-weight: 500;
font-size: 2rem;
}Add PostCSS Mixins to your project:
npm install postcss @csstools/postcss-mixins --save-devUse it as a PostCSS plugin:
const postcss = require('postcss');
const postcssMixins = require('@csstools/postcss-mixins');
postcss([
postcssMixins(/* pluginOptions */)
]).process(YOUR_CSS /*, processOptions */);PostCSS Mixins runs in all Node environments, with special instructions for:
The preserve option determines whether the original notation
is preserved. By default, it is not preserved.
postcssMixins({ preserve: true })@mixin --foo() {
color: green;
}
.foo {
@apply --foo;
}
@mixin --bar(--baz) {
color: rgb(var(--baz) 127 0);
}
.bar {
@apply --bar(200);
}
@mixin --a {
font-weight: 500;
}
@mixin --b {
@apply --a;
font-size: 2rem;
}
.c {
@apply --b;
}
/* becomes */
@mixin --foo() {
color: green;
}
.foo {
color: green;
@apply --foo;
}
@mixin --bar(--baz) {
color: rgb(var(--baz) 127 0);
}
.bar {
--_csstools-mix-1gice1e-arg-0: 200;
--_csstools-m-1gice1e-0--baz: var(--_csstools-mix-1gice1e-arg-0);
color: rgb(var(--_csstools-m-1gice1e-0--baz) 127 0);
@apply --bar(200);
}
@mixin --a {
font-weight: 500;
}
@mixin --b {
@apply --a;
font-size: 2rem;
}
.c {
font-weight: 500;
font-size: 2rem;
@apply --b;
}