UNPKG

@vnmfify/core

Version:

```shell npm i @vnmfify/core -S ```

96 lines (78 loc) 2.03 kB
import _isString from "lodash/isString"; import _isNumber from "lodash/isNumber"; import { getSystemInfoSync } from "@vnxjs/vnmf"; export function addUnitPx(value) { return value === undefined ? "" : "".concat(unitToPx(value), "px"); } export function getSizeStyle(originSize) { if (_isNumber(originSize) || _isString(originSize)) { var size = addUnitPx(originSize); return { width: size, height: size }; } return {}; } export function getZIndexStyle(zIndex) { var style = {}; if (zIndex !== undefined) { style.zIndex = +zIndex; } return style; } var rootFontSize; function getRootFontSize() { if (!rootFontSize) { var doc = document.documentElement; var fontSize = doc.style.fontSize || window.getComputedStyle(doc).fontSize; rootFontSize = parseFloat(fontSize); } return rootFontSize; } function convertRpx(value) { value = value.replace(/rpx/g, ""); var { windowWidth } = getSystemInfoSync(); var pixelRatio = 750 / windowWidth; return +value / pixelRatio; } function convertPx(value) { value = value.replace(/px/g, ""); return +value; } function convertRem(value) { value = value.replace(/rem/g, ""); return +value * getRootFontSize(); } function convertVw(value) { value = value.replace(/vw/g, ""); return +value * window.innerWidth / 100; } function convertVh(value) { value = value.replace(/vh/g, ""); return +value * window.innerHeight / 100; } export function unitToPx(value) { if (typeof value === "number") { return value; } if (value.includes("rpx")) { return convertRpx(value); } if (value.includes("px")) { return convertPx(value); } if (value.includes("rem")) { return convertRem(value); } if (value.includes("vw")) { return convertVw(value); } if (value.includes("vh")) { return convertVh(value); } return parseFloat(value); } //# sourceMappingURL=unit.js.map