Skip to content

Commit 3eb282a

Browse files
committed
docs: add api descriptions
1 parent 6f0dde2 commit 3eb282a

File tree

1 file changed

+226
-5
lines changed

1 file changed

+226
-5
lines changed

README.md

+226-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,23 @@
1616
<summary>Table of Contents</summary>
1717
<ol>
1818
<li><a href="#about-the-project">About The Project</a></li>
19-
<li><a href="#getting-started">Installation</a></li>
19+
<li>
20+
<a href="#getting-started">Getting Started</a>
21+
<ul>
22+
<li><a href="#peer-dependencies">Peer Dependencies</a></li>
23+
<li><a href="#installation">Installation</a></li>
24+
</ul>
25+
</li>
26+
<li><a href="#usage">Usage</a></li>
27+
<li>
28+
<a href="#api">API</a>
29+
<ul>
30+
<li><a href="#responsivescalabilityprovider">ResponsiveScalabilityProvider</li>
31+
<li><a href="#useresponsivescalability">useResponsiveScalability</li>
32+
<li><a href="#usescale">useScale</li>
33+
<li><a href="#usestyle">useStyle</li>
34+
</ul>
35+
</li>
2036
<li><a href="#contributing">Contributing</a></li>
2137
<li><a href="#license">License</a></li>
2238
<li><a href="#acknowledgments">Acknowledgments</a></li>
@@ -27,20 +43,225 @@
2743

2844
The package provides utility hooks that help React Native developers create responsive, cross-platform applications that are aware of orientation changes.
2945

30-
## Installation
46+
## Getting Started
3147

32-
### npm
48+
### Peer Dependencies
49+
50+
#### `react-native-safe-area-context`
3351

3452
```sh
35-
npm install react-native-responsive-scalability
53+
npm install react-native-safe-area-context
54+
```
55+
56+
```sh
57+
yarn add react-native-safe-area-context
3658
```
3759

38-
### yarn
60+
- (MacOS only) Install Cocoa packages
61+
62+
```sh
63+
npx pod-install
64+
```
65+
66+
Add `<SafeAreaProvider>` in your app root component.
67+
68+
```tsx
69+
import { SafeAreaProvider } from 'react-native-safe-area-context';
70+
71+
export const App = () => {
72+
return <SafeAreaProvider>...</SafeAreaProvider>;
73+
};
74+
```
75+
76+
### Installation
77+
78+
```sh
79+
npm install react-native-responsive-scalability
80+
```
3981

4082
```sh
4183
yarn add react-native-responsive-scalability
4284
```
4385

86+
- Add following rules to your eslint config file:
87+
88+
```json
89+
{
90+
"rules": {
91+
"react-hooks/exhaustive-deps": [
92+
"error",
93+
{
94+
"additionalHooks": "(useStyle)"
95+
}
96+
]
97+
}
98+
}
99+
```
100+
101+
<p align="right">(<a href="#title">back to top</a>)</p>
102+
103+
## Usage
104+
105+
For usage details, please refer to the `src` folder of the example app.
106+
107+
https://github.com/chsdwn/react-native-responsive-scalability/tree/main/example/src
108+
109+
<p align="right">(<a href="#title">back to top</a>)</p>
110+
111+
## API
112+
113+
### ResponsiveScalabilityProvider
114+
115+
You should to add `ResponsiveScalabilityProvider` in your app root component.
116+
117+
#### Props
118+
119+
`config`: Define your base device's dimensions, orientation, and breakpoints.
120+
121+
| Prop | Type | Default |
122+
| --------------- | -------- | ---------------------------------------------------: |
123+
| baseHeight | `number` | `430` |
124+
| baseWidth | `number` | `932` |
125+
| baseOrientation | `string` | `'portrait'` |
126+
| breakpoints | `object` | `{ sm: 640, md: 940, lg: undefined, xl: undefined }` |
127+
128+
#### Example
129+
130+
```tsx
131+
import React from 'React';
132+
import { SafeAreaProvider } from 'react-native-safe-area-context';
133+
import {
134+
IResponsiveScalabilityConfig,
135+
ResponsiveScalabilityProvider,
136+
} from 'react-native-responsive-scalability';
137+
138+
const config: IResponsiveScalabilityConfig = {
139+
// # Default device: iPhone 14 Pro Max.
140+
// baseWidth: 430,
141+
// baseHeight: 932,
142+
143+
// # "landscape" option can be selected. Default base orientation is "portrait".
144+
// baseOrientation: 'portrait',
145+
146+
breakpoints: {
147+
// # Default breakpoints
148+
// sm: 640,
149+
// md: 940,
150+
lg: 1280,
151+
xl: 1536,
152+
},
153+
};
154+
155+
export const App = () => {
156+
return (
157+
<SafeAreaProvider>
158+
<ResponsiveScalabilityProvider config={config}>
159+
...
160+
</ResponsiveScalabilityProvider>
161+
</SafeAreaProvider>
162+
);
163+
};
164+
```
165+
166+
### useResponsiveScalability
167+
168+
Use `useResponsiveScalability` hook to retrieve `baseHeight`, `baseWidth`, `baseOrientation`, and `breakpoints` values.
169+
170+
#### Example
171+
172+
```tsx
173+
import React from 'react';
174+
import { FlatList } from 'react-native';
175+
import { useSafeAreaFrame } from 'react-native-safe-area-context';
176+
import { useResponsiveScalability } from 'react-native-responsive-scalability';
177+
178+
export const Home = () => {
179+
const { width } = useSafeAreaFrame();
180+
const { baseHeight, baseWidth, baseOrientation, breakpoints } =
181+
useResponsiveScalability();
182+
183+
let numColumns = 1;
184+
if (width >= breakpoints.sm) numColumns = 2;
185+
186+
return (
187+
<FlatList
188+
key={`flatlist-column-${numColumns}`}
189+
numColumns={numColumns}
190+
...
191+
/>
192+
);
193+
};
194+
```
195+
196+
### useScale
197+
198+
The `useScale` hook returns scale utility functions `scaleByHeight` and `scaleByWidth`. The `scaleByHeight` function is NOT aware of orientation changes; it calculates window height based on the `baseOrientation` value and uses it for calculating scale changes.
199+
200+
#### Example
201+
202+
```ts
203+
import React from 'react';
204+
import { useScale } from 'react-native-responsive-scalability';
205+
206+
const size = 10;
207+
208+
export const Home = () => {
209+
const { scaleByHeight, scaleByWidth } = useScale();
210+
211+
console.log(`scaleByHeight(${size}) =`, scaleByHeight(size));
212+
console.log(`scaleByWidth(${size}) =`, scaleByWidth(size));
213+
214+
return <></>;
215+
};
216+
```
217+
218+
- On a iPhone 14 Pro Max device/simulator with default config values.
219+
220+
`Portrait Mode`
221+
222+
```
223+
scaleByHeight(16) = 16
224+
scaleByWidth(16) = 16
225+
```
226+
227+
`Landscape Mode`
228+
229+
```
230+
scaleByHeight(16) = 16
231+
scaleByWidth(16) = 17.333333333333332
232+
```
233+
234+
### useStyle
235+
236+
Use `useStyle` hook to memoize inline styles.
237+
238+
#### Example
239+
240+
```tsx
241+
import React from 'react';
242+
import { StyleSheet, Text as RNText } from 'react-native';
243+
import { useScale, useStyle } from 'react-native-responsive-scalability';
244+
245+
export const Text = () => {
246+
const { scaleByWidth } = useScale();
247+
248+
return (
249+
<RNText
250+
style={useStyle(
251+
() => [styles.text, { fontSize: scaleByWidth(16) }],
252+
[scaleByWidth],
253+
)}
254+
>
255+
React Native Responsive Scalability
256+
</RNText>
257+
);
258+
};
259+
260+
const styles = StyleSheet.create({
261+
text: { color: 'red' },
262+
});
263+
```
264+
44265
<p align="right">(<a href="#title">back to top</a>)</p>
45266

46267
## Contributing

0 commit comments

Comments
 (0)