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/docs/API.md
ADDED
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
# API reference
|
|
2
|
+
|
|
3
|
+
This is the complete reference for every export of `react-native-everything-responsive`. For concepts and guides, see the [README](../README.md).
|
|
4
|
+
|
|
5
|
+
**Conventions**
|
|
6
|
+
- All sizes are in **dp** (density-independent pixels) unless stated otherwise.
|
|
7
|
+
- `BreakpointKey` = `'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'xxl'`.
|
|
8
|
+
- `ResponsiveValue<T>` = `T`, or an object such as `{ xs?: T; md?: T; base?: T; ... }`.
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## Contents
|
|
13
|
+
|
|
14
|
+
| Category | Exports |
|
|
15
|
+
|---|---|
|
|
16
|
+
| [Components](#components) | `ResponsiveProvider`, `ResponsiveView`, `ResponsiveGrid`, `Show`, `Hide` |
|
|
17
|
+
| [Hooks](#hooks) | `useResponsive`, `useBreakpoint`, `useResponsiveValue`, `useResponsiveConfig`, `useMatches`, `useGridLayout`, `useResponsiveStyles` |
|
|
18
|
+
| [Styles](#styles) | `createStyles`, `resolveStyles` |
|
|
19
|
+
| [Non-reactive](#non-reactive) | `getResponsive` |
|
|
20
|
+
| [Pure utilities](#pure-utilities) | `createConfig`, `getBreakpoint`, `isFrom`, `isBelow`, `isBetween`, `isResponsiveObject`, `resolveResponsiveValue`, `createScaler`, `classifyDevice`, `computeGridLayout`, `floorToPixel` |
|
|
21
|
+
| [Constants](#constants) | `BREAKPOINT_KEYS`, `DEFAULT_BREAKPOINTS`, `DEFAULT_CONFIG` |
|
|
22
|
+
| [Types](#types) | `ResponsiveInfo`, `LayoutInfo`, `Scaler`, `DeviceInfo`, `ResponsiveConfig`, … |
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## Components
|
|
27
|
+
|
|
28
|
+
### `<ResponsiveProvider>`
|
|
29
|
+
|
|
30
|
+
Optional. Customises the configuration for its subtree. Nested providers inherit from their parent.
|
|
31
|
+
|
|
32
|
+
| Prop | Type | Default | Description |
|
|
33
|
+
|---|---|---|---|
|
|
34
|
+
| `config` | `ResponsiveConfigInput` | none | Partial configuration merged over the parent's or the defaults. Inline objects are fine, because the config is compared by value. |
|
|
35
|
+
| `children` | `ReactNode` | none | |
|
|
36
|
+
|
|
37
|
+
Throws on invalid configuration (non-ascending breakpoints, unknown keys, non-positive numbers, `minScale > maxScale`).
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
### `<ResponsiveView>`
|
|
42
|
+
|
|
43
|
+
A `View` that becomes a **container query** boundary. Descendants that use `source: 'auto'` (the default) or `'container'` measure this view instead of the window.
|
|
44
|
+
|
|
45
|
+
Accepts all `ViewProps`, plus:
|
|
46
|
+
|
|
47
|
+
| Prop | Type | Default | Description |
|
|
48
|
+
|---|---|---|---|
|
|
49
|
+
| `breakpoints` | `Partial<Record<BreakpointKey, number>>` | global breakpoints | Breakpoints for this container only. |
|
|
50
|
+
| `initialWidth` | `number` | parent container width, or window width | Width assumed before the first layout. |
|
|
51
|
+
| `initialHeight` | `number` | parent container height, or window height | Height assumed before the first layout. |
|
|
52
|
+
| `waitForLayout` | `boolean` | `false` | Render children only after the first measurement. |
|
|
53
|
+
|
|
54
|
+
Your own `onLayout` is still called.
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
### `<ResponsiveGrid>`
|
|
59
|
+
|
|
60
|
+
Lays out children in responsive columns. Accepts all `ViewProps`, plus:
|
|
61
|
+
|
|
62
|
+
| Prop | Type | Default | Description |
|
|
63
|
+
|---|---|---|---|
|
|
64
|
+
| `columns` | `ResponsiveValue<number>` | `{ xs: 1, sm: 2, md: 3, lg: 4 }` | Fixed column count. |
|
|
65
|
+
| `minItemWidth` | `ResponsiveValue<number>` | none | Auto-fit mode. Overrides `columns`. |
|
|
66
|
+
| `maxColumns` | `ResponsiveValue<number>` | none | Column limit in auto-fit mode. |
|
|
67
|
+
| `gap` | `ResponsiveValue<number>` | `0` | Row and column gap. |
|
|
68
|
+
| `rowGap` | `ResponsiveValue<number>` | `gap` | Overrides `gap` vertically. |
|
|
69
|
+
| `columnGap` | `ResponsiveValue<number>` | `gap` | Overrides `gap` horizontally. |
|
|
70
|
+
| `itemStyle` | `StyleProp<ViewStyle>` | none | Applied to every cell wrapper. |
|
|
71
|
+
| `source` | `LayoutSource` | `'auto'` | Breakpoint source for the responsive props. |
|
|
72
|
+
|
|
73
|
+
**Behaviour**
|
|
74
|
+
- Cell width = `(containerWidth − columnGap × (columns − 1)) / columns`, floored to a physical pixel.
|
|
75
|
+
- Uses `marginStart` and `marginTop`, so it supports right-to-left layouts and React Native versions without the `gap` style.
|
|
76
|
+
- Responsive props resolve against the **surrounding** source (window or container), while the cell width uses the grid's **own** measured width.
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
### `<Show>` and `<Hide>`
|
|
81
|
+
|
|
82
|
+
`<Show>` renders children when **all** given conditions match. `<Hide>` does the opposite.
|
|
83
|
+
|
|
84
|
+
| Prop | Type | Description |
|
|
85
|
+
|---|---|---|
|
|
86
|
+
| `from` | `BreakpointKey` | Matches at this breakpoint and larger. |
|
|
87
|
+
| `below` | `BreakpointKey` | Matches strictly below this breakpoint. |
|
|
88
|
+
| `only` | `BreakpointKey \| BreakpointKey[]` | Matches exactly these breakpoints. |
|
|
89
|
+
| `device` | `DeviceType \| DeviceType[]` | Matches these hardware classes. |
|
|
90
|
+
| `orientation` | `'portrait' \| 'landscape'` | Orientation of the measured area. |
|
|
91
|
+
| `when` | `(r: ResponsiveInfo) => boolean` | Custom predicate. |
|
|
92
|
+
| `source` | `LayoutSource` | Default `'auto'`. |
|
|
93
|
+
| `fallback` | `ReactNode` | Rendered when the children are hidden. |
|
|
94
|
+
| `mode` | `'unmount' \| 'hide'` | Default `'unmount'`. `'hide'` keeps children mounted in a `display: 'none'` wrapper. |
|
|
95
|
+
|
|
96
|
+
If no condition is given, `<Show>` always renders and `<Hide>` never does.
|
|
97
|
+
|
|
98
|
+
---
|
|
99
|
+
|
|
100
|
+
## Hooks
|
|
101
|
+
|
|
102
|
+
### `useResponsive(options?)`
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
function useResponsive(options?: { source?: LayoutSource }): ResponsiveInfo;
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Returns everything in one frozen object (see [`ResponsiveInfo`](#responsiveinfo)). It re-renders on window, screen, font-scale, configuration or container changes. The returned object is memoised: its identity is stable while nothing changes.
|
|
109
|
+
|
|
110
|
+
---
|
|
111
|
+
|
|
112
|
+
### `useBreakpoint(options?)`
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
function useBreakpoint(options?: { source?: LayoutSource }): BreakpointKey;
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Returns only the active breakpoint. When measuring the window, the component re-renders **only when the breakpoint changes**.
|
|
119
|
+
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
### `useResponsiveValue(value, options?)`
|
|
123
|
+
|
|
124
|
+
```ts
|
|
125
|
+
function useResponsiveValue<V extends ResponsiveObject<unknown>>(value: V, options?): V[keyof V];
|
|
126
|
+
function useResponsiveValue<T>(value: ResponsiveValue<T>, options?): T;
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
Resolves a value for the active breakpoint using the mobile-first cascade. Literal and mixed types are inferred: `useResponsiveValue({ xs: '100%', md: 320 })` is typed `'100%' | 320`.
|
|
130
|
+
|
|
131
|
+
---
|
|
132
|
+
|
|
133
|
+
### `useResponsiveConfig()`
|
|
134
|
+
|
|
135
|
+
Returns the resolved `ResponsiveConfig` from the nearest provider, or the defaults.
|
|
136
|
+
|
|
137
|
+
---
|
|
138
|
+
|
|
139
|
+
### `useMatches(conditions)`
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
function useMatches(conditions: VisibilityConditions): boolean;
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
The hook form of `<Show>`. It takes the same condition props.
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
### `useGridLayout(options?)`
|
|
150
|
+
|
|
151
|
+
```ts
|
|
152
|
+
function useGridLayout(options?: GridOptions): {
|
|
153
|
+
columns: number;
|
|
154
|
+
itemWidth: number;
|
|
155
|
+
rowGap: number;
|
|
156
|
+
columnGap: number;
|
|
157
|
+
onLayout: (e: LayoutChangeEvent) => void;
|
|
158
|
+
};
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
Grid calculations for virtualised lists. Attach `onLayout` to the list, and pass `key={columns}` to `FlatList`. `GridOptions` is the same as the `ResponsiveGrid` props `columns`, `minItemWidth`, `maxColumns`, `gap`, `rowGap`, `columnGap` and `source`.
|
|
162
|
+
|
|
163
|
+
---
|
|
164
|
+
|
|
165
|
+
### `useResponsiveStyles(factory, deps, options?)`
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
function useResponsiveStyles<T>(
|
|
169
|
+
factory: (r: ResponsiveInfo) => T,
|
|
170
|
+
deps: DependencyList,
|
|
171
|
+
options?: { source?: LayoutSource },
|
|
172
|
+
): ResolvedStyles<T>;
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
Inline version of `createStyles`, for styles that depend on props or state. It is re-computed when metrics or `deps` change.
|
|
176
|
+
|
|
177
|
+
---
|
|
178
|
+
|
|
179
|
+
## Styles
|
|
180
|
+
|
|
181
|
+
### `createStyles(stylesOrFactory, options?)`
|
|
182
|
+
|
|
183
|
+
```ts
|
|
184
|
+
function createStyles<T>(
|
|
185
|
+
stylesOrFactory: T | ((r: ResponsiveInfo) => T),
|
|
186
|
+
options?: { source?: LayoutSource },
|
|
187
|
+
): () => ResolvedStyles<T>;
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
Defines a responsive style sheet and returns a hook.
|
|
191
|
+
|
|
192
|
+
- **Any** style property may be a responsive object, at any depth, including inside `transform` arrays and `shadowOffset`.
|
|
193
|
+
- Objects whose keys are not all breakpoint names (such as `shadowOffset: { width, height }`) are left unchanged.
|
|
194
|
+
- **Static object**: resolved at most once per breakpoint and cached for the app's lifetime. The component re-renders only on breakpoint changes.
|
|
195
|
+
- **Factory function**: receives `ResponsiveInfo`, and is memoised per metrics snapshot.
|
|
196
|
+
- **Types**: invalid values are compile errors, and literal values such as `'row'` are kept in the output type.
|
|
197
|
+
|
|
198
|
+
### `resolveStyles(sheet, breakpoint)`
|
|
199
|
+
|
|
200
|
+
Pure function that resolves every responsive value in a named sheet for one breakpoint.
|
|
201
|
+
|
|
202
|
+
---
|
|
203
|
+
|
|
204
|
+
## Non-reactive
|
|
205
|
+
|
|
206
|
+
### `getResponsive(config?)`
|
|
207
|
+
|
|
208
|
+
```ts
|
|
209
|
+
function getResponsive(config?: ResponsiveConfigInput): ResponsiveInfo;
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
Reads the current metrics **once**. It does not update on rotation or resize. Use it outside components only. It ignores providers, so pass `config` explicitly if you customised it.
|
|
213
|
+
|
|
214
|
+
---
|
|
215
|
+
|
|
216
|
+
## Pure utilities
|
|
217
|
+
|
|
218
|
+
These functions have no React or React Native dependency. They are useful for tests, server code and custom integrations.
|
|
219
|
+
|
|
220
|
+
| Function | Signature | Description |
|
|
221
|
+
|---|---|---|
|
|
222
|
+
| `createConfig` | `(input?, base = DEFAULT_CONFIG) => ResponsiveConfig` | Merges and validates the configuration. |
|
|
223
|
+
| `getBreakpoint` | `(width, breakpoints) => BreakpointKey` | Breakpoint for a width. |
|
|
224
|
+
| `isFrom` | `(current, target) => boolean` | `current ≥ target` |
|
|
225
|
+
| `isBelow` | `(current, target) => boolean` | `current < target` |
|
|
226
|
+
| `isBetween` | `(current, min, max) => boolean` | `min ≤ current < max` |
|
|
227
|
+
| `isResponsiveObject` | `(value) => boolean` | A plain object whose keys are all breakpoint names or `base`. |
|
|
228
|
+
| `resolveResponsiveValue` | `(value, bp) => T \| undefined` | Mobile-first cascade. Returns `undefined` only for `{}`. |
|
|
229
|
+
| `createScaler` | `({ width, height, fontScale, config, round? }) => Scaler` | Scaling helpers. |
|
|
230
|
+
| `classifyDevice` | `(env: DeviceEnvironment, config) => DeviceType` | Hardware classification. |
|
|
231
|
+
| `computeGridLayout` | `(input: GridInput) => { columns, itemWidth }` | Grid calculations. |
|
|
232
|
+
| `floorToPixel` | `(value, pixelRatio) => number` | Floors a value to the physical pixel grid. |
|
|
233
|
+
|
|
234
|
+
---
|
|
235
|
+
|
|
236
|
+
## Constants
|
|
237
|
+
|
|
238
|
+
| Name | Value |
|
|
239
|
+
|---|---|
|
|
240
|
+
| `BREAKPOINT_KEYS` | `['xs', 'sm', 'md', 'lg', 'xl', 'xxl']` |
|
|
241
|
+
| `DEFAULT_BREAKPOINTS` | `{ xs: 0, sm: 360, md: 600, lg: 900, xl: 1200, xxl: 1536 }` |
|
|
242
|
+
| `DEFAULT_CONFIG` | See the table below |
|
|
243
|
+
|
|
244
|
+
### Configuration options
|
|
245
|
+
|
|
246
|
+
| Option | Default | Description |
|
|
247
|
+
|---|---|---|
|
|
248
|
+
| `breakpoints` | `DEFAULT_BREAKPOINTS` | Must be strictly ascending. |
|
|
249
|
+
| `guidelineBaseWidth` | `375` | Width of your design's artboard. |
|
|
250
|
+
| `guidelineBaseHeight` | `812` | Height of your design's artboard. |
|
|
251
|
+
| `minScale` | `0.85` | Lower clamp for scale factors. |
|
|
252
|
+
| `maxScale` | `1.35` | Upper clamp for scale factors. |
|
|
253
|
+
| `maxFontScale` | `2` | Cap on the honoured system font scale. |
|
|
254
|
+
| `tabletMinShortSide` | `600` | Android and web tablet threshold (short screen side). |
|
|
255
|
+
| `desktopMinWidth` | `1024` | Web desktop threshold (long screen side, fine pointer). |
|
|
256
|
+
|
|
257
|
+
---
|
|
258
|
+
|
|
259
|
+
## Types
|
|
260
|
+
|
|
261
|
+
### `ResponsiveInfo`
|
|
262
|
+
|
|
263
|
+
Returned by `useResponsive()` and `getResponsive()`. It combines `LayoutInfo`, `Scaler` and `DeviceInfo`, plus:
|
|
264
|
+
|
|
265
|
+
| Field | Type | Description |
|
|
266
|
+
|---|---|---|
|
|
267
|
+
| `window` | `LayoutInfo` | Always the app window. |
|
|
268
|
+
| `container` | `LayoutInfo \| null` | Nearest `<ResponsiveView>`, or `null`. |
|
|
269
|
+
| `config` | `ResponsiveConfig` | The active configuration. |
|
|
270
|
+
|
|
271
|
+
### `LayoutInfo`
|
|
272
|
+
|
|
273
|
+
Describes the **active area** (chosen by `source`).
|
|
274
|
+
|
|
275
|
+
| Field | Type | Description |
|
|
276
|
+
|---|---|---|
|
|
277
|
+
| `kind` | `'window' \| 'container'` | Which area is measured. |
|
|
278
|
+
| `width`, `height` | `number` | Size in dp. |
|
|
279
|
+
| `breakpoint` | `BreakpointKey` | Active breakpoint. |
|
|
280
|
+
| `breakpoints` | `Breakpoints` | The table used for this area. |
|
|
281
|
+
| `orientation` | `Orientation` | `'landscape'` when width > height. |
|
|
282
|
+
| `isPortrait`, `isLandscape` | `boolean` | |
|
|
283
|
+
| `from(bp)`, `below(bp)`, `between(a, b)`, `only(...bps)` | `boolean` | Breakpoint comparisons. |
|
|
284
|
+
| `select(values)` | `T` | Resolves a responsive value. |
|
|
285
|
+
| `wp(percent)`, `hp(percent)` | `number` | Percentage of the width or height, pixel-rounded. |
|
|
286
|
+
|
|
287
|
+
### `Scaler`
|
|
288
|
+
|
|
289
|
+
Always based on the **window**, never on a container.
|
|
290
|
+
|
|
291
|
+
| Field | Description |
|
|
292
|
+
|---|---|
|
|
293
|
+
| `scaleFactor` | `clamp(shortSide / shortGuideline, minScale, maxScale)` |
|
|
294
|
+
| `verticalScaleFactor` | `clamp(longSide / longGuideline, minScale, maxScale)` |
|
|
295
|
+
| `s(n)`, `vs(n)` | Linear scaling. |
|
|
296
|
+
| `ms(n, factor = 0.5)` | Partial scaling. |
|
|
297
|
+
| `fs(n, factor = 0.5)` | Font size with the font-scale cap applied. |
|
|
298
|
+
|
|
299
|
+
### `DeviceInfo`
|
|
300
|
+
|
|
301
|
+
| Field | Type | Description |
|
|
302
|
+
|---|---|---|
|
|
303
|
+
| `deviceType` | `'phone' \| 'tablet' \| 'desktop' \| 'tv'` | Hardware class. |
|
|
304
|
+
| `isPhone`, `isTablet`, `isDesktop`, `isTV` | `boolean` | Shortcuts. |
|
|
305
|
+
| `screenWidth`, `screenHeight` | `number` | Physical screen size in dp. |
|
|
306
|
+
| `fontScale` | `number` | The user's system font scale. |
|
|
307
|
+
| `pixelRatio` | `number` | Physical pixels per dp. |
|
|
308
|
+
|
|
309
|
+
### Other exported types
|
|
310
|
+
|
|
311
|
+
`BreakpointKey`, `Breakpoints`, `ResponsiveValue<T>`, `ResponsiveObject<T>`, `DeviceType`, `Orientation`, `LayoutSource`, `ResponsiveConfig`, `ResponsiveConfigInput`, `ResolvedStyle<S>`, `ResolvedStyles<T>`, `SelectResult<V>`, `ResponsiveStyle<S>`, `AnyResponsiveStyle`, `ResponsiveNamedStyles<T>`, `VisibilityConditions`, `VisibilityProps`, `GridOptions`, `GridLayoutResult`, `GridInput`, `GridLayout`, `DeviceEnvironment`, `SourceOptions`, and the props types of every component.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { BreakpointKey, Breakpoints, ResponsiveObject, ResponsiveValue } from './types';
|
|
2
|
+
/** Returns the breakpoint whose range contains `width`. */
|
|
3
|
+
export declare function getBreakpoint(width: number, breakpoints: Breakpoints): BreakpointKey;
|
|
4
|
+
/** Position of a breakpoint in the ascending order (xs = 0). Throws on unknown names. */
|
|
5
|
+
export declare function breakpointIndex(bp: BreakpointKey): number;
|
|
6
|
+
/** `current >= target` */
|
|
7
|
+
export declare function isFrom(current: BreakpointKey, target: BreakpointKey): boolean;
|
|
8
|
+
/** `current < target` */
|
|
9
|
+
export declare function isBelow(current: BreakpointKey, target: BreakpointKey): boolean;
|
|
10
|
+
/** `min <= current < max` */
|
|
11
|
+
export declare function isBetween(current: BreakpointKey, min: BreakpointKey, max: BreakpointKey): boolean;
|
|
12
|
+
/**
|
|
13
|
+
* True when `value` is a plain object whose keys are all breakpoint names (or `base`).
|
|
14
|
+
* Objects such as `shadowOffset: { width, height }` are therefore never mistaken for
|
|
15
|
+
* responsive objects.
|
|
16
|
+
*/
|
|
17
|
+
export declare function isResponsiveObject(value: unknown): value is ResponsiveObject<unknown>;
|
|
18
|
+
/**
|
|
19
|
+
* Resolve a responsive value for a breakpoint (mobile-first cascade):
|
|
20
|
+
* 1. the value at `bp`, else the closest smaller breakpoint that has a value;
|
|
21
|
+
* 2. else `base`;
|
|
22
|
+
* 3. else the closest larger breakpoint that has a value (so you never get `undefined`
|
|
23
|
+
* just because you forgot to define the smallest size).
|
|
24
|
+
*/
|
|
25
|
+
export declare function resolveResponsiveValue<T>(value: ResponsiveValue<T>, bp: BreakpointKey): T | undefined;
|
|
26
|
+
/** Recursively resolve every responsive object inside a value (arrays and plain objects). */
|
|
27
|
+
export declare function resolveDeep(value: unknown, bp: BreakpointKey): unknown;
|
|
28
|
+
//# sourceMappingURL=breakpoints.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"breakpoints.d.ts","sourceRoot":"","sources":["../../src/core/breakpoints.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,WAAW,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAI7F,2DAA2D;AAC3D,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,WAAW,EAAE,WAAW,GAAG,aAAa,CAMpF;AAED,yFAAyF;AACzF,wBAAgB,eAAe,CAAC,EAAE,EAAE,aAAa,GAAG,MAAM,CAQzD;AAED,0BAA0B;AAC1B,wBAAgB,MAAM,CAAC,OAAO,EAAE,aAAa,EAAE,MAAM,EAAE,aAAa,GAAG,OAAO,CAE7E;AAED,yBAAyB;AACzB,wBAAgB,OAAO,CAAC,OAAO,EAAE,aAAa,EAAE,MAAM,EAAE,aAAa,GAAG,OAAO,CAE9E;AAED,6BAA6B;AAC7B,wBAAgB,SAAS,CAAC,OAAO,EAAE,aAAa,EAAE,GAAG,EAAE,aAAa,EAAE,GAAG,EAAE,aAAa,GAAG,OAAO,CAEjG;AAQD;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,gBAAgB,CAAC,OAAO,CAAC,CAQrF;AAED;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CAAC,CAAC,EACtC,KAAK,EAAE,eAAe,CAAC,CAAC,CAAC,EACzB,EAAE,EAAE,aAAa,GAChB,CAAC,GAAG,SAAS,CAcf;AAED,6FAA6F;AAC7F,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE,EAAE,aAAa,GAAG,OAAO,CAYtE"}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getBreakpoint = getBreakpoint;
|
|
4
|
+
exports.breakpointIndex = breakpointIndex;
|
|
5
|
+
exports.isFrom = isFrom;
|
|
6
|
+
exports.isBelow = isBelow;
|
|
7
|
+
exports.isBetween = isBetween;
|
|
8
|
+
exports.isResponsiveObject = isResponsiveObject;
|
|
9
|
+
exports.resolveResponsiveValue = resolveResponsiveValue;
|
|
10
|
+
exports.resolveDeep = resolveDeep;
|
|
11
|
+
const config_1 = require("./config");
|
|
12
|
+
const RESPONSIVE_KEYS = new Set([...config_1.BREAKPOINT_KEYS, 'base']);
|
|
13
|
+
/** Returns the breakpoint whose range contains `width`. */
|
|
14
|
+
function getBreakpoint(width, breakpoints) {
|
|
15
|
+
for (let i = config_1.BREAKPOINT_KEYS.length - 1; i > 0; i--) {
|
|
16
|
+
const key = config_1.BREAKPOINT_KEYS[i];
|
|
17
|
+
if (width >= breakpoints[key])
|
|
18
|
+
return key;
|
|
19
|
+
}
|
|
20
|
+
return 'xs';
|
|
21
|
+
}
|
|
22
|
+
/** Position of a breakpoint in the ascending order (xs = 0). Throws on unknown names. */
|
|
23
|
+
function breakpointIndex(bp) {
|
|
24
|
+
const index = config_1.BREAKPOINT_KEYS.indexOf(bp);
|
|
25
|
+
if (index < 0) {
|
|
26
|
+
throw new Error(`[react-native-everything-responsive] Unknown breakpoint "${String(bp)}". Expected one of: ${config_1.BREAKPOINT_KEYS.join(', ')}.`);
|
|
27
|
+
}
|
|
28
|
+
return index;
|
|
29
|
+
}
|
|
30
|
+
/** `current >= target` */
|
|
31
|
+
function isFrom(current, target) {
|
|
32
|
+
return breakpointIndex(current) >= breakpointIndex(target);
|
|
33
|
+
}
|
|
34
|
+
/** `current < target` */
|
|
35
|
+
function isBelow(current, target) {
|
|
36
|
+
return breakpointIndex(current) < breakpointIndex(target);
|
|
37
|
+
}
|
|
38
|
+
/** `min <= current < max` */
|
|
39
|
+
function isBetween(current, min, max) {
|
|
40
|
+
return isFrom(current, min) && isBelow(current, max);
|
|
41
|
+
}
|
|
42
|
+
function isPlainObject(value) {
|
|
43
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value))
|
|
44
|
+
return false;
|
|
45
|
+
const proto = Object.getPrototypeOf(value);
|
|
46
|
+
return proto === Object.prototype || proto === null;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* True when `value` is a plain object whose keys are all breakpoint names (or `base`).
|
|
50
|
+
* Objects such as `shadowOffset: { width, height }` are therefore never mistaken for
|
|
51
|
+
* responsive objects.
|
|
52
|
+
*/
|
|
53
|
+
function isResponsiveObject(value) {
|
|
54
|
+
if (!isPlainObject(value))
|
|
55
|
+
return false;
|
|
56
|
+
const keys = Object.keys(value);
|
|
57
|
+
if (keys.length === 0)
|
|
58
|
+
return false;
|
|
59
|
+
for (const key of keys) {
|
|
60
|
+
if (!RESPONSIVE_KEYS.has(key))
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Resolve a responsive value for a breakpoint (mobile-first cascade):
|
|
67
|
+
* 1. the value at `bp`, else the closest smaller breakpoint that has a value;
|
|
68
|
+
* 2. else `base`;
|
|
69
|
+
* 3. else the closest larger breakpoint that has a value (so you never get `undefined`
|
|
70
|
+
* just because you forgot to define the smallest size).
|
|
71
|
+
*/
|
|
72
|
+
function resolveResponsiveValue(value, bp) {
|
|
73
|
+
if (!isResponsiveObject(value))
|
|
74
|
+
return value;
|
|
75
|
+
const map = value;
|
|
76
|
+
const index = breakpointIndex(bp);
|
|
77
|
+
for (let i = index; i >= 0; i--) {
|
|
78
|
+
const candidate = map[config_1.BREAKPOINT_KEYS[i]];
|
|
79
|
+
if (candidate !== undefined)
|
|
80
|
+
return candidate;
|
|
81
|
+
}
|
|
82
|
+
if (map.base !== undefined)
|
|
83
|
+
return map.base;
|
|
84
|
+
for (let i = index + 1; i < config_1.BREAKPOINT_KEYS.length; i++) {
|
|
85
|
+
const candidate = map[config_1.BREAKPOINT_KEYS[i]];
|
|
86
|
+
if (candidate !== undefined)
|
|
87
|
+
return candidate;
|
|
88
|
+
}
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
/** Recursively resolve every responsive object inside a value (arrays and plain objects). */
|
|
92
|
+
function resolveDeep(value, bp) {
|
|
93
|
+
if (isResponsiveObject(value))
|
|
94
|
+
return resolveDeep(resolveResponsiveValue(value, bp), bp);
|
|
95
|
+
if (Array.isArray(value))
|
|
96
|
+
return value.map((item) => resolveDeep(item, bp));
|
|
97
|
+
if (isPlainObject(value)) {
|
|
98
|
+
const out = {};
|
|
99
|
+
for (const key of Object.keys(value)) {
|
|
100
|
+
const resolved = resolveDeep(value[key], bp);
|
|
101
|
+
if (resolved !== undefined)
|
|
102
|
+
out[key] = resolved;
|
|
103
|
+
}
|
|
104
|
+
return out;
|
|
105
|
+
}
|
|
106
|
+
return value;
|
|
107
|
+
}
|
|
108
|
+
//# sourceMappingURL=breakpoints.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"breakpoints.js","sourceRoot":"","sources":["../../src/core/breakpoints.ts"],"names":[],"mappings":";;AAMA,sCAMC;AAGD,0CAQC;AAGD,wBAEC;AAGD,0BAEC;AAGD,8BAEC;AAaD,gDAQC;AASD,wDAiBC;AAGD,kCAYC;AApGD,qCAA2C;AAG3C,MAAM,eAAe,GAAwB,IAAI,GAAG,CAAS,CAAC,GAAG,wBAAe,EAAE,MAAM,CAAC,CAAC,CAAC;AAE3F,2DAA2D;AAC3D,SAAgB,aAAa,CAAC,KAAa,EAAE,WAAwB;IACnE,KAAK,IAAI,CAAC,GAAG,wBAAe,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QACpD,MAAM,GAAG,GAAG,wBAAe,CAAC,CAAC,CAAE,CAAC;QAChC,IAAI,KAAK,IAAI,WAAW,CAAC,GAAG,CAAC;YAAE,OAAO,GAAG,CAAC;IAC5C,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED,yFAAyF;AACzF,SAAgB,eAAe,CAAC,EAAiB;IAC/C,MAAM,KAAK,GAAG,wBAAe,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;IAC1C,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;QACd,MAAM,IAAI,KAAK,CACb,4DAA4D,MAAM,CAAC,EAAE,CAAC,uBAAuB,wBAAe,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAC3H,CAAC;IACJ,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED,0BAA0B;AAC1B,SAAgB,MAAM,CAAC,OAAsB,EAAE,MAAqB;IAClE,OAAO,eAAe,CAAC,OAAO,CAAC,IAAI,eAAe,CAAC,MAAM,CAAC,CAAC;AAC7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E,EAAE,CAAC,CAAC;YAC7C,IAAI,QAAQ,KAAK,SAAS;gBAAE,GAAG,CAAC,GAAG,CAAC,GAAG,QAAQ,CAAC;QAClD,CAAC;QACD,OAAO,GAAG,CAAC;IACb,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { BreakpointKey, Breakpoints, ResponsiveConfig, ResponsiveConfigInput } from './types';
|
|
2
|
+
/** Breakpoint names in ascending order. */
|
|
3
|
+
export declare const BREAKPOINT_KEYS: readonly BreakpointKey[];
|
|
4
|
+
/**
|
|
5
|
+
* Default breakpoints (dp, measured against the window or container width).
|
|
6
|
+
*
|
|
7
|
+
* | key | from | typical target |
|
|
8
|
+
* |-----|--------|--------------------------------------------------|
|
|
9
|
+
* | xs | 0 | small phones (iPhone SE, compact Android) |
|
|
10
|
+
* | sm | 360 | standard and large phones |
|
|
11
|
+
* | md | 600 | small tablets, unfolded foldables, phone landscape|
|
|
12
|
+
* | lg | 900 | tablets in landscape, iPad Pro portrait |
|
|
13
|
+
* | xl | 1200 | laptops, small desktop windows |
|
|
14
|
+
* | xxl | 1536 | large desktop monitors |
|
|
15
|
+
*/
|
|
16
|
+
export declare const DEFAULT_BREAKPOINTS: Breakpoints;
|
|
17
|
+
export declare const DEFAULT_CONFIG: ResponsiveConfig;
|
|
18
|
+
/** Throws a descriptive error if the breakpoint table is not strictly ascending. */
|
|
19
|
+
export declare function validateBreakpoints(breakpoints: Breakpoints): void;
|
|
20
|
+
/** Merge a partial breakpoint table into a base table and validate the result. */
|
|
21
|
+
export declare function mergeBreakpoints(base: Breakpoints, override?: Partial<Record<BreakpointKey, number>>): Breakpoints;
|
|
22
|
+
/** Merge user configuration on top of a base configuration and validate everything. */
|
|
23
|
+
export declare function createConfig(input?: ResponsiveConfigInput, base?: ResponsiveConfig): ResponsiveConfig;
|
|
24
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/core/config.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,aAAa,EACb,WAAW,EACX,gBAAgB,EAChB,qBAAqB,EACtB,MAAM,SAAS,CAAC;AAIjB,2CAA2C;AAC3C,eAAO,MAAM,eAAe,EAAE,SAAS,aAAa,EAOzC,CAAC;AAEZ;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,mBAAmB,EAAE,WAOhC,CAAC;AAEH,eAAO,MAAM,cAAc,EAAE,gBAS3B,CAAC;AAcH,oFAAoF;AACpF,wBAAgB,mBAAmB,CAAC,WAAW,EAAE,WAAW,GAAG,IAAI,CAclE;AAED,kFAAkF;AAClF,wBAAgB,gBAAgB,CAC9B,IAAI,EAAE,WAAW,EACjB,QAAQ,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,aAAa,EAAE,MAAM,CAAC,CAAC,GAChD,WAAW,CAcb;AAED,uFAAuF;AACvF,wBAAgB,YAAY,CAC1B,KAAK,CAAC,EAAE,qBAAqB,EAC7B,IAAI,GAAE,gBAAiC,GACtC,gBAAgB,CAyBlB"}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_CONFIG = exports.DEFAULT_BREAKPOINTS = exports.BREAKPOINT_KEYS = void 0;
|
|
4
|
+
exports.validateBreakpoints = validateBreakpoints;
|
|
5
|
+
exports.mergeBreakpoints = mergeBreakpoints;
|
|
6
|
+
exports.createConfig = createConfig;
|
|
7
|
+
const PREFIX = '[react-native-everything-responsive]';
|
|
8
|
+
/** Breakpoint names in ascending order. */
|
|
9
|
+
exports.BREAKPOINT_KEYS = Object.freeze([
|
|
10
|
+
'xs',
|
|
11
|
+
'sm',
|
|
12
|
+
'md',
|
|
13
|
+
'lg',
|
|
14
|
+
'xl',
|
|
15
|
+
'xxl',
|
|
16
|
+
]);
|
|
17
|
+
/**
|
|
18
|
+
* Default breakpoints (dp, measured against the window or container width).
|
|
19
|
+
*
|
|
20
|
+
* | key | from | typical target |
|
|
21
|
+
* |-----|--------|--------------------------------------------------|
|
|
22
|
+
* | xs | 0 | small phones (iPhone SE, compact Android) |
|
|
23
|
+
* | sm | 360 | standard and large phones |
|
|
24
|
+
* | md | 600 | small tablets, unfolded foldables, phone landscape|
|
|
25
|
+
* | lg | 900 | tablets in landscape, iPad Pro portrait |
|
|
26
|
+
* | xl | 1200 | laptops, small desktop windows |
|
|
27
|
+
* | xxl | 1536 | large desktop monitors |
|
|
28
|
+
*/
|
|
29
|
+
exports.DEFAULT_BREAKPOINTS = Object.freeze({
|
|
30
|
+
xs: 0,
|
|
31
|
+
sm: 360,
|
|
32
|
+
md: 600,
|
|
33
|
+
lg: 900,
|
|
34
|
+
xl: 1200,
|
|
35
|
+
xxl: 1536,
|
|
36
|
+
});
|
|
37
|
+
exports.DEFAULT_CONFIG = Object.freeze({
|
|
38
|
+
breakpoints: exports.DEFAULT_BREAKPOINTS,
|
|
39
|
+
guidelineBaseWidth: 375,
|
|
40
|
+
guidelineBaseHeight: 812,
|
|
41
|
+
minScale: 0.85,
|
|
42
|
+
maxScale: 1.35,
|
|
43
|
+
maxFontScale: 2,
|
|
44
|
+
tabletMinShortSide: 600,
|
|
45
|
+
desktopMinWidth: 1024,
|
|
46
|
+
});
|
|
47
|
+
function assertFiniteNumber(name, value, min, exclusive) {
|
|
48
|
+
const ok = typeof value === 'number' &&
|
|
49
|
+
Number.isFinite(value) &&
|
|
50
|
+
(exclusive ? value > min : value >= min);
|
|
51
|
+
if (!ok) {
|
|
52
|
+
throw new Error(`${PREFIX} "${name}" must be a finite number ${exclusive ? '>' : '>='} ${min}, received ${String(value)}.`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/** Throws a descriptive error if the breakpoint table is not strictly ascending. */
|
|
56
|
+
function validateBreakpoints(breakpoints) {
|
|
57
|
+
let previous = -Infinity;
|
|
58
|
+
let previousKey = '';
|
|
59
|
+
for (const key of exports.BREAKPOINT_KEYS) {
|
|
60
|
+
const value = breakpoints[key];
|
|
61
|
+
assertFiniteNumber(`breakpoints.${key}`, value, 0, false);
|
|
62
|
+
if (value <= previous) {
|
|
63
|
+
throw new Error(`${PREFIX} Breakpoints must be strictly ascending, but "${key}" (${value}) is not greater than "${previousKey}" (${previous}).`);
|
|
64
|
+
}
|
|
65
|
+
previous = value;
|
|
66
|
+
previousKey = key;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/** Merge a partial breakpoint table into a base table and validate the result. */
|
|
70
|
+
function mergeBreakpoints(base, override) {
|
|
71
|
+
if (!override)
|
|
72
|
+
return base;
|
|
73
|
+
const merged = { ...base };
|
|
74
|
+
for (const key of Object.keys(override)) {
|
|
75
|
+
if (!exports.BREAKPOINT_KEYS.includes(key)) {
|
|
76
|
+
throw new Error(`${PREFIX} Unknown breakpoint "${key}". Expected one of: ${exports.BREAKPOINT_KEYS.join(', ')}.`);
|
|
77
|
+
}
|
|
78
|
+
const value = override[key];
|
|
79
|
+
if (value !== undefined)
|
|
80
|
+
merged[key] = value;
|
|
81
|
+
}
|
|
82
|
+
validateBreakpoints(merged);
|
|
83
|
+
return Object.freeze(merged);
|
|
84
|
+
}
|
|
85
|
+
/** Merge user configuration on top of a base configuration and validate everything. */
|
|
86
|
+
function createConfig(input, base = exports.DEFAULT_CONFIG) {
|
|
87
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
88
|
+
if (!input)
|
|
89
|
+
return base;
|
|
90
|
+
const config = {
|
|
91
|
+
breakpoints: mergeBreakpoints(base.breakpoints, input.breakpoints),
|
|
92
|
+
guidelineBaseWidth: (_a = input.guidelineBaseWidth) !== null && _a !== void 0 ? _a : base.guidelineBaseWidth,
|
|
93
|
+
guidelineBaseHeight: (_b = input.guidelineBaseHeight) !== null && _b !== void 0 ? _b : base.guidelineBaseHeight,
|
|
94
|
+
minScale: (_c = input.minScale) !== null && _c !== void 0 ? _c : base.minScale,
|
|
95
|
+
maxScale: (_d = input.maxScale) !== null && _d !== void 0 ? _d : base.maxScale,
|
|
96
|
+
maxFontScale: (_e = input.maxFontScale) !== null && _e !== void 0 ? _e : base.maxFontScale,
|
|
97
|
+
tabletMinShortSide: (_f = input.tabletMinShortSide) !== null && _f !== void 0 ? _f : base.tabletMinShortSide,
|
|
98
|
+
desktopMinWidth: (_g = input.desktopMinWidth) !== null && _g !== void 0 ? _g : base.desktopMinWidth,
|
|
99
|
+
};
|
|
100
|
+
assertFiniteNumber('guidelineBaseWidth', config.guidelineBaseWidth, 0, true);
|
|
101
|
+
assertFiniteNumber('guidelineBaseHeight', config.guidelineBaseHeight, 0, true);
|
|
102
|
+
assertFiniteNumber('minScale', config.minScale, 0, true);
|
|
103
|
+
assertFiniteNumber('maxScale', config.maxScale, 0, true);
|
|
104
|
+
assertFiniteNumber('maxFontScale', config.maxFontScale, 0, true);
|
|
105
|
+
assertFiniteNumber('tabletMinShortSide', config.tabletMinShortSide, 0, true);
|
|
106
|
+
assertFiniteNumber('desktopMinWidth', config.desktopMinWidth, 0, true);
|
|
107
|
+
if (config.minScale > config.maxScale) {
|
|
108
|
+
throw new Error(`${PREFIX} "minScale" (${config.minScale}) must not be greater than "maxScale" (${config.maxScale}).`);
|
|
109
|
+
}
|
|
110
|
+
return Object.freeze(config);
|
|
111
|
+
}
|
|
112
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/core/config.ts"],"names":[],"mappings":";;;AAgEA,kDAcC;AAGD,4CAiBC;AAGD,oCA4BC;AA1HD,MAAM,MAAM,GAAG,sCAAsC,CAAC;AAEtD,2CAA2C;AAC9B,QAAA,eAAe,GAA6B,MAAM,CAAC,MAAM,CAAC;IACrE,IAAI;IACJ,IAAI;IACJ,IAAI;IACJ,IAAI;IACJ,IAAI;IACJ,KAAK;CACG,CAAC,CAAC;AAEZ;;;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EAAE,CAAC;QACtC,MAAM,IAAI,KAAK,CACb,GAAG,MAAM,gBAAgB,MAAM,CAAC,QAAQ,0CAA0C,MAAM,CAAC,QAAQ,IAAI,CACtG,CAAC;IACJ,CAAC;IACD,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;AAC/B,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { DeviceType, ResponsiveConfig } from './types';
|
|
2
|
+
/** Platform facts needed to classify a device. Collected by the native layer. */
|
|
3
|
+
export interface DeviceEnvironment {
|
|
4
|
+
/** `Platform.OS`: 'ios' | 'android' | 'web' | 'windows' | 'macos' | ... */
|
|
5
|
+
os: string;
|
|
6
|
+
/** `Platform.isPad` on iOS. */
|
|
7
|
+
isPad?: boolean;
|
|
8
|
+
/** `Platform.isTV`. */
|
|
9
|
+
isTV?: boolean;
|
|
10
|
+
/** Physical screen size in dp (not the window). */
|
|
11
|
+
screenWidth: number;
|
|
12
|
+
screenHeight: number;
|
|
13
|
+
/** Web only: result of `matchMedia('(pointer: coarse)')`, or undefined when unknown. */
|
|
14
|
+
hasCoarsePointer?: boolean;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Classifies the **hardware**, not the current window. An iPad in 1/3 split view is still
|
|
18
|
+
* a `tablet` (use breakpoints for layout), and a desktop browser resized to 400 dp is still
|
|
19
|
+
* a `desktop`.
|
|
20
|
+
*
|
|
21
|
+
* Rules:
|
|
22
|
+
* - TV flag → `tv`
|
|
23
|
+
* - Windows / macOS (react-native-windows / -macos) → `desktop`
|
|
24
|
+
* - iOS → `Platform.isPad` decides between `tablet` and `phone`
|
|
25
|
+
* - Web → fine pointer and long side ≥ `desktopMinWidth` → `desktop`;
|
|
26
|
+
* otherwise short side ≥ `tabletMinShortSide` → `tablet`; else `phone`
|
|
27
|
+
* - Android and others → short side ≥ `tabletMinShortSide` → `tablet` (the Android
|
|
28
|
+
* "sw600dp" convention, which also classifies unfolded foldables correctly)
|
|
29
|
+
*/
|
|
30
|
+
export declare function classifyDevice(env: DeviceEnvironment, config: ResponsiveConfig): DeviceType;
|
|
31
|
+
//# sourceMappingURL=device.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"device.d.ts","sourceRoot":"","sources":["../../src/core/device.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAE5D,iFAAiF;AACjF,MAAM,WAAW,iBAAiB;IAChC,2EAA2E;IAC3E,EAAE,EAAE,MAAM,CAAC;IACX,+BAA+B;IAC/B,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,uBAAuB;IACvB,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,mDAAmD;IACnD,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,wFAAwF;IACxF,gBAAgB,CAAC,EAAE,OAAO,CAAC;CAC5B;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,iBAAiB,EAAE,MAAM,EAAE,gBAAgB,GAAG,UAAU,CAc3F"}
|