react-native-everything-responsive 0.0.0-stage → 1.0.1
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/src/core/grid.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export interface GridInput {
|
|
2
|
+
/** Measured width of the grid container in dp. */
|
|
3
|
+
containerWidth: number;
|
|
4
|
+
/** Fixed column count. Ignored when `minItemWidth` is set. */
|
|
5
|
+
columns?: number;
|
|
6
|
+
/** Auto-fit mode: as many columns as fit with each item at least this wide. */
|
|
7
|
+
minItemWidth?: number;
|
|
8
|
+
/** Upper bound for the column count in auto-fit mode. */
|
|
9
|
+
maxColumns?: number;
|
|
10
|
+
/** Horizontal gap between columns in dp. */
|
|
11
|
+
columnGap: number;
|
|
12
|
+
/** Physical pixels per dp, used to floor widths onto the pixel grid. */
|
|
13
|
+
pixelRatio: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface GridLayout {
|
|
17
|
+
columns: number;
|
|
18
|
+
/** Width of each item, floored to a physical pixel so a row never overflows and wraps. */
|
|
19
|
+
itemWidth: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Floors a dp value to the physical pixel grid. */
|
|
23
|
+
export function floorToPixel(value: number, pixelRatio: number): number {
|
|
24
|
+
const ratio = pixelRatio > 0 ? pixelRatio : 1;
|
|
25
|
+
return Math.floor(value * ratio + 1e-7) / ratio;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Computes column count and item width.
|
|
30
|
+
*
|
|
31
|
+
* Fixes the classic React Native grid bug: `width: '33.33%'` plus margins rounds up on some
|
|
32
|
+
* pixel densities, the row becomes 0.5px too wide, and the last item wraps onto a new line.
|
|
33
|
+
* Widths here are floored to the physical pixel grid, so rows always fit.
|
|
34
|
+
*/
|
|
35
|
+
export function computeGridLayout(input: GridInput): GridLayout {
|
|
36
|
+
const width = Math.max(0, input.containerWidth);
|
|
37
|
+
const gap = Math.max(0, input.columnGap);
|
|
38
|
+
let columns: number;
|
|
39
|
+
|
|
40
|
+
if (input.minItemWidth !== undefined && input.minItemWidth > 0) {
|
|
41
|
+
// CSS equivalent: repeat(auto-fill, minmax(minItemWidth, 1fr))
|
|
42
|
+
columns = Math.floor((width + gap) / (input.minItemWidth + gap));
|
|
43
|
+
if (input.maxColumns !== undefined && input.maxColumns > 0) {
|
|
44
|
+
columns = Math.min(columns, Math.floor(input.maxColumns));
|
|
45
|
+
}
|
|
46
|
+
} else {
|
|
47
|
+
columns = Math.floor(input.columns ?? 1);
|
|
48
|
+
}
|
|
49
|
+
columns = Math.max(1, columns);
|
|
50
|
+
|
|
51
|
+
const available = width - gap * (columns - 1);
|
|
52
|
+
const itemWidth = Math.max(0, floorToPixel(available / columns, input.pixelRatio));
|
|
53
|
+
return { columns, itemWidth };
|
|
54
|
+
}
|
package/src/core/info.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { createLayoutInfo } from './layout';
|
|
2
|
+
import { createScaler } from './scaling';
|
|
3
|
+
import type {
|
|
4
|
+
DeviceType,
|
|
5
|
+
LayoutInfo,
|
|
6
|
+
LayoutSource,
|
|
7
|
+
ResponsiveConfig,
|
|
8
|
+
ResponsiveInfo,
|
|
9
|
+
} from './types';
|
|
10
|
+
|
|
11
|
+
export interface ResponsiveInfoInput {
|
|
12
|
+
window: { width: number; height: number };
|
|
13
|
+
screen: { width: number; height: number };
|
|
14
|
+
fontScale: number;
|
|
15
|
+
pixelRatio: number;
|
|
16
|
+
deviceType: DeviceType;
|
|
17
|
+
config: ResponsiveConfig;
|
|
18
|
+
container: LayoutInfo | null;
|
|
19
|
+
source: LayoutSource;
|
|
20
|
+
round?: (value: number) => number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Combines window, container, device and scaling information into one object.
|
|
25
|
+
* The top-level layout fields (`width`, `breakpoint`, `select`, ...) describe the
|
|
26
|
+
* *active* area chosen by `source`; scaling and device fields always describe the
|
|
27
|
+
* real window and screen.
|
|
28
|
+
*/
|
|
29
|
+
export function createResponsiveInfo(input: ResponsiveInfoInput): ResponsiveInfo {
|
|
30
|
+
const { config, container, source, round } = input;
|
|
31
|
+
const windowLayout = createLayoutInfo(
|
|
32
|
+
'window',
|
|
33
|
+
input.window.width,
|
|
34
|
+
input.window.height,
|
|
35
|
+
config.breakpoints,
|
|
36
|
+
round,
|
|
37
|
+
);
|
|
38
|
+
const active = source === 'window' ? windowLayout : container ?? windowLayout;
|
|
39
|
+
const scaler = createScaler({
|
|
40
|
+
width: input.window.width,
|
|
41
|
+
height: input.window.height,
|
|
42
|
+
fontScale: input.fontScale,
|
|
43
|
+
config,
|
|
44
|
+
round,
|
|
45
|
+
});
|
|
46
|
+
const deviceType = input.deviceType;
|
|
47
|
+
|
|
48
|
+
return Object.freeze({
|
|
49
|
+
...active,
|
|
50
|
+
...scaler,
|
|
51
|
+
deviceType,
|
|
52
|
+
isPhone: deviceType === 'phone',
|
|
53
|
+
isTablet: deviceType === 'tablet',
|
|
54
|
+
isDesktop: deviceType === 'desktop',
|
|
55
|
+
isTV: deviceType === 'tv',
|
|
56
|
+
screenWidth: input.screen.width,
|
|
57
|
+
screenHeight: input.screen.height,
|
|
58
|
+
fontScale: input.fontScale,
|
|
59
|
+
pixelRatio: input.pixelRatio,
|
|
60
|
+
window: windowLayout,
|
|
61
|
+
container,
|
|
62
|
+
config,
|
|
63
|
+
} satisfies ResponsiveInfo);
|
|
64
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import {
|
|
2
|
+
breakpointIndex,
|
|
3
|
+
getBreakpoint,
|
|
4
|
+
isBelow,
|
|
5
|
+
isBetween,
|
|
6
|
+
isFrom,
|
|
7
|
+
resolveResponsiveValue,
|
|
8
|
+
} from './breakpoints';
|
|
9
|
+
import type { BreakpointKey, Breakpoints, LayoutInfo, Narrowable, ResponsiveValue } from './types';
|
|
10
|
+
|
|
11
|
+
const identity = (value: number): number => value;
|
|
12
|
+
|
|
13
|
+
/** Builds the immutable layout description for a window or container of a given size. */
|
|
14
|
+
export function createLayoutInfo(
|
|
15
|
+
kind: 'window' | 'container',
|
|
16
|
+
width: number,
|
|
17
|
+
height: number,
|
|
18
|
+
breakpoints: Breakpoints,
|
|
19
|
+
round: (value: number) => number = identity,
|
|
20
|
+
): LayoutInfo {
|
|
21
|
+
const breakpoint = getBreakpoint(width, breakpoints);
|
|
22
|
+
const isLandscape = width > height;
|
|
23
|
+
return Object.freeze({
|
|
24
|
+
kind,
|
|
25
|
+
width,
|
|
26
|
+
height,
|
|
27
|
+
breakpoint,
|
|
28
|
+
breakpoints,
|
|
29
|
+
orientation: isLandscape ? 'landscape' : 'portrait',
|
|
30
|
+
isPortrait: !isLandscape,
|
|
31
|
+
isLandscape,
|
|
32
|
+
from: (bp: BreakpointKey) => isFrom(breakpoint, bp),
|
|
33
|
+
below: (bp: BreakpointKey) => isBelow(breakpoint, bp),
|
|
34
|
+
between: (min: BreakpointKey, max: BreakpointKey) => isBetween(breakpoint, min, max),
|
|
35
|
+
only: (...bps: BreakpointKey[]) => {
|
|
36
|
+
const current = breakpointIndex(breakpoint);
|
|
37
|
+
return bps.some((bp) => breakpointIndex(bp) === current);
|
|
38
|
+
},
|
|
39
|
+
select: ((values: ResponsiveValue<Narrowable>) =>
|
|
40
|
+
resolveResponsiveValue(values, breakpoint)) as LayoutInfo['select'],
|
|
41
|
+
wp: (percent: number) => round((width * percent) / 100),
|
|
42
|
+
hp: (percent: number) => round((height * percent) / 100),
|
|
43
|
+
} satisfies LayoutInfo);
|
|
44
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { ResponsiveConfig, Scaler } from './types';
|
|
2
|
+
|
|
3
|
+
export interface ScalerInput {
|
|
4
|
+
/** Window width in dp. */
|
|
5
|
+
width: number;
|
|
6
|
+
/** Window height in dp. */
|
|
7
|
+
height: number;
|
|
8
|
+
/** System font scale (1 = default). */
|
|
9
|
+
fontScale: number;
|
|
10
|
+
config: ResponsiveConfig;
|
|
11
|
+
/** Pixel-rounding function. Defaults to identity (useful in tests and on the server). */
|
|
12
|
+
round?: (value: number) => number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const identity = (value: number): number => value;
|
|
16
|
+
|
|
17
|
+
export function clamp(value: number, min: number, max: number): number {
|
|
18
|
+
return Math.min(max, Math.max(min, value));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Creates orientation-stable, clamped scaling helpers.
|
|
23
|
+
*
|
|
24
|
+
* Why this is different from the usual `width / 375` approach:
|
|
25
|
+
* 1. **Orientation-stable**: it scales by the *short* side (and the *long* side for
|
|
26
|
+
* vertical sizes), so rotating a phone does not suddenly make everything 2x bigger.
|
|
27
|
+
* 2. **Clamped**: on a 1366 dp wide iPad Pro, naive scaling gives 3.6x. Here the factor
|
|
28
|
+
* is clamped to `[minScale, maxScale]`, and large screens should use breakpoints to
|
|
29
|
+
* show *more content*, not bigger content.
|
|
30
|
+
* 3. **Pixel-perfect**: results are rounded to physical pixels to avoid blurry borders
|
|
31
|
+
* and 1px gaps.
|
|
32
|
+
* 4. **Accessible fonts**: `fs()` honours the user's font size setting up to a cap.
|
|
33
|
+
*/
|
|
34
|
+
export function createScaler({ width, height, fontScale, config, round = identity }: ScalerInput): Scaler {
|
|
35
|
+
const shortSide = Math.max(0, Math.min(width, height));
|
|
36
|
+
const longSide = Math.max(0, Math.max(width, height));
|
|
37
|
+
const baseShort = Math.min(config.guidelineBaseWidth, config.guidelineBaseHeight);
|
|
38
|
+
const baseLong = Math.max(config.guidelineBaseWidth, config.guidelineBaseHeight);
|
|
39
|
+
|
|
40
|
+
// A 0x0 window happens during server rendering and in some headless tests.
|
|
41
|
+
const rawScale = shortSide > 0 ? shortSide / baseShort : 1;
|
|
42
|
+
const rawVerticalScale = longSide > 0 ? longSide / baseLong : 1;
|
|
43
|
+
const scaleFactor = clamp(rawScale, config.minScale, config.maxScale);
|
|
44
|
+
const verticalScaleFactor = clamp(rawVerticalScale, config.minScale, config.maxScale);
|
|
45
|
+
|
|
46
|
+
const safeFontScale = fontScale > 0 && Number.isFinite(fontScale) ? fontScale : 1;
|
|
47
|
+
// React Native multiplies every font size by fontScale. Dividing first and
|
|
48
|
+
// multiplying by the capped value means the *rendered* size honours the user's
|
|
49
|
+
// setting only up to maxFontScale.
|
|
50
|
+
const fontCompensation = Math.min(safeFontScale, config.maxFontScale) / safeFontScale;
|
|
51
|
+
|
|
52
|
+
const moderate = (size: number, factor: number): number => size + (size * scaleFactor - size) * factor;
|
|
53
|
+
|
|
54
|
+
return Object.freeze({
|
|
55
|
+
scaleFactor,
|
|
56
|
+
verticalScaleFactor,
|
|
57
|
+
s: (size: number) => round(size * scaleFactor),
|
|
58
|
+
vs: (size: number) => round(size * verticalScaleFactor),
|
|
59
|
+
ms: (size: number, factor = 0.5) => round(moderate(size, factor)),
|
|
60
|
+
fs: (size: number, factor = 0.5) => round(moderate(size, factor) * fontCompensation),
|
|
61
|
+
});
|
|
62
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core type definitions for ReactNativeEverythingResponsive.
|
|
3
|
+
*
|
|
4
|
+
* This file is platform-free: it must never import from `react` or `react-native`.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** The six named breakpoints, ordered from smallest to largest. */
|
|
8
|
+
export type BreakpointKey = 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'xxl';
|
|
9
|
+
|
|
10
|
+
/** Minimum width (in density-independent pixels, "dp") at which each breakpoint starts. */
|
|
11
|
+
export type Breakpoints = Readonly<Record<BreakpointKey, number>>;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A value that changes with the breakpoint. Mobile-first: a key applies from that
|
|
15
|
+
* breakpoint upward until a larger key overrides it. `base` is an alias for "smallest".
|
|
16
|
+
*
|
|
17
|
+
* @example { xs: 1, md: 2, xl: 4 } // 1 column on phones, 2 on tablets, 4 on desktops
|
|
18
|
+
*/
|
|
19
|
+
export type ResponsiveObject<T> = { readonly [K in BreakpointKey | 'base']?: T };
|
|
20
|
+
|
|
21
|
+
/** Either a plain value or a per-breakpoint object. */
|
|
22
|
+
export type ResponsiveValue<T> = T | ResponsiveObject<T>;
|
|
23
|
+
|
|
24
|
+
/** Hardware class of the device (based on the physical screen, not the app window). */
|
|
25
|
+
export type DeviceType = 'phone' | 'tablet' | 'desktop' | 'tv';
|
|
26
|
+
|
|
27
|
+
export type Orientation = 'portrait' | 'landscape';
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Where layout measurements come from:
|
|
31
|
+
* - `window`: the app window (correct in split-screen / multi-window, unlike the screen).
|
|
32
|
+
* - `container`: the nearest `<ResponsiveView>` ancestor (container queries).
|
|
33
|
+
* - `auto` (default): the nearest container if there is one, otherwise the window.
|
|
34
|
+
*/
|
|
35
|
+
export type LayoutSource = 'auto' | 'window' | 'container';
|
|
36
|
+
|
|
37
|
+
/** Fully resolved configuration. */
|
|
38
|
+
export interface ResponsiveConfig {
|
|
39
|
+
/** Breakpoint start widths in dp. Must be strictly ascending. */
|
|
40
|
+
readonly breakpoints: Breakpoints;
|
|
41
|
+
/** Width of the device the design was drawn for (dp). Default 375 (iPhone 11 Pro/X class). */
|
|
42
|
+
readonly guidelineBaseWidth: number;
|
|
43
|
+
/** Height of the device the design was drawn for (dp). Default 812. */
|
|
44
|
+
readonly guidelineBaseHeight: number;
|
|
45
|
+
/** Lower bound for the scale factor, so tiny phones never shrink UI too much. */
|
|
46
|
+
readonly minScale: number;
|
|
47
|
+
/** Upper bound for the scale factor, so tablets/desktops never blow UI up 3x. */
|
|
48
|
+
readonly maxScale: number;
|
|
49
|
+
/**
|
|
50
|
+
* Upper bound for the user's system font scale that `fs()` will honour.
|
|
51
|
+
* Default 2 (meets WCAG 1.4.4 "resize text to 200%").
|
|
52
|
+
*/
|
|
53
|
+
readonly maxFontScale: number;
|
|
54
|
+
/** Shortest screen side (dp) from which an Android/web device counts as a tablet. Default 600. */
|
|
55
|
+
readonly tabletMinShortSide: number;
|
|
56
|
+
/** Longest screen side (dp) from which a fine-pointer web device counts as a desktop. Default 1024. */
|
|
57
|
+
readonly desktopMinWidth: number;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Partial configuration accepted by `<ResponsiveProvider config={...}>`. */
|
|
61
|
+
export interface ResponsiveConfigInput {
|
|
62
|
+
breakpoints?: Partial<Record<BreakpointKey, number>>;
|
|
63
|
+
guidelineBaseWidth?: number;
|
|
64
|
+
guidelineBaseHeight?: number;
|
|
65
|
+
minScale?: number;
|
|
66
|
+
maxScale?: number;
|
|
67
|
+
maxFontScale?: number;
|
|
68
|
+
tabletMinShortSide?: number;
|
|
69
|
+
desktopMinWidth?: number;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Helper constraint that keeps literal types when inferring generics, so
|
|
74
|
+
* `select({ xs: 'column', md: 'row' })` is typed `'column' | 'row'`, not `string`.
|
|
75
|
+
*/
|
|
76
|
+
export type Narrowable =
|
|
77
|
+
| string
|
|
78
|
+
| number
|
|
79
|
+
| bigint
|
|
80
|
+
| boolean
|
|
81
|
+
| symbol
|
|
82
|
+
| object
|
|
83
|
+
| null
|
|
84
|
+
| undefined;
|
|
85
|
+
|
|
86
|
+
/** Union of the values of a responsive object literal. */
|
|
87
|
+
export type SelectResult<V> = Exclude<V[keyof V], undefined>;
|
|
88
|
+
|
|
89
|
+
/** Measurements and breakpoint helpers for one layout area (window or container). */
|
|
90
|
+
export interface LayoutInfo {
|
|
91
|
+
/** Which area these measurements describe. */
|
|
92
|
+
readonly kind: 'window' | 'container';
|
|
93
|
+
readonly width: number;
|
|
94
|
+
readonly height: number;
|
|
95
|
+
/** The active breakpoint for `width`. */
|
|
96
|
+
readonly breakpoint: BreakpointKey;
|
|
97
|
+
/** The breakpoint table used for this area. */
|
|
98
|
+
readonly breakpoints: Breakpoints;
|
|
99
|
+
readonly orientation: Orientation;
|
|
100
|
+
readonly isPortrait: boolean;
|
|
101
|
+
readonly isLandscape: boolean;
|
|
102
|
+
/** True when the current breakpoint is `bp` or larger. */
|
|
103
|
+
from(bp: BreakpointKey): boolean;
|
|
104
|
+
/** True when the current breakpoint is strictly smaller than `bp`. */
|
|
105
|
+
below(bp: BreakpointKey): boolean;
|
|
106
|
+
/** True when `min <= current < max`. */
|
|
107
|
+
between(min: BreakpointKey, max: BreakpointKey): boolean;
|
|
108
|
+
/** True when the current breakpoint is one of `bps`. */
|
|
109
|
+
only(...bps: BreakpointKey[]): boolean;
|
|
110
|
+
/**
|
|
111
|
+
* Pick the value for the current breakpoint (mobile-first cascade).
|
|
112
|
+
* Literal and mixed types are inferred: `select({ xs: '100%', md: 320 })` → `'100%' | 320`.
|
|
113
|
+
*/
|
|
114
|
+
select<const V extends ResponsiveObject<unknown>>(values: V): SelectResult<V>;
|
|
115
|
+
select<T extends Narrowable>(values: ResponsiveValue<T>): T;
|
|
116
|
+
/** Percentage of the area's width, rounded to the nearest physical pixel. */
|
|
117
|
+
wp(percent: number): number;
|
|
118
|
+
/** Percentage of the area's height, rounded to the nearest physical pixel. */
|
|
119
|
+
hp(percent: number): number;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Size-scaling helpers. All results are rounded to the nearest physical pixel. */
|
|
123
|
+
export interface Scaler {
|
|
124
|
+
/** Clamped horizontal scale factor (short screen side / guideline width). */
|
|
125
|
+
readonly scaleFactor: number;
|
|
126
|
+
/** Clamped vertical scale factor (long screen side / guideline height). */
|
|
127
|
+
readonly verticalScaleFactor: number;
|
|
128
|
+
/** Scale linearly with the short side (widths, icon sizes, horizontal spacing). */
|
|
129
|
+
s(size: number): number;
|
|
130
|
+
/** Scale linearly with the long side (heights, vertical spacing). */
|
|
131
|
+
vs(size: number): number;
|
|
132
|
+
/** Scale partially: `factor` 0 = no scaling, 1 = full `s()`. Default 0.5. */
|
|
133
|
+
ms(size: number, factor?: number): number;
|
|
134
|
+
/**
|
|
135
|
+
* Font size: moderately scaled, and compensated so the user's system font scale
|
|
136
|
+
* is honoured only up to `config.maxFontScale`.
|
|
137
|
+
*/
|
|
138
|
+
fs(size: number, factor?: number): number;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Facts about the physical device and accessibility settings. */
|
|
142
|
+
export interface DeviceInfo {
|
|
143
|
+
readonly deviceType: DeviceType;
|
|
144
|
+
readonly isPhone: boolean;
|
|
145
|
+
readonly isTablet: boolean;
|
|
146
|
+
readonly isDesktop: boolean;
|
|
147
|
+
readonly isTV: boolean;
|
|
148
|
+
readonly screenWidth: number;
|
|
149
|
+
readonly screenHeight: number;
|
|
150
|
+
/** The user's system font scale (1 = default). */
|
|
151
|
+
readonly fontScale: number;
|
|
152
|
+
/** Physical pixels per dp. */
|
|
153
|
+
readonly pixelRatio: number;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Everything `useResponsive()` returns. */
|
|
157
|
+
export interface ResponsiveInfo extends LayoutInfo, Scaler, DeviceInfo {
|
|
158
|
+
/** Layout info for the app window, always available. */
|
|
159
|
+
readonly window: LayoutInfo;
|
|
160
|
+
/** Layout info for the nearest `<ResponsiveView>`, or `null` when there is none. */
|
|
161
|
+
readonly container: LayoutInfo | null;
|
|
162
|
+
readonly config: ResponsiveConfig;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Unwraps a responsive object type to the union of its values (type level only). */
|
|
166
|
+
export type UnwrapResponsive<V> = V extends readonly unknown[]
|
|
167
|
+
? V
|
|
168
|
+
: V extends object
|
|
169
|
+
? [Exclude<keyof V, BreakpointKey | 'base'>] extends [never]
|
|
170
|
+
? Exclude<V[keyof V], undefined>
|
|
171
|
+
: V
|
|
172
|
+
: V;
|
|
173
|
+
|
|
174
|
+
/** A style object whose responsive values have been resolved. */
|
|
175
|
+
export type ResolvedStyle<S> = { [K in keyof S]: UnwrapResponsive<S[K]> };
|
|
176
|
+
|
|
177
|
+
/** A named style sheet whose responsive values have been resolved. */
|
|
178
|
+
export type ResolvedStyles<T> = { [P in keyof T]: ResolvedStyle<T[P]> };
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ReactNativeEverythingResponsive
|
|
3
|
+
* Zero-dependency responsive design toolkit for React Native.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// Components
|
|
7
|
+
export { ResponsiveProvider } from './native/ResponsiveProvider';
|
|
8
|
+
export type { ResponsiveProviderProps } from './native/ResponsiveProvider';
|
|
9
|
+
export { ResponsiveView } from './native/ResponsiveView';
|
|
10
|
+
export type { ResponsiveViewProps } from './native/ResponsiveView';
|
|
11
|
+
export { ResponsiveGrid, useGridLayout } from './native/ResponsiveGrid';
|
|
12
|
+
export type { GridLayoutResult, GridOptions, ResponsiveGridProps } from './native/ResponsiveGrid';
|
|
13
|
+
export { Hide, Show, matchesConditions, useMatches } from './native/Visibility';
|
|
14
|
+
export type { VisibilityConditions, VisibilityProps } from './native/Visibility';
|
|
15
|
+
|
|
16
|
+
// Hooks
|
|
17
|
+
export { useBreakpoint, useResponsive, useResponsiveConfig, useResponsiveValue } from './native/hooks';
|
|
18
|
+
export type { SourceOptions } from './native/hooks';
|
|
19
|
+
export { createStyles, resolveStyles, useResponsiveStyles } from './native/createStyles';
|
|
20
|
+
export type { AnyResponsiveStyle, ResponsiveNamedStyles, ResponsiveStyle } from './native/createStyles';
|
|
21
|
+
|
|
22
|
+
// Non-reactive access
|
|
23
|
+
export { getResponsive } from './native/snapshot';
|
|
24
|
+
|
|
25
|
+
// Pure utilities (platform-free)
|
|
26
|
+
export { BREAKPOINT_KEYS, DEFAULT_BREAKPOINTS, DEFAULT_CONFIG, createConfig } from './core/config';
|
|
27
|
+
export {
|
|
28
|
+
getBreakpoint,
|
|
29
|
+
isBelow,
|
|
30
|
+
isBetween,
|
|
31
|
+
isFrom,
|
|
32
|
+
isResponsiveObject,
|
|
33
|
+
resolveResponsiveValue,
|
|
34
|
+
} from './core/breakpoints';
|
|
35
|
+
export { createScaler } from './core/scaling';
|
|
36
|
+
export { classifyDevice } from './core/device';
|
|
37
|
+
export type { DeviceEnvironment } from './core/device';
|
|
38
|
+
export { computeGridLayout, floorToPixel } from './core/grid';
|
|
39
|
+
export type { GridInput, GridLayout } from './core/grid';
|
|
40
|
+
|
|
41
|
+
// Types
|
|
42
|
+
export type {
|
|
43
|
+
BreakpointKey,
|
|
44
|
+
Breakpoints,
|
|
45
|
+
DeviceInfo,
|
|
46
|
+
DeviceType,
|
|
47
|
+
LayoutInfo,
|
|
48
|
+
LayoutSource,
|
|
49
|
+
Orientation,
|
|
50
|
+
ResolvedStyle,
|
|
51
|
+
ResolvedStyles,
|
|
52
|
+
ResponsiveConfig,
|
|
53
|
+
ResponsiveConfigInput,
|
|
54
|
+
ResponsiveInfo,
|
|
55
|
+
ResponsiveObject,
|
|
56
|
+
ResponsiveValue,
|
|
57
|
+
Scaler,
|
|
58
|
+
SelectResult,
|
|
59
|
+
} from './core/types';
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import React, { Children, useCallback, useContext, useMemo, useState } from 'react';
|
|
2
|
+
import type { ReactNode } from 'react';
|
|
3
|
+
import { PixelRatio, View } from 'react-native';
|
|
4
|
+
import type { LayoutChangeEvent, StyleProp, ViewProps, ViewStyle } from 'react-native';
|
|
5
|
+
import { resolveResponsiveValue } from '../core/breakpoints';
|
|
6
|
+
import { computeGridLayout } from '../core/grid';
|
|
7
|
+
import type { GridLayout } from '../core/grid';
|
|
8
|
+
import type { ResponsiveValue } from '../core/types';
|
|
9
|
+
import { ContainerContext } from './context';
|
|
10
|
+
import { useBreakpoint } from './hooks';
|
|
11
|
+
import type { SourceOptions } from './hooks';
|
|
12
|
+
import { getSnapshot } from './store';
|
|
13
|
+
|
|
14
|
+
export interface GridOptions extends SourceOptions {
|
|
15
|
+
/** Fixed number of columns. Default `{ xs: 1, sm: 2, md: 3, lg: 4 }`. */
|
|
16
|
+
columns?: ResponsiveValue<number>;
|
|
17
|
+
/**
|
|
18
|
+
* Auto-fit mode (like CSS `repeat(auto-fill, minmax(X, 1fr))`): fit as many columns
|
|
19
|
+
* as possible with each item at least this wide. Overrides `columns`.
|
|
20
|
+
*/
|
|
21
|
+
minItemWidth?: ResponsiveValue<number>;
|
|
22
|
+
/** Maximum columns in auto-fit mode. */
|
|
23
|
+
maxColumns?: ResponsiveValue<number>;
|
|
24
|
+
/** Gap between rows and columns. Default 0. */
|
|
25
|
+
gap?: ResponsiveValue<number>;
|
|
26
|
+
/** Gap between rows. Overrides `gap`. */
|
|
27
|
+
rowGap?: ResponsiveValue<number>;
|
|
28
|
+
/** Gap between columns. Overrides `gap`. */
|
|
29
|
+
columnGap?: ResponsiveValue<number>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface ResponsiveGridProps extends ViewProps, GridOptions {
|
|
33
|
+
/** Extra style for every cell wrapper. */
|
|
34
|
+
itemStyle?: StyleProp<ViewStyle>;
|
|
35
|
+
children?: ReactNode;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const DEFAULT_COLUMNS: ResponsiveValue<number> = { xs: 1, sm: 2, md: 3, lg: 4 };
|
|
39
|
+
|
|
40
|
+
export interface GridLayoutResult extends GridLayout {
|
|
41
|
+
rowGap: number;
|
|
42
|
+
columnGap: number;
|
|
43
|
+
/** Attach to the grid's outer View (or FlatList) so its width is measured. */
|
|
44
|
+
onLayout: (event: LayoutChangeEvent) => void;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Grid maths as a hook, for use with `FlatList`/`SectionList` virtualised grids.
|
|
49
|
+
*
|
|
50
|
+
* React Native does not allow changing `numColumns` on a mounted FlatList, so pass
|
|
51
|
+
* `key={columns}` to remount it when the column count changes.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* const grid = useGridLayout({ minItemWidth: 160, gap: 12 });
|
|
55
|
+
* <FlatList key={grid.columns} numColumns={grid.columns} onLayout={grid.onLayout}
|
|
56
|
+
* columnWrapperStyle={grid.columns > 1 ? { gap: grid.columnGap } : undefined}
|
|
57
|
+
* renderItem={({ item }) => <Card style={{ width: grid.itemWidth }} />} />
|
|
58
|
+
*/
|
|
59
|
+
export function useGridLayout(options: GridOptions = {}): GridLayoutResult {
|
|
60
|
+
const breakpoint = useBreakpoint({ source: options.source });
|
|
61
|
+
const container = useContext(ContainerContext);
|
|
62
|
+
const [measuredWidth, setMeasuredWidth] = useState<number | null>(null);
|
|
63
|
+
|
|
64
|
+
const onLayout = useCallback((event: LayoutChangeEvent) => {
|
|
65
|
+
const width = event.nativeEvent.layout.width;
|
|
66
|
+
setMeasuredWidth((previous) => (previous !== null && Math.abs(previous - width) < 0.5 ? previous : width));
|
|
67
|
+
}, []);
|
|
68
|
+
|
|
69
|
+
const pick = <T,>(value: ResponsiveValue<T> | undefined): T | undefined =>
|
|
70
|
+
value === undefined ? undefined : resolveResponsiveValue(value, breakpoint);
|
|
71
|
+
|
|
72
|
+
const gap = pick(options.gap) ?? 0;
|
|
73
|
+
const rowGap = pick(options.rowGap) ?? gap;
|
|
74
|
+
const columnGap = pick(options.columnGap) ?? gap;
|
|
75
|
+
const columns = pick(options.columns ?? DEFAULT_COLUMNS);
|
|
76
|
+
const minItemWidth = pick(options.minItemWidth);
|
|
77
|
+
const maxColumns = pick(options.maxColumns);
|
|
78
|
+
const width = measuredWidth ?? container?.width ?? getSnapshot().window.width;
|
|
79
|
+
|
|
80
|
+
const layout = useMemo(
|
|
81
|
+
() =>
|
|
82
|
+
computeGridLayout({
|
|
83
|
+
containerWidth: width,
|
|
84
|
+
columns,
|
|
85
|
+
minItemWidth,
|
|
86
|
+
maxColumns,
|
|
87
|
+
columnGap,
|
|
88
|
+
pixelRatio: PixelRatio.get(),
|
|
89
|
+
}),
|
|
90
|
+
[width, columns, minItemWidth, maxColumns, columnGap],
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
return { ...layout, rowGap, columnGap, onLayout };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* A responsive grid for a small or medium number of children (for long lists use
|
|
98
|
+
* `useGridLayout` with a FlatList). Supports fixed and auto-fit columns, independent
|
|
99
|
+
* row/column gaps, RTL layouts, and pixel-exact widths so rows never wrap unexpectedly.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* <ResponsiveGrid columns={{ xs: 1, md: 2, xl: 4 }} gap={16}>
|
|
103
|
+
* {products.map((p) => <ProductCard key={p.id} product={p} />)}
|
|
104
|
+
* </ResponsiveGrid>
|
|
105
|
+
*
|
|
106
|
+
* <ResponsiveGrid minItemWidth={180} gap={12}>...</ResponsiveGrid>
|
|
107
|
+
*/
|
|
108
|
+
export function ResponsiveGrid({
|
|
109
|
+
columns,
|
|
110
|
+
minItemWidth,
|
|
111
|
+
maxColumns,
|
|
112
|
+
gap,
|
|
113
|
+
rowGap,
|
|
114
|
+
columnGap,
|
|
115
|
+
source,
|
|
116
|
+
itemStyle,
|
|
117
|
+
style,
|
|
118
|
+
onLayout,
|
|
119
|
+
children,
|
|
120
|
+
...viewProps
|
|
121
|
+
}: ResponsiveGridProps): React.JSX.Element {
|
|
122
|
+
const grid = useGridLayout({ columns, minItemWidth, maxColumns, gap, rowGap, columnGap, source });
|
|
123
|
+
const gridOnLayout = grid.onLayout;
|
|
124
|
+
|
|
125
|
+
const handleLayout = useCallback(
|
|
126
|
+
(event: LayoutChangeEvent) => {
|
|
127
|
+
gridOnLayout(event);
|
|
128
|
+
onLayout?.(event);
|
|
129
|
+
},
|
|
130
|
+
[gridOnLayout, onLayout],
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
const items = Children.toArray(children);
|
|
134
|
+
|
|
135
|
+
return (
|
|
136
|
+
<View {...viewProps} style={[{ flexDirection: 'row', flexWrap: 'wrap' }, style]} onLayout={handleLayout}>
|
|
137
|
+
{items.map((child, index) => {
|
|
138
|
+
const column = index % grid.columns;
|
|
139
|
+
const row = Math.floor(index / grid.columns);
|
|
140
|
+
const key = React.isValidElement(child) && child.key != null ? child.key : index;
|
|
141
|
+
return (
|
|
142
|
+
<View
|
|
143
|
+
key={key}
|
|
144
|
+
style={[
|
|
145
|
+
{
|
|
146
|
+
width: grid.itemWidth,
|
|
147
|
+
marginStart: column === 0 ? 0 : grid.columnGap,
|
|
148
|
+
marginTop: row === 0 ? 0 : grid.rowGap,
|
|
149
|
+
},
|
|
150
|
+
itemStyle,
|
|
151
|
+
]}
|
|
152
|
+
>
|
|
153
|
+
{child}
|
|
154
|
+
</View>
|
|
155
|
+
);
|
|
156
|
+
})}
|
|
157
|
+
</View>
|
|
158
|
+
);
|
|
159
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import React, { useContext, useMemo } from 'react';
|
|
2
|
+
import type { ReactNode } from 'react';
|
|
3
|
+
import { createConfig } from '../core/config';
|
|
4
|
+
import type { ResponsiveConfigInput } from '../core/types';
|
|
5
|
+
import { ConfigContext } from './context';
|
|
6
|
+
|
|
7
|
+
export interface ResponsiveProviderProps {
|
|
8
|
+
/** Partial configuration merged over the parent provider's (or the defaults). */
|
|
9
|
+
config?: ResponsiveConfigInput;
|
|
10
|
+
children?: ReactNode;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Optional. Use it only to customise breakpoints or scaling; every hook and component
|
|
15
|
+
* works without it. Providers can be nested: inner ones inherit from outer ones.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* <ResponsiveProvider config={{ breakpoints: { md: 640 }, maxScale: 1.25 }}>
|
|
19
|
+
* <App />
|
|
20
|
+
* </ResponsiveProvider>
|
|
21
|
+
*/
|
|
22
|
+
export function ResponsiveProvider({ config, children }: ResponsiveProviderProps): React.JSX.Element {
|
|
23
|
+
const parent = useContext(ConfigContext);
|
|
24
|
+
// Config is plain data, so a serialised key lets callers pass inline objects
|
|
25
|
+
// without re-rendering the whole tree on every parent render.
|
|
26
|
+
const key = config ? JSON.stringify(config) : '';
|
|
27
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
28
|
+
const value = useMemo(() => createConfig(config, parent), [parent, key]);
|
|
29
|
+
return <ConfigContext.Provider value={value}>{children}</ConfigContext.Provider>;
|
|
30
|
+
}
|