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-pxtorempoor

v2.0.0

Published

✨ A CSS post-processor that converts px to rem.

Downloads

14

Readme

postcss-pxtorem

English | 中文

A plugin for PostCSS that generates rem units from pixel units. This project is based on Hemengke1997/PostCSS- PXTOREM. Thanks to Hemengke1997. The change i made is only to add the ConvertinMediaque parameter to avoid changing the unit in media query.

✨ New Features

  • override any postcss-pxtorem options in css.
  • ignore next line in css.
  • compatible with vite.

🔧 Install

pnpm install postcss postcss-pxtorempoor -D

✍️ Usage

Pixels are the easiest unit to use (opinion). The only issue with them is that they don't let browsers change the default font size of 16. This script converts every px value to a rem from the properties you choose to allow the browser to set the font size.

postcss.config.js

example

// postcss.config.js
import pxtorom from 'postcss-pxtorempoor'

export default {
  plugins: [
    pxtorom({
      rootValue: 16,
      selectorBlackList: ['some-class'],
      propList: ['*'],
      atRules: ['media'],
      // ...
    }),
  ],
}

options

| Name | Type | Default | Description | | ------------------- | ------------------------------------------------------------ | --------------- | ------------------------------------------------------------ | | rootValue | number | ((input: Input) => number) | 16 | Represents the root element font size or returns the root element font size based on the input parameter | | unitToConvert | string | px | unit to convert, by default, it is px | | unitPrecision | number | 5 | The decimal numbers to allow the REM units to grow to. | | propList | string[] | ['*'] | The properties that can change from px to rem. Refer to: propList | | selectorBlackList | (string \| RegExp)[] | [] | The selectors to ignore and leave as px. Refer to: selectorBlackList | | replace | boolean | true | Replaces rules containing rems instead of adding fallbacks. | | atRules | boolean | string[] | false | Allow px to be converted in at-rules. Refer to At-rule | | minPixelValue | number | 0 | Set the minimum pixel value to replace. | | exclude | string | RegExp | ((filePath: string) => boolean) \| null | /node_modules/i | The file path to ignore and leave as px. Refer to: exclude | | include | string | RegExp | ((filePath: string) => boolean) | null | null | The file path to convert px to rem, in contrast to exclude, have higher priority than exclude. Same rules as exclude | | disable | boolean | false | disable plugin | | convertUnitOnEnd | ConvertUnit | ConvertUnit[] | false | null | null | convert unit when plugin process end | | convertInMediaQuery | boolean | true | convert unit in media queries |

propList

  • Values need to be exact matches.
  • Use wildcard * to enable all properties. Example: ['*']
  • Use * at the start or end of a word. (['*position*'] will match background-position-y)
  • Use ! to not match a property. Example: ['*', '!letter-spacing']
  • Combine the "not" prefix with the other prefixes. Example: ['*', '!font*']

selectorBlackList

  • If value is string, it checks to see if selector contains the string.
    • ['body'] will match .body-class
  • If value is regexp, it checks to see if the selector matches the regexp.
    • [/^body$/] will match body but not .body

exclude

  • If value is string, it checks to see if file path contains the string.
    • 'exclude' will match \project\postcss-pxtorem\exclude\path
  • If value is regexp, it checks to see if file path matches the regexp.
    • /exclude/i will match \project\postcss-pxtorem\exclude\path
  • If value is function, you can use exclude function to return a true and the file will be ignored.
    • the callback will pass the file path as a parameter, it should returns a Boolean result.
    • function (file) { return file.includes('exclude') }

✨ About new features

⚙️ Dynamically set plugin options in css

disable plugin

/* pxtorem?disable=true */
.rule {
  font-size: 15px; // 15px
}

set rootValue

/* pxtorem?rootValue=32 */
.rule {
  font-size: 30px; // 0.9375rem
}

🌰 The above is just a simple example, you can set any of the options supported by postcss-pxtorem in the css file

You may have seen that the css comment is very much like the browser url?😼. That's right. For the specification, just refer to: query-string

example

/* pxtorem?disable=false&rootValue=32&propList[]=*&replace=false&selectorBlackList[]=/some-class/i */

disable the next line in css file

.rule {
  /* pxtorem-disable-next-line */
  font-size: 15px; // 15px
}

If you write 15PX (as long as it's not px), the plugin also ignores it, because unitToConvert defaults to px If you want to use PX to ignore and want the final unit to be px, you can:

// postcss.config.js
import pxtorem from 'postcss-pxtorempoor'

export default {
  plugins: [
    pxtorem({
      convertUnitOnEnd: {
        sourceUnit: /[p|P][x|X]$/,
        targetUnit: 'px',
      },
    }),
  ],
}

use with flexible js

example

;(function () {
  if (typeof window === 'undefined') return

  const maxWidth = 1024
  const uiWidth = 375

  function resize() {
    let width = window.innerWidth

    if (width > window.screen.width) {
    } else {
      if (width >= maxWidth) {
        width = maxWidth
      }
      document.documentElement.style.fontSize = `${(width * 16) / uiWidth}px`
    }
  }

  resize()

  let timer: NodeJS.Timer
  const interval = 1000

  window.addEventListener('resize', () => {
    clearTimeout(timer)
    timer = setTimeout(resize, interval)
  })
  window.addEventListener('pageshow', (e) => {
    if (e.persisted) {
      clearTimeout(timer)
      timer = setTimeout(resize, interval)
      resize()
    }
  })
})()

❤️ Thanks

hemengke1997/postcss-pxtorem

postcss-pxtorem

@tcstory/postcss-px-to-viewport

👀 Related

A CSS post-processor that converts px to viewport: postcss-pxtoviewport

💕 Support

If this has helped you, please don't hesitate to give a STAR, thanks! 😎