Created
June 19, 2023 07:18
-
-
Save bryanltobing/1ed535d82f31f67de1816d1be62f86b2 to your computer and use it in GitHub Desktop.
Responsive unit size for react native using `Dimensions` Api
This file contains 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 { Dimensions } from 'react-native'; | |
const { width, height } = Dimensions.get('window'); | |
const guidelineBaseWidth = 360; | |
const guidelineBaseHeight = 724; | |
/** | |
* | |
* @param size number | |
* @returns responsive horizontal number size | |
* @description usage: height, marginTop, marginBottom, marginVertical, lineHeight, paddingTop, paddingBottom, padingVertical | |
*/ | |
const horizontalScale = (size: number) => (width / guidelineBaseWidth) * size; | |
/** | |
* | |
* @param size | |
* @returns responsive vertical number size | |
* @description usage: width, marginLeft, marginRight, marginHorizontal, paddingLeft, paddingRight, padingHorizontal | |
*/ | |
const verticalScale = (size: number) => (height / guidelineBaseHeight) * size; | |
/** | |
* | |
* @param size number | |
* @param factor number | |
* @returns usage: fontSize, borderRadius | |
*/ | |
const moderateScale = (size: number, factor = 0.5) => size + (horizontalScale(size) - size) * factor; | |
export { horizontalScale, verticalScale, moderateScale }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment