Skip to content

Commit

Permalink
Fix addUnitPx can not convert rpx to px (#361)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluescurry committed Feb 16, 2022
1 parent fa425af commit ceb4c3d
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/core/src/utils/format/unit.ts
@@ -1,5 +1,6 @@
import * as _ from "lodash"
import { CSSProperties } from "react"
import { getSystemInfoSync } from "@tarojs/taro"

export function addUnitPx(value?: string | number): string {
return value === undefined ? "" : `${unitToPx(value)}px`
Expand Down Expand Up @@ -38,6 +39,12 @@ function getRootFontSize() {
return rootFontSize
}

function convertRpx(value: string) {
value = value.replace(/rpx/g, "")
const pixelRatio = 750 / getSystemInfoSync().windowWidth
return +value / pixelRatio
}

function convertPx(value: string) {
value = value.replace(/px/g, "")
return +value
Expand All @@ -63,6 +70,9 @@ export function unitToPx(value: string | number): number {
return value
}

if (value.includes("rpx")) {
return convertRpx(value)
}
if (value.includes("px")) {
return convertPx(value)
}
Expand Down

0 comments on commit ceb4c3d

Please sign in to comment.