@a16n/web-sim 0.0.0-stage → 0.1.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/README.md +13 -2
- package/bin/cli.ts +255 -0
- package/bin/web-sim.js +39 -0
- package/overrides/@backpackapp-io/react-native-toast/overrides.ts +15 -0
- package/overrides/@react-native-firebase/app/overrides.ts +34 -0
- package/overrides/@tiktok-for-business/react-native-sdk/NativeTiktokSdk.ts +29 -0
- package/overrides/@tiktok-for-business/react-native-sdk/overrides.ts +9 -0
- package/overrides/expo-application/ExpoApplication.ts +81 -0
- package/overrides/expo-application/overrides.ts +16 -0
- package/overrides/expo-background-task/ExpoBackgroundTaskModule.ts +30 -0
- package/overrides/expo-background-task/overrides.ts +9 -0
- package/overrides/expo-constants/ExponentConstants.ts +66 -0
- package/overrides/expo-constants/overrides.ts +9 -0
- package/overrides/expo-device/ExpoDevice.ts +104 -0
- package/overrides/expo-device/overrides.ts +9 -0
- package/overrides/expo-glass-effect/GlassContainer.tsx +9 -0
- package/overrides/expo-glass-effect/GlassView.tsx +95 -0
- package/overrides/expo-glass-effect/availability.ts +14 -0
- package/overrides/expo-glass-effect/overrides.ts +14 -0
- package/overrides/expo-haptics/Haptics.ts +67 -0
- package/overrides/expo-haptics/overrides.ts +5 -0
- package/overrides/expo-mail-composer/ExpoMailComposer.ts +40 -0
- package/overrides/expo-mail-composer/overrides.ts +9 -0
- package/overrides/expo-mesh-gradient/MeshGradientView.tsx +44 -0
- package/overrides/expo-mesh-gradient/overrides.ts +9 -0
- package/overrides/expo-modules-core/overrides.ts +12 -0
- package/overrides/expo-navigation-bar/ExpoNavigationBar.ts +29 -0
- package/overrides/expo-navigation-bar/NavigationBar.ts +43 -0
- package/overrides/expo-navigation-bar/overrides.ts +12 -0
- package/overrides/expo-router/NativeStackView.tsx +74 -0
- package/overrides/expo-router/NativeTabsView.tsx +263 -0
- package/overrides/expo-router/SafeAreaProvider.ts +6 -0
- package/overrides/expo-router/overrides.ts +23 -0
- package/overrides/expo-router/statusBarRequests.ts +51 -0
- package/overrides/expo-screen-orientation/ExpoScreenOrientation.ts +173 -0
- package/overrides/expo-screen-orientation/overrides.ts +21 -0
- package/overrides/expo-sharing/SharingNativeModule.ts +21 -0
- package/overrides/expo-sharing/overrides.ts +9 -0
- package/overrides/expo-sms/ExpoSMS.ts +24 -0
- package/overrides/expo-sms/overrides.ts +9 -0
- package/overrides/expo-splash-screen/SplashScreen.ts +25 -0
- package/overrides/expo-splash-screen/overrides.ts +5 -0
- package/overrides/expo-status-bar/StatusBar.tsx +44 -0
- package/overrides/expo-status-bar/overrides.ts +5 -0
- package/overrides/expo-store-review/ExpoStoreReview.ts +18 -0
- package/overrides/expo-store-review/overrides.ts +9 -0
- package/overrides/expo-symbols/SymbolView.tsx +192 -0
- package/overrides/expo-symbols/overrides.ts +9 -0
- package/overrides/expo-system-ui/ExpoSystemUI.ts +34 -0
- package/overrides/expo-system-ui/overrides.ts +9 -0
- package/overrides/expo-task-manager/ExpoTaskManager.ts +39 -0
- package/overrides/expo-task-manager/overrides.ts +9 -0
- package/overrides/expo-tracking-transparency/ExpoTrackingTransparency.ts +30 -0
- package/overrides/expo-tracking-transparency/overrides.ts +11 -0
- package/overrides/expo-video/ExpoVideo.ts +16 -0
- package/overrides/expo-video/overrides.ts +9 -0
- package/overrides/lottie-react-native/LottieView.tsx +87 -0
- package/overrides/lottie-react-native/overrides.ts +9 -0
- package/overrides/react-native-pager-view/PagerViewNativeComponent.tsx +254 -0
- package/overrides/react-native-pager-view/overrides.ts +9 -0
- package/overrides/react-native-reanimated/overrides.ts +28 -0
- package/overrides/react-native-safe-area-context/NativeSafeAreaProvider.tsx +40 -0
- package/overrides/react-native-safe-area-context/overrides.ts +12 -0
- package/overrides/react-native-share/NativeRNShare.ts +22 -0
- package/overrides/react-native-share/overrides.ts +12 -0
- package/overrides/react-native-share/platform.ts +14 -0
- package/overrides/react-native-turbo-image/TurboImage.tsx +101 -0
- package/overrides/react-native-turbo-image/overrides.ts +9 -0
- package/overrides/react-native-web/RefreshControl.tsx +57 -0
- package/overrides/react-native-web/overrides.ts +153 -0
- package/overrides/react-native-web/useElementLayout.ts +140 -0
- package/package.json +78 -4
- package/plugins/battery/overrides/expo-battery/ExpoBattery.ts +57 -0
- package/plugins/battery/overrides/expo-battery/overrides.ts +9 -0
- package/plugins/battery/plugin.ts +8 -0
- package/plugins/battery/shell.tsx +56 -0
- package/plugins/battery/state.ts +11 -0
- package/plugins/biometrics/overrides/expo-local-authentication/ExpoLocalAuthentication.ts +90 -0
- package/plugins/biometrics/overrides/expo-local-authentication/overrides.ts +11 -0
- package/plugins/biometrics/plugin.ts +8 -0
- package/plugins/biometrics/shell.tsx +88 -0
- package/plugins/biometrics/state.ts +41 -0
- package/plugins/brightness/app.ts +5 -0
- package/plugins/brightness/overrides/expo-brightness/Brightness.ts +98 -0
- package/plugins/brightness/overrides/expo-brightness/overrides.ts +10 -0
- package/plugins/brightness/plugin.ts +9 -0
- package/plugins/brightness/shell.tsx +55 -0
- package/plugins/brightness/state.ts +19 -0
- package/plugins/calendar/calendar.ts +299 -0
- package/plugins/calendar/overrides/expo-calendar/ExpoCalendar.ts +358 -0
- package/plugins/calendar/overrides/expo-calendar/LegacyExpoCalendar.ts +163 -0
- package/plugins/calendar/overrides/expo-calendar/overrides.ts +14 -0
- package/plugins/calendar/plugin.ts +8 -0
- package/plugins/calendar/shell.tsx +123 -0
- package/plugins/camera/overrides/expo-camera/ExpoCamera.tsx +269 -0
- package/plugins/camera/overrides/expo-camera/ExpoCameraManager.ts +56 -0
- package/plugins/camera/overrides/expo-camera/overrides.ts +12 -0
- package/plugins/camera/plugin.ts +9 -0
- package/plugins/camera/shell.tsx +107 -0
- package/plugins/camera/state.ts +61 -0
- package/plugins/clipboard/overrides/expo-clipboard/ExpoClipboard.ts +138 -0
- package/plugins/clipboard/overrides/expo-clipboard/overrides.ts +9 -0
- package/plugins/clipboard/plugin.ts +8 -0
- package/plugins/clipboard/shell.tsx +122 -0
- package/plugins/clipboard/state.ts +93 -0
- package/plugins/clock/app.ts +25 -0
- package/plugins/clock/plugin.ts +8 -0
- package/plugins/clock/shell.tsx +225 -0
- package/plugins/clock/state.ts +12 -0
- package/plugins/contacts/contacts.ts +125 -0
- package/plugins/contacts/overrides/expo-contacts/ExpoContacts.ts +153 -0
- package/plugins/contacts/overrides/expo-contacts/ExpoContactsNext.ts +232 -0
- package/plugins/contacts/overrides/expo-contacts/overrides.ts +12 -0
- package/plugins/contacts/plugin.ts +8 -0
- package/plugins/contacts/shell.tsx +109 -0
- package/plugins/home-indicator/app.ts +5 -0
- package/plugins/home-indicator/plugin.ts +9 -0
- package/plugins/home-indicator/requests.ts +48 -0
- package/plugins/home-indicator/shell.tsx +68 -0
- package/plugins/home-indicator/state.ts +31 -0
- package/plugins/keep-awake/app.ts +5 -0
- package/plugins/keep-awake/overrides/expo-keep-awake/ExpoKeepAwake.ts +27 -0
- package/plugins/keep-awake/overrides/expo-keep-awake/overrides.ts +9 -0
- package/plugins/keep-awake/plugin.ts +9 -0
- package/plugins/keep-awake/shell.tsx +80 -0
- package/plugins/keep-awake/state.ts +12 -0
- package/plugins/keyboard/Keyboard.tsx +354 -0
- package/plugins/keyboard/app.ts +90 -0
- package/plugins/keyboard/controller.ts +384 -0
- package/plugins/keyboard/fields.ts +244 -0
- package/plugins/keyboard/icons.tsx +283 -0
- package/plugins/keyboard/keyboard.css +362 -0
- package/plugins/keyboard/layouts/android.ts +175 -0
- package/plugins/keyboard/layouts/index.ts +10 -0
- package/plugins/keyboard/layouts/ios.ts +219 -0
- package/plugins/keyboard/layouts/types.ts +80 -0
- package/plugins/keyboard/overrides/react-native-keyboard-controller/KeyboardController.tsx +470 -0
- package/plugins/keyboard/overrides/react-native-keyboard-controller/overrides.ts +9 -0
- package/plugins/keyboard/plugin.ts +13 -0
- package/plugins/keyboard/shell.tsx +133 -0
- package/plugins/keyboard/state.ts +92 -0
- package/plugins/keyboard/suggestions.ts +63 -0
- package/plugins/keyboard/taps.ts +77 -0
- package/plugins/keyboard/typing.ts +112 -0
- package/plugins/locale/app.ts +232 -0
- package/plugins/locale/locale.ts +143 -0
- package/plugins/locale/overrides/expo-localization/ExpoLocalization.ts +41 -0
- package/plugins/locale/overrides/expo-localization/overrides.ts +9 -0
- package/plugins/locale/plugin.ts +9 -0
- package/plugins/locale/shell.tsx +147 -0
- package/plugins/locale/state.ts +36 -0
- package/plugins/location/geo.ts +56 -0
- package/plugins/location/overrides/expo-location/ExpoLocation.ts +235 -0
- package/plugins/location/overrides/expo-location/overrides.ts +11 -0
- package/plugins/location/places.ts +38 -0
- package/plugins/location/plugin.ts +8 -0
- package/plugins/location/shell.tsx +188 -0
- package/plugins/location/state.ts +51 -0
- package/plugins/motion/overrides/expo-sensors/DeviceSensor.ts +169 -0
- package/plugins/motion/overrides/expo-sensors/ExponentPedometer.ts +54 -0
- package/plugins/motion/overrides/expo-sensors/overrides.ts +14 -0
- package/plugins/motion/plugin.ts +8 -0
- package/plugins/motion/sensors.ts +116 -0
- package/plugins/motion/shell.tsx +136 -0
- package/plugins/motion/state.ts +46 -0
- package/plugins/network/app.ts +400 -0
- package/plugins/network/overrides/expo-network/ExpoNetwork.ts +51 -0
- package/plugins/network/overrides/expo-network/overrides.ts +9 -0
- package/plugins/network/plugin.ts +8 -0
- package/plugins/network/shell.tsx +67 -0
- package/plugins/network/state.ts +29 -0
- package/plugins/notifications/center.ts +255 -0
- package/plugins/notifications/content.ts +36 -0
- package/plugins/notifications/overrides/expo-notifications/NotificationsModule.ts +175 -0
- package/plugins/notifications/overrides/expo-notifications/channels.ts +108 -0
- package/plugins/notifications/overrides/expo-notifications/getDevicePushTokenAsync.ts +19 -0
- package/plugins/notifications/overrides/expo-notifications/getExpoPushTokenAsync.ts +16 -0
- package/plugins/notifications/overrides/expo-notifications/overrides.ts +36 -0
- package/plugins/notifications/plugin.ts +8 -0
- package/plugins/notifications/shell.tsx +192 -0
- package/plugins/notifications/state.ts +56 -0
- package/plugins/notifications/tokens.ts +47 -0
- package/plugins/notifications/trigger.ts +107 -0
- package/plugins/permissions/plugin.ts +6 -0
- package/plugins/permissions/shell.tsx +50 -0
- package/plugins/permissions/state.ts +38 -0
- package/plugins/photos/appLibrary.ts +114 -0
- package/plugins/photos/library.ts +322 -0
- package/plugins/photos/overrides/expo-image-picker/ExponentImagePicker.ts +153 -0
- package/plugins/photos/overrides/expo-image-picker/overrides.ts +13 -0
- package/plugins/photos/overrides/expo-media-library/ExpoMediaLibrary.ts +235 -0
- package/plugins/photos/overrides/expo-media-library/NativeMediaLibraryModule.ts +318 -0
- package/plugins/photos/overrides/expo-media-library/overrides.ts +19 -0
- package/plugins/photos/plugin.ts +8 -0
- package/plugins/photos/samples.ts +367 -0
- package/plugins/photos/shell.tsx +164 -0
- package/plugins/photos/state.ts +88 -0
- package/plugins/storage/files.ts +351 -0
- package/plugins/storage/overrides/expo-file-system/ExpoFileSystem.ts +770 -0
- package/plugins/storage/overrides/expo-file-system/ExponentFileSystem.ts +196 -0
- package/plugins/storage/overrides/expo-file-system/overrides.ts +13 -0
- package/plugins/storage/overrides/expo-secure-store/ExpoSecureStore.ts +75 -0
- package/plugins/storage/overrides/expo-secure-store/overrides.ts +9 -0
- package/plugins/storage/plugin.ts +8 -0
- package/plugins/storage/reset.ts +136 -0
- package/plugins/storage/secure.ts +71 -0
- package/plugins/storage/shell.tsx +365 -0
- package/plugins/storage/state.ts +13 -0
- package/src/client/app/appDocument.ts +113 -0
- package/src/client/app/appFocus.ts +97 -0
- package/src/client/app/console.ts +118 -0
- package/src/client/app/fonts.ts +59 -0
- package/src/client/app/main.tsx +99 -0
- package/src/client/app/routeContext.ts +45 -0
- package/src/client/app/touch.ts +640 -0
- package/src/client/entry.ts +20 -0
- package/src/client/modules.d.ts +8 -0
- package/src/client/react-native/app.ts +57 -0
- package/src/client/react-native/deep.ts +13 -0
- package/src/client/react-native/keyboard.tsx +192 -0
- package/src/client/react-native/library.ts +256 -0
- package/src/client/shell/Cookies.tsx +95 -0
- package/src/client/shell/LogDrawer.tsx +415 -0
- package/src/client/shell/PhotoPicker.tsx +174 -0
- package/src/client/shell/PluginOverlays.tsx +42 -0
- package/src/client/shell/PluginPanels.tsx +82 -0
- package/src/client/shell/ResetApp.tsx +87 -0
- package/src/client/shell/Resizer.tsx +99 -0
- package/src/client/shell/Shell.tsx +531 -0
- package/src/client/shell/SystemBars.tsx +376 -0
- package/src/client/shell/SystemCamera.tsx +181 -0
- package/src/client/shell/SystemOverlay.tsx +422 -0
- package/src/client/shell/SystemSheets.tsx +462 -0
- package/src/client/shell/backdrop.ts +229 -0
- package/src/client/shell/control.ts +91 -0
- package/src/client/shell/events.ts +226 -0
- package/src/client/shell/geometry.ts +68 -0
- package/src/client/shell/host.ts +492 -0
- package/src/client/shell/icons.tsx +34 -0
- package/src/client/shell/logFeed.ts +102 -0
- package/src/client/shell/main.tsx +22 -0
- package/src/client/shell/permissionCopy.ts +169 -0
- package/src/client/shell/plugins.ts +266 -0
- package/src/client/shell/popout.ts +185 -0
- package/src/client/shell/popover.ts +27 -0
- package/src/client/shell/shell.css +878 -0
- package/src/client/shell/statusTime.ts +31 -0
- package/src/client/shell/tooltips.ts +104 -0
- package/src/client/virtual.d.ts +21 -0
- package/src/devices/catalog.ts +223 -0
- package/src/devices/types.ts +83 -0
- package/src/node/appInfo.ts +186 -0
- package/src/node/appSource.ts +138 -0
- package/src/node/config.ts +180 -0
- package/src/node/control.ts +144 -0
- package/src/node/doctor.ts +199 -0
- package/src/node/events/cli.ts +206 -0
- package/src/node/events/shared.ts +238 -0
- package/src/node/events/store.ts +266 -0
- package/src/node/index.ts +5 -0
- package/src/node/overrides/define.ts +56 -0
- package/src/node/overrides/packages.ts +136 -0
- package/src/node/overrides/platformPatch.ts +56 -0
- package/src/node/overrides/plugin.ts +166 -0
- package/src/node/overrides/registry.ts +258 -0
- package/src/node/paths.ts +18 -0
- package/src/node/platform.ts +171 -0
- package/src/node/plugins.ts +98 -0
- package/src/node/proxy.ts +421 -0
- package/src/node/server.ts +253 -0
- package/src/plugin/index.ts +33 -0
- package/src/plugin/types.ts +127 -0
- package/src/plugin/ui.tsx +562 -0
- package/src/runtime/clock.ts +112 -0
- package/src/runtime/draw.ts +196 -0
- package/src/runtime/index.ts +513 -0
- package/src/runtime/media.ts +379 -0
- package/src/runtime/records.ts +76 -0
- package/src/runtime/timezone.ts +81 -0
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { defineOverrides } from '../../src/node/overrides/define.ts'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Font families resolve as on the simulated platform: `System`, Android's `normal`, `sans-serif-medium` and
|
|
5
|
+
* `monospace`, iOS's `ui-rounded`, and the system font as the fallback for any family without a glyph, where a
|
|
6
|
+
* browser would fall back to Times. The app document defines `__webSimFontFamily` per platform
|
|
7
|
+
* (`src/client/app/fonts.ts`).
|
|
8
|
+
*/
|
|
9
|
+
const FONT_FAMILY = `} else if (prop === 'fontFamily') {
|
|
10
|
+
resolvedStyle[prop] = typeof window !== 'undefined' && window.__webSimFontFamily ? window.__webSimFontFamily(value) : value;
|
|
11
|
+
} else if (prop === 'textDecorationLine') {`
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* React Native clamps opacity to between 0 and 1. The browser drops a value outside that range, or NaN, and draws
|
|
15
|
+
* the element opaque: a sheet's backdrop that divides by a height starting at 0 flashes.
|
|
16
|
+
*/
|
|
17
|
+
function clampOpacity(prop: string, value: string): string {
|
|
18
|
+
return `if (${prop} === 'opacity' && typeof ${value} === 'number') {
|
|
19
|
+
${value} = Number.isFinite(${value}) ? Math.min(1, Math.max(0, ${value})) : 0;
|
|
20
|
+
}`
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export default defineOverrides('react-native-web', [
|
|
24
|
+
{
|
|
25
|
+
versions: '>=0.19 <0.22',
|
|
26
|
+
note: 'system font, press timing, paged scroll view children, Appearance.setColorScheme, TextInput keyboardAppearance, scrolling cancels a press, opacity, layout events when a view moves or mounts at zero size, and their target, iOS overscroll and pull to refresh (src/client/app/touch.ts)',
|
|
27
|
+
files: {
|
|
28
|
+
'dist/exports/RefreshControl/index.js': './RefreshControl.tsx',
|
|
29
|
+
'dist/modules/useElementLayout/index.js': './useElementLayout.ts',
|
|
30
|
+
},
|
|
31
|
+
patch: [
|
|
32
|
+
{
|
|
33
|
+
file: /^dist\/(cjs\/)?exports\/StyleSheet\/compiler\/createReactDOMStyle\.js$/,
|
|
34
|
+
replace: (source) =>
|
|
35
|
+
source
|
|
36
|
+
.replace("var SYSTEM_FONT_STACK = '", "var SYSTEM_FONT_STACK = 'var(--web-sim-system-font),")
|
|
37
|
+
.replace(/\} else if \(prop === 'fontFamily'\) \{[\s\S]*?\} else if \(prop === 'textDecorationLine'\) \{/, FONT_FAMILY)
|
|
38
|
+
.replace('var value = style[prop];', `var value = style[prop];\n${clampOpacity('prop', 'value')}`)
|
|
39
|
+
.replace('(style[prop], prop)', '(value, prop)'),
|
|
40
|
+
},
|
|
41
|
+
// React Native styles gradients with `experimental_backgroundImage`, which is not a CSS property.
|
|
42
|
+
{
|
|
43
|
+
file: /^dist\/(cjs\/)?exports\/StyleSheet\/preprocess\.js$/,
|
|
44
|
+
replace: (source) =>
|
|
45
|
+
source
|
|
46
|
+
.replace('var _value = originalValue;', `var _value = originalValue;\n${clampOpacity('prop', '_value')}`)
|
|
47
|
+
.replace(
|
|
48
|
+
'var prop = PROPERTIES_STANDARD[originalProp] || originalProp;',
|
|
49
|
+
"var prop = originalProp === 'experimental_backgroundImage' ? 'backgroundImage' : PROPERTIES_STANDARD[originalProp] || originalProp;",
|
|
50
|
+
),
|
|
51
|
+
},
|
|
52
|
+
// A press shows at once and lasts at least 130ms, as on a device. react-native-web waits 50ms before showing
|
|
53
|
+
// it and ends it on release, so a quick tap never draws as pressed.
|
|
54
|
+
{
|
|
55
|
+
file: /^dist\/(cjs\/)?modules\/usePressEvents\/PressResponder\.js$/,
|
|
56
|
+
replace: (source) =>
|
|
57
|
+
source
|
|
58
|
+
.replace('var DEFAULT_LONG_PRESS_DELAY_MS = 450; // 500 - 50', 'var DEFAULT_LONG_PRESS_DELAY_MS = 500;')
|
|
59
|
+
.replace('var DEFAULT_PRESS_DELAY_MS = 50;', 'var DEFAULT_PRESS_DELAY_MS = 0;\nvar MIN_PRESS_DURATION_MS = 130;')
|
|
60
|
+
.replace(' _activate(event) {', ' _activate(event) {\n this._activatedAt = performance.now();')
|
|
61
|
+
.replace(
|
|
62
|
+
'var delayPressEnd = normalizeDelay(this._config.delayPressEnd);',
|
|
63
|
+
'var delayPressEnd = Math.max(normalizeDelay(this._config.delayPressEnd), MIN_PRESS_DURATION_MS - (performance.now() - this._activatedAt));',
|
|
64
|
+
)
|
|
65
|
+
// A press the responder system took away (a list scrolling under the finger) is cancelled, as on a
|
|
66
|
+
// device. react-native-web presses on the browser's click, which still comes when the pointer is let go
|
|
67
|
+
// over the element it went down on, so a drag that scrolls and comes back would press.
|
|
68
|
+
.replace('this._selectionTerminated = false;', 'this._selectionTerminated = false;\n this._terminated = false;')
|
|
69
|
+
.replace(
|
|
70
|
+
"if (event.nativeEvent.type === 'selectionchange') {",
|
|
71
|
+
"this._terminated = true;\n if (event.nativeEvent.type === 'selectionchange') {",
|
|
72
|
+
)
|
|
73
|
+
.replace(
|
|
74
|
+
'if (this._longPressDispatched || this._selectionTerminated) {',
|
|
75
|
+
'if (this._longPressDispatched || this._selectionTerminated || this._terminated) {',
|
|
76
|
+
),
|
|
77
|
+
},
|
|
78
|
+
// A scroll view's `keyboardShouldPersistTaps` goes on its element, for the keyboard plugin's taps
|
|
79
|
+
// (plugins/keyboard/taps.ts). `true` and `false` are its old spellings of `always` and `never`. So do
|
|
80
|
+
// `bounces` and `alwaysBounceVertical`, for iOS's overscroll (src/client/app/touch.ts): `none` never bounces,
|
|
81
|
+
// `content` only when there is more to scroll.
|
|
82
|
+
{
|
|
83
|
+
file: /^dist\/(cjs\/)?exports\/ScrollView\/index\.js$/,
|
|
84
|
+
replace: (source) =>
|
|
85
|
+
source.replace(
|
|
86
|
+
/var props = (.*)\(\{\}, other\), \{\}, \{/,
|
|
87
|
+
"var persistTaps = this.props.keyboardShouldPersistTaps;\n var props = $1({}, other), {}, {\n dataSet: Object.assign({}, other.dataSet, { keyboardShouldPersistTaps: persistTaps === true ? 'always' : persistTaps === false || persistTaps == null ? 'never' : persistTaps, webSimBounce: other.bounces === false ? 'none' : other.alwaysBounceVertical === false ? 'content' : undefined }),",
|
|
88
|
+
),
|
|
89
|
+
},
|
|
90
|
+
// With `pagingEnabled` react-native-web wraps each child in a view for `scroll-snap-align`, which React
|
|
91
|
+
// Native doesn't. The wrapper is a column, so in a horizontal list the child no longer stretches to the list's
|
|
92
|
+
// height and a page sized `height: '100%'` (a carousel's) comes out 0 tall. Laid out along the scroll
|
|
93
|
+
// direction, the wrapper passes the stretch through.
|
|
94
|
+
{
|
|
95
|
+
file: /^dist\/(cjs\/)?exports\/ScrollView\/index\.js$/,
|
|
96
|
+
replace: (source) =>
|
|
97
|
+
source.replace(
|
|
98
|
+
'pagingEnabled && styles.pagingEnabledChild]',
|
|
99
|
+
"pagingEnabled && styles.pagingEnabledChild, pagingEnabled && horizontal && { flexDirection: 'row' }]",
|
|
100
|
+
),
|
|
101
|
+
},
|
|
102
|
+
// A text input's `keyboardAppearance` goes on its element, for the keyboard plugin to draw the keyboard light
|
|
103
|
+
// or dark (plugins/keyboard/fields.ts). react-native-web drops it.
|
|
104
|
+
{
|
|
105
|
+
file: /^dist\/(cjs\/)?exports\/TextInput\/index\.js$/,
|
|
106
|
+
replace: (source) =>
|
|
107
|
+
source.replace(
|
|
108
|
+
'supportedProps.inputMode = _inputMode;',
|
|
109
|
+
"supportedProps.inputMode = _inputMode;\n if (props.keyboardAppearance != null) {\n supportedProps.dataSet = Object.assign({}, supportedProps.dataSet, { keyboardAppearance: props.keyboardAppearance });\n }",
|
|
110
|
+
),
|
|
111
|
+
},
|
|
112
|
+
// `Appearance.setColorScheme`, which react-native-web lacks, pins the app's appearance as it does on a device:
|
|
113
|
+
// `prefers-color-scheme` in the app's frame, and so `getColorScheme` and its listeners, follow it, and so do the
|
|
114
|
+
// keyboard and the status bar's `auto` (src/client/shell/host.ts).
|
|
115
|
+
{
|
|
116
|
+
file: /^dist\/(cjs\/)?exports\/Appearance\/index\.js$/,
|
|
117
|
+
replace: (source) =>
|
|
118
|
+
source.replace(
|
|
119
|
+
'var Appearance = {',
|
|
120
|
+
`var Appearance = {
|
|
121
|
+
setColorScheme(scheme) {
|
|
122
|
+
var host = window.__webSimHost || (window.parent !== window ? window.parent.__webSimHost : undefined);
|
|
123
|
+
if (host && host.setAppColorScheme) {
|
|
124
|
+
host.setAppColorScheme(scheme === 'light' || scheme === 'dark' ? scheme : null);
|
|
125
|
+
}
|
|
126
|
+
},`,
|
|
127
|
+
),
|
|
128
|
+
},
|
|
129
|
+
// Past an end on iOS the list is drawn with space before or after its content, which its offset and size
|
|
130
|
+
// leave out, so the offset goes below zero or past the end as on a device.
|
|
131
|
+
{
|
|
132
|
+
file: /^dist\/(cjs\/)?exports\/ScrollView\/ScrollViewBase\.js$/,
|
|
133
|
+
replace: (source) =>
|
|
134
|
+
source
|
|
135
|
+
.replace('return e.target.scrollTop;', 'return e.target.scrollTop - (e.target.__webSimPadTop || 0);')
|
|
136
|
+
.replace(
|
|
137
|
+
'return e.target.scrollHeight;',
|
|
138
|
+
'return e.target.scrollHeight - (e.target.__webSimPadTop || 0) - (e.target.__webSimPadBottom || 0);',
|
|
139
|
+
),
|
|
140
|
+
},
|
|
141
|
+
// React Native gives layout events the view as `target`; react-native-web only sets `nativeEvent.target`. The
|
|
142
|
+
// module build is replaced outright (useElementLayout.ts); this is for the CommonJS one.
|
|
143
|
+
{
|
|
144
|
+
file: /^dist\/cjs\/modules\/useElementLayout\/index\.js$/,
|
|
145
|
+
replace: (source) =>
|
|
146
|
+
source.replace(
|
|
147
|
+
'onLayout(event);',
|
|
148
|
+
"Object.defineProperty(event, 'target', { enumerable: true, get: () => entry.target });\n onLayout(event);",
|
|
149
|
+
),
|
|
150
|
+
},
|
|
151
|
+
],
|
|
152
|
+
},
|
|
153
|
+
])
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// @ts-nocheck: compiled in react-native-web's place, against its untyped modules.
|
|
2
|
+
import canUseDOM from '../canUseDom'
|
|
3
|
+
import useLayoutEffect from '../useLayoutEffect'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* react-native-web's `onLayout`, made to fire when React Native's does: whenever a view's frame (its position in its
|
|
7
|
+
* parent, and its size) changes, and once when it mounts. react-native-web listens only for resizes, so a view that
|
|
8
|
+
* moves without changing size (a tab pushed along when another tab appears, a row when the one above it grows)
|
|
9
|
+
* never reports its new `x` and `y`, and a view of zero size never reports at all.
|
|
10
|
+
*
|
|
11
|
+
* A ResizeObserver still catches resizes. Anything else that can move a view (nodes added or removed, text, classes
|
|
12
|
+
* and inline styles changing) schedules one check, on the next frame, of every view with `onLayout`; each reports
|
|
13
|
+
* only if its frame differs from the last it reported. The check reads offsets, not rects, so it stays cheap when a
|
|
14
|
+
* long list scrolls or animates.
|
|
15
|
+
*/
|
|
16
|
+
const DOM_LAYOUT_HANDLER_NAME = '__reactLayoutHandler'
|
|
17
|
+
|
|
18
|
+
type Frame = { x: number; y: number; width: number; height: number }
|
|
19
|
+
|
|
20
|
+
const frames = new WeakMap<Element, Frame>()
|
|
21
|
+
const observed = new Set<Element>()
|
|
22
|
+
let resizeObserver: ResizeObserver | null = null
|
|
23
|
+
let mutationObserver: MutationObserver | null = null
|
|
24
|
+
let scheduled = false
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The view's frame as Yoga gives it: its border box's position in its parent's border box, and its size. It is read
|
|
28
|
+
* from the offset properties, so it ignores transforms and scrolling as Yoga does, and costs four reads when the
|
|
29
|
+
* parent positions the view (react-native-web's views are all `position: relative`). react-native-web's own
|
|
30
|
+
* measure walks every ancestor, and counts the parent's scroll, so a list's content would seem to move as it scrolls.
|
|
31
|
+
*/
|
|
32
|
+
function frameOf(node: HTMLElement): Frame {
|
|
33
|
+
const parent = node.parentElement
|
|
34
|
+
const width = node.offsetWidth
|
|
35
|
+
const height = node.offsetHeight
|
|
36
|
+
if (parent === null) {
|
|
37
|
+
return { x: 0, y: 0, width, height }
|
|
38
|
+
}
|
|
39
|
+
const offsetParent = node.offsetParent
|
|
40
|
+
if (offsetParent === parent) {
|
|
41
|
+
return { x: node.offsetLeft + parent.clientLeft, y: node.offsetTop + parent.clientTop, width, height }
|
|
42
|
+
}
|
|
43
|
+
if (offsetParent === parent.offsetParent) {
|
|
44
|
+
return { x: node.offsetLeft - parent.offsetLeft, y: node.offsetTop - parent.offsetTop, width, height }
|
|
45
|
+
}
|
|
46
|
+
const own = node.getBoundingClientRect()
|
|
47
|
+
const outer = parent.getBoundingClientRect()
|
|
48
|
+
return { x: own.left - outer.left, y: own.top - outer.top, width, height }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function report(node: Element) {
|
|
52
|
+
const onLayout = node[DOM_LAYOUT_HANDLER_NAME]
|
|
53
|
+
if (typeof onLayout !== 'function' || !(node instanceof HTMLElement)) {
|
|
54
|
+
return
|
|
55
|
+
}
|
|
56
|
+
const frame = frameOf(node)
|
|
57
|
+
const last = frames.get(node)
|
|
58
|
+
if (last !== undefined && last.x === frame.x && last.y === frame.y && last.width === frame.width && last.height === frame.height) {
|
|
59
|
+
return
|
|
60
|
+
}
|
|
61
|
+
frames.set(node, frame)
|
|
62
|
+
const page = node.getBoundingClientRect()
|
|
63
|
+
const event = { nativeEvent: { layout: { ...frame, left: page.left, top: page.top } }, timeStamp: Date.now() }
|
|
64
|
+
Object.defineProperty(event.nativeEvent, 'target', { enumerable: true, get: () => node })
|
|
65
|
+
// React Native gives layout events the view as `target`; react-native-web only sets `nativeEvent.target`.
|
|
66
|
+
Object.defineProperty(event, 'target', { enumerable: true, get: () => node })
|
|
67
|
+
onLayout(event)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function checkAll() {
|
|
71
|
+
scheduled = false
|
|
72
|
+
for (const node of observed) {
|
|
73
|
+
if (node.isConnected) {
|
|
74
|
+
report(node)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function schedule() {
|
|
80
|
+
if (!scheduled) {
|
|
81
|
+
scheduled = true
|
|
82
|
+
requestAnimationFrame(checkAll)
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function start(): ResizeObserver | null {
|
|
87
|
+
if (!canUseDOM || typeof window.ResizeObserver === 'undefined') {
|
|
88
|
+
return null
|
|
89
|
+
}
|
|
90
|
+
if (resizeObserver === null) {
|
|
91
|
+
resizeObserver = new window.ResizeObserver((entries) => {
|
|
92
|
+
for (const entry of entries) {
|
|
93
|
+
report(entry.target)
|
|
94
|
+
}
|
|
95
|
+
})
|
|
96
|
+
mutationObserver = new MutationObserver(schedule)
|
|
97
|
+
mutationObserver.observe(document.documentElement, {
|
|
98
|
+
subtree: true,
|
|
99
|
+
childList: true,
|
|
100
|
+
characterData: true,
|
|
101
|
+
attributes: true,
|
|
102
|
+
attributeFilter: ['style', 'class'],
|
|
103
|
+
})
|
|
104
|
+
window.addEventListener('resize', schedule)
|
|
105
|
+
}
|
|
106
|
+
return resizeObserver
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export default function useElementLayout(ref, onLayout) {
|
|
110
|
+
const observer = start()
|
|
111
|
+
useLayoutEffect(() => {
|
|
112
|
+
const node = ref.current
|
|
113
|
+
if (node != null) {
|
|
114
|
+
node[DOM_LAYOUT_HANDLER_NAME] = onLayout
|
|
115
|
+
}
|
|
116
|
+
}, [ref, onLayout])
|
|
117
|
+
|
|
118
|
+
// Observing is done in a separate effect to avoid this effect running when 'onLayout' changes.
|
|
119
|
+
useLayoutEffect(() => {
|
|
120
|
+
const node = ref.current
|
|
121
|
+
if (node != null && observer != null) {
|
|
122
|
+
if (typeof node[DOM_LAYOUT_HANDLER_NAME] === 'function') {
|
|
123
|
+
observer.observe(node)
|
|
124
|
+
observed.add(node)
|
|
125
|
+
// A view of zero size gets no first resize; this reports it as mounted.
|
|
126
|
+
schedule()
|
|
127
|
+
} else {
|
|
128
|
+
observer.unobserve(node)
|
|
129
|
+
observed.delete(node)
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return () => {
|
|
133
|
+
if (node != null && observer != null) {
|
|
134
|
+
observer.unobserve(node)
|
|
135
|
+
observed.delete(node)
|
|
136
|
+
frames.delete(node)
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}, [ref, observer])
|
|
140
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,80 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@a16n/web-sim",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A React Native device simulator that runs in a Chrome tab",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/a16n-dev/web-sim.git",
|
|
9
|
+
"directory": "packages/web-sim"
|
|
10
|
+
},
|
|
11
|
+
"bin": {
|
|
12
|
+
"web-sim": "./bin/web-sim.js"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"bin",
|
|
16
|
+
"src",
|
|
17
|
+
"overrides",
|
|
18
|
+
"plugins",
|
|
19
|
+
"README.md"
|
|
20
|
+
],
|
|
21
|
+
"exports": {
|
|
22
|
+
".": "./src/node/index.ts",
|
|
23
|
+
"./runtime": "./src/runtime/index.ts",
|
|
24
|
+
"./overrides": "./src/node/overrides/define.ts",
|
|
25
|
+
"./shell": "./src/plugin/index.ts"
|
|
26
|
+
},
|
|
27
|
+
"engines": {
|
|
28
|
+
"node": ">=22.18"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"typecheck": "tsc --noEmit -p ."
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@phosphor-icons/react": "^2.1.10",
|
|
38
|
+
"@vitejs/plugin-react": "^5.1.0",
|
|
39
|
+
"playwright-core": "^1.65.1",
|
|
40
|
+
"semver": "^7.7.3",
|
|
41
|
+
"vite": "^7.2.0",
|
|
42
|
+
"esbuild": "^0.25.0"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"expo": ">=58 <59",
|
|
46
|
+
"react": ">=19",
|
|
47
|
+
"react-dom": ">=19",
|
|
48
|
+
"react-native": ">=0.88 <0.89",
|
|
49
|
+
"react-native-reanimated": ">=4.7.1 <4.8",
|
|
50
|
+
"react-native-web": ">=0.19 <0.22"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@types/node": "^24.10.0",
|
|
54
|
+
"@types/react": "~19.3.0",
|
|
55
|
+
"@types/react-dom": "~19.2.0",
|
|
56
|
+
"@types/semver": "^7.7.1",
|
|
57
|
+
"react-native": "0.88.1",
|
|
58
|
+
"typescript": "~5.9.3"
|
|
59
|
+
},
|
|
60
|
+
"peerDependenciesMeta": {
|
|
61
|
+
"expo": {
|
|
62
|
+
"optional": true
|
|
63
|
+
},
|
|
64
|
+
"react": {
|
|
65
|
+
"optional": true
|
|
66
|
+
},
|
|
67
|
+
"react-dom": {
|
|
68
|
+
"optional": true
|
|
69
|
+
},
|
|
70
|
+
"react-native": {
|
|
71
|
+
"optional": true
|
|
72
|
+
},
|
|
73
|
+
"react-native-reanimated": {
|
|
74
|
+
"optional": true
|
|
75
|
+
},
|
|
76
|
+
"react-native-web": {
|
|
77
|
+
"optional": true
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { platform, plugin } from 'web-sim/runtime'
|
|
2
|
+
import { type BatteryState as Battery, INITIAL_BATTERY } from '../../state.ts'
|
|
3
|
+
|
|
4
|
+
/** `BatteryState` from `Battery.types`. */
|
|
5
|
+
const BatteryState = { UNKNOWN: 0, UNPLUGGED: 1, CHARGING: 2, FULL: 3 } as const
|
|
6
|
+
|
|
7
|
+
const POWER = { unplugged: BatteryState.UNPLUGGED, charging: BatteryState.CHARGING, full: BatteryState.FULL } as const
|
|
8
|
+
|
|
9
|
+
const EVENTS = {
|
|
10
|
+
level: 'Expo.batteryLevelDidChange',
|
|
11
|
+
state: 'Expo.batteryStateDidChange',
|
|
12
|
+
power: 'Expo.powerModeDidChange',
|
|
13
|
+
} as const
|
|
14
|
+
|
|
15
|
+
type Listener = (event: Record<string, unknown>) => void
|
|
16
|
+
|
|
17
|
+
const battery = () => plugin<Battery>('battery', INITIAL_BATTERY)
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* `ExpoBattery`, reporting the battery panel. Each change in the panel reaches the matching listeners.
|
|
21
|
+
*/
|
|
22
|
+
const ExpoBattery = {
|
|
23
|
+
isSupported: true,
|
|
24
|
+
async getBatteryLevelAsync() {
|
|
25
|
+
return battery().get().level
|
|
26
|
+
},
|
|
27
|
+
async getBatteryStateAsync() {
|
|
28
|
+
return POWER[battery().get().power]
|
|
29
|
+
},
|
|
30
|
+
async isLowPowerModeEnabledAsync() {
|
|
31
|
+
return battery().get().lowPowerMode
|
|
32
|
+
},
|
|
33
|
+
...(platform() === 'android'
|
|
34
|
+
? {
|
|
35
|
+
async isBatteryOptimizationEnabledAsync() {
|
|
36
|
+
return battery().get().optimized
|
|
37
|
+
},
|
|
38
|
+
}
|
|
39
|
+
: {}),
|
|
40
|
+
addListener(eventName: string, listener: Listener) {
|
|
41
|
+
const remove = battery().watch((next, previous) => {
|
|
42
|
+
if (eventName === EVENTS.level && next.level !== previous.level) {
|
|
43
|
+
listener({ batteryLevel: next.level })
|
|
44
|
+
} else if (eventName === EVENTS.state && next.power !== previous.power) {
|
|
45
|
+
listener({ batteryState: POWER[next.power] })
|
|
46
|
+
} else if (eventName === EVENTS.power && next.lowPowerMode !== previous.lowPowerMode) {
|
|
47
|
+
listener({ lowPowerMode: next.lowPowerMode })
|
|
48
|
+
}
|
|
49
|
+
})
|
|
50
|
+
return { remove }
|
|
51
|
+
},
|
|
52
|
+
removeAllListeners() {},
|
|
53
|
+
startObserving() {},
|
|
54
|
+
stopObserving() {},
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export default ExpoBattery
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { defineOverrides } from '../../../../src/node/overrides/define.ts'
|
|
2
|
+
|
|
3
|
+
export default defineOverrides('expo-battery', [
|
|
4
|
+
{
|
|
5
|
+
versions: '>=58 <59',
|
|
6
|
+
note: 'reports the battery panel; its changes reach the listeners',
|
|
7
|
+
files: { 'build/ExpoBattery.web.js': './ExpoBattery.ts' },
|
|
8
|
+
},
|
|
9
|
+
])
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { BatteryMediumIcon } from '@phosphor-icons/react'
|
|
2
|
+
import { defineShellPlugin, Field, Segmented, Slider, Toggle } from 'web-sim/shell'
|
|
3
|
+
import { type BatteryState, INITIAL_BATTERY } from './state.ts'
|
|
4
|
+
|
|
5
|
+
type Animation = { to: number; seconds?: number }
|
|
6
|
+
|
|
7
|
+
export default defineShellPlugin<BatteryState>({
|
|
8
|
+
id: 'battery',
|
|
9
|
+
title: 'Battery',
|
|
10
|
+
icon: BatteryMediumIcon,
|
|
11
|
+
initialState: INITIAL_BATTERY,
|
|
12
|
+
statusBar: (state) => ({
|
|
13
|
+
battery: { level: state.level, charging: state.power !== 'unplugged', lowPower: state.lowPowerMode },
|
|
14
|
+
}),
|
|
15
|
+
actions: {
|
|
16
|
+
/** Moves the level to `to` over `seconds` (default 10), a percent at a time, as a draining or charging phone. */
|
|
17
|
+
animate: (context, args) => {
|
|
18
|
+
const { to, seconds = 10 } = args as Animation
|
|
19
|
+
const from = context.state().level
|
|
20
|
+
const steps = Math.max(1, Math.round(Math.abs(to - from) * 100))
|
|
21
|
+
return new Promise<BatteryState>((resolve) => {
|
|
22
|
+
let step = 0
|
|
23
|
+
const timer = setInterval(() => {
|
|
24
|
+
step += 1
|
|
25
|
+
const level = Math.round((from + ((to - from) * step) / steps) * 100) / 100
|
|
26
|
+
context.set({ level, power: level >= 1 && context.state().power !== 'unplugged' ? 'full' : context.state().power })
|
|
27
|
+
if (step >= steps) {
|
|
28
|
+
clearInterval(timer)
|
|
29
|
+
resolve(context.state())
|
|
30
|
+
}
|
|
31
|
+
}, (seconds * 1000) / steps)
|
|
32
|
+
})
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
Panel: ({ state, set, sim }) => (
|
|
36
|
+
<>
|
|
37
|
+
<Field label="Level" value={`${Math.round(state.level * 100)}%`}>
|
|
38
|
+
<Slider label="Battery level" value={state.level} onChange={(level) => set({ level, power: level < 1 && state.power === 'full' ? 'charging' : state.power })} />
|
|
39
|
+
</Field>
|
|
40
|
+
<Segmented
|
|
41
|
+
label="Power"
|
|
42
|
+
value={state.power}
|
|
43
|
+
options={[
|
|
44
|
+
{ value: 'unplugged', label: 'Unplugged' },
|
|
45
|
+
{ value: 'charging', label: 'Charging' },
|
|
46
|
+
{ value: 'full', label: 'Full' },
|
|
47
|
+
]}
|
|
48
|
+
onChange={(power) => set(power === 'full' ? { power, level: 1 } : { power })}
|
|
49
|
+
/>
|
|
50
|
+
<Toggle label={sim.device.platform === 'ios' ? 'Low Power Mode' : 'Battery Saver'} checked={state.lowPowerMode} onChange={(lowPowerMode) => set({ lowPowerMode })} />
|
|
51
|
+
{sim.device.platform === 'android' && (
|
|
52
|
+
<Toggle label="Battery optimisation" checked={state.optimized} onChange={(optimized) => set({ optimized })} />
|
|
53
|
+
)}
|
|
54
|
+
</>
|
|
55
|
+
),
|
|
56
|
+
})
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** The battery the panel sets and `expo-battery` reports. Shared by the shell module and the override. */
|
|
2
|
+
export type BatteryState = {
|
|
3
|
+
/** 0 to 1. */
|
|
4
|
+
level: number
|
|
5
|
+
power: 'unplugged' | 'charging' | 'full'
|
|
6
|
+
lowPowerMode: boolean
|
|
7
|
+
/** Android's battery optimisation for the app. */
|
|
8
|
+
optimized: boolean
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const INITIAL_BATTERY: BatteryState = { level: 0.8, power: 'unplugged', lowPowerMode: false, optimized: true }
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { appendLog, getState, platform, plugin, requestPermission, getPermission, showDialog } from 'web-sim/runtime'
|
|
2
|
+
import { type BiometricsState, INITIAL_BIOMETRICS, sensorOf } from '../../state.ts'
|
|
3
|
+
|
|
4
|
+
const AuthenticationType = { FINGERPRINT: 1, FACIAL_RECOGNITION: 2 } as const
|
|
5
|
+
const SecurityLevel = { NONE: 0, BIOMETRIC_STRONG: 3 } as const
|
|
6
|
+
|
|
7
|
+
type Options = { promptMessage?: string; cancelLabel?: string; disableDeviceFallback?: boolean; fallbackLabel?: string }
|
|
8
|
+
|
|
9
|
+
type Result = { success: true } | { success: false; error: string }
|
|
10
|
+
|
|
11
|
+
const RESULTS: Record<string, string> = {
|
|
12
|
+
success: 'matched',
|
|
13
|
+
authentication_failed: 'failed',
|
|
14
|
+
user_cancel: 'cancelled',
|
|
15
|
+
system_cancel: 'cancelled by the app',
|
|
16
|
+
not_enrolled: 'not enrolled',
|
|
17
|
+
not_available: 'Face ID off for the app',
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const biometrics = () => plugin<BiometricsState>('biometrics', INITIAL_BIOMETRICS)
|
|
21
|
+
|
|
22
|
+
const sensor = () => sensorOf(biometrics().get(), getState().device)
|
|
23
|
+
|
|
24
|
+
/** Face ID the user turned off for the app is reported as unavailable, as iOS reports it. */
|
|
25
|
+
function faceIdDenied(): boolean {
|
|
26
|
+
return platform() === 'ios' && sensor() === 'face' && getPermission('face-id').status === 'denied'
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function enrolled(): boolean {
|
|
30
|
+
return biometrics().get().enrolled && !faceIdDenied()
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function logged(reason: string, result: Result): Result {
|
|
34
|
+
const outcome = RESULTS[result.success ? 'success' : result.error] ?? (result.success ? 'success' : result.error)
|
|
35
|
+
biometrics().set({ log: appendLog(biometrics().get().log, `${reason || 'No reason'}: ${outcome}`, 10) })
|
|
36
|
+
return result
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The simulated phone's biometrics, as the panel sets them: the sensor, whether a face or finger is enrolled, and
|
|
41
|
+
* what each scan does. `authenticateAsync` shows the system prompt, after iOS's one-time Face ID permission prompt,
|
|
42
|
+
* and maps what the sensor saw to the module's result.
|
|
43
|
+
*/
|
|
44
|
+
export default {
|
|
45
|
+
async hasHardwareAsync() {
|
|
46
|
+
return true
|
|
47
|
+
},
|
|
48
|
+
async isEnrolledAsync() {
|
|
49
|
+
return enrolled()
|
|
50
|
+
},
|
|
51
|
+
async getEnrolledLevelAsync() {
|
|
52
|
+
return enrolled() ? SecurityLevel.BIOMETRIC_STRONG : SecurityLevel.NONE
|
|
53
|
+
},
|
|
54
|
+
async supportedAuthenticationTypesAsync() {
|
|
55
|
+
return [sensor() === 'face' ? AuthenticationType.FACIAL_RECOGNITION : AuthenticationType.FINGERPRINT]
|
|
56
|
+
},
|
|
57
|
+
async authenticateAsync(options: Options): Promise<Result> {
|
|
58
|
+
const reason = options.promptMessage ?? ''
|
|
59
|
+
if (!biometrics().get().enrolled) {
|
|
60
|
+
return logged(reason, { success: false, error: 'not_enrolled' })
|
|
61
|
+
}
|
|
62
|
+
const kind = sensor()
|
|
63
|
+
if (platform() === 'ios' && kind === 'face') {
|
|
64
|
+
const permission = await requestPermission('face-id')
|
|
65
|
+
if (!permission.granted) {
|
|
66
|
+
return logged(reason, { success: false, error: 'not_available' })
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
biometrics().set({ prompt: reason })
|
|
70
|
+
biometrics().emit('prompt')
|
|
71
|
+
try {
|
|
72
|
+
const { button } = await showDialog({ kind: 'biometric', reason: options.promptMessage, cancelLabel: options.cancelLabel, sensor: kind })
|
|
73
|
+
switch (button) {
|
|
74
|
+
case 0:
|
|
75
|
+
return logged(reason, { success: true })
|
|
76
|
+
case 1:
|
|
77
|
+
return logged(reason, { success: false, error: 'authentication_failed' })
|
|
78
|
+
case -1:
|
|
79
|
+
return logged(reason, { success: false, error: 'system_cancel' })
|
|
80
|
+
default:
|
|
81
|
+
return logged(reason, { success: false, error: 'user_cancel' })
|
|
82
|
+
}
|
|
83
|
+
} finally {
|
|
84
|
+
biometrics().set({ prompt: null })
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
async cancelAuthenticate() {
|
|
88
|
+
biometrics().emit('cancel')
|
|
89
|
+
},
|
|
90
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { defineOverrides } from '../../../../src/node/overrides/define.ts'
|
|
2
|
+
import { simulatedPlatform } from '../../../../src/node/overrides/platformPatch.ts'
|
|
3
|
+
|
|
4
|
+
export default defineOverrides('expo-local-authentication', [
|
|
5
|
+
{
|
|
6
|
+
versions: '>=58 <59',
|
|
7
|
+
note: 'the biometrics panel: its sensor, enrolment and next scan; Face ID asks its permission first',
|
|
8
|
+
files: { 'build/ExpoLocalAuthentication.web.js': './ExpoLocalAuthentication.ts' },
|
|
9
|
+
patch: [simulatedPlatform('build/LocalAuthentication.types.js')],
|
|
10
|
+
},
|
|
11
|
+
])
|