Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit 76174c0

Browse files
committed
updated readme
1 parent 1aac162 commit 76174c0

File tree

1 file changed

+135
-19
lines changed

1 file changed

+135
-19
lines changed

README.md

Lines changed: 135 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,40 @@
1-
# react-native-responsive-dimensions
1+
<div align="center">
22

3-
Responsive height, width and responsive fontSize for react native components that automatically adjust themselves based on the screen size of the device. The dimensions can be used for any components like View, Image, Text etc.
3+
# 📐📱 React Native Responsive Dimensions 🌐📏
44

5-
##### Responsive Font Size uses only the width of the device and auto adjusts to the screen. It will not depend on Height due to different aspect ratio across many devices.
5+
Responsive height, width and responsive fontSize for your react native components!
66

7-
Installation
7+
The dimensions auto adjust based on the window size (view port) or the screen size of the device ✨
8+
9+
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!
10+
11+
<!-- [![Build Status][build-badge]][build]
12+
[![Maintainability][maintainability-badge]][maintainability-url]
13+
[![Test Coverage][coverage-badge]][coverage-url] -->
14+
15+
[![Version][version-badge]][package]
16+
[![Downloads][downloads-badge]][npmtrends]
17+
[![Bundlephobia][bundle-phobia-badge]][bundle-phobia]
18+
19+
[![Star on GitHub][github-star-badge]][github-star]
20+
[![Watch on GitHub][github-watch-badge]][github-watch]
21+
[![Twitter Follow][twitter-badge]][twitter]
22+
23+
---
24+
25+
### Compatible with Expo & React Native Web 🚀
26+
27+
### PRs Welcome 👍✨
28+
29+
</div>
30+
31+
- 📦 [Installation](#installation)
32+
- ℹ️ [Usage](#usage)
33+
- 🎣 [Responsive Hooks](#responsive-hooks)
34+
- 💡 [Examples](#examples)
35+
-[Why Responsive Dimensions?](#why-responsive-dimensions)
36+
37+
## Installation
838

939
```sh
1040
#npm
@@ -14,7 +44,14 @@ npm install --save react-native-responsive-dimensions
1444
yarn add react-native-responsive-dimensions
1545
```
1646

17-
The below snippet will create styles with a container of dynamic height, width and a sample text with dynamic font-size relative to the screen size of the device the app is run.
47+
## Usage
48+
49+
While working with mobile devices, there are two kinds of dimensions you will have to focus on
50+
51+
- Window Dimensions ﹣ which is the size of the window / view port on which your app is being displayed
52+
- 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)
53+
54+
### Working with Window Dimensions
1855

1956
```js
2057
import { StyleSheet } from "react-native";
@@ -27,32 +64,111 @@ import {
2764
const styles = StyleSheet.create({
2865
container: {
2966
justifyContent: "center",
30-
height: responsiveHeight(50), // 50% of screen height
31-
width: responsiveWidth(50), // 50% of screen width
67+
height: responsiveHeight(50), // 50% of window height
68+
width: responsiveWidth(50), // 50% of window width
3269
alignItems: "center"
3370
},
3471
sampleText: {
35-
fontSize: responsiveFontSize(2) // 2% of total screen size
72+
fontSize: responsiveFontSize(2) // 2% of total window size
3673
}
3774
});
3875
```
3976

40-
This will create a container(view/image) with height that is exactly 50% of the device's screen-height and width exactly 50% of the device's screen-height and a font with fontSize that occupies exactly 2% of the screen size.
77+
### Working with Screen Dimensions
4178

42-
## Major Update - Responsive Hooks are here!!
79+
```js
80+
import { StyleSheet } from "react-native";
81+
import {
82+
responsiveScreenHeight,
83+
responsiveScreenWidth,
84+
responsiveScreenFontSize
85+
} from "react-native-responsive-dimensions";
4386

44-
One of the most important feedback received from the community is that handling responsive height and width when the device orientation changes is a huge pain point. Also, we have a new lineup of foldable devices whose dimensions are going to by dynamic.
87+
const styles = StyleSheet.create({
88+
container: {
89+
justifyContent: "center",
90+
height: responsiveScreenHeight(50), // 50% of Screen height
91+
width: responsiveScreenWidth(50), // 50% of Screen width
92+
alignItems: "center"
93+
},
94+
sampleText: {
95+
fontSize: responsiveScreenFontSize(2) // 2% of total screen size
96+
}
97+
});
98+
```
99+
100+
## Responsive hooks
101+
102+
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).
103+
104+
The values set by these hooks auto respond to changes. The following hooks are available for use ﹣
105+
106+
- `useResponsiveHeight`
107+
- `useResponsiveWidth`
108+
- `useResponsiveFontSize`
109+
- `useResponsiveScreenHeight`
110+
- `useResponsiveScreenWidth`
111+
- `useResponsiveScreenFontSize`
112+
- `useDimensionsChange`
113+
114+
### Sample Usage
45115

46-
To address this, taking advantage of the new React Hooks API, responsive dimensions now include three custom hooks `useResponsiveHeight`, `useResponsiveWidth`, `useResponsiveFontSize` which will automatically re-calculate the responsive height, width and font size every time the device orientation or the display size changes.
116+
```jsx
117+
import React from "react";
118+
import { View } from "react-native";
119+
import {
120+
useResponsiveHeight,
121+
useResponsiveWidth
122+
} from "react-native-responsive-dimensions";
123+
124+
const App = () => {
125+
const height = useResponsiveHeight(25);
126+
const width = useResponsiveWidth(25);
127+
128+
return <View style={{ height, width }} />;
129+
};
130+
```
47131

48-
An example project has been included in [RNResponsiveDimensionsExample](https://github.com/DaniAkash/react-native-responsive-dimensions/tree/master/RNResponsiveDimensionsExample) directory which will explain how to use the new custom hooks and how they will differ from the usual responsive functions.
132+
### React to dimension changes with `useDimensionsChange`
49133

50-
In portrait mode, both of them will produce the same result:
51-
![responsive-dimensions](./assets/Screenshot%202019-10-02%20at%2010.39.44%20PM.png)
134+
`useDimensionsChange` basically calls a function whenever the dimensions update. This is a good place to include your layoutanimations if your UI react to dimension updates and you want to make the transitions smooth.
52135

53-
However, in landscape mode, only the views using responsive hooks will be updated as per the new height & width:
54-
![responsive-dimensions](./assets/Screenshot%202019-10-02%20at%2010.39.51%20PM.png)
136+
```jsx
137+
import React from "react";
138+
import { View, LayoutAnimation } from "react-native";
139+
import {
140+
useResponsiveHeight,
141+
useResponsiveWidth,
142+
useDimensionsChange
143+
} from "react-native-responsive-dimensions";
144+
145+
const App = () => {
146+
const height = useResponsiveHeight(25);
147+
const width = useResponsiveWidth(25);
55148

56-
## License
149+
useDimensionsChange(() => {
150+
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
151+
});
152+
153+
return <View style={{ height, width }} />;
154+
};
155+
```
57156

58-
MIT
157+
[build]: https://github.com/DaniAkash/react-native-responsive-dimensions/actions
158+
[build-badge]: https://github.com/daniakash/react-native-responsive-dimensions/workflows/build/badge.svg
159+
[coverage-badge]: https://api.codeclimate.com/v1/badges/f7954c1e1686cabeeb97/test_coverage
160+
[coverage-url]: https://codeclimate.com/github/DaniAkash/react-native-responsive-dimensions/test_coverage
161+
[maintainability-badge]: https://api.codeclimate.com/v1/badges/f7954c1e1686cabeeb97/maintainability
162+
[maintainability-url]: https://codeclimate.com/github/DaniAkash/react-native-responsive-dimensions/maintainability
163+
[bundle-phobia-badge]: https://badgen.net/bundlephobia/minzip/react-native-responsive-dimensions
164+
[bundle-phobia]: https://bundlephobia.com/result?p=react-native-responsive-dimensions
165+
[downloads-badge]: https://img.shields.io/npm/dm/react-native-responsive-dimensions.svg?style=flat-square
166+
[npmtrends]: http://www.npmtrends.com/react-native-responsive-dimensions
167+
[package]: https://www.npmjs.com/package/react-native-responsive-dimensions
168+
[version-badge]: https://img.shields.io/npm/v/react-native-responsive-dimensions.svg?style=flat-square
169+
[twitter]: https://twitter.com/dani_akash_
170+
[twitter-badge]: https://img.shields.io/twitter/follow/dani_akash_?style=social
171+
[github-watch-badge]: https://img.shields.io/github/watchers/DaniAkash/react-native-responsive-dimensions.svg?style=social
172+
[github-watch]: https://github.com/DaniAkash/react-native-responsive-dimensions/watchers
173+
[github-star-badge]: https://img.shields.io/github/stars/DaniAkash/react-native-responsive-dimensions.svg?style=social
174+
[github-star]: https://github.com/DaniAkash/react-native-responsive-dimensions/stargazers

0 commit comments

Comments
 (0)