npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

postcss-relaxed-unit

v1.1.1

Published

PostCSS plugin Make write csss easier with custom unit

Downloads

157

Readme

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! :tada:

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.