Last active
December 23, 2019 14:51
-
-
Save mmazzarolo/daa05789c18378f8929f810a625fb1b7 to your computer and use it in GitHub Desktop.
React-Native metrics scaling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { PixelRatio, Dimensions } from "react-native"; | |
const isTabletLike = () => { | |
const pixelDensity = PixelRatio.get(); | |
const adjustedWidth = Dimensions.get("screen").width * pixelDensity; | |
const adjustedHeight = Dimensions.get("screen").height * pixelDensity; | |
return ( | |
(pixelDensity < 2 && (adjustedWidth >= 1000 || adjustedHeight >= 1000)) || | |
(pixelDensity === 2 && (adjustedWidth >= 1920 || adjustedHeight >= 1920)) | |
); | |
}; | |
export const scale = (size: number) => { | |
const guidelineBaseWidth = isTabletLike() ? 520 : 350; | |
return (Dimensions.get("screen").width / guidelineBaseWidth) * size; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment