English doc | 中文文档
Postcss-relaxed-unit is a postcss plugin for unit tranformation and make write css easier with custom unit.
You can define a rule to determine the mapping relationship between custom unit and target unit, and you can use some operators to calculate the target value (like add, sub, mul, div, unit), so you can write style relaxed without care about calculate unit.
Sometimes, we need to care about calculating the style values, for example, px to rem, rem to vw , px to whatever that mobile side need and we don't need many plugins. so, postcss-relaxed-unit can help you forget that, you just need define a rule that describes the custom unit to target unit mapping! that's all! 🎉
You don't have to care about precision overflow, because postcss-relaxed-unit wraps bignumber.js.
postcss-relaxed-unit is publish to npm,so you can install it using npm or yarn
npm i postcss-relaxed-unit -D
or
yarn add postcss-relaxed-unit -D
because postcss-relaxed-unit is depends on PostCSS, you need to install postcss.
You only need to define rule to get start~
postcss.config.js
const RelaxedUnit = require("postcss-relaxed-unit");
module.exports = {
plugins: [
RelaxedUnit({
rules: { rx: "add(1).sub(2).mul(3).div(9).unit(rem)" }
})
]
};You can define more rules :)
const RelaxedUnit = require("postcss-relaxed-unit");
module.exports = {
plugins: [
RelaxedUnit({
rules: {
rx: "add(1).sub(2).mul(3).div(9).unit(rem)",
ex: "div(100).unit(rem)"
}
})
]
};In Nuxt.js, you need to define the config in nuxt.config.js
nuxt.config.js
module.exports = {
build: {
extractCSS: true,
postcss: {
plugins: {
'postcss-relaxed-unit': {
rules: { rx: 'div(100).unit(rem)' },
},
},
},
}-
rules {[custom unit name]: 'operators'}custom unit to target unit mapping container -
add Operatortarget value plus+ -
sub Operatortarget value subtraction- -
mul Operatortarget value multiplication* -
div Operatortarget value divition/ -
unit Operatorunit of output
custom unit will output does not change missing unit Operator, e.g.
{
"postcss-relaxed-unit": {
"rules": { "rx": "add(10).sub(2)" }
}
}origin style
.hello-relaxed-unit {
width: 10rx;
}output style
.hello-relaxed-unit {
width: 18rx;
}The signature of operator function like
type OperatorFunction = (arg: number | string) => string;so, if you call the operator function like add(aas) , it will compile passing, the aas wiil be convert to 0.
{"rx": "add(aas).unit(px)"} => {"rx": "add(0).unit(px)"}run yarn example or npm run example if you want to see the results of postcss-relaxed-unit working.⚙️
MIT.