📐📱 React Native Responsive Dimensions 🌐📏
Responsive height, width and responsive fontSize for your react native components!
The dimensions auto adjust based on the window size (view port) or the screen size of the device 🙌🏽
Support for responsive dimension hooks to help auto-adjust dimensions for devices whose display or screen sizes may change such as foldable phones or browser windows! 😎
Compatible with Expo & React Native Web 🚀
PRs Welcome 👍✨
#npmnpm install --save react-native-responsive-dimensions#yarnyarn add react-native-responsive-dimensions
While working with mobile devices, there are two kinds of dimensions you will have to focus on
- Window Dimensions ﹣ which is the size of the window / view port on which your app is being displayed
- Screen Dimensions ﹣ this is the total screen dimensions of your device (your app may occupy the entire screen or only a portion of the screen)
Working with Window Dimensions
;;const styles = StyleSheet;
Working with Screen Dimensions
;;const styles = StyleSheet;
The above responsive dimension methods do not auto update once the value is set. They are suitable for using within
StyleSheet.create method as the values don't change once set. However, you might want your views to respond to dimension changes such as screen rotation, device folding (in foldable devices) & browser window resizing (react-native-web).
The values set by these hooks auto respond to changes. The following hooks are available for use ﹣
import React from "react";import View from "react-native";importuseResponsiveHeightuseResponsiveWidthfrom "react-native-responsive-dimensions";const App =const height = ;const width = ;return <View = />;;
Reacting to dimension changes with
useDimensionsChange basically calls a function whenever the dimensions update with new window & screen dimensions as arguments. This is a good place to include your layout animations if your UI layout reacts to dimension updates and you want to make the transitions smooth.
import React useCallback from "react";import View LayoutAnimation from "react-native";importuseResponsiveHeightuseResponsiveWidthuseDimensionsChangefrom "react-native-responsive-dimensions";const App =const height = ;const width = ;;return <View = />;;
Why Responsive Dimensions
I built responsive dimensions as a personal tool to tackle some of my problems I face during my everyday app development. You might want to use it if your usecase comes under one of the following scenarios.
While working with React Native UI (especially animations) there are lots of scenarios that require calculating a certain percentage of the display area.
If your app supports tablets then you might want to scale some of your fonts & UI Components based on the display size.
If you are using react-native-web or targetting foldable devices your UI needs to react to the changes in the window dimensions.