react-native-use-responsive 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +111 -0
- package/package.json +26 -0
- package/src/index.ts +4 -0
- package/src/useBreakpoint.ts +104 -0
- package/src/useResponsive.ts +73 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Muhammad Suleman
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# react-native-use-responsive
|
|
2
|
+
|
|
3
|
+
> Responsive hooks for React Native — zero dependencies, TypeScript-first.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install react-native-use-responsive
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## Hooks
|
|
12
|
+
|
|
13
|
+
### `useBreakpoint()`
|
|
14
|
+
|
|
15
|
+
Detects the current screen size breakpoint. Updates automatically on rotation/resize.
|
|
16
|
+
|
|
17
|
+
```tsx
|
|
18
|
+
import { useBreakpoint } from 'react-native-use-responsive';
|
|
19
|
+
|
|
20
|
+
function MyComponent() {
|
|
21
|
+
const breakpoint = useBreakpoint();
|
|
22
|
+
// 'sm' | 'md' | 'lg' | 'xl'
|
|
23
|
+
|
|
24
|
+
const columns = breakpoint === 'sm' ? 1 : breakpoint === 'md' ? 2 : 3;
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
**Default breakpoints:** `sm: 640`, `md: 768`, `lg: 1024` (px)
|
|
29
|
+
|
|
30
|
+
Custom breakpoints:
|
|
31
|
+
|
|
32
|
+
```tsx
|
|
33
|
+
const breakpoint = useBreakpoint({ sm: 480, md: 720, lg: 1080 });
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
### `useResponsive(value)`
|
|
39
|
+
|
|
40
|
+
Returns a value based on the current breakpoint. Falls back to the nearest smaller breakpoint.
|
|
41
|
+
|
|
42
|
+
```tsx
|
|
43
|
+
import { useResponsive } from 'react-native-use-responsive';
|
|
44
|
+
|
|
45
|
+
function MyGrid() {
|
|
46
|
+
const columns = useResponsive({ sm: 1, md: 2, lg: 3, xl: 4 });
|
|
47
|
+
const padding = useResponsive({ sm: 8, md: 16, lg: 24 });
|
|
48
|
+
|
|
49
|
+
return <FlatList numColumns={columns} /* ... */ />;
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Also exports `resolveResponsive()` for use outside of components:
|
|
54
|
+
|
|
55
|
+
```tsx
|
|
56
|
+
import { resolveResponsive, useBreakpoint } from 'react-native-use-responsive';
|
|
57
|
+
|
|
58
|
+
const styles = {
|
|
59
|
+
fontSize: { sm: 14, md: 16, lg: 18 },
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
// Inside a render function
|
|
63
|
+
const breakpoint = useBreakpoint();
|
|
64
|
+
const font = resolveResponsive(styles.fontSize, breakpoint);
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
### `useOrientation()`
|
|
70
|
+
|
|
71
|
+
Returns `'portrait'` or `'landscape'`.
|
|
72
|
+
|
|
73
|
+
```tsx
|
|
74
|
+
import { useOrientation } from 'react-native-use-responsive';
|
|
75
|
+
|
|
76
|
+
function MyComponent() {
|
|
77
|
+
const orientation = useOrientation();
|
|
78
|
+
return orientation === 'landscape' ? <LandscapeUI /> : <PortraitUI />;
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
## API
|
|
85
|
+
|
|
86
|
+
### `useBreakpoint(customBreakpoints?)`
|
|
87
|
+
|
|
88
|
+
| Param | Type | Default |
|
|
89
|
+
|-------|------|---------|
|
|
90
|
+
| `customBreakpoints` | `{ sm?: number, md?: number, lg?: number }` | `{ sm: 640, md: 768, lg: 1024 }` |
|
|
91
|
+
|
|
92
|
+
Returns: `'sm' | 'md' | 'lg' | 'xl'`
|
|
93
|
+
|
|
94
|
+
### `useResponsive(value, customBreakpoints?)`
|
|
95
|
+
|
|
96
|
+
| Param | Type | Description |
|
|
97
|
+
|-------|------|-------------|
|
|
98
|
+
| `value` | `T \| { sm?: T, md?: T, lg?: T, xl?: T }` | Responsive value map |
|
|
99
|
+
| `customBreakpoints` | `{ sm?: number, ... }` | Optional custom breakpoints |
|
|
100
|
+
|
|
101
|
+
Returns: `T`
|
|
102
|
+
|
|
103
|
+
### `useOrientation()`
|
|
104
|
+
|
|
105
|
+
Returns: `'portrait' | 'landscape'`
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
## License
|
|
110
|
+
|
|
111
|
+
MIT © [Muhammad Suleman](https://github.com/MuhammadSuleman97)
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "react-native-use-responsive",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Responsive hooks for React Native — useBreakpoint, useResponsive, useOrientation",
|
|
5
|
+
"main": "src/index.ts",
|
|
6
|
+
"types": "src/index.ts",
|
|
7
|
+
"files": ["src", "README.md", "LICENSE"],
|
|
8
|
+
"keywords": [
|
|
9
|
+
"react-native", "hooks", "responsive", "breakpoint",
|
|
10
|
+
"orientation", "adaptive", "layout", "useBreakpoint",
|
|
11
|
+
"useResponsive", "useOrientation"
|
|
12
|
+
],
|
|
13
|
+
"author": "Muhammad Suleman",
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/MuhammadSuleman97/react-native-use-responsive"
|
|
18
|
+
},
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"react": ">=17.0.0",
|
|
21
|
+
"react-native": ">=0.64.0"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"typescript": "^5.0.0"
|
|
25
|
+
}
|
|
26
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from 'react';
|
|
2
|
+
import { Dimensions, ScaledSize } from 'react-native';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Standard breakpoints inspired by Tailwind CSS defaults.
|
|
6
|
+
* - sm: 640px (small phones in landscape, large phones)
|
|
7
|
+
* - md: 768px (tablets)
|
|
8
|
+
* - lg: 1024px (tablets in landscape, small laptops)
|
|
9
|
+
* - xl: 1280px (desktops)
|
|
10
|
+
*/
|
|
11
|
+
export type Breakpoint = 'sm' | 'md' | 'lg' | 'xl';
|
|
12
|
+
|
|
13
|
+
export interface BreakpointConfig {
|
|
14
|
+
sm: number;
|
|
15
|
+
md: number;
|
|
16
|
+
lg: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const DEFAULT_BREAKPOINTS: BreakpointConfig = {
|
|
20
|
+
sm: 640,
|
|
21
|
+
md: 768,
|
|
22
|
+
lg: 1024,
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
function getBreakpoint(width: number, config: BreakpointConfig): Breakpoint {
|
|
26
|
+
if (width >= config.lg) return 'xl';
|
|
27
|
+
if (width >= config.md) return 'lg';
|
|
28
|
+
if (width >= config.sm) return 'md';
|
|
29
|
+
return 'sm';
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* A hook that returns the current screen size breakpoint.
|
|
34
|
+
* Updates automatically when the screen dimensions change.
|
|
35
|
+
*
|
|
36
|
+
* @param customBreakpoints - Optional custom breakpoint thresholds
|
|
37
|
+
* @returns The current breakpoint ('sm' | 'md' | 'lg' | 'xl')
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```tsx
|
|
41
|
+
* const breakpoint = useBreakpoint();
|
|
42
|
+
* const columns = breakpoint === 'sm' ? 1 : breakpoint === 'md' ? 2 : 3;
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export function useBreakpoint(
|
|
46
|
+
customBreakpoints?: Partial<BreakpointConfig>,
|
|
47
|
+
): Breakpoint {
|
|
48
|
+
const config: BreakpointConfig = {
|
|
49
|
+
...DEFAULT_BREAKPOINTS,
|
|
50
|
+
...customBreakpoints,
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const [breakpoint, setBreakpoint] = useState<Breakpoint>(() =>
|
|
54
|
+
getBreakpoint(Dimensions.get('window').width, config),
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
const handleChange = useCallback(
|
|
58
|
+
({ window }: { window: ScaledSize }) => {
|
|
59
|
+
setBreakpoint(getBreakpoint(window.width, config));
|
|
60
|
+
},
|
|
61
|
+
[config],
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
useEffect(() => {
|
|
65
|
+
const subscription = Dimensions.addEventListener('change', handleChange);
|
|
66
|
+
return () => subscription.remove();
|
|
67
|
+
}, [handleChange]);
|
|
68
|
+
|
|
69
|
+
return breakpoint;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* A lightweight hook that returns only whether the device is in
|
|
74
|
+
* landscape or portrait orientation.
|
|
75
|
+
*
|
|
76
|
+
* @returns 'portrait' | 'landscape'
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```tsx
|
|
80
|
+
* const orientation = useOrientation();
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export function useOrientation(): 'portrait' | 'landscape' {
|
|
84
|
+
const [orientation, setOrientation] = useState<'portrait' | 'landscape'>(
|
|
85
|
+
() => {
|
|
86
|
+
const { width, height } = Dimensions.get('window');
|
|
87
|
+
return width > height ? 'landscape' : 'portrait';
|
|
88
|
+
},
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
useEffect(() => {
|
|
92
|
+
const subscription = Dimensions.addEventListener(
|
|
93
|
+
'change',
|
|
94
|
+
({ window }) => {
|
|
95
|
+
setOrientation(
|
|
96
|
+
window.width > window.height ? 'landscape' : 'portrait',
|
|
97
|
+
);
|
|
98
|
+
},
|
|
99
|
+
);
|
|
100
|
+
return () => subscription.remove();
|
|
101
|
+
}, []);
|
|
102
|
+
|
|
103
|
+
return orientation;
|
|
104
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import { useBreakpoint, type Breakpoint, type BreakpointConfig } from './useBreakpoint';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Type for responsive values — either a single value for all breakpoints
|
|
6
|
+
* or an object mapping breakpoint names to values.
|
|
7
|
+
*
|
|
8
|
+
* Missing breakpoints fall back to the nearest smaller one.
|
|
9
|
+
*/
|
|
10
|
+
export type ResponsiveValue<T> =
|
|
11
|
+
| T
|
|
12
|
+
| { [K in Breakpoint]?: T };
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Resolves a responsive value to the correct value for the current breakpoint.
|
|
16
|
+
* Falls back to the nearest smaller breakpoint if the current one isn't defined.
|
|
17
|
+
*
|
|
18
|
+
* @param value - A single value or a breakpoint-to-value map
|
|
19
|
+
* @param breakpoint - The current breakpoint
|
|
20
|
+
* @returns The resolved value
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```tsx
|
|
24
|
+
* const fontSize = resolveResponsive(
|
|
25
|
+
* { sm: 14, md: 16, lg: 18 },
|
|
26
|
+
* 'lg',
|
|
27
|
+
* ); // => 18
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export function resolveResponsive<T>(
|
|
31
|
+
value: ResponsiveValue<T>,
|
|
32
|
+
breakpoint: Breakpoint,
|
|
33
|
+
): T {
|
|
34
|
+
if (value === null || value === undefined || typeof value !== 'object') {
|
|
35
|
+
return value as T;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const map = value as Record<string, T>;
|
|
39
|
+
const order: Breakpoint[] = ['sm', 'md', 'lg', 'xl'];
|
|
40
|
+
const index = order.indexOf(breakpoint);
|
|
41
|
+
|
|
42
|
+
// Walk backwards through breakpoints to find a defined value
|
|
43
|
+
for (let i = index; i >= 0; i--) {
|
|
44
|
+
const bp = order[i];
|
|
45
|
+
if (bp in map && map[bp] !== undefined) {
|
|
46
|
+
return map[bp];
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return map.sm as T;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* A hook that returns a value based on the current breakpoint.
|
|
55
|
+
* Falls back to the nearest smaller breakpoint's value if undefined.
|
|
56
|
+
*
|
|
57
|
+
* @param value - An object mapping breakpoints to values, or a plain value
|
|
58
|
+
* @param customBreakpoints - Optional custom breakpoint thresholds
|
|
59
|
+
* @returns The resolved value for the current breakpoint
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```tsx
|
|
63
|
+
* const columns = useResponsive({ sm: 1, md: 2, lg: 3, xl: 4 });
|
|
64
|
+
* const padding = useResponsive({ sm: 8, md: 16, lg: 24 });
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export function useResponsive<T>(
|
|
68
|
+
value: ResponsiveValue<T>,
|
|
69
|
+
customBreakpoints?: Partial<BreakpointConfig>,
|
|
70
|
+
): T {
|
|
71
|
+
const breakpoint = useBreakpoint(customBreakpoints);
|
|
72
|
+
return useMemo(() => resolveResponsive(value, breakpoint), [value, breakpoint]);
|
|
73
|
+
}
|