react-native-everything-responsive 0.0.0-stage → 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/CHANGELOG.md +16 -0
- package/LICENSE +21 -0
- package/README.md +459 -2
- package/docs/API.md +311 -0
- package/lib/core/breakpoints.d.ts +28 -0
- package/lib/core/breakpoints.d.ts.map +1 -0
- package/lib/core/breakpoints.js +108 -0
- package/lib/core/breakpoints.js.map +1 -0
- package/lib/core/config.d.ts +24 -0
- package/lib/core/config.d.ts.map +1 -0
- package/lib/core/config.js +112 -0
- package/lib/core/config.js.map +1 -0
- package/lib/core/device.d.ts +31 -0
- package/lib/core/device.d.ts.map +1 -0
- package/lib/core/device.js +35 -0
- package/lib/core/device.js.map +1 -0
- package/lib/core/grid.d.ts +30 -0
- package/lib/core/grid.d.ts.map +1 -0
- package/lib/core/grid.js +37 -0
- package/lib/core/grid.js.map +1 -0
- package/lib/core/info.d.ts +26 -0
- package/lib/core/info.d.ts.map +1 -0
- package/lib/core/info.js +41 -0
- package/lib/core/info.js.map +1 -0
- package/lib/core/layout.d.ts +4 -0
- package/lib/core/layout.d.ts.map +1 -0
- package/lib/core/layout.js +31 -0
- package/lib/core/layout.js.map +1 -0
- package/lib/core/scaling.d.ts +28 -0
- package/lib/core/scaling.d.ts.map +1 -0
- package/lib/core/scaling.js +47 -0
- package/lib/core/scaling.js.map +1 -0
- package/lib/core/types.d.ts +153 -0
- package/lib/core/types.d.ts.map +1 -0
- package/lib/core/types.js +8 -0
- package/lib/core/types.js.map +1 -0
- package/lib/index.d.ts +26 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +54 -0
- package/lib/index.js.map +1 -0
- package/lib/native/ResponsiveGrid.d.ts +61 -0
- package/lib/native/ResponsiveGrid.d.ts.map +1 -0
- package/lib/native/ResponsiveGrid.js +110 -0
- package/lib/native/ResponsiveGrid.js.map +1 -0
- package/lib/native/ResponsiveProvider.d.ts +19 -0
- package/lib/native/ResponsiveProvider.d.ts.map +1 -0
- package/lib/native/ResponsiveProvider.js +26 -0
- package/lib/native/ResponsiveProvider.js.map +1 -0
- package/lib/native/ResponsiveView.d.ts +30 -0
- package/lib/native/ResponsiveView.d.ts.map +1 -0
- package/lib/native/ResponsiveView.js +51 -0
- package/lib/native/ResponsiveView.js.map +1 -0
- package/lib/native/Visibility.d.ts +45 -0
- package/lib/native/Visibility.d.ts.map +1 -0
- package/lib/native/Visibility.js +53 -0
- package/lib/native/Visibility.js.map +1 -0
- package/lib/native/context.d.ts +6 -0
- package/lib/native/context.d.ts.map +1 -0
- package/lib/native/context.js +12 -0
- package/lib/native/context.js.map +1 -0
- package/lib/native/createStyles.d.ts +46 -0
- package/lib/native/createStyles.d.ts.map +1 -0
- package/lib/native/createStyles.js +68 -0
- package/lib/native/createStyles.js.map +1 -0
- package/lib/native/hooks.d.ts +33 -0
- package/lib/native/hooks.d.ts.map +1 -0
- package/lib/native/hooks.js +68 -0
- package/lib/native/hooks.js.map +1 -0
- package/lib/native/platform.d.ts +10 -0
- package/lib/native/platform.d.ts.map +1 -0
- package/lib/native/platform.js +50 -0
- package/lib/native/platform.js.map +1 -0
- package/lib/native/snapshot.d.ts +8 -0
- package/lib/native/snapshot.d.ts.map +1 -0
- package/lib/native/snapshot.js +29 -0
- package/lib/native/snapshot.js.map +1 -0
- package/lib/native/store.d.ts +14 -0
- package/lib/native/store.d.ts.map +1 -0
- package/lib/native/store.js +51 -0
- package/lib/native/store.js.map +1 -0
- package/package.json +50 -4
- package/src/core/breakpoints.ts +101 -0
- package/src/core/config.ts +130 -0
- package/src/core/device.ts +46 -0
- package/src/core/grid.ts +54 -0
- package/src/core/info.ts +64 -0
- package/src/core/layout.ts +44 -0
- package/src/core/scaling.ts +62 -0
- package/src/core/types.ts +178 -0
- package/src/index.ts +59 -0
- package/src/native/ResponsiveGrid.tsx +159 -0
- package/src/native/ResponsiveProvider.tsx +30 -0
- package/src/native/ResponsiveView.tsx +98 -0
- package/src/native/Visibility.tsx +82 -0
- package/src/native/context.ts +11 -0
- package/src/native/createStyles.ts +94 -0
- package/src/native/hooks.ts +102 -0
- package/src/native/platform.ts +54 -0
- package/src/native/snapshot.ts +27 -0
- package/src/native/store.ts +58 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 1.0.0
|
|
4
|
+
|
|
5
|
+
Initial release.
|
|
6
|
+
|
|
7
|
+
- Breakpoints with a mobile-first cascade, `base` alias and upward fallback
|
|
8
|
+
- `useResponsive`, `useBreakpoint` (selector-optimised), `useResponsiveValue`, `useResponsiveConfig`
|
|
9
|
+
- `createStyles` / `useResponsiveStyles` with type-safe responsive style values
|
|
10
|
+
- Container queries via `<ResponsiveView>`
|
|
11
|
+
- `<Show>` / `<Hide>` with breakpoint, device, orientation and custom conditions, plus `mode="hide"`
|
|
12
|
+
- `<ResponsiveGrid>` and `useGridLayout` (fixed and auto-fit columns, pixel-exact widths, RTL)
|
|
13
|
+
- Orientation-stable, clamped, pixel-rounded scaling (`s`, `vs`, `ms`, `fs`, `wp`, `hp`)
|
|
14
|
+
- Accessible font sizing with a configurable cap (`maxFontScale`, default 2)
|
|
15
|
+
- Device classification for iOS, Android (incl. foldables), web, Windows, macOS and TV
|
|
16
|
+
- `getResponsive()` for non-reactive access
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 react-native-everything-responsive contributors
|
|
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
CHANGED
|
@@ -1,3 +1,460 @@
|
|
|
1
|
-
#
|
|
1
|
+
# ReactNativeEverythingResponsive
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`npm install react-native-everything-responsive`
|
|
4
|
+
|
|
5
|
+
**Zero-dependency responsive design for React Native.**
|
|
6
|
+
One small toolkit that makes the same UI work on small phones, large phones, foldables, tablets, split-screen, desktops and the web.
|
|
7
|
+
|
|
8
|
+
- **0 dependencies.** It only needs `react` and `react-native`, which your app already has.
|
|
9
|
+
- **Container queries.** Components adapt to the space they are given, not just to the screen.
|
|
10
|
+
- **Correct device detection.** An iPad in split view is still a tablet, but gets a phone layout.
|
|
11
|
+
- **Safe scaling.** It is stable across orientation changes, clamped on large screens and snapped to the pixel grid.
|
|
12
|
+
- **Accessible fonts.** It honours the user's text-size setting up to a cap you control (default 200%, per WCAG).
|
|
13
|
+
- **Fast.** One shared listener for the whole app. Breakpoint hooks re-render only when the breakpoint changes.
|
|
14
|
+
- **Fully typed.** Responsive style values are type-checked, and literal types such as `'row'` are kept.
|
|
15
|
+
|
|
16
|
+
```tsx
|
|
17
|
+
import { createStyles, Show, ResponsiveGrid } from 'react-native-everything-responsive';
|
|
18
|
+
|
|
19
|
+
const useStyles = createStyles((r) => ({
|
|
20
|
+
page: { padding: r.s(16), flexDirection: { xs: 'column', lg: 'row' } },
|
|
21
|
+
title: { fontSize: r.fs(24) },
|
|
22
|
+
}));
|
|
23
|
+
|
|
24
|
+
export function Shop() {
|
|
25
|
+
const styles = useStyles();
|
|
26
|
+
return (
|
|
27
|
+
<View style={styles.page}>
|
|
28
|
+
<Show from="lg"><Sidebar /></Show>
|
|
29
|
+
<ResponsiveGrid minItemWidth={200} gap={16}>
|
|
30
|
+
{products.map((p) => <ProductCard key={p.id} product={p} />)}
|
|
31
|
+
</ResponsiveGrid>
|
|
32
|
+
</View>
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## Table of contents
|
|
40
|
+
|
|
41
|
+
1. [The problem this solves](#1-the-problem-this-solves)
|
|
42
|
+
2. [Installation](#2-installation)
|
|
43
|
+
3. [Quick start](#3-quick-start)
|
|
44
|
+
4. [Core concepts](#4-core-concepts)
|
|
45
|
+
5. [Guides](#5-guides)
|
|
46
|
+
6. [Performance](#6-performance)
|
|
47
|
+
7. [Compatibility](#7-compatibility)
|
|
48
|
+
8. [FAQ and troubleshooting](#8-faq-and-troubleshooting)
|
|
49
|
+
9. [Full API reference](docs/API.md)
|
|
50
|
+
|
|
51
|
+
---
|
|
52
|
+
|
|
53
|
+
## 1. The problem this solves
|
|
54
|
+
|
|
55
|
+
React Native gives you `Dimensions` and `useWindowDimensions`, but it gives you nothing for *responsive design*. The common workarounds have known failure modes.
|
|
56
|
+
|
|
57
|
+
| Common approach | What goes wrong | How this library handles it |
|
|
58
|
+
|---|---|---|
|
|
59
|
+
| `width / 375 * size` scaling | Rotating a phone makes everything roughly 2× larger. On a 1366 dp iPad Pro, UI becomes 3.6× larger. | Scales by the **short side**, so orientation does not matter, and clamps the factor (default 0.85–1.35×). |
|
|
60
|
+
| `if (width > 768)` checks spread through the code | Duplicated logic and inconsistent thresholds | Named **breakpoints** with a mobile-first cascade, configured once. |
|
|
61
|
+
| `isTablet = width >= 600` | An iPad in 1/3 split view is "not a tablet", and a resized desktop browser becomes a "phone". | Separates **hardware** (`deviceType`) from **layout** (`breakpoint`). |
|
|
62
|
+
| Using `Dimensions.get('screen')` for layout | Ignores split-screen, Android multi-window and resizable desktop windows | Layout always uses the **window** or a **container**. |
|
|
63
|
+
| Media queries based only on the window | A card can't tell whether it is in a 300 dp sidebar or a 900 dp main column. | **Container queries** through `<ResponsiveView>` |
|
|
64
|
+
| `width: '33.33%'` grid cells | Rounding on 2.625× / 2.75× Android screens makes a row about 0.5 px too wide, so the last item wraps. | Widths are **floored to physical pixels**. Tested across 7 densities and 6 column counts. |
|
|
65
|
+
| Ignoring the system font scale | At "Largest" text size, buttons overflow and labels clip. | `fs()` honours the user's setting up to `maxFontScale`. |
|
|
66
|
+
| Each component subscribing to `Dimensions` | Hundreds of listeners, and a full-tree re-render on every pixel of a window drag | **One** shared subscription. `useBreakpoint` only re-renders when the breakpoint changes. |
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
## 2. Installation
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
npm install react-native-everything-responsive
|
|
74
|
+
# or
|
|
75
|
+
yarn add react-native-everything-responsive
|
|
76
|
+
# or
|
|
77
|
+
npx expo install react-native-everything-responsive
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Requirements
|
|
81
|
+
|
|
82
|
+
| Peer | Minimum version | Why |
|
|
83
|
+
|---|---|---|
|
|
84
|
+
| `react` | 18.0 | `useSyncExternalStore` |
|
|
85
|
+
| `react-native` | 0.70 | `Dimensions.addEventListener` returns a subscription |
|
|
86
|
+
| TypeScript (optional) | 5.0 | `const` type parameters, used for literal inference |
|
|
87
|
+
|
|
88
|
+
No native code, no linking and no `pod install` are needed. It works in Expo Go.
|
|
89
|
+
|
|
90
|
+
---
|
|
91
|
+
|
|
92
|
+
## 3. Quick start
|
|
93
|
+
|
|
94
|
+
### Step 1: no setup required
|
|
95
|
+
|
|
96
|
+
Every hook and component works without a provider. Add `<ResponsiveProvider>` only when you want to change the defaults:
|
|
97
|
+
|
|
98
|
+
```tsx
|
|
99
|
+
import { ResponsiveProvider } from 'react-native-everything-responsive';
|
|
100
|
+
|
|
101
|
+
export default function App() {
|
|
102
|
+
return (
|
|
103
|
+
<ResponsiveProvider config={{ guidelineBaseWidth: 390, guidelineBaseHeight: 844 }}>
|
|
104
|
+
<RootNavigator />
|
|
105
|
+
</ResponsiveProvider>
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Step 2: pick the tool for the job
|
|
111
|
+
|
|
112
|
+
| You want to… | Use |
|
|
113
|
+
|---|---|
|
|
114
|
+
| Write styles that change per breakpoint | `createStyles` |
|
|
115
|
+
| Read breakpoint, device type or sizes in a component | `useResponsive()` |
|
|
116
|
+
| Choose one value per breakpoint | `useResponsiveValue({ xs: 1, md: 2 })` |
|
|
117
|
+
| Show or hide parts of the UI | `<Show>` / `<Hide>` |
|
|
118
|
+
| Lay out cards in columns | `<ResponsiveGrid>` or `useGridLayout` (FlatList) |
|
|
119
|
+
| Make a component adapt to its parent's width | `<ResponsiveView>` |
|
|
120
|
+
| Read values outside React | `getResponsive()` |
|
|
121
|
+
|
|
122
|
+
### Step 3: write responsive styles
|
|
123
|
+
|
|
124
|
+
```tsx
|
|
125
|
+
const useStyles = createStyles((r) => ({
|
|
126
|
+
container: {
|
|
127
|
+
padding: r.s(16), // scaled spacing
|
|
128
|
+
flexDirection: { xs: 'column', md: 'row' }, // per-breakpoint value
|
|
129
|
+
gap: { xs: 8, lg: 24 },
|
|
130
|
+
},
|
|
131
|
+
heading: {
|
|
132
|
+
fontSize: r.fs(22), // accessible font size
|
|
133
|
+
textAlign: r.isTablet ? 'center' : 'left', // device-based decision
|
|
134
|
+
},
|
|
135
|
+
}));
|
|
136
|
+
|
|
137
|
+
function Header() {
|
|
138
|
+
const styles = useStyles();
|
|
139
|
+
return (
|
|
140
|
+
<View style={styles.container}>
|
|
141
|
+
<Text style={styles.heading}>Welcome</Text>
|
|
142
|
+
</View>
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
## 4. Core concepts
|
|
150
|
+
|
|
151
|
+
### 4.1 Breakpoints
|
|
152
|
+
|
|
153
|
+
A **breakpoint** is a named width range. All checks use **dp** (density-independent pixels), the unit React Native uses for layout.
|
|
154
|
+
|
|
155
|
+
| Key | From (dp) | Typical targets |
|
|
156
|
+
|---|---|---|
|
|
157
|
+
| `xs` | 0 | Small phones (iPhone SE, compact Android) |
|
|
158
|
+
| `sm` | 360 | Most phones in portrait |
|
|
159
|
+
| `md` | 600 | Small tablets, unfolded foldables, phones in landscape |
|
|
160
|
+
| `lg` | 900 | Tablets in landscape, iPad Pro in portrait |
|
|
161
|
+
| `xl` | 1200 | Laptops, desktop windows |
|
|
162
|
+
| `xxl` | 1536 | Large monitors |
|
|
163
|
+
|
|
164
|
+
**Mobile-first cascade.** A value applies from its breakpoint *upward*, until a larger key overrides it:
|
|
165
|
+
|
|
166
|
+
```ts
|
|
167
|
+
{ xs: 1, md: 2, xl: 4 }
|
|
168
|
+
// xs → 1, sm → 1, md → 2, lg → 2, xl → 4, xxl → 4
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
Resolution order:
|
|
172
|
+
1. The current breakpoint, or the nearest smaller one that has a value.
|
|
173
|
+
2. Otherwise `base` (an alias for "the default").
|
|
174
|
+
3. Otherwise the nearest *larger* one. A missing `xs` therefore never produces `undefined`.
|
|
175
|
+
|
|
176
|
+
**Comparison helpers** (on `useResponsive()` and as `<Show>` props):
|
|
177
|
+
|
|
178
|
+
| Helper | Meaning | Example (current = `md`) |
|
|
179
|
+
|---|---|---|
|
|
180
|
+
| `from('md')` | current ≥ md | `true` |
|
|
181
|
+
| `below('md')` | current < md | `false` |
|
|
182
|
+
| `between('sm', 'lg')` | sm ≤ current < lg | `true` |
|
|
183
|
+
| `only('md', 'xl')` | current is one of these | `true` |
|
|
184
|
+
|
|
185
|
+
### 4.2 Device type vs. layout
|
|
186
|
+
|
|
187
|
+
Two different questions often get mixed up:
|
|
188
|
+
|
|
189
|
+
| Question | Answered by | Based on | Use it for |
|
|
190
|
+
|---|---|---|---|
|
|
191
|
+
| *What hardware is this?* | `deviceType`, `isTablet`, … | The physical **screen** and platform | Input style, hover effects, haptics, analytics |
|
|
192
|
+
| *How much space do I have?* | `breakpoint`, `width`, `from()` | The **window** or **container** | Layout: columns, sidebars, sizes |
|
|
193
|
+
|
|
194
|
+
> **Rule of thumb:** use breakpoints for layout and device type for behaviour.
|
|
195
|
+
|
|
196
|
+
**Example:** an iPad running your app in 1/3 split view (320 dp wide):
|
|
197
|
+
- `deviceType === 'tablet'`, because the hardware is a tablet.
|
|
198
|
+
- `breakpoint === 'xs'`, because the window is phone-sized, so you get the phone layout.
|
|
199
|
+
|
|
200
|
+
How each platform is classified:
|
|
201
|
+
|
|
202
|
+
| Platform | Rule |
|
|
203
|
+
|---|---|
|
|
204
|
+
| iOS | `Platform.isPad` → `tablet`, otherwise `phone` |
|
|
205
|
+
| Android | Shortest screen side ≥ 600 dp → `tablet` (the Android `sw600dp` convention; unfolded foldables count as tablets) |
|
|
206
|
+
| Web | Fine pointer (mouse) and long side ≥ 1024 → `desktop`; otherwise short side ≥ 600 → `tablet`; otherwise `phone` |
|
|
207
|
+
| Windows / macOS | `desktop` |
|
|
208
|
+
| TV | `tv` |
|
|
209
|
+
|
|
210
|
+
### 4.3 Measurement source: window or container
|
|
211
|
+
|
|
212
|
+
Every hook and component accepts `source`:
|
|
213
|
+
|
|
214
|
+
| `source` | Measures | When to use |
|
|
215
|
+
|---|---|---|
|
|
216
|
+
| `'auto'` (default) | Nearest `<ResponsiveView>` if any, otherwise the window | Almost always |
|
|
217
|
+
| `'window'` | The app window | Page-level decisions such as navigation layout |
|
|
218
|
+
| `'container'` | Nearest `<ResponsiveView>` (warns in development if there is none) | When you need to be explicit |
|
|
219
|
+
|
|
220
|
+
**Analogy:** the window is the size of the room; a container is the size of the shelf a component sits on. A book (component) should fit the shelf, not the room.
|
|
221
|
+
|
|
222
|
+
### 4.4 Scaling
|
|
223
|
+
|
|
224
|
+
Scaling adjusts sizes for screen density and size, relative to the device your design was drawn for (`guidelineBaseWidth × guidelineBaseHeight`, default 375 × 812).
|
|
225
|
+
|
|
226
|
+
| Function | Formula | Use for |
|
|
227
|
+
|---|---|---|
|
|
228
|
+
| `s(n)` | `n × clamp(shortSide / 375)` | Widths, icons, horizontal spacing |
|
|
229
|
+
| `vs(n)` | `n × clamp(longSide / 812)` | Heights, vertical spacing |
|
|
230
|
+
| `ms(n, f = 0.5)` | `n + (s(n) − n) × f` | Sizes that should grow only partially |
|
|
231
|
+
| `fs(n, f = 0.5)` | `ms(n, f)`, then font-scale compensation | Font sizes |
|
|
232
|
+
| `wp(p)` / `hp(p)` | `p %` of the measured width or height | Proportional sizes |
|
|
233
|
+
|
|
234
|
+
All results are rounded to the nearest **physical pixel**, which avoids blurry hairlines and 1 px gaps.
|
|
235
|
+
|
|
236
|
+
**Why clamp?** Larger screens should show *more content* (handled by breakpoints), not *bigger content*. Without clamping, a 16 dp font becomes 44 dp on an iPad Pro.
|
|
237
|
+
|
|
238
|
+
### 4.5 Accessible font sizes
|
|
239
|
+
|
|
240
|
+
React Native multiplies every font size by the user's **system font scale** (Settings → Display → Text Size). This is essential for accessibility, but extreme settings (up to 3.1× on iOS) can break layouts.
|
|
241
|
+
|
|
242
|
+
`fs()` honours the setting **up to `maxFontScale`** (default `2`, the WCAG 1.4.4 requirement):
|
|
243
|
+
|
|
244
|
+
| User setting | Rendered size of `fs(16)` (default config, 375 dp phone) |
|
|
245
|
+
|---|---|
|
|
246
|
+
| 1.0× | 16 |
|
|
247
|
+
| 1.5× | 24 |
|
|
248
|
+
| 2.0× | 32 |
|
|
249
|
+
| 3.0× | 32 (capped) |
|
|
250
|
+
|
|
251
|
+
This works with React Native's default `allowFontScaling={true}`. No custom `Text` component is needed.
|
|
252
|
+
|
|
253
|
+
---
|
|
254
|
+
|
|
255
|
+
## 5. Guides
|
|
256
|
+
|
|
257
|
+
### 5.1 Container queries: one card, any slot
|
|
258
|
+
|
|
259
|
+
```tsx
|
|
260
|
+
import { ResponsiveView, createStyles } from 'react-native-everything-responsive';
|
|
261
|
+
|
|
262
|
+
const useStyles = createStyles({
|
|
263
|
+
card: { flexDirection: { xs: 'column', sm: 'row' }, gap: 12 },
|
|
264
|
+
image: { width: { xs: '100%', sm: 96 }, aspectRatio: 1 },
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
function ArticleCardContent() {
|
|
268
|
+
const styles = useStyles(); // measures the nearest ResponsiveView
|
|
269
|
+
return (
|
|
270
|
+
<View style={styles.card}>
|
|
271
|
+
<Image style={styles.image} source={...} />
|
|
272
|
+
<Text>...</Text>
|
|
273
|
+
</View>
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export function ArticleCard() {
|
|
278
|
+
return (
|
|
279
|
+
// Container-specific breakpoints: horizontal from 280 dp of *card* width.
|
|
280
|
+
<ResponsiveView breakpoints={{ sm: 280 }}>
|
|
281
|
+
<ArticleCardContent />
|
|
282
|
+
</ResponsiveView>
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
The same `ArticleCard` is stacked in a 250 dp sidebar and horizontal in a 700 dp main column, on the same screen.
|
|
288
|
+
|
|
289
|
+
> **Note:** a component reads the *nearest ancestor* container. Put the hook in a child of `<ResponsiveView>`, not in the component that renders it.
|
|
290
|
+
|
|
291
|
+
**Avoiding a layout flash.** Before its first layout pass, a container assumes its parent's or the window's width. When you know it will be narrow, pass `initialWidth={300}`, or use `waitForLayout` to render nothing until it has been measured.
|
|
292
|
+
|
|
293
|
+
### 5.2 Showing and hiding UI
|
|
294
|
+
|
|
295
|
+
```tsx
|
|
296
|
+
<Show from="lg"><Sidebar /></Show>
|
|
297
|
+
<Show below="lg" fallback={<TabBar />}><BottomSheetMenu /></Show>
|
|
298
|
+
<Show device={['tablet', 'desktop']} orientation="landscape"><TwoPane /></Show>
|
|
299
|
+
<Hide device="phone"><KeyboardShortcutsHint /></Hide>
|
|
300
|
+
<Show when={(r) => r.fontScale > 1.5}><SimplifiedLayout /></Show>
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
- When several props are given, **all** must match (logical AND).
|
|
304
|
+
- By default, hidden children are **unmounted**. Use `mode="hide"` to keep them mounted, for example to preserve video playback, form input or scroll position.
|
|
305
|
+
- The hook form is `useMatches({ from: 'md' })`, which returns a boolean.
|
|
306
|
+
|
|
307
|
+
### 5.3 Grids
|
|
308
|
+
|
|
309
|
+
**Small or medium lists**: `<ResponsiveGrid>`:
|
|
310
|
+
|
|
311
|
+
```tsx
|
|
312
|
+
// Fixed column counts per breakpoint
|
|
313
|
+
<ResponsiveGrid columns={{ xs: 1, sm: 2, lg: 4 }} gap={16}>{cards}</ResponsiveGrid>
|
|
314
|
+
|
|
315
|
+
// Auto-fit: as many ≥180 dp columns as fit (CSS `repeat(auto-fill, minmax(180px, 1fr))`)
|
|
316
|
+
<ResponsiveGrid minItemWidth={180} maxColumns={6} rowGap={24} columnGap={12}>{cards}</ResponsiveGrid>
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
**Long lists**: `useGridLayout` with `FlatList`, so items stay virtualised:
|
|
320
|
+
|
|
321
|
+
```tsx
|
|
322
|
+
function Catalogue({ items }) {
|
|
323
|
+
const grid = useGridLayout({ minItemWidth: 160, gap: 12 });
|
|
324
|
+
return (
|
|
325
|
+
<FlatList
|
|
326
|
+
key={grid.columns} // required: numColumns cannot change while mounted
|
|
327
|
+
numColumns={grid.columns}
|
|
328
|
+
onLayout={grid.onLayout}
|
|
329
|
+
data={items}
|
|
330
|
+
columnWrapperStyle={grid.columns > 1 ? { gap: grid.columnGap } : undefined}
|
|
331
|
+
ItemSeparatorComponent={() => <View style={{ height: grid.rowGap }} />}
|
|
332
|
+
renderItem={({ item }) => <Card style={{ width: grid.itemWidth }} item={item} />}
|
|
333
|
+
/>
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
The grid uses `marginStart`, so it lays out correctly in right-to-left languages.
|
|
339
|
+
|
|
340
|
+
### 5.4 Styles that depend on props
|
|
341
|
+
|
|
342
|
+
`createStyles` factories run outside the component and cannot see props. For prop-dependent styles, use `useResponsiveStyles` with a dependency list:
|
|
343
|
+
|
|
344
|
+
```tsx
|
|
345
|
+
function Avatar({ size }: { size: number }) {
|
|
346
|
+
const styles = useResponsiveStyles((r) => ({
|
|
347
|
+
image: { width: r.s(size), height: r.s(size), borderRadius: r.s(size) / 2 },
|
|
348
|
+
}), [size]);
|
|
349
|
+
return <Image style={styles.image} source={...} />;
|
|
350
|
+
}
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
### 5.5 Outside React components
|
|
354
|
+
|
|
355
|
+
```ts
|
|
356
|
+
import { getResponsive } from 'react-native-everything-responsive';
|
|
357
|
+
|
|
358
|
+
// Read once: does NOT update on rotation or resize.
|
|
359
|
+
const { isTablet, fs } = getResponsive();
|
|
360
|
+
navigation.setOptions({ headerTitleStyle: { fontSize: fs(18) } });
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
### 5.6 Custom configuration
|
|
364
|
+
|
|
365
|
+
```tsx
|
|
366
|
+
<ResponsiveProvider
|
|
367
|
+
config={{
|
|
368
|
+
breakpoints: { md: 640, lg: 1024 }, // only the keys you change; must stay ascending
|
|
369
|
+
guidelineBaseWidth: 390, // your design's artboard
|
|
370
|
+
guidelineBaseHeight: 844,
|
|
371
|
+
minScale: 0.9,
|
|
372
|
+
maxScale: 1.25,
|
|
373
|
+
maxFontScale: 1.8,
|
|
374
|
+
tabletMinShortSide: 600,
|
|
375
|
+
desktopMinWidth: 1024,
|
|
376
|
+
}}
|
|
377
|
+
>
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
Providers can be nested. An inner provider inherits from the outer one, which is useful for a section of the app with its own rules. Invalid configuration fails fast with a clear message, for example: `Breakpoints must be strictly ascending, but "md" (300) is not greater than "sm" (360).`
|
|
381
|
+
|
|
382
|
+
### 5.7 Web and desktop
|
|
383
|
+
|
|
384
|
+
- Window resizes update everything live. `useBreakpoint` avoids re-renders while the user drags the window edge within one breakpoint.
|
|
385
|
+
- `deviceType` uses `matchMedia('(pointer: coarse)')`, so an iPad in Safari is a `tablet` and a laptop is a `desktop`.
|
|
386
|
+
- Cap content width on large monitors with `maxWidth` plus `alignSelf: 'center'`. See `example/App.tsx`.
|
|
387
|
+
|
|
388
|
+
---
|
|
389
|
+
|
|
390
|
+
## 6. Performance
|
|
391
|
+
|
|
392
|
+
| Technique | Effect |
|
|
393
|
+
|---|---|
|
|
394
|
+
| A single shared `Dimensions` subscription, created on first use and removed when unused | No listener build-up, no leaks |
|
|
395
|
+
| `useSyncExternalStore` | Tear-free reads in React 18 concurrent rendering |
|
|
396
|
+
| `useBreakpoint` / `useResponsiveValue` use a selector | Re-render only when the breakpoint changes |
|
|
397
|
+
| Static `createStyles({...})` sheets are cached per breakpoint | At most 6 resolutions per sheet, for the app's whole lifetime |
|
|
398
|
+
| Factory sheets are memoised per metrics snapshot | No work on unrelated re-renders |
|
|
399
|
+
| Container size updates ignore changes under 0.5 dp | No sub-pixel re-render loops |
|
|
400
|
+
|
|
401
|
+
**Tip:** in components that only care about breakpoints, prefer `useBreakpoint` / `useResponsiveValue` over `useResponsive`.
|
|
402
|
+
|
|
403
|
+
---
|
|
404
|
+
|
|
405
|
+
## 7. Compatibility
|
|
406
|
+
|
|
407
|
+
| Platform | Status |
|
|
408
|
+
|---|---|
|
|
409
|
+
| iOS (phone, iPad, Split View, Slide Over, Stage Manager) | ✅ |
|
|
410
|
+
| Android (phones, tablets, foldables, multi-window, freeform windows) | ✅ |
|
|
411
|
+
| Web (react-native-web) | ✅ (including server rendering: a 0×0 window falls back safely) |
|
|
412
|
+
| Windows / macOS (react-native-windows / -macos) | ✅ classified as `desktop` |
|
|
413
|
+
| Expo (managed and bare) | ✅ No native code |
|
|
414
|
+
| New Architecture (Fabric) and the old architecture | ✅ Uses only public JS APIs |
|
|
415
|
+
|
|
416
|
+
---
|
|
417
|
+
|
|
418
|
+
## 8. FAQ and troubleshooting
|
|
419
|
+
|
|
420
|
+
**My component doesn't react to its container.**
|
|
421
|
+
The hook must run in a *descendant* of `<ResponsiveView>`. Move the content into a child component (see §5.1).
|
|
422
|
+
|
|
423
|
+
**Can I add a custom breakpoint name like `tablet`?**
|
|
424
|
+
No. The six names are fixed, which keeps everything type-safe. You can change their *values* freely through the configuration.
|
|
425
|
+
|
|
426
|
+
**Why does `select({ xs: 'a' })` return `'a'` at `xl`?**
|
|
427
|
+
Because of the mobile-first cascade: `xs` applies from `xs` upward.
|
|
428
|
+
|
|
429
|
+
**Why did my `s(10)` return `10.333…`?**
|
|
430
|
+
It is rounded to the physical pixel grid (10.4 dp × 3 = 31.2 px → 31 px = 10.333 dp). This is intentional and keeps edges sharp.
|
|
431
|
+
|
|
432
|
+
**Does this replace safe-area handling?**
|
|
433
|
+
No. Safe areas (notches, home indicator) need native insets. Use React Native's `SafeAreaView` on iOS, or your existing safe-area solution, together with this library.
|
|
434
|
+
|
|
435
|
+
**How do I test components that use it?**
|
|
436
|
+
Mock `Dimensions` as you normally would. The pure helpers (`createScaler`, `getBreakpoint`, `computeGridLayout`, `classifyDevice`, `resolveResponsiveValue`) have no platform dependencies and can be unit-tested directly.
|
|
437
|
+
|
|
438
|
+
---
|
|
439
|
+
|
|
440
|
+
## Project layout
|
|
441
|
+
|
|
442
|
+
```
|
|
443
|
+
src/
|
|
444
|
+
core/ Pure logic with no React or React Native imports (testable anywhere)
|
|
445
|
+
native/ React Native bindings: store, provider, hooks, components
|
|
446
|
+
index.ts Public API
|
|
447
|
+
lib/ Compiled JavaScript and type declarations
|
|
448
|
+
docs/API.md Full API reference
|
|
449
|
+
example/ Complete example app
|
|
450
|
+
tests/ Unit, component and type-level tests
|
|
451
|
+
```
|
|
452
|
+
|
|
453
|
+
```bash
|
|
454
|
+
npm run typecheck # strict TypeScript, including type-level tests
|
|
455
|
+
npm test # build and run 26 tests
|
|
456
|
+
```
|
|
457
|
+
|
|
458
|
+
## License
|
|
459
|
+
|
|
460
|
+
MIT
|