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

@x.render/babel-plugin-transform-jsx-style-units-to-vw

v1.0.2

Published

处理react组件style属性中所有属性,符合条件的转换成vw

Downloads

25

Readme

babel-plugin-transform-style-units-to-vw

English document

用于将 React 组件中的 style 没有单位的属性值转换为视口单位(vw)。该插件支持多种配置选项,允许你自定义视口宽度、单位精度以及需要处理或排除的属性

安装

npm  install  --save-dev  @x.render/babel-plugin-transform-jsx-style-units-to-vw

或者

yarn  add  --dev  @x.render/babel-plugin-transform-jsx-style-units-to-vw

使用

在你的 Babel 配置文件中(例如 .babelrc 或 babel.config.js),添加此插件并配置相关选项。

.babelrc

{
  "presets": ["@babel/preset-react"],
  "plugins": [
    [
      "@x.render/babel-plugin-transform-jsx-style-units-to-vw",
      {
        "viewportWidth": 750,
        "unitPrecision": 5,
      }
    ]
  ]
}

babel.config.js

module.exports = {
  presets: ['@babel/preset-react'],
  plugins: [
    [
      '@x.render/babel-plugin-transform-jsx-style-units-to-vw',
    ]
  ]
};

配置选项

  • viewportWidth(默认值: 750)
    • 视口宽度(设计图尺寸),用于计算vw单位。
  • unitPrecision(默认值: 5)
    • 转换后的数值的小数位数。
  • include(可选)
    • 一个字符串数组,包含需要转换为vw单位的 CSS 属性名称。
  • exclude(可选)
    • 一个字符串数组,包含不需要转换为vw单位的 CSS 属性名称。这些属性将被排除在外,即使它们在include数组中。

示例

假设你有以下 React 组件:

import React from 'react';
const MyComponent = () => (
  <div style={{ width: 100, height: 200, fontSize: 16 }}>Hello, World!</div>
);
export default MyComponent;

使用插件后,生成的代码将如下所示:

import React from 'react';
const MyComponent = () => (
  <div
    style={{ width: '13.33333vw', height: '26.66667vw', fontSize: '2.13333vw' }}
  >
    Hello, World!
  </div>
);

支持动态参数解析:

import React from 'react';
const MyComponent = ({ height }) => (
  <div style={{ width: 100, height, fontSize: 16 }}>Hello, World!</div>
);
export default MyComponent;

使用插件后,生成的代码将如下所示:

import React from 'react';
const MyComponent = ({ height }) => (
  <div
    style={{
      width: '13.33333vw',
      height:
        (10 * Math.round(Math.floor((j / 750) * 100 * 1e6) / 10)) / 1e6 + 'vw',
      fontSize: '2.13333vw',
    }}
  >
    Hello, World!
  </div>
);
export default MyComponent;

注意

  • 插件只会转换插件内部和 include 的属性,如果你使用过程中发现有些属性不进行转换,请使用 exclude 进行配置。
  • 插件只会转换没有携带单位的属性,比如:{height:100}或者{height:'100'},会被处理,而{height:'100px'}、{height:'100%'}、{height:'100vm'}等带有其他单位的则不回被处理。