wix-style-processor
An alternative Wix Styles TPA processor.
This package provides a parser / transformer that scans your inlined CSS, and replaces its dynamic style declarations to the values defined by the user's website / template.
Installation
$ npm i -S wix-style-processor
Usage
CSS
.my-selector {
--my-font: "font(Body-M)";
--default-width: "number(42)";
font: "font(--my-font)";
width: "number(--default-width)"px;
color: "color(color-8)";
background-color: "join(opacity(color-1, 0.5), opacity(color-8, 0.5))";
color: "opacity(color-8, 0.3)";
}
Module initialization
import styleProcessor from 'wix-style-processor';
$(document).ready(() => {
styleProcessor.init().then(() => {
})
});
Plugin support
You can customize and extend the module's behavior with the use of plugins.
Plugins are invoked during the processing phase of the CSS declarations, and they let you override the built-in transformations (such as opacity or join), or add transformations of your own.
There are 2 kinds of plugins, which will be detailed below.
1. Value transformation plugins
These plugins define functions that transform the value-side of the CSS declaration.
Usage
Plugin definition (JS):
import styleProcessor from 'wix-style-processor';
styleProcessor.valuePlugin(
'increment',
(params, siteParams) => parseInt(params[0]) + 1
);
CSS definition:
.foo {
--baz: 1;
bar: "increment(number(--baz))"px;
}
The CSS above will be replaced to:
.foo {
--baz: 1;
bar: 2px;
}
Declaration transformation plugins
These plugins allow you to transform the entire key / value of the CSS declaration.
Since they're invoked upon each and every declaration, there's no need to name them.
Example
Plugin definition (JS):
import styleProcessor from 'wix-style-processor';
styleProcessor.declarationPlugin((key, value, siteParams) => ({
key: 'ZzZ' + key + 'ZzZ',
value: '#' + value + '#'
}));
CSS definition:
.foo {
bar: 4;
}
The CSS above will be replaced to:
.foo {
ZzZbarZzZ: #4#;
}
If you need dynamic LTR/RTL replacements in your CSS, you can use this plugin.
Important
This module only parses inline CSS.
It won't process any wix style params from an external (linked) CSS file.
The recommended approach for CSS inlining is by automating it in your build step - e.g. by using Webpack's style-loader.