0
# Platform Utilities
1
2
Extended platform detection and capability checking beyond React Native's built-in Platform, providing comprehensive environment detection for cross-platform development.
3
4
## Capabilities
5
6
### Platform Object
7
8
Enhanced platform detection with additional capabilities and environment checks.
9
10
```typescript { .api }
11
/**
12
* Extended platform utilities with capability detection
13
*/
14
interface PlatformInterface {
15
/**
16
* Current platform OS
17
*/
18
OS: PlatformOSType;
19
20
/**
21
* Select platform-specific values
22
*/
23
select: <T>(specifics: { [platform in PlatformSelectOSType]?: T }) => T;
24
25
/**
26
* Whether DOM API is available
27
*/
28
isDOMAvailable: boolean;
29
30
/**
31
* Whether window event listeners can be attached
32
*/
33
canUseEventListeners: boolean;
34
35
/**
36
* Whether screen properties can be inspected
37
*/
38
canUseViewport: boolean;
39
40
/**
41
* Whether JavaScript is executing remotely
42
*/
43
isAsyncDebugging: boolean;
44
}
45
46
const Platform: PlatformInterface;
47
```
48
49
**Usage Examples:**
50
51
```typescript
52
import { Platform } from "@unimodules/core";
53
54
// Basic platform detection
55
console.log('Running on:', Platform.OS); // 'ios', 'android', or 'web'
56
57
// Platform-specific logic
58
if (Platform.OS === 'ios') {
59
// iOS-specific code
60
} else if (Platform.OS === 'android') {
61
// Android-specific code
62
} else if (Platform.OS === 'web') {
63
// Web-specific code
64
}
65
66
// Check capabilities
67
if (Platform.isDOMAvailable) {
68
// Safe to use DOM APIs
69
document.addEventListener('click', handler);
70
}
71
72
if (Platform.canUseEventListeners) {
73
// Safe to attach window event listeners
74
window.addEventListener('resize', handleResize);
75
}
76
77
if (Platform.canUseViewport) {
78
// Safe to inspect screen properties
79
console.log('Screen width:', window.screen.width);
80
}
81
82
if (Platform.isAsyncDebugging) {
83
console.warn('Running in remote debugger - synchronous native calls unavailable');
84
}
85
```
86
87
### Platform Selection
88
89
Select values based on the current platform with fallback support.
90
91
```typescript { .api }
92
/**
93
* Platform-specific value selection
94
*/
95
type PlatformSelect = <T>(specifics: {
96
[platform in PlatformSelectOSType]?: T
97
}) => T;
98
99
type PlatformSelectOSType =
100
| 'ios'
101
| 'android'
102
| 'web'
103
| 'native' // Matches ios or android
104
| 'electron' // Electron apps
105
| 'default'; // Fallback value
106
```
107
108
**Usage Examples:**
109
110
```typescript
111
import { Platform } from "@unimodules/core";
112
113
// Select platform-specific values
114
const fontSize = Platform.select({
115
ios: 16,
116
android: 14,
117
web: 18,
118
default: 16
119
});
120
121
// Platform-specific components
122
const StatusBarHeight = Platform.select({
123
ios: 20,
124
android: 24,
125
web: 0,
126
default: 0
127
});
128
129
// Platform-specific styles
130
const styles = StyleSheet.create({
131
container: {
132
paddingTop: Platform.select({
133
ios: 20,
134
android: 10,
135
default: 0
136
}),
137
...Platform.select({
138
native: {
139
// Applies to both iOS and Android
140
shadowColor: '#000',
141
shadowOffset: { width: 0, height: 2 },
142
shadowOpacity: 0.25,
143
shadowRadius: 3.84,
144
},
145
web: {
146
// Web-specific shadow
147
boxShadow: '0px 2px 4px rgba(0, 0, 0, 0.25)',
148
}
149
})
150
}
151
});
152
153
// Complex platform-specific configuration
154
const apiConfig = Platform.select({
155
ios: {
156
baseURL: 'https://api-ios.example.com',
157
timeout: 5000,
158
retries: 3
159
},
160
android: {
161
baseURL: 'https://api-android.example.com',
162
timeout: 7000,
163
retries: 2
164
},
165
web: {
166
baseURL: 'https://api-web.example.com',
167
timeout: 10000,
168
retries: 1
169
},
170
default: {
171
baseURL: 'https://api.example.com',
172
timeout: 5000,
173
retries: 2
174
}
175
});
176
```
177
178
### Environment Capability Detection
179
180
Detect specific environment capabilities for safe feature usage.
181
182
**Usage Examples:**
183
184
```typescript
185
import { Platform } from "@unimodules/core";
186
187
// DOM availability check
188
function setupDocumentListeners() {
189
if (Platform.isDOMAvailable) {
190
document.addEventListener('visibilitychange', () => {
191
console.log('Visibility changed:', document.visibilityState);
192
});
193
} else {
194
console.log('DOM not available - skipping document listeners');
195
}
196
}
197
198
// Window event listeners
199
function setupWindowListeners() {
200
if (Platform.canUseEventListeners) {
201
window.addEventListener('online', () => {
202
console.log('Network back online');
203
});
204
205
window.addEventListener('offline', () => {
206
console.log('Network went offline');
207
});
208
}
209
}
210
211
// Viewport/screen detection
212
function getScreenInfo() {
213
if (Platform.canUseViewport) {
214
return {
215
width: window.screen.width,
216
height: window.screen.height,
217
orientation: window.screen.orientation?.type || 'unknown'
218
};
219
}
220
return null;
221
}
222
223
// Debugging detection
224
function performanceOptimization() {
225
if (Platform.isAsyncDebugging) {
226
console.warn('Remote debugging detected - disabling performance-sensitive features');
227
return false;
228
}
229
return true;
230
}
231
```
232
233
## Advanced Usage
234
235
### Custom Platform Detection
236
237
```typescript
238
import { Platform } from "@unimodules/core";
239
240
// Custom platform utilities
241
const PlatformUtils = {
242
isNative: Platform.OS === 'ios' || Platform.OS === 'android',
243
isMobile: Platform.OS === 'ios' || Platform.OS === 'android',
244
isTablet: Platform.OS === 'ios' && Platform.isPad, // Note: isPad from React Native
245
isDesktop: Platform.OS === 'web' && Platform.canUseViewport && window.screen.width > 1024,
246
247
supportsNativeModules: Platform.OS !== 'web',
248
supportsWebAPIs: Platform.isDOMAvailable,
249
supportsBackgroundTasks: Platform.OS === 'ios' || Platform.OS === 'android',
250
};
251
252
// Usage
253
if (PlatformUtils.isNative) {
254
// Use native features
255
} else if (PlatformUtils.supportsWebAPIs) {
256
// Use web APIs
257
}
258
```
259
260
### Feature Detection Pattern
261
262
```typescript
263
import { Platform } from "@unimodules/core";
264
265
class FeatureDetector {
266
static canUseCamera(): boolean {
267
return Platform.OS === 'ios' || Platform.OS === 'android';
268
}
269
270
static canUseFileSystem(): boolean {
271
return Platform.OS !== 'web' || Platform.isDOMAvailable;
272
}
273
274
static canUseNotifications(): boolean {
275
if (Platform.OS === 'web') {
276
return Platform.isDOMAvailable &&
277
'Notification' in window &&
278
'serviceWorker' in navigator;
279
}
280
return true; // Native platforms support notifications
281
}
282
283
static canUseBiometrics(): boolean {
284
return Platform.OS === 'ios' || Platform.OS === 'android';
285
}
286
}
287
288
// Usage
289
if (FeatureDetector.canUseCamera()) {
290
// Initialize camera
291
}
292
293
if (FeatureDetector.canUseNotifications()) {
294
// Setup push notifications
295
}
296
```
297
298
### Runtime Environment Detection
299
300
```typescript
301
import { Platform } from "@unimodules/core";
302
303
// Detect specific runtime environments
304
const RuntimeEnvironment = {
305
isExpoClient: typeof expo !== 'undefined',
306
isStandalone: Platform.OS !== 'web' && typeof expo === 'undefined',
307
isWeb: Platform.OS === 'web',
308
isElectron: Platform.select({ electron: true, default: false }),
309
310
// Development vs production
311
isDevelopment: __DEV__,
312
isProduction: !__DEV__,
313
314
// Debugging
315
isRemoteDebugging: Platform.isAsyncDebugging,
316
hasDevTools: Platform.isDOMAvailable && window.__REACT_DEVTOOLS_GLOBAL_HOOK__,
317
};
318
319
// Conditional initialization
320
if (RuntimeEnvironment.isDevelopment) {
321
console.log('Development mode - enabling debug features');
322
}
323
324
if (RuntimeEnvironment.isRemoteDebugging) {
325
console.warn('Remote debugging - some features may not work correctly');
326
}
327
```
328
329
## Types
330
331
```typescript { .api }
332
type PlatformOSType = 'ios' | 'android' | 'windows' | 'macos' | 'web';
333
334
type PlatformSelectOSType =
335
| PlatformOSType
336
| 'native'
337
| 'electron'
338
| 'default';
339
340
interface EnvironmentCapabilities {
341
isDOMAvailable: boolean;
342
canUseEventListeners: boolean;
343
canUseViewport: boolean;
344
isAsyncDebugging: boolean;
345
}
346
```