All Projects → mistricky → postcss-relaxed-unit

mistricky / postcss-relaxed-unit

Licence: MIT License
🍮Postcss-relaxed-unit is a postcss plugin for unit tranformation and make write css easier with custom unit.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
CSS
56736 projects

Projects that are alternatives of or similar to postcss-relaxed-unit

postcss-purgecss
PostCSS plugin for purgecss
Stars: ✭ 92 (+109.09%)
Mutual labels:  postcss, postcss-plugin
postcss-gtk
Processes GTK+ CSS into browser CSS
Stars: ✭ 23 (-47.73%)
Mutual labels:  postcss, postcss-plugin
postcss-prefixwrap
A PostCSS plugin that is used to wrap css styles with a css selector to constrain their affect on parent elements in a page.
Stars: ✭ 54 (+22.73%)
Mutual labels:  postcss, postcss-plugin
postcss-rename
Replace class names based on a customizable renaming scheme.
Stars: ✭ 77 (+75%)
Mutual labels:  postcss, postcss-plugin
react-cli
基于 create-react-app 搭建的前端脚手架
Stars: ✭ 18 (-59.09%)
Mutual labels:  postcss, rem
postcss
No description or website provided.
Stars: ✭ 59 (+34.09%)
Mutual labels:  postcss, postcss-plugin
postcss-typed-css-classes
PostCSS plugin that generates typed entities from CSS classes for chosen programming language.
Stars: ✭ 12 (-72.73%)
Mutual labels:  postcss, postcss-plugin
postcss-inline-media
Media queries shortcut, built on PostCSS, example: font-size: 20px @1200 18px @480 16px;
Stars: ✭ 47 (+6.82%)
Mutual labels:  postcss, postcss-plugin
postcss-lazyimagecss
A PostCSS plugin that generates images's CSS width & height properties automatically.
Stars: ✭ 38 (-13.64%)
Mutual labels:  postcss, postcss-plugin
postcss-font-pack
PostCSS plugin to simplify font declarations by validating only configured font packs are used and adding fallbacks.
Stars: ✭ 18 (-59.09%)
Mutual labels:  postcss, postcss-plugin
postcss-sort-media-queries
PostCSS plugin for combine and sort CSS media queries with mobile first or desktop first methods.
Stars: ✭ 118 (+168.18%)
Mutual labels:  postcss, postcss-plugin
postcss-clean
PostCss plugin to minify your CSS with clean-css
Stars: ✭ 41 (-6.82%)
Mutual labels:  postcss, postcss-plugin
postcss-critical-css
PostCSS plugin to define and output critical CSS using custom atRules, and/or custom CSS properties. Critical CSS may be output to one or more files, as defined within the plugin options or within the CSS.
Stars: ✭ 84 (+90.91%)
Mutual labels:  postcss, postcss-plugin
postcss-aspect-ratio
A PostCSS plugin to fix an element's dimensions to an aspect ratio.
Stars: ✭ 38 (-13.64%)
Mutual labels:  postcss, postcss-plugin
postcss-styl
PostCSS parser plugin for converting Stylus syntax to PostCSS AST.
Stars: ✭ 15 (-65.91%)
Mutual labels:  postcss, postcss-plugin
postcss-windicss
PostCSS integrations for Windi CSS
Stars: ✭ 33 (-25%)
Mutual labels:  postcss, postcss-plugin
postcss-center
PostCSS plugin to center elements.
Stars: ✭ 44 (+0%)
Mutual labels:  postcss, postcss-plugin
postcss-import-url
PostCSS plugin inlines remote files.
Stars: ✭ 47 (+6.82%)
Mutual labels:  postcss, postcss-plugin
postcss-flexible
🔲 dpr/rem/url transformer for flexible
Stars: ✭ 19 (-56.82%)
Mutual labels:  postcss, rem
postcss-rem
PostCSS plugin to use rem units with optional pixel fallback.
Stars: ✭ 14 (-68.18%)
Mutual labels:  postcss, rem

Postcss-relaxed-unit 🍮

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.

Motivation

Sometimes, we need to care about calculating the style values, for example, px to remrem 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! 🎉

Precision overflow 👌

You don't have to care about precision overflow, because postcss-relaxed-unit wraps bignumber.js.

Install

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.

Usage

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)" }
    })
  ]
};

Multiple rules

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)"
      }
    })
  ]
};

Nuxt

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)' },
        },
    },
  },
}

Options

  • rules {[custom unit name]: 'operators'} custom unit to target unit mapping container

  • add Operator target value plus +

  • sub Operator target value subtraction -

  • mul Operator target value multiplication *

  • div Operator target value divition /

  • unit Operator unit 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)"}

Example

run yarn example or npm run example if you want to see the results of postcss-relaxed-unit working.⚙️

LICENSE

MIT.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].