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.
Files changed (100) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/LICENSE +21 -0
  3. package/README.md +459 -2
  4. package/docs/API.md +311 -0
  5. package/lib/core/breakpoints.d.ts +28 -0
  6. package/lib/core/breakpoints.d.ts.map +1 -0
  7. package/lib/core/breakpoints.js +108 -0
  8. package/lib/core/breakpoints.js.map +1 -0
  9. package/lib/core/config.d.ts +24 -0
  10. package/lib/core/config.d.ts.map +1 -0
  11. package/lib/core/config.js +112 -0
  12. package/lib/core/config.js.map +1 -0
  13. package/lib/core/device.d.ts +31 -0
  14. package/lib/core/device.d.ts.map +1 -0
  15. package/lib/core/device.js +35 -0
  16. package/lib/core/device.js.map +1 -0
  17. package/lib/core/grid.d.ts +30 -0
  18. package/lib/core/grid.d.ts.map +1 -0
  19. package/lib/core/grid.js +37 -0
  20. package/lib/core/grid.js.map +1 -0
  21. package/lib/core/info.d.ts +26 -0
  22. package/lib/core/info.d.ts.map +1 -0
  23. package/lib/core/info.js +41 -0
  24. package/lib/core/info.js.map +1 -0
  25. package/lib/core/layout.d.ts +4 -0
  26. package/lib/core/layout.d.ts.map +1 -0
  27. package/lib/core/layout.js +31 -0
  28. package/lib/core/layout.js.map +1 -0
  29. package/lib/core/scaling.d.ts +28 -0
  30. package/lib/core/scaling.d.ts.map +1 -0
  31. package/lib/core/scaling.js +47 -0
  32. package/lib/core/scaling.js.map +1 -0
  33. package/lib/core/types.d.ts +153 -0
  34. package/lib/core/types.d.ts.map +1 -0
  35. package/lib/core/types.js +8 -0
  36. package/lib/core/types.js.map +1 -0
  37. package/lib/index.d.ts +26 -0
  38. package/lib/index.d.ts.map +1 -0
  39. package/lib/index.js +54 -0
  40. package/lib/index.js.map +1 -0
  41. package/lib/native/ResponsiveGrid.d.ts +61 -0
  42. package/lib/native/ResponsiveGrid.d.ts.map +1 -0
  43. package/lib/native/ResponsiveGrid.js +110 -0
  44. package/lib/native/ResponsiveGrid.js.map +1 -0
  45. package/lib/native/ResponsiveProvider.d.ts +19 -0
  46. package/lib/native/ResponsiveProvider.d.ts.map +1 -0
  47. package/lib/native/ResponsiveProvider.js +26 -0
  48. package/lib/native/ResponsiveProvider.js.map +1 -0
  49. package/lib/native/ResponsiveView.d.ts +30 -0
  50. package/lib/native/ResponsiveView.d.ts.map +1 -0
  51. package/lib/native/ResponsiveView.js +51 -0
  52. package/lib/native/ResponsiveView.js.map +1 -0
  53. package/lib/native/Visibility.d.ts +45 -0
  54. package/lib/native/Visibility.d.ts.map +1 -0
  55. package/lib/native/Visibility.js +53 -0
  56. package/lib/native/Visibility.js.map +1 -0
  57. package/lib/native/context.d.ts +6 -0
  58. package/lib/native/context.d.ts.map +1 -0
  59. package/lib/native/context.js +12 -0
  60. package/lib/native/context.js.map +1 -0
  61. package/lib/native/createStyles.d.ts +46 -0
  62. package/lib/native/createStyles.d.ts.map +1 -0
  63. package/lib/native/createStyles.js +68 -0
  64. package/lib/native/createStyles.js.map +1 -0
  65. package/lib/native/hooks.d.ts +33 -0
  66. package/lib/native/hooks.d.ts.map +1 -0
  67. package/lib/native/hooks.js +68 -0
  68. package/lib/native/hooks.js.map +1 -0
  69. package/lib/native/platform.d.ts +10 -0
  70. package/lib/native/platform.d.ts.map +1 -0
  71. package/lib/native/platform.js +50 -0
  72. package/lib/native/platform.js.map +1 -0
  73. package/lib/native/snapshot.d.ts +8 -0
  74. package/lib/native/snapshot.d.ts.map +1 -0
  75. package/lib/native/snapshot.js +29 -0
  76. package/lib/native/snapshot.js.map +1 -0
  77. package/lib/native/store.d.ts +14 -0
  78. package/lib/native/store.d.ts.map +1 -0
  79. package/lib/native/store.js +51 -0
  80. package/lib/native/store.js.map +1 -0
  81. package/package.json +50 -4
  82. package/src/core/breakpoints.ts +101 -0
  83. package/src/core/config.ts +130 -0
  84. package/src/core/device.ts +46 -0
  85. package/src/core/grid.ts +54 -0
  86. package/src/core/info.ts +64 -0
  87. package/src/core/layout.ts +44 -0
  88. package/src/core/scaling.ts +62 -0
  89. package/src/core/types.ts +178 -0
  90. package/src/index.ts +59 -0
  91. package/src/native/ResponsiveGrid.tsx +159 -0
  92. package/src/native/ResponsiveProvider.tsx +30 -0
  93. package/src/native/ResponsiveView.tsx +98 -0
  94. package/src/native/Visibility.tsx +82 -0
  95. package/src/native/context.ts +11 -0
  96. package/src/native/createStyles.ts +94 -0
  97. package/src/native/hooks.ts +102 -0
  98. package/src/native/platform.ts +54 -0
  99. package/src/native/snapshot.ts +27 -0
  100. package/src/native/store.ts +58 -0
@@ -0,0 +1,33 @@
1
+ import type { BreakpointKey, LayoutSource, Narrowable, ResponsiveConfig, ResponsiveInfo, ResponsiveObject, ResponsiveValue, SelectResult } from '../core/types';
2
+ export interface SourceOptions {
3
+ /**
4
+ * Which area to measure. Default `'auto'`: nearest `<ResponsiveView>` if any,
5
+ * otherwise the window.
6
+ */
7
+ source?: LayoutSource;
8
+ }
9
+ /** Returns the resolved configuration from the nearest provider (or the defaults). */
10
+ export declare function useResponsiveConfig(): ResponsiveConfig;
11
+ /**
12
+ * The main hook: layout, breakpoints, device type and scaling in one object.
13
+ * Re-renders when the window, screen, font scale or nearest container changes.
14
+ *
15
+ * @example
16
+ * const r = useResponsive();
17
+ * <View style={{ padding: r.s(16), flexDirection: r.from('md') ? 'row' : 'column' }} />
18
+ */
19
+ export declare function useResponsive(options?: SourceOptions): ResponsiveInfo;
20
+ /**
21
+ * Returns only the active breakpoint. Optimised: when measuring the window, the component
22
+ * re-renders only when the breakpoint *changes*, not on every pixel of a window resize.
23
+ */
24
+ export declare function useBreakpoint(options?: SourceOptions): BreakpointKey;
25
+ /**
26
+ * Resolves a responsive value for the active breakpoint.
27
+ *
28
+ * @example
29
+ * const columns = useResponsiveValue({ xs: 1, md: 2, xl: 4 });
30
+ */
31
+ export declare function useResponsiveValue<const V extends ResponsiveObject<unknown>>(value: V, options?: SourceOptions): SelectResult<V>;
32
+ export declare function useResponsiveValue<T extends Narrowable>(value: ResponsiveValue<T>, options?: SourceOptions): T;
33
+ //# sourceMappingURL=hooks.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../src/native/hooks.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACV,aAAa,EAEb,YAAY,EACZ,UAAU,EACV,gBAAgB,EAChB,cAAc,EACd,gBAAgB,EAChB,eAAe,EACf,YAAY,EACb,MAAM,eAAe,CAAC;AAKvB,MAAM,WAAW,aAAa;IAC5B;;;OAGG;IACH,MAAM,CAAC,EAAE,YAAY,CAAC;CACvB;AASD,sFAAsF;AACtF,wBAAgB,mBAAmB,IAAI,gBAAgB,CAEtD;AAED;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,OAAO,CAAC,EAAE,aAAa,GAAG,cAAc,CAsBrE;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,OAAO,CAAC,EAAE,aAAa,GAAG,aAAa,CAUpE;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,CAAC,CAAC,SAAS,gBAAgB,CAAC,OAAO,CAAC,EAC1E,KAAK,EAAE,CAAC,EACR,OAAO,CAAC,EAAE,aAAa,GACtB,YAAY,CAAC,CAAC,CAAC,CAAC;AACnB,wBAAgB,kBAAkB,CAAC,CAAC,SAAS,UAAU,EAAE,KAAK,EAAE,eAAe,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,CAAC,CAAC"}
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useResponsiveConfig = useResponsiveConfig;
4
+ exports.useResponsive = useResponsive;
5
+ exports.useBreakpoint = useBreakpoint;
6
+ exports.useResponsiveValue = useResponsiveValue;
7
+ const react_1 = require("react");
8
+ const breakpoints_1 = require("../core/breakpoints");
9
+ const device_1 = require("../core/device");
10
+ const info_1 = require("../core/info");
11
+ const context_1 = require("./context");
12
+ const platform_1 = require("./platform");
13
+ const store_1 = require("./store");
14
+ function pickContainer(source, container) {
15
+ if (source === 'container' && !container) {
16
+ (0, platform_1.warnOnce)('source "container" was requested but there is no <ResponsiveView> ancestor; using the window.');
17
+ }
18
+ return source === 'window' ? null : container;
19
+ }
20
+ /** Returns the resolved configuration from the nearest provider (or the defaults). */
21
+ function useResponsiveConfig() {
22
+ return (0, react_1.useContext)(context_1.ConfigContext);
23
+ }
24
+ /**
25
+ * The main hook: layout, breakpoints, device type and scaling in one object.
26
+ * Re-renders when the window, screen, font scale or nearest container changes.
27
+ *
28
+ * @example
29
+ * const r = useResponsive();
30
+ * <View style={{ padding: r.s(16), flexDirection: r.from('md') ? 'row' : 'column' }} />
31
+ */
32
+ function useResponsive(options) {
33
+ var _a;
34
+ const config = (0, react_1.useContext)(context_1.ConfigContext);
35
+ const containerContext = (0, react_1.useContext)(context_1.ContainerContext);
36
+ const source = (_a = options === null || options === void 0 ? void 0 : options.source) !== null && _a !== void 0 ? _a : 'auto';
37
+ const container = pickContainer(source, containerContext);
38
+ const { window, screen } = (0, react_1.useSyncExternalStore)(store_1.subscribe, store_1.getSnapshot, store_1.getSnapshot);
39
+ return (0, react_1.useMemo)(() => (0, info_1.createResponsiveInfo)({
40
+ window,
41
+ screen,
42
+ fontScale: window.fontScale,
43
+ pixelRatio: window.scale,
44
+ deviceType: (0, device_1.classifyDevice)((0, platform_1.getDeviceEnvironment)(screen.width, screen.height), config),
45
+ config,
46
+ container,
47
+ source,
48
+ round: platform_1.roundToPixel,
49
+ }), [window, screen, config, container, source]);
50
+ }
51
+ /**
52
+ * Returns only the active breakpoint. Optimised: when measuring the window, the component
53
+ * re-renders only when the breakpoint *changes*, not on every pixel of a window resize.
54
+ */
55
+ function useBreakpoint(options) {
56
+ var _a;
57
+ const config = (0, react_1.useContext)(context_1.ConfigContext);
58
+ const containerContext = (0, react_1.useContext)(context_1.ContainerContext);
59
+ const container = pickContainer((_a = options === null || options === void 0 ? void 0 : options.source) !== null && _a !== void 0 ? _a : 'auto', containerContext);
60
+ const getWindowBreakpoint = (0, react_1.useCallback)(() => (0, breakpoints_1.getBreakpoint)((0, store_1.getSnapshot)().window.width, config.breakpoints), [config.breakpoints]);
61
+ const windowBreakpoint = (0, react_1.useSyncExternalStore)(store_1.subscribe, getWindowBreakpoint, getWindowBreakpoint);
62
+ return container ? container.breakpoint : windowBreakpoint;
63
+ }
64
+ function useResponsiveValue(value, options) {
65
+ const breakpoint = useBreakpoint(options);
66
+ return (0, breakpoints_1.resolveResponsiveValue)(value, breakpoint);
67
+ }
68
+ //# sourceMappingURL=hooks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hooks.js","sourceRoot":"","sources":["../../src/native/hooks.ts"],"names":[],"mappings":";;AAmCA,kDAEC;AAUD,sCAsBC;AAMD,sCAUC;AAaD,gDAGC;AArGD,iCAA+E;AAC/E,qDAA4E;AAC5E,2CAAgD;AAChD,uCAAoD;AAYpD,uCAA4D;AAC5D,yCAA0E;AAC1E,mCAAiD;AAUjD,SAAS,aAAa,CAAC,MAAoB,EAAE,SAA4B;IACvE,IAAI,MAAM,KAAK,WAAW,IAAI,CAAC,SAAS,EAAE,CAAC;QACzC,IAAA,mBAAQ,EAAC,+FAA+F,CAAC,CAAC;IAC5G,CAAC;IACD,OAAO,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC;AAChD,CAAC;AAED,sFAAsF;AACtF,SAAgB,mBAAmB;IACjC,OAAO,IAAA,kBAAU,EAAC,uBAAa,CAAC,CAAC;AACnC,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,aAAa,CAAC,OAAuB;;IACnD,MAAM,MAAM,GAAG,IAAA,kBAAU,EAAC,uBAAa,CAAC,CAAC;IACzC,MAAM,gBAAgB,GAAG,IAAA,kBAAU,EAAC,0BAAgB,CAAC,CAAC;IACtD,MAAM,MAAM,GAAG,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,MAAM,mCAAI,MAAM,CAAC;IACzC,MAAM,SAAS,GAAG,aAAa,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAC;IAC1D,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,IAAA,4BAAoB,EAAC,iBAAS,EAAE,mBAAW,EAAE,mBAAW,CAAC,CAAC;IAErF,OAAO,IAAA,eAAO,EACZ,GAAG,EAAE,CACH,IAAA,2BAAoB,EAAC;QACnB,MAAM;QACN,MAAM;QACN,SAAS,EAAE,MAAM,CAAC,SAAS;QAC3B,UAAU,EAAE,MAAM,CAAC,KAAK;QACxB,UAAU,EAAE,IAAA,uBAAc,EAAC,IAAA,+BAAoB,EAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;QACrF,MAAM;QACN,SAAS;QACT,MAAM;QACN,KAAK,EAAE,uBAAY;KACpB,CAAC,EACJ,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,CAAC,CAC5C,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,SAAgB,aAAa,CAAC,OAAuB;;IACnD,MAAM,MAAM,GAAG,IAAA,kBAAU,EAAC,uBAAa,CAAC,CAAC;IACzC,MAAM,gBAAgB,GAAG,IAAA,kBAAU,EAAC,0BAAgB,CAAC,CAAC;IACtD,MAAM,SAAS,GAAG,aAAa,CAAC,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,MAAM,mCAAI,MAAM,EAAE,gBAAgB,CAAC,CAAC;IAC7E,MAAM,mBAAmB,GAAG,IAAA,mBAAW,EACrC,GAAG,EAAE,CAAC,IAAA,2BAAa,EAAC,IAAA,mBAAW,GAAE,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,WAAW,CAAC,EACnE,CAAC,MAAM,CAAC,WAAW,CAAC,CACrB,CAAC;IACF,MAAM,gBAAgB,GAAG,IAAA,4BAAoB,EAAC,iBAAS,EAAE,mBAAmB,EAAE,mBAAmB,CAAC,CAAC;IACnG,OAAO,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC,gBAAgB,CAAC;AAC7D,CAAC;AAaD,SAAgB,kBAAkB,CAAC,KAA+B,EAAE,OAAuB;IACzF,MAAM,UAAU,GAAG,aAAa,CAAC,OAAO,CAAC,CAAC;IAC1C,OAAO,IAAA,oCAAsB,EAAC,KAAK,EAAE,UAAU,CAAC,CAAC;AACnD,CAAC"}
@@ -0,0 +1,10 @@
1
+ import type { DeviceEnvironment } from '../core/device';
2
+ /** Web only: whether the primary pointer is coarse (touch). Undefined when unknown. */
3
+ export declare function detectCoarsePointer(): boolean | undefined;
4
+ export declare function getDeviceEnvironment(screenWidth: number, screenHeight: number): DeviceEnvironment;
5
+ /** Rounds a dp value to the nearest physical pixel. */
6
+ export declare function roundToPixel(value: number): number;
7
+ export declare function isDev(): boolean;
8
+ /** Logs a development-only warning once per message. */
9
+ export declare function warnOnce(message: string): void;
10
+ //# sourceMappingURL=platform.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"platform.d.ts","sourceRoot":"","sources":["../../src/native/platform.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAWxD,uFAAuF;AACvF,wBAAgB,mBAAmB,IAAI,OAAO,GAAG,SAAS,CASzD;AAED,wBAAgB,oBAAoB,CAAC,WAAW,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,iBAAiB,CAUjG;AAED,uDAAuD;AACvD,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAElD;AAED,wBAAgB,KAAK,IAAI,OAAO,CAE/B;AAID,wDAAwD;AACxD,wBAAgB,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAK9C"}
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.detectCoarsePointer = detectCoarsePointer;
4
+ exports.getDeviceEnvironment = getDeviceEnvironment;
5
+ exports.roundToPixel = roundToPixel;
6
+ exports.isDev = isDev;
7
+ exports.warnOnce = warnOnce;
8
+ const react_native_1 = require("react-native");
9
+ /** Web only: whether the primary pointer is coarse (touch). Undefined when unknown. */
10
+ function detectCoarsePointer() {
11
+ if (react_native_1.Platform.OS !== 'web')
12
+ return undefined;
13
+ const host = globalThis;
14
+ try {
15
+ if (typeof host.matchMedia === 'function')
16
+ return host.matchMedia('(pointer: coarse)').matches;
17
+ }
18
+ catch {
19
+ // Not available (e.g. server rendering). Fall through.
20
+ }
21
+ return undefined;
22
+ }
23
+ function getDeviceEnvironment(screenWidth, screenHeight) {
24
+ const flags = react_native_1.Platform;
25
+ return {
26
+ os: react_native_1.Platform.OS,
27
+ isPad: flags.isPad === true,
28
+ isTV: flags.isTV === true,
29
+ screenWidth,
30
+ screenHeight,
31
+ hasCoarsePointer: detectCoarsePointer(),
32
+ };
33
+ }
34
+ /** Rounds a dp value to the nearest physical pixel. */
35
+ function roundToPixel(value) {
36
+ return react_native_1.PixelRatio.roundToNearestPixel(value);
37
+ }
38
+ function isDev() {
39
+ return typeof __DEV__ !== 'undefined' && __DEV__ === true;
40
+ }
41
+ const warned = new Set();
42
+ /** Logs a development-only warning once per message. */
43
+ function warnOnce(message) {
44
+ if (!isDev() || warned.has(message))
45
+ return;
46
+ warned.add(message);
47
+ // eslint-disable-next-line no-console
48
+ console.warn(`[react-native-everything-responsive] ${message}`);
49
+ }
50
+ //# sourceMappingURL=platform.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"platform.js","sourceRoot":"","sources":["../../src/native/platform.ts"],"names":[],"mappings":";;AAaA,kDASC;AAED,oDAUC;AAGD,oCAEC;AAED,sBAEC;AAKD,4BAKC;AArDD,+CAAoD;AAYpD,uFAAuF;AACvF,SAAgB,mBAAmB;IACjC,IAAI,uBAAQ,CAAC,EAAE,KAAK,KAAK;QAAE,OAAO,SAAS,CAAC;IAC5C,MAAM,IAAI,GAAG,UAAuC,CAAC;IACrD,IAAI,CAAC;QACH,IAAI,OAAO,IAAI,CAAC,UAAU,KAAK,UAAU;YAAE,OAAO,IAAI,CAAC,UAAU,CAAC,mBAAmB,CAAC,CAAC,OAAO,CAAC;IACjG,CAAC;IAAC,MAAM,CAAC;QACP,uDAAuD;IACzD,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,SAAgB,oBAAoB,CAAC,WAAmB,EAAE,YAAoB;IAC5E,MAAM,KAAK,GAAG,uBAAoC,CAAC;IACnD,OAAO;QACL,EAAE,EAAE,uBAAQ,CAAC,EAAE;QACf,KAAK,EAAE,KAAK,CAAC,KAAK,KAAK,IAAI;QAC3B,IAAI,EAAE,KAAK,CAAC,IAAI,KAAK,IAAI;QACzB,WAAW;QACX,YAAY;QACZ,gBAAgB,EAAE,mBAAmB,EAAE;KACxC,CAAC;AACJ,CAAC;AAED,uDAAuD;AACvD,SAAgB,YAAY,CAAC,KAAa;IACxC,OAAO,yBAAU,CAAC,mBAAmB,CAAC,KAAK,CAAC,CAAC;AAC/C,CAAC;AAED,SAAgB,KAAK;IACnB,OAAO,OAAO,OAAO,KAAK,WAAW,IAAI,OAAO,KAAK,IAAI,CAAC;AAC5D,CAAC;AAED,MAAM,MAAM,GAAG,IAAI,GAAG,EAAU,CAAC;AAEjC,wDAAwD;AACxD,SAAgB,QAAQ,CAAC,OAAe;IACtC,IAAI,CAAC,KAAK,EAAE,IAAI,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC;QAAE,OAAO;IAC5C,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IACpB,sCAAsC;IACtC,OAAO,CAAC,IAAI,CAAC,wCAAwC,OAAO,EAAE,CAAC,CAAC;AAClE,CAAC"}
@@ -0,0 +1,8 @@
1
+ import type { ResponsiveConfigInput, ResponsiveInfo } from '../core/types';
2
+ /**
3
+ * Non-reactive snapshot for code that runs outside React components (navigation options,
4
+ * analytics, module-level constants). It reads the metrics **once**: the result does not
5
+ * update on rotation or resize. Inside components, use `useResponsive()` instead.
6
+ */
7
+ export declare function getResponsive(config?: ResponsiveConfigInput): ResponsiveInfo;
8
+ //# sourceMappingURL=snapshot.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"snapshot.d.ts","sourceRoot":"","sources":["../../src/native/snapshot.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,qBAAqB,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAI3E;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,MAAM,CAAC,EAAE,qBAAqB,GAAG,cAAc,CAc5E"}
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getResponsive = getResponsive;
4
+ const config_1 = require("../core/config");
5
+ const device_1 = require("../core/device");
6
+ const info_1 = require("../core/info");
7
+ const platform_1 = require("./platform");
8
+ const store_1 = require("./store");
9
+ /**
10
+ * Non-reactive snapshot for code that runs outside React components (navigation options,
11
+ * analytics, module-level constants). It reads the metrics **once**: the result does not
12
+ * update on rotation or resize. Inside components, use `useResponsive()` instead.
13
+ */
14
+ function getResponsive(config) {
15
+ const resolved = config ? (0, config_1.createConfig)(config, config_1.DEFAULT_CONFIG) : config_1.DEFAULT_CONFIG;
16
+ const { window, screen } = (0, store_1.getSnapshot)();
17
+ return (0, info_1.createResponsiveInfo)({
18
+ window,
19
+ screen,
20
+ fontScale: window.fontScale,
21
+ pixelRatio: window.scale,
22
+ deviceType: (0, device_1.classifyDevice)((0, platform_1.getDeviceEnvironment)(screen.width, screen.height), resolved),
23
+ config: resolved,
24
+ container: null,
25
+ source: 'window',
26
+ round: platform_1.roundToPixel,
27
+ });
28
+ }
29
+ //# sourceMappingURL=snapshot.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"snapshot.js","sourceRoot":"","sources":["../../src/native/snapshot.ts"],"names":[],"mappings":";;AAYA,sCAcC;AA1BD,2CAA8D;AAC9D,2CAAgD;AAChD,uCAAoD;AAEpD,yCAAgE;AAChE,mCAAsC;AAEtC;;;;GAIG;AACH,SAAgB,aAAa,CAAC,MAA8B;IAC1D,MAAM,QAAQ,GAAG,MAAM,CAAC,CAAC,CAAC,IAAA,qBAAY,EAAC,MAAM,EAAE,uBAAc,CAAC,CAAC,CAAC,CAAC,uBAAc,CAAC;IAChF,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,IAAA,mBAAW,GAAE,CAAC;IACzC,OAAO,IAAA,2BAAoB,EAAC;QAC1B,MAAM;QACN,MAAM;QACN,SAAS,EAAE,MAAM,CAAC,SAAS;QAC3B,UAAU,EAAE,MAAM,CAAC,KAAK;QACxB,UAAU,EAAE,IAAA,uBAAc,EAAC,IAAA,+BAAoB,EAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,QAAQ,CAAC;QACvF,MAAM,EAAE,QAAQ;QAChB,SAAS,EAAE,IAAI;QACf,MAAM,EAAE,QAAQ;QAChB,KAAK,EAAE,uBAAY;KACpB,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,14 @@
1
+ import type { ScaledSize } from 'react-native';
2
+ /** Immutable snapshot of window and screen metrics. */
3
+ export interface DimensionsSnapshot {
4
+ readonly window: ScaledSize;
5
+ readonly screen: ScaledSize;
6
+ }
7
+ /**
8
+ * One shared `Dimensions` subscription for the whole app, no matter how many components
9
+ * use the hooks. It is created on first use and removed when the last listener leaves.
10
+ */
11
+ export declare function subscribe(listener: () => void): () => void;
12
+ /** Current snapshot. Its identity changes only when a metric actually changes. */
13
+ export declare function getSnapshot(): DimensionsSnapshot;
14
+ //# sourceMappingURL=store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../src/native/store.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE/C,uDAAuD;AACvD,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,MAAM,EAAE,UAAU,CAAC;IAC5B,QAAQ,CAAC,MAAM,EAAE,UAAU,CAAC;CAC7B;AAyBD;;;GAGG;AACH,wBAAgB,SAAS,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI,CAgB1D;AAED,kFAAkF;AAClF,wBAAgB,WAAW,IAAI,kBAAkB,CAEhD"}
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.subscribe = subscribe;
4
+ exports.getSnapshot = getSnapshot;
5
+ const react_native_1 = require("react-native");
6
+ function read() {
7
+ return { window: react_native_1.Dimensions.get('window'), screen: react_native_1.Dimensions.get('screen') };
8
+ }
9
+ function sameSize(a, b) {
10
+ return a.width === b.width && a.height === b.height && a.scale === b.scale && a.fontScale === b.fontScale;
11
+ }
12
+ function sameSnapshot(a, b) {
13
+ return sameSize(a.window, b.window) && sameSize(a.screen, b.screen);
14
+ }
15
+ let snapshot = read();
16
+ const listeners = new Set();
17
+ let subscription;
18
+ function handleChange(next) {
19
+ const fresh = { window: { ...next.window }, screen: { ...next.screen } };
20
+ if (sameSnapshot(fresh, snapshot))
21
+ return;
22
+ snapshot = fresh;
23
+ listeners.forEach((listener) => listener());
24
+ }
25
+ /**
26
+ * One shared `Dimensions` subscription for the whole app, no matter how many components
27
+ * use the hooks. It is created on first use and removed when the last listener leaves.
28
+ */
29
+ function subscribe(listener) {
30
+ listeners.add(listener);
31
+ if (!subscription) {
32
+ subscription = react_native_1.Dimensions.addEventListener('change', handleChange);
33
+ // Metrics may have changed while nobody was listening. Refresh silently:
34
+ // useSyncExternalStore re-reads the snapshot right after subscribing.
35
+ const fresh = read();
36
+ if (!sameSnapshot(fresh, snapshot))
37
+ snapshot = fresh;
38
+ }
39
+ return () => {
40
+ listeners.delete(listener);
41
+ if (listeners.size === 0 && subscription) {
42
+ subscription.remove();
43
+ subscription = undefined;
44
+ }
45
+ };
46
+ }
47
+ /** Current snapshot. Its identity changes only when a metric actually changes. */
48
+ function getSnapshot() {
49
+ return snapshot;
50
+ }
51
+ //# sourceMappingURL=store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.js","sourceRoot":"","sources":["../../src/native/store.ts"],"names":[],"mappings":";;AAoCA,8BAgBC;AAGD,kCAEC;AAzDD,+CAA0C;AAS1C,SAAS,IAAI;IACX,OAAO,EAAE,MAAM,EAAE,yBAAU,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,MAAM,EAAE,yBAAU,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC;AAChF,CAAC;AAED,SAAS,QAAQ,CAAC,CAAa,EAAE,CAAa;IAC5C,OAAO,CAAC,CAAC,KAAK,KAAK,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM,IAAI,CAAC,CAAC,KAAK,KAAK,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,SAAS,KAAK,CAAC,CAAC,SAAS,CAAC;AAC5G,CAAC;AAED,SAAS,YAAY,CAAC,CAAqB,EAAE,CAAqB;IAChE,OAAO,QAAQ,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,IAAI,QAAQ,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC;AACtE,CAAC;AAED,IAAI,QAAQ,GAAuB,IAAI,EAAE,CAAC;AAC1C,MAAM,SAAS,GAAG,IAAI,GAAG,EAAc,CAAC;AACxC,IAAI,YAAgD,CAAC;AAErD,SAAS,YAAY,CAAC,IAAwB;IAC5C,MAAM,KAAK,GAAG,EAAE,MAAM,EAAE,EAAE,GAAG,IAAI,CAAC,MAAM,EAAE,EAAE,MAAM,EAAE,EAAE,GAAG,IAAI,CAAC,MAAM,EAAE,EAAE,CAAC;IACzE,IAAI,YAAY,CAAC,KAAK,EAAE,QAAQ,CAAC;QAAE,OAAO;IAC1C,QAAQ,GAAG,KAAK,CAAC;IACjB,SAAS,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAC;AAC9C,CAAC;AAED;;;GAGG;AACH,SAAgB,SAAS,CAAC,QAAoB;IAC5C,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IACxB,IAAI,CAAC,YAAY,EAAE,CAAC;QAClB,YAAY,GAAG,yBAAU,CAAC,gBAAgB,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC;QACnE,yEAAyE;QACzE,sEAAsE;QACtE,MAAM,KAAK,GAAG,IAAI,EAAE,CAAC;QACrB,IAAI,CAAC,YAAY,CAAC,KAAK,EAAE,QAAQ,CAAC;YAAE,QAAQ,GAAG,KAAK,CAAC;IACvD,CAAC;IACD,OAAO,GAAG,EAAE;QACV,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QAC3B,IAAI,SAAS,CAAC,IAAI,KAAK,CAAC,IAAI,YAAY,EAAE,CAAC;YACzC,YAAY,CAAC,MAAM,EAAE,CAAC;YACtB,YAAY,GAAG,SAAS,CAAC;QAC3B,CAAC;IACH,CAAC,CAAC;AACJ,CAAC;AAED,kFAAkF;AAClF,SAAgB,WAAW;IACzB,OAAO,QAAQ,CAAC;AAClB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,52 @@
1
1
  {
2
2
  "name": "react-native-everything-responsive",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.1",
4
+ "description": "Zero-dependency responsive design toolkit for React Native: breakpoints, container queries, device detection, safe scaling and accessible font sizing for phones, tablets, foldables, desktops and web.",
5
+ "main": "lib/index.js",
6
+ "types": "lib/index.d.ts",
7
+ "react-native": "src/index.ts",
8
+ "source": "src/index.ts",
9
+ "files": [
10
+ "src",
11
+ "lib",
12
+ "README.md",
13
+ "docs",
14
+ "LICENSE",
15
+ "CHANGELOG.md"
16
+ ],
17
+ "sideEffects": false,
18
+ "scripts": {
19
+ "build": "rm -rf lib && tsc -p tsconfig.build.json",
20
+ "typecheck": "tsc -p tsconfig.json --noEmit",
21
+ "test": "npm run build && node --test tests/*.test.js",
22
+ "prepublishOnly": "npm run typecheck && npm test"
23
+ },
24
+ "keywords": [
25
+ "breakpoints",
26
+ "container-queries",
27
+ "everything-responsive",
28
+ "foldable",
29
+ "media-query",
30
+ "react-native",
31
+ "react-native-web",
32
+ "responsive",
33
+ "scaling",
34
+ "tablet",
35
+ "zero-dependency"
36
+ ],
37
+ "license": "MIT",
38
+ "peerDependencies": {
39
+ "react": ">=18.0.0",
40
+ "react-native": ">=0.70.0"
41
+ },
42
+ "devDependencies": {
43
+ "@types/react": "^18.3.0",
44
+ "react": "18.3.1",
45
+ "react-native": "0.76.5",
46
+ "react-test-renderer": "18.3.1",
47
+ "typescript": "^5.6.0"
48
+ },
49
+ "publishConfig": {
50
+ "access": "public"
51
+ }
52
+ }
@@ -0,0 +1,101 @@
1
+ import { BREAKPOINT_KEYS } from './config';
2
+ import type { BreakpointKey, Breakpoints, ResponsiveObject, ResponsiveValue } from './types';
3
+
4
+ const RESPONSIVE_KEYS: ReadonlySet<string> = new Set<string>([...BREAKPOINT_KEYS, 'base']);
5
+
6
+ /** Returns the breakpoint whose range contains `width`. */
7
+ export function getBreakpoint(width: number, breakpoints: Breakpoints): BreakpointKey {
8
+ for (let i = BREAKPOINT_KEYS.length - 1; i > 0; i--) {
9
+ const key = BREAKPOINT_KEYS[i]!;
10
+ if (width >= breakpoints[key]) return key;
11
+ }
12
+ return 'xs';
13
+ }
14
+
15
+ /** Position of a breakpoint in the ascending order (xs = 0). Throws on unknown names. */
16
+ export function breakpointIndex(bp: BreakpointKey): number {
17
+ const index = BREAKPOINT_KEYS.indexOf(bp);
18
+ if (index < 0) {
19
+ throw new Error(
20
+ `[react-native-everything-responsive] Unknown breakpoint "${String(bp)}". Expected one of: ${BREAKPOINT_KEYS.join(', ')}.`,
21
+ );
22
+ }
23
+ return index;
24
+ }
25
+
26
+ /** `current >= target` */
27
+ export function isFrom(current: BreakpointKey, target: BreakpointKey): boolean {
28
+ return breakpointIndex(current) >= breakpointIndex(target);
29
+ }
30
+
31
+ /** `current < target` */
32
+ export function isBelow(current: BreakpointKey, target: BreakpointKey): boolean {
33
+ return breakpointIndex(current) < breakpointIndex(target);
34
+ }
35
+
36
+ /** `min <= current < max` */
37
+ export function isBetween(current: BreakpointKey, min: BreakpointKey, max: BreakpointKey): boolean {
38
+ return isFrom(current, min) && isBelow(current, max);
39
+ }
40
+
41
+ function isPlainObject(value: unknown): value is Record<string, unknown> {
42
+ if (value === null || typeof value !== 'object' || Array.isArray(value)) return false;
43
+ const proto: unknown = Object.getPrototypeOf(value);
44
+ return proto === Object.prototype || proto === null;
45
+ }
46
+
47
+ /**
48
+ * True when `value` is a plain object whose keys are all breakpoint names (or `base`).
49
+ * Objects such as `shadowOffset: { width, height }` are therefore never mistaken for
50
+ * responsive objects.
51
+ */
52
+ export function isResponsiveObject(value: unknown): value is ResponsiveObject<unknown> {
53
+ if (!isPlainObject(value)) return false;
54
+ const keys = Object.keys(value);
55
+ if (keys.length === 0) return false;
56
+ for (const key of keys) {
57
+ if (!RESPONSIVE_KEYS.has(key)) return false;
58
+ }
59
+ return true;
60
+ }
61
+
62
+ /**
63
+ * Resolve a responsive value for a breakpoint (mobile-first cascade):
64
+ * 1. the value at `bp`, else the closest smaller breakpoint that has a value;
65
+ * 2. else `base`;
66
+ * 3. else the closest larger breakpoint that has a value (so you never get `undefined`
67
+ * just because you forgot to define the smallest size).
68
+ */
69
+ export function resolveResponsiveValue<T>(
70
+ value: ResponsiveValue<T>,
71
+ bp: BreakpointKey,
72
+ ): T | undefined {
73
+ if (!isResponsiveObject(value)) return value as T;
74
+ const map = value as Record<string, T | undefined>;
75
+ const index = breakpointIndex(bp);
76
+ for (let i = index; i >= 0; i--) {
77
+ const candidate = map[BREAKPOINT_KEYS[i]!];
78
+ if (candidate !== undefined) return candidate;
79
+ }
80
+ if (map.base !== undefined) return map.base;
81
+ for (let i = index + 1; i < BREAKPOINT_KEYS.length; i++) {
82
+ const candidate = map[BREAKPOINT_KEYS[i]!];
83
+ if (candidate !== undefined) return candidate;
84
+ }
85
+ return undefined;
86
+ }
87
+
88
+ /** Recursively resolve every responsive object inside a value (arrays and plain objects). */
89
+ export function resolveDeep(value: unknown, bp: BreakpointKey): unknown {
90
+ if (isResponsiveObject(value)) return resolveDeep(resolveResponsiveValue(value, bp), bp);
91
+ if (Array.isArray(value)) return value.map((item) => resolveDeep(item, bp));
92
+ if (isPlainObject(value)) {
93
+ const out: Record<string, unknown> = {};
94
+ for (const key of Object.keys(value)) {
95
+ const resolved = resolveDeep(value[key], bp);
96
+ if (resolved !== undefined) out[key] = resolved;
97
+ }
98
+ return out;
99
+ }
100
+ return value;
101
+ }
@@ -0,0 +1,130 @@
1
+ import type {
2
+ BreakpointKey,
3
+ Breakpoints,
4
+ ResponsiveConfig,
5
+ ResponsiveConfigInput,
6
+ } from './types';
7
+
8
+ const PREFIX = '[react-native-everything-responsive]';
9
+
10
+ /** Breakpoint names in ascending order. */
11
+ export const BREAKPOINT_KEYS: readonly BreakpointKey[] = Object.freeze([
12
+ 'xs',
13
+ 'sm',
14
+ 'md',
15
+ 'lg',
16
+ 'xl',
17
+ 'xxl',
18
+ ] as const);
19
+
20
+ /**
21
+ * Default breakpoints (dp, measured against the window or container width).
22
+ *
23
+ * | key | from | typical target |
24
+ * |-----|--------|--------------------------------------------------|
25
+ * | xs | 0 | small phones (iPhone SE, compact Android) |
26
+ * | sm | 360 | standard and large phones |
27
+ * | md | 600 | small tablets, unfolded foldables, phone landscape|
28
+ * | lg | 900 | tablets in landscape, iPad Pro portrait |
29
+ * | xl | 1200 | laptops, small desktop windows |
30
+ * | xxl | 1536 | large desktop monitors |
31
+ */
32
+ export const DEFAULT_BREAKPOINTS: Breakpoints = Object.freeze({
33
+ xs: 0,
34
+ sm: 360,
35
+ md: 600,
36
+ lg: 900,
37
+ xl: 1200,
38
+ xxl: 1536,
39
+ });
40
+
41
+ export const DEFAULT_CONFIG: ResponsiveConfig = Object.freeze({
42
+ breakpoints: DEFAULT_BREAKPOINTS,
43
+ guidelineBaseWidth: 375,
44
+ guidelineBaseHeight: 812,
45
+ minScale: 0.85,
46
+ maxScale: 1.35,
47
+ maxFontScale: 2,
48
+ tabletMinShortSide: 600,
49
+ desktopMinWidth: 1024,
50
+ });
51
+
52
+ function assertFiniteNumber(name: string, value: unknown, min: number, exclusive: boolean): void {
53
+ const ok =
54
+ typeof value === 'number' &&
55
+ Number.isFinite(value) &&
56
+ (exclusive ? value > min : value >= min);
57
+ if (!ok) {
58
+ throw new Error(
59
+ `${PREFIX} "${name}" must be a finite number ${exclusive ? '>' : '>='} ${min}, received ${String(value)}.`,
60
+ );
61
+ }
62
+ }
63
+
64
+ /** Throws a descriptive error if the breakpoint table is not strictly ascending. */
65
+ export function validateBreakpoints(breakpoints: Breakpoints): void {
66
+ let previous = -Infinity;
67
+ let previousKey = '';
68
+ for (const key of BREAKPOINT_KEYS) {
69
+ const value = breakpoints[key];
70
+ assertFiniteNumber(`breakpoints.${key}`, value, 0, false);
71
+ if (value <= previous) {
72
+ throw new Error(
73
+ `${PREFIX} Breakpoints must be strictly ascending, but "${key}" (${value}) is not greater than "${previousKey}" (${previous}).`,
74
+ );
75
+ }
76
+ previous = value;
77
+ previousKey = key;
78
+ }
79
+ }
80
+
81
+ /** Merge a partial breakpoint table into a base table and validate the result. */
82
+ export function mergeBreakpoints(
83
+ base: Breakpoints,
84
+ override?: Partial<Record<BreakpointKey, number>>,
85
+ ): Breakpoints {
86
+ if (!override) return base;
87
+ const merged: Record<BreakpointKey, number> = { ...base };
88
+ for (const key of Object.keys(override) as BreakpointKey[]) {
89
+ if (!BREAKPOINT_KEYS.includes(key)) {
90
+ throw new Error(
91
+ `${PREFIX} Unknown breakpoint "${key}". Expected one of: ${BREAKPOINT_KEYS.join(', ')}.`,
92
+ );
93
+ }
94
+ const value = override[key];
95
+ if (value !== undefined) merged[key] = value;
96
+ }
97
+ validateBreakpoints(merged);
98
+ return Object.freeze(merged);
99
+ }
100
+
101
+ /** Merge user configuration on top of a base configuration and validate everything. */
102
+ export function createConfig(
103
+ input?: ResponsiveConfigInput,
104
+ base: ResponsiveConfig = DEFAULT_CONFIG,
105
+ ): ResponsiveConfig {
106
+ if (!input) return base;
107
+ const config: ResponsiveConfig = {
108
+ breakpoints: mergeBreakpoints(base.breakpoints, input.breakpoints),
109
+ guidelineBaseWidth: input.guidelineBaseWidth ?? base.guidelineBaseWidth,
110
+ guidelineBaseHeight: input.guidelineBaseHeight ?? base.guidelineBaseHeight,
111
+ minScale: input.minScale ?? base.minScale,
112
+ maxScale: input.maxScale ?? base.maxScale,
113
+ maxFontScale: input.maxFontScale ?? base.maxFontScale,
114
+ tabletMinShortSide: input.tabletMinShortSide ?? base.tabletMinShortSide,
115
+ desktopMinWidth: input.desktopMinWidth ?? base.desktopMinWidth,
116
+ };
117
+ assertFiniteNumber('guidelineBaseWidth', config.guidelineBaseWidth, 0, true);
118
+ assertFiniteNumber('guidelineBaseHeight', config.guidelineBaseHeight, 0, true);
119
+ assertFiniteNumber('minScale', config.minScale, 0, true);
120
+ assertFiniteNumber('maxScale', config.maxScale, 0, true);
121
+ assertFiniteNumber('maxFontScale', config.maxFontScale, 0, true);
122
+ assertFiniteNumber('tabletMinShortSide', config.tabletMinShortSide, 0, true);
123
+ assertFiniteNumber('desktopMinWidth', config.desktopMinWidth, 0, true);
124
+ if (config.minScale > config.maxScale) {
125
+ throw new Error(
126
+ `${PREFIX} "minScale" (${config.minScale}) must not be greater than "maxScale" (${config.maxScale}).`,
127
+ );
128
+ }
129
+ return Object.freeze(config);
130
+ }
@@ -0,0 +1,46 @@
1
+ import type { DeviceType, ResponsiveConfig } from './types';
2
+
3
+ /** Platform facts needed to classify a device. Collected by the native layer. */
4
+ export interface DeviceEnvironment {
5
+ /** `Platform.OS`: 'ios' | 'android' | 'web' | 'windows' | 'macos' | ... */
6
+ os: string;
7
+ /** `Platform.isPad` on iOS. */
8
+ isPad?: boolean;
9
+ /** `Platform.isTV`. */
10
+ isTV?: boolean;
11
+ /** Physical screen size in dp (not the window). */
12
+ screenWidth: number;
13
+ screenHeight: number;
14
+ /** Web only: result of `matchMedia('(pointer: coarse)')`, or undefined when unknown. */
15
+ hasCoarsePointer?: boolean;
16
+ }
17
+
18
+ /**
19
+ * Classifies the **hardware**, not the current window. An iPad in 1/3 split view is still
20
+ * a `tablet` (use breakpoints for layout), and a desktop browser resized to 400 dp is still
21
+ * a `desktop`.
22
+ *
23
+ * Rules:
24
+ * - TV flag → `tv`
25
+ * - Windows / macOS (react-native-windows / -macos) → `desktop`
26
+ * - iOS → `Platform.isPad` decides between `tablet` and `phone`
27
+ * - Web → fine pointer and long side ≥ `desktopMinWidth` → `desktop`;
28
+ * otherwise short side ≥ `tabletMinShortSide` → `tablet`; else `phone`
29
+ * - Android and others → short side ≥ `tabletMinShortSide` → `tablet` (the Android
30
+ * "sw600dp" convention, which also classifies unfolded foldables correctly)
31
+ */
32
+ export function classifyDevice(env: DeviceEnvironment, config: ResponsiveConfig): DeviceType {
33
+ if (env.isTV) return 'tv';
34
+ const os = env.os;
35
+ if (os === 'windows' || os === 'macos') return 'desktop';
36
+ if (os === 'ios') return env.isPad ? 'tablet' : 'phone';
37
+
38
+ const shortSide = Math.min(env.screenWidth, env.screenHeight);
39
+ const longSide = Math.max(env.screenWidth, env.screenHeight);
40
+
41
+ if (os === 'web') {
42
+ if (env.hasCoarsePointer !== true && longSide >= config.desktopMinWidth) return 'desktop';
43
+ return shortSide >= config.tabletMinShortSide ? 'tablet' : 'phone';
44
+ }
45
+ return shortSide >= config.tabletMinShortSide ? 'tablet' : 'phone';
46
+ }