Git Product home page Git Product logo

styled-px2vw's Introduction

styled-px2vw ✨

Travis (.org) npm npm David David GitHub stars

English | 简体中文

Extension of styled-components with features for convert px to vw units.
See the documentation at styled-components.com/docs for more information about using styled-components!

Quicklinks to some of the most-visited pages:

Important

Based on 750px design draft.

Motivation

Styled-px2vw mainly solves the problem of screen adaptation of multiple device sizes on mobile, The size of mobile devices is various, The current popular solution is to use vw units, Unfortunately, styled-components does not support this feature, so we developed the styled-px2vw component to solve the adaptation problem, Of course, there may be many problems in this, I hope that developers join together to optimize styled-px2vw, together to create a better front-end ecosystem.

Features

Installation

yarn:

yarn add styled-px2vw

npm:

npm i --save styled-px2vw

Example

style

Output

converted

Screenshot

screenshot

Migrate

Just change styled-components to styled-px2vw

import styled, { createGlobalStyle,... } from 'styled-components';

const Button = styled.button`
  color: white;
  font-size: 36px;
  margin: 10px;
  height: 85px;
  border: 2px solid palevioletred;
  border-radius: 3px;
`;

to

import styled, { createGlobalStyle,... } from 'styled-px2vw';

const Button = styled.button`
  color: white;
  font-size: 36px;
  margin: 10px;
  height: 85px;
  border: 2px solid palevioletred;
  border-radius: 3px;
`;

License

Licensed under the MIT License, Copyright © 2018-present hnzycfc.com, https://www.hnzycfc.com/

See LICENSE for more information.

styled-px2vw's People

Contributors

gulewei avatar hnzycfc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

styled-px2vw's Issues

样式嵌套解析错误

const SearchView = styled.div`
display: flex;
align-items: flex-start;
justify-content: space-around;
width: 100%;
height: 131px;
box-sizing: border-box;
padding-top: 5px;
background-image: linear-gradient(1deg, rgba(255,255,255,0.30) 27%, #FFFFFF 99%);
font-family: PingFangSC-Regular;
.item{
position: relative;
padding-right: 22px;
height: 92px;
line-height: 92px;
font-size: 24px;
color: #999999;
text-align: center;
span{
position: absolute;
top: 0;
right: 0;
display: inline-block;
width: 12px;
height: 92px;
background-image: url("data:image/svg+xml,%3Csvg width='6' height='15' viewBox='0 0 6 15' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='%23999' fill-rule='evenodd'%3E%3Cg transform='matrix(1 0 0 -1 0 7)'%3E%3Crect transform='rotate(45 2.04 2.232)' x='.039' y='1.732' width='4' height='1' rx='.5'/%3E%3Crect transform='scale(-1 1) rotate(45 0 -7.827)' x='2.166' y='1.732' width='4' height='1' rx='.5'/%3E%3C/g%3E%3Cg transform='translate(0 8)'%3E%3Crect transform='rotate(45 2.04 2.5)' x='.039' y='2' width='4' height='1' rx='.5'/%3E%3Crect transform='scale(-1 1) rotate(45 0 -7.559)' x='2.166' y='2' width='4' height='1' rx='.5'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
background-size: 12px 30px;
background-position: center;
background-repeat: no-repeat;
}
&.red{
font-weight: bold;
color: #333333;
}
}

.red span{
    background-color: red;
}

`

解析为,嵌套部分解析错误,.red span解析为.iPNFtH .item span.red:

.iPNFtH .item{position:relative;padding-right:2.93333vw;height:12.26667vw;line-height:12.26667vw;font-size:3.2vw;color:#999999;text-align:center;}
.iPNFtH .item span{position:absolute;top:0;right:0;display:inline-block;width:1.6vw;height:12.26667vw;background-image:url("data:image/svg+xml,%3Csvg width='6' height='15' viewBox='0 0 6 15' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='%23999' fill-rule='evenodd'%3E%3Cg transform='matrix(1 0 0 -1 0 7)'%3E%3Crect transform='rotate(45 2.04 2.232)' x='.039' y='1.732' width='4' height='1' rx='.5'/%3E%3Crect transform='scale(-1 1) rotate(45 0 -7.827)' x='2.166' y='1.732' width='4' height='1' rx='.5'/%3E%3C/g%3E%3Cg transform='translate(0 8)'%3E%3Crect transform='rotate(45 2.04 2.5)' x='.039' y='2' width='4' height='1' rx='.5'/%3E%3Crect transform='scale(-1 1) rotate(45 0 -7.559)' x='2.166' y='2' width='4' height='1' rx='.5'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");background-size:1.6vw 4vw;background-position:center;background-repeat:no-repeat;}
.iPNFtH .item span.red{font-weight:bold;color:#333333;} .iPNFtH .item span .red span{background-color:red;}

不支持传参

对于

styled.div`
  width: ${(props: { width: number }) => props.width}px;
`;

width传100的话,输出的css是
width: 1000;

看了下代码,strings 和 interpolations 分开处理的

strings中的'px',由于去掉单位后是'',所以直接返回了0

interpolations 中的 100,没有被处理。因为不是px结尾,所以没有匹配到正则,直接返回了

自己试了下,可以这样写:

const A = styled.div`
  width: ${(props: { width: number }) => `${props.width}px`};
`;

但这样和styled-component的API就不一致了

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.