Formatters
Formatters are functions that return a single CSS declaration value. They can receive arguments, process them and return the value.
Currently, any argument passed through to a formatter is of type string
. We are in the process of adding support for more complex types.
For example a calc-font-size
formatter can return a different value for the font size depending on the provided argument.
If you need to return multiple declaration values, we recommend using Stylable mixins.
/* ./calc-font-size.js */
module.exports = function (baseSize, modifier) {
switch (modifier) {
case 'header':
return `${Number(baseSize) * 2}px`;
case 'aside':
return `${Number(baseSize) * 0.75}px`;
default:
return baseSize + 'px';
}
};
@st-import calcFontSize from "./calc-font-size";
.header {
font-size: calcFontSize(16, header);
}
.form {
font-size: calcFontSize(16, body);
}
/* CSS output */
.header {
font-size: 32px;
}
.form {
font-size: 16px;
}
/* CSS output */
.header {
font-size: 32px;
}
.form {
font-size: 16px;
}
Currently you cannot use formatters inside a native URL function. As a suggested workaround, you can return a URL function from a formattter.
Formatters with variables
When the formatter is imported into the CSS, it can also be used with a variable.
In this example the CSS imports the same formatter as the previous example, calc-font-size
, but the variable baseFontSize
is added to the calculation.
@st-import calcFontSize from "./calc-font-size";
:vars {
baseFontSize: 12;
}
.header {
font-size: calcFontSize(value(baseFontSize), header);
}
.form {
font-size: calcFontSize(value(baseFontSize), body);
}
/* CSS output */
.header {
font-size: 24px;
}
.form {
font-size: 12px;
}