@a16n/web-sim 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/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 +157 -0
- package/overrides/react-native-web/useElementLayout.ts +152 -0
- package/package.json +80 -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 +129 -0
- package/src/client/app/appFocus.ts +97 -0
- package/src/client/app/console.ts +118 -0
- package/src/client/app/fonts.ts +76 -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 +23 -0
- package/src/devices/catalog.ts +223 -0
- package/src/devices/types.ts +83 -0
- package/src/node/appInfo.ts +188 -0
- package/src/node/appSource.ts +140 -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/fonts.ts +377 -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 +169 -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 +254 -0
- package/src/node/worklets.ts +86 -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 +519 -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,57 @@
|
|
|
1
|
+
import ReactNative, { Dimensions, PixelRatio as WebPixelRatio, StatusBar as LibraryStatusBar } from 'react-native'
|
|
2
|
+
import { getState } from '../../runtime/index.ts'
|
|
3
|
+
|
|
4
|
+
export * from 'react-native'
|
|
5
|
+
|
|
6
|
+
const { device } = getState()
|
|
7
|
+
|
|
8
|
+
type PlatformSpec<T> = { ios?: T; android?: T; native?: T; default?: T; web?: T }
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* App code sees the simulated device's platform. A platform change reloads the app, so it is read once.
|
|
12
|
+
*/
|
|
13
|
+
export const Platform = {
|
|
14
|
+
OS: device.platform,
|
|
15
|
+
Version: device.platform === 'ios' ? device.osVersion : Number(device.osVersion),
|
|
16
|
+
isPad: false,
|
|
17
|
+
isTV: false,
|
|
18
|
+
isVision: false,
|
|
19
|
+
isTesting: false,
|
|
20
|
+
constants:
|
|
21
|
+
device.platform === 'ios'
|
|
22
|
+
? { osVersion: device.osVersion, systemName: 'iOS', interfaceIdiom: 'phone', isTesting: false }
|
|
23
|
+
: { Version: Number(device.osVersion), Release: device.osVersion, Model: device.label, Brand: 'google', isTesting: false },
|
|
24
|
+
select<T>(spec: PlatformSpec<T>): T | undefined {
|
|
25
|
+
return device.platform in spec ? spec[device.platform] : 'native' in spec ? spec.native : spec.default
|
|
26
|
+
},
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const PixelRatio = {
|
|
30
|
+
...WebPixelRatio,
|
|
31
|
+
get: () => device.pixelRatio,
|
|
32
|
+
getPixelSizeForLayoutSize: (size: number) => Math.round(size * device.pixelRatio),
|
|
33
|
+
roundToNearestPixel: (size: number) => Math.round(size * device.pixelRatio) / device.pixelRatio,
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* `Dimensions`' scale is the phone's pixel ratio. react-native-web reads the frame's devicePixelRatio, which is the
|
|
38
|
+
* screen's ratio times the shell's zoom. Patched in place, so react-native-web's own `useWindowDimensions` sees it.
|
|
39
|
+
*/
|
|
40
|
+
const libraryGet = Dimensions.get.bind(Dimensions)
|
|
41
|
+
const scaled = new WeakMap<object, ReturnType<typeof Dimensions.get>>()
|
|
42
|
+
Dimensions.get = (dimension) => {
|
|
43
|
+
const measured = libraryGet(dimension)
|
|
44
|
+
let result = scaled.get(measured)
|
|
45
|
+
if (result === undefined) {
|
|
46
|
+
result = { ...measured, scale: device.pixelRatio }
|
|
47
|
+
scaled.set(measured, result)
|
|
48
|
+
}
|
|
49
|
+
return result
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** On Android the status bar's height is a constant the app reads. */
|
|
53
|
+
export const StatusBar = Object.assign(LibraryStatusBar, {
|
|
54
|
+
currentHeight: device.platform === 'android' ? device.statusBarHeight : undefined,
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
export default { ...ReactNative, Platform, PixelRatio, StatusBar }
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stand-ins for deep imports into React Native's source, such as
|
|
3
|
+
* `react-native/Libraries/Utilities/codegenNativeComponent`. See `DEEP_IMPORTS` in `src/node/platform.ts`.
|
|
4
|
+
*/
|
|
5
|
+
export { codegenNativeCommands, codegenNativeComponent, TurboModuleRegistry } from './library.ts'
|
|
6
|
+
export { Platform } from 'react-native-web'
|
|
7
|
+
|
|
8
|
+
export function resolveAssetSource(source: unknown) {
|
|
9
|
+
if (typeof source === 'string') {
|
|
10
|
+
return { uri: source }
|
|
11
|
+
}
|
|
12
|
+
return source
|
|
13
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { Component, createRef, type ComponentProps, type ReactNode } from 'react'
|
|
2
|
+
import { StyleSheet, View } from 'react-native-web'
|
|
3
|
+
import { platform, plugin } from '../../runtime/index.ts'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* React Native's `Keyboard` and `KeyboardAvoidingView`, against the keyboard the `keyboard` plugin draws on the screen.
|
|
7
|
+
* The plugin's shell side sends React Native's keyboard events with their payloads (iOS the `Will` and `Did` events,
|
|
8
|
+
* Android only `Did`) and keeps `frame`, the settled keyboard's position, in its state. With the plugin left out the
|
|
9
|
+
* keyboard never shows.
|
|
10
|
+
*
|
|
11
|
+
* The keyboard covers the app: it changes no insets and does not resize the window, on Android as under
|
|
12
|
+
* edge-to-edge, so an app keeps clear of it with `KeyboardAvoidingView` or react-native-keyboard-controller.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
type KeyboardMetrics = { screenX: number; screenY: number; width: number; height: number }
|
|
16
|
+
type KeyboardEvent = { duration: number; easing: string; endCoordinates: KeyboardMetrics; startCoordinates?: KeyboardMetrics; isEventFromThisApp?: boolean }
|
|
17
|
+
|
|
18
|
+
const keyboard = () => plugin<{ frame: KeyboardMetrics | null }>('keyboard', { frame: null })
|
|
19
|
+
|
|
20
|
+
/** Taps the focused field out, as react-native-web's `TextInput` and any other field. */
|
|
21
|
+
function blurFocusedField(): void {
|
|
22
|
+
const active = document.activeElement as HTMLElement | null
|
|
23
|
+
if (active !== null && (active.tagName === 'INPUT' || active.tagName === 'TEXTAREA' || active.isContentEditable)) {
|
|
24
|
+
active.blur()
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const Keyboard = {
|
|
29
|
+
addListener(eventName: string, listener: (event: KeyboardEvent) => void) {
|
|
30
|
+
const remove = keyboard().on(eventName, (payload) => {
|
|
31
|
+
// The shell keeps listeners from a document that has gone (an app reload).
|
|
32
|
+
if (document.defaultView === null) {
|
|
33
|
+
remove()
|
|
34
|
+
return
|
|
35
|
+
}
|
|
36
|
+
listener(payload as KeyboardEvent)
|
|
37
|
+
})
|
|
38
|
+
return { remove }
|
|
39
|
+
},
|
|
40
|
+
removeListener() {},
|
|
41
|
+
removeAllListeners() {},
|
|
42
|
+
removeSubscription(subscription: { remove(): void }) {
|
|
43
|
+
subscription.remove()
|
|
44
|
+
},
|
|
45
|
+
dismiss() {
|
|
46
|
+
blurFocusedField()
|
|
47
|
+
},
|
|
48
|
+
isVisible(): boolean {
|
|
49
|
+
return keyboard().get().frame !== null
|
|
50
|
+
},
|
|
51
|
+
metrics(): KeyboardMetrics | undefined {
|
|
52
|
+
return keyboard().get().frame ?? undefined
|
|
53
|
+
},
|
|
54
|
+
scheduleLayoutAnimation() {},
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const CURVES: Record<string, string> = {
|
|
58
|
+
keyboard: 'cubic-bezier(0.32, 0.72, 0, 1)',
|
|
59
|
+
easeIn: 'ease-in',
|
|
60
|
+
easeOut: 'ease-out',
|
|
61
|
+
easeInEaseOut: 'ease-in-out',
|
|
62
|
+
linear: 'linear',
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
type Behavior = 'height' | 'position' | 'padding'
|
|
66
|
+
|
|
67
|
+
type KeyboardAvoidingViewProps = ComponentProps<typeof View> & {
|
|
68
|
+
behavior?: Behavior
|
|
69
|
+
contentContainerStyle?: ComponentProps<typeof View>['style']
|
|
70
|
+
enabled?: boolean
|
|
71
|
+
keyboardVerticalOffset?: number
|
|
72
|
+
children?: ReactNode
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
type Layout = { x: number; y: number; width: number; height: number }
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* React Native's `KeyboardAvoidingView`, ported: it measures how far the keyboard's top overlaps its own frame (from
|
|
79
|
+
* `onLayout`, so relative to its parent, as on the device; `keyboardVerticalOffset` makes up the difference) and
|
|
80
|
+
* takes that much off by `behavior`. The change animates with the keyboard event's duration and easing, as
|
|
81
|
+
* `LayoutAnimation` does on the device; Android reports no duration, so there it jumps.
|
|
82
|
+
*/
|
|
83
|
+
export class KeyboardAvoidingView extends Component<KeyboardAvoidingViewProps, { bottom: number; transition: string | null }> {
|
|
84
|
+
state = { bottom: 0, transition: null as string | null }
|
|
85
|
+
private frame: Layout | null = null
|
|
86
|
+
private initialFrameHeight = 0
|
|
87
|
+
private event: KeyboardEvent | null = null
|
|
88
|
+
private subscriptions: { remove(): void }[] = []
|
|
89
|
+
viewRef = createRef<unknown>()
|
|
90
|
+
|
|
91
|
+
private relativeKeyboardHeight(keyboardFrame: KeyboardMetrics): number {
|
|
92
|
+
const frame = this.frame
|
|
93
|
+
if (frame === null) {
|
|
94
|
+
return 0
|
|
95
|
+
}
|
|
96
|
+
const keyboardY = keyboardFrame.screenY - (this.props.keyboardVerticalOffset ?? 0)
|
|
97
|
+
if (this.props.behavior === 'height') {
|
|
98
|
+
return Math.max(this.state.bottom + frame.y + frame.height - keyboardY, 0)
|
|
99
|
+
}
|
|
100
|
+
return Math.max(frame.y + frame.height - keyboardY, 0)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
private update(): void {
|
|
104
|
+
if (this.event === null) {
|
|
105
|
+
this.setState({ bottom: 0 })
|
|
106
|
+
return
|
|
107
|
+
}
|
|
108
|
+
const { duration, easing, endCoordinates } = this.event
|
|
109
|
+
const bottom = this.relativeKeyboardHeight(endCoordinates)
|
|
110
|
+
if (bottom === this.state.bottom) {
|
|
111
|
+
return
|
|
112
|
+
}
|
|
113
|
+
const transition = duration > 0 && easing ? `${Math.max(duration, 10)}ms ${CURVES[easing] ?? CURVES.keyboard}` : null
|
|
114
|
+
this.setState({ bottom, transition })
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
private onKeyboardChange = (event: KeyboardEvent) => {
|
|
118
|
+
this.event = event
|
|
119
|
+
this.update()
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
private onKeyboardHide = (event: KeyboardEvent) => {
|
|
123
|
+
this.event = null
|
|
124
|
+
const transition = event.duration > 0 && event.easing ? `${Math.max(event.duration, 10)}ms ${CURVES[event.easing] ?? CURVES.keyboard}` : null
|
|
125
|
+
this.setState({ bottom: 0, transition })
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
private onLayout = (event: { nativeEvent: { layout: Layout } }) => {
|
|
129
|
+
const previous = this.frame
|
|
130
|
+
this.frame = event.nativeEvent.layout
|
|
131
|
+
if (this.initialFrameHeight === 0) {
|
|
132
|
+
this.initialFrameHeight = this.frame.height
|
|
133
|
+
}
|
|
134
|
+
if (previous === null || previous.height !== this.frame.height) {
|
|
135
|
+
this.update()
|
|
136
|
+
}
|
|
137
|
+
;(this.props.onLayout as ((event: unknown) => void) | undefined)?.(event)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
componentDidMount() {
|
|
141
|
+
const ios = platform() === 'ios'
|
|
142
|
+
this.subscriptions = ios
|
|
143
|
+
? [Keyboard.addListener('keyboardWillHide', this.onKeyboardHide), Keyboard.addListener('keyboardWillShow', this.onKeyboardChange)]
|
|
144
|
+
: [Keyboard.addListener('keyboardDidHide', this.onKeyboardHide), Keyboard.addListener('keyboardDidShow', this.onKeyboardChange)]
|
|
145
|
+
const frame = Keyboard.metrics()
|
|
146
|
+
if (frame !== undefined) {
|
|
147
|
+
this.event = { duration: 0, easing: 'keyboard', endCoordinates: frame }
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
componentWillUnmount() {
|
|
152
|
+
this.subscriptions.forEach((subscription) => subscription.remove())
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
render() {
|
|
156
|
+
const { behavior, children, contentContainerStyle, enabled = true, keyboardVerticalOffset: _offset, style, onLayout: _onLayout, ...props } = this.props
|
|
157
|
+
const bottom = enabled ? this.state.bottom : 0
|
|
158
|
+
const transition = this.state.transition
|
|
159
|
+
const animated = (property: string) =>
|
|
160
|
+
transition === null ? null : ({ transitionProperty: property, transitionDuration: transition.split(' ')[0], transitionTimingFunction: transition.slice(transition.indexOf(' ') + 1) } as object)
|
|
161
|
+
const shared = { ref: this.viewRef as never, onLayout: this.onLayout as never, ...props }
|
|
162
|
+
switch (behavior) {
|
|
163
|
+
case 'height': {
|
|
164
|
+
// React Native's `flex: 0`, which react-native-web would turn into a zero flex basis that ignores the height.
|
|
165
|
+
const heightStyle = this.frame !== null && bottom > 0 ? { height: this.initialFrameHeight - bottom, flexGrow: 0, flexShrink: 0, flexBasis: 'auto' } : null
|
|
166
|
+
return (
|
|
167
|
+
<View {...shared} style={StyleSheet.compose(style, [heightStyle, animated('height')] as never)}>
|
|
168
|
+
{children}
|
|
169
|
+
</View>
|
|
170
|
+
)
|
|
171
|
+
}
|
|
172
|
+
case 'position':
|
|
173
|
+
return (
|
|
174
|
+
<View {...shared} style={style}>
|
|
175
|
+
<View style={StyleSheet.compose(contentContainerStyle, [{ bottom }, animated('bottom')] as never)}>{children}</View>
|
|
176
|
+
</View>
|
|
177
|
+
)
|
|
178
|
+
case 'padding':
|
|
179
|
+
return (
|
|
180
|
+
<View {...shared} style={StyleSheet.compose(style, [{ paddingBottom: bottom }, animated('padding-bottom')] as never)}>
|
|
181
|
+
{children}
|
|
182
|
+
</View>
|
|
183
|
+
)
|
|
184
|
+
default:
|
|
185
|
+
return (
|
|
186
|
+
<View {...shared} style={style}>
|
|
187
|
+
{children}
|
|
188
|
+
</View>
|
|
189
|
+
)
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import { useEffect } from 'react'
|
|
2
|
+
import { AccessibilityInfo as WebAccessibilityInfo, View } from 'react-native-web'
|
|
3
|
+
import * as ReactNativeWeb from 'react-native-web'
|
|
4
|
+
import {
|
|
5
|
+
addBackListener,
|
|
6
|
+
getPermission,
|
|
7
|
+
requestPermission,
|
|
8
|
+
setStatusBar,
|
|
9
|
+
showDialog,
|
|
10
|
+
type DialogButton,
|
|
11
|
+
type PermissionKind,
|
|
12
|
+
type StatusBarStyle,
|
|
13
|
+
} from '../../runtime/index.ts'
|
|
14
|
+
|
|
15
|
+
export * from 'react-native-web'
|
|
16
|
+
/** Against the keyboard the `keyboard` plugin draws. */
|
|
17
|
+
import { Keyboard, KeyboardAvoidingView } from './keyboard.tsx'
|
|
18
|
+
|
|
19
|
+
export { Keyboard, KeyboardAvoidingView }
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* react-native-web reports a screen reader as on, which makes libraries take their accessibility paths (some toast
|
|
23
|
+
* libraries render nothing). A simulated phone has VoiceOver and TalkBack off.
|
|
24
|
+
*/
|
|
25
|
+
export const AccessibilityInfo = {
|
|
26
|
+
...WebAccessibilityInfo,
|
|
27
|
+
isScreenReaderEnabled: () => Promise.resolve(false),
|
|
28
|
+
fetch: () => Promise.resolve(false),
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* React Native internals that native-only libraries import at module load. react-native-web does not export them,
|
|
33
|
+
* so these inert versions let those libraries load. Native components render as plain views.
|
|
34
|
+
*/
|
|
35
|
+
export const TurboModuleRegistry = {
|
|
36
|
+
get: () => null,
|
|
37
|
+
getEnforcing: () => new Proxy({}, { get: () => () => undefined }),
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function requireNativeComponent() {
|
|
41
|
+
return View
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function codegenNativeComponent() {
|
|
45
|
+
return View
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function codegenNativeCommands() {
|
|
49
|
+
return new Proxy({}, { get: () => () => undefined })
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export const DevSettings = {
|
|
53
|
+
reload: () => window.location.reload(),
|
|
54
|
+
addMenuItem: () => undefined,
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export const DynamicColorIOS = (colors: { light: string; dark: string }) =>
|
|
58
|
+
window.matchMedia('(prefers-color-scheme: dark)').matches ? colors.dark : colors.light
|
|
59
|
+
|
|
60
|
+
export const PlatformColor = () => undefined
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Android's hardware back button, pressed from the shell's navigation bar or `web-sim back`.
|
|
64
|
+
*/
|
|
65
|
+
export const BackHandler = {
|
|
66
|
+
addEventListener(type: string, handler: () => boolean | null | undefined) {
|
|
67
|
+
const remove = type === 'hardwareBackPress' ? addBackListener(() => handler() === true) : () => undefined
|
|
68
|
+
return { remove }
|
|
69
|
+
},
|
|
70
|
+
removeEventListener: () => undefined,
|
|
71
|
+
exitApp: () => undefined,
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
type BarStyle = 'default' | 'light-content' | 'dark-content'
|
|
75
|
+
|
|
76
|
+
const STYLE_FOR_BAR: Record<BarStyle, StatusBarStyle> = {
|
|
77
|
+
default: 'default',
|
|
78
|
+
'light-content': 'light',
|
|
79
|
+
'dark-content': 'dark',
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The status bar is drawn by the shell, so the component tells it what the app asked for.
|
|
84
|
+
*/
|
|
85
|
+
export function StatusBar({ barStyle, hidden }: { barStyle?: BarStyle; hidden?: boolean }) {
|
|
86
|
+
useEffect(() => {
|
|
87
|
+
setStatusBar({ style: barStyle === undefined ? undefined : STYLE_FOR_BAR[barStyle], hidden })
|
|
88
|
+
}, [barStyle, hidden])
|
|
89
|
+
return null
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
StatusBar.currentHeight = 0
|
|
93
|
+
StatusBar.setBarStyle = (style: BarStyle) => setStatusBar({ style: STYLE_FOR_BAR[style] })
|
|
94
|
+
StatusBar.setHidden = (hidden: boolean) => setStatusBar({ hidden })
|
|
95
|
+
StatusBar.setBackgroundColor = () => undefined
|
|
96
|
+
StatusBar.setTranslucent = () => undefined
|
|
97
|
+
StatusBar.setNetworkActivityIndicatorVisible = () => undefined
|
|
98
|
+
StatusBar.pushStackEntry = (props: { barStyle?: BarStyle; hidden?: boolean }) => {
|
|
99
|
+
setStatusBar({ style: props.barStyle === undefined ? undefined : STYLE_FOR_BAR[props.barStyle], hidden: props.hidden })
|
|
100
|
+
return props
|
|
101
|
+
}
|
|
102
|
+
StatusBar.popStackEntry = () => undefined
|
|
103
|
+
StatusBar.replaceStackEntry = (_: unknown, props: { barStyle?: BarStyle; hidden?: boolean }) =>
|
|
104
|
+
StatusBar.pushStackEntry(props)
|
|
105
|
+
|
|
106
|
+
type AlertButton = { text?: string; onPress?: (value?: string) => void; style?: 'default' | 'cancel' | 'destructive' }
|
|
107
|
+
|
|
108
|
+
function toDialogButtons(buttons: AlertButton[] | undefined): AlertButton[] {
|
|
109
|
+
return buttons !== undefined && buttons.length > 0 ? buttons : [{ text: 'OK' }]
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* `Alert` draws the platform's alert in the shell; react-native-web's does nothing. Android shows at most three
|
|
114
|
+
* buttons, as it does on a device.
|
|
115
|
+
*/
|
|
116
|
+
export const Alert = {
|
|
117
|
+
alert(title: string, message?: string, buttons?: AlertButton[], options?: { cancelable?: boolean; onDismiss?: () => void }) {
|
|
118
|
+
const list = toDialogButtons(buttons)
|
|
119
|
+
void showDialog({
|
|
120
|
+
kind: 'alert',
|
|
121
|
+
title,
|
|
122
|
+
message,
|
|
123
|
+
buttons: list.map((button): DialogButton => ({ text: button.text ?? 'OK', style: button.style })),
|
|
124
|
+
}).then(({ button }) => {
|
|
125
|
+
if (button < 0) {
|
|
126
|
+
options?.onDismiss?.()
|
|
127
|
+
return
|
|
128
|
+
}
|
|
129
|
+
list[button]?.onPress?.()
|
|
130
|
+
})
|
|
131
|
+
},
|
|
132
|
+
prompt(
|
|
133
|
+
title: string,
|
|
134
|
+
message?: string,
|
|
135
|
+
callbackOrButtons?: ((text: string) => void) | AlertButton[],
|
|
136
|
+
type: 'plain-text' | 'secure-text' | 'login-password' | 'default' = 'plain-text',
|
|
137
|
+
defaultValue?: string,
|
|
138
|
+
) {
|
|
139
|
+
const list: AlertButton[] =
|
|
140
|
+
typeof callbackOrButtons === 'function'
|
|
141
|
+
? [{ text: 'Cancel', style: 'cancel' }, { text: 'OK', onPress: (text) => callbackOrButtons(text ?? '') }]
|
|
142
|
+
: toDialogButtons(callbackOrButtons)
|
|
143
|
+
void showDialog({
|
|
144
|
+
kind: 'alert',
|
|
145
|
+
title,
|
|
146
|
+
message,
|
|
147
|
+
buttons: list.map((button): DialogButton => ({ text: button.text ?? 'OK', style: button.style })),
|
|
148
|
+
input: { secure: type === 'secure-text', defaultValue },
|
|
149
|
+
}).then(({ button, text }) => list[button]?.onPress?.(text ?? ''))
|
|
150
|
+
},
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export const ActionSheetIOS = {
|
|
154
|
+
showActionSheetWithOptions(
|
|
155
|
+
options: { options: string[]; cancelButtonIndex?: number; destructiveButtonIndex?: number | number[]; title?: string; message?: string },
|
|
156
|
+
callback: (index: number) => void,
|
|
157
|
+
) {
|
|
158
|
+
const destructive = new Set([options.destructiveButtonIndex ?? []].flat())
|
|
159
|
+
void showDialog({
|
|
160
|
+
kind: 'action-sheet',
|
|
161
|
+
title: options.title,
|
|
162
|
+
message: options.message,
|
|
163
|
+
buttons: options.options.map((text, index) => ({
|
|
164
|
+
text,
|
|
165
|
+
style: index === options.cancelButtonIndex ? 'cancel' : destructive.has(index) ? 'destructive' : 'default',
|
|
166
|
+
})),
|
|
167
|
+
}).then(({ button }) => callback(button < 0 ? (options.cancelButtonIndex ?? -1) : button))
|
|
168
|
+
},
|
|
169
|
+
showShareActionSheetWithOptions: () => undefined,
|
|
170
|
+
dismissActionSheet: () => undefined,
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Android's runtime permissions, by their `android.permission.*` name, for those the permissions plugin tracks. */
|
|
174
|
+
const ANDROID_PERMISSION_KINDS: Record<string, PermissionKind> = {
|
|
175
|
+
CAMERA: 'camera',
|
|
176
|
+
RECORD_AUDIO: 'microphone',
|
|
177
|
+
ACCESS_FINE_LOCATION: 'location',
|
|
178
|
+
ACCESS_COARSE_LOCATION: 'location',
|
|
179
|
+
ACCESS_BACKGROUND_LOCATION: 'location-background',
|
|
180
|
+
READ_CONTACTS: 'contacts',
|
|
181
|
+
WRITE_CONTACTS: 'contacts',
|
|
182
|
+
READ_CALENDAR: 'calendar',
|
|
183
|
+
WRITE_CALENDAR: 'calendar',
|
|
184
|
+
POST_NOTIFICATIONS: 'notifications',
|
|
185
|
+
READ_MEDIA_IMAGES: 'photos',
|
|
186
|
+
READ_MEDIA_VIDEO: 'photos',
|
|
187
|
+
READ_EXTERNAL_STORAGE: 'photos',
|
|
188
|
+
WRITE_EXTERNAL_STORAGE: 'photos',
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const ANDROID_PERMISSION_NAMES = [
|
|
192
|
+
...Object.keys(ANDROID_PERMISSION_KINDS),
|
|
193
|
+
'BLUETOOTH_CONNECT',
|
|
194
|
+
'BLUETOOTH_SCAN',
|
|
195
|
+
'READ_PHONE_STATE',
|
|
196
|
+
'CALL_PHONE',
|
|
197
|
+
'SEND_SMS',
|
|
198
|
+
'BODY_SENSORS',
|
|
199
|
+
'ACTIVITY_RECOGNITION',
|
|
200
|
+
]
|
|
201
|
+
|
|
202
|
+
type AndroidPermissionResult = 'granted' | 'denied' | 'never_ask_again'
|
|
203
|
+
|
|
204
|
+
function androidKind(permission: string): PermissionKind | undefined {
|
|
205
|
+
return ANDROID_PERMISSION_KINDS[permission.replace(/^android\.permission\./, '')]
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
async function requestAndroidPermission(permission: string): Promise<AndroidPermissionResult> {
|
|
209
|
+
const kind = androidKind(permission)
|
|
210
|
+
if (kind === undefined) {
|
|
211
|
+
return 'granted'
|
|
212
|
+
}
|
|
213
|
+
const { status, canAskAgain } = await requestPermission(kind)
|
|
214
|
+
return status === 'granted' ? 'granted' : canAskAgain ? 'denied' : 'never_ask_again'
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Android's runtime permissions, answered from the permissions plugin, so they agree with Expo modules' permissions.
|
|
219
|
+
* Permissions the plugin doesn't track are granted.
|
|
220
|
+
*/
|
|
221
|
+
export const PermissionsAndroid = {
|
|
222
|
+
PERMISSIONS: Object.fromEntries(ANDROID_PERMISSION_NAMES.map((name) => [name, `android.permission.${name}`])),
|
|
223
|
+
RESULTS: { GRANTED: 'granted', DENIED: 'denied', NEVER_ASK_AGAIN: 'never_ask_again' } as const,
|
|
224
|
+
check: (permission: string) => {
|
|
225
|
+
const kind = androidKind(permission)
|
|
226
|
+
return Promise.resolve(kind === undefined || getPermission(kind).granted)
|
|
227
|
+
},
|
|
228
|
+
request: requestAndroidPermission,
|
|
229
|
+
async requestMultiple(permissions: string[]): Promise<Record<string, AndroidPermissionResult>> {
|
|
230
|
+
const results: Record<string, AndroidPermissionResult> = {}
|
|
231
|
+
for (const permission of permissions) {
|
|
232
|
+
results[permission] = await requestAndroidPermission(permission)
|
|
233
|
+
}
|
|
234
|
+
return results
|
|
235
|
+
},
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** Some libraries `import ReactNative from 'react-native'`, which Metro's interop answers with the whole module. */
|
|
239
|
+
export default {
|
|
240
|
+
...ReactNativeWeb,
|
|
241
|
+
AccessibilityInfo,
|
|
242
|
+
TurboModuleRegistry,
|
|
243
|
+
requireNativeComponent,
|
|
244
|
+
codegenNativeComponent,
|
|
245
|
+
codegenNativeCommands,
|
|
246
|
+
DevSettings,
|
|
247
|
+
DynamicColorIOS,
|
|
248
|
+
PlatformColor,
|
|
249
|
+
BackHandler,
|
|
250
|
+
StatusBar,
|
|
251
|
+
Alert,
|
|
252
|
+
ActionSheetIOS,
|
|
253
|
+
PermissionsAndroid,
|
|
254
|
+
Keyboard,
|
|
255
|
+
KeyboardAvoidingView,
|
|
256
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { CookieIcon, TrashIcon } from '@phosphor-icons/react'
|
|
2
|
+
import { useEffect, useState } from 'react'
|
|
3
|
+
import type { HostStore } from './host.ts'
|
|
4
|
+
import { usePopover } from './popover.ts'
|
|
5
|
+
|
|
6
|
+
/** The dev server's cookie jar, which its proxy keeps for the app's requests (`src/node/proxy.ts`). */
|
|
7
|
+
const COOKIES_PATH = '/__web-sim/cookies'
|
|
8
|
+
|
|
9
|
+
type Cookies = Record<string, { name: string; path: string; expires: number | null }[]>
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The jar's cookies, kept current from the dev server's `web-sim:cookies` messages. Null while loading, or when there
|
|
13
|
+
* is no jar (the proxy is off).
|
|
14
|
+
*/
|
|
15
|
+
function useCookies(): Cookies | null {
|
|
16
|
+
const [cookies, setCookies] = useState<Cookies | null>(null)
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
let live = true
|
|
19
|
+
fetch(COOKIES_PATH)
|
|
20
|
+
.then((response) => (response.ok && (response.headers.get('content-type') ?? '').includes('json') ? response.json() : null))
|
|
21
|
+
.then(
|
|
22
|
+
(list: Cookies | null) => live && setCookies(list),
|
|
23
|
+
() => live && setCookies(null),
|
|
24
|
+
)
|
|
25
|
+
const hot = import.meta.hot
|
|
26
|
+
const onChange = (list: Cookies) => live && setCookies(list)
|
|
27
|
+
hot?.on('web-sim:cookies', onChange)
|
|
28
|
+
return () => {
|
|
29
|
+
live = false
|
|
30
|
+
hot?.off('web-sim:cookies', onChange)
|
|
31
|
+
}
|
|
32
|
+
}, [])
|
|
33
|
+
return cookies
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The toolbar's cookie button and its popover: the phone's cookies by domain, cleared in full or one domain at a
|
|
38
|
+
* time. A clear goes through the network plugin's `clearCookies` action, as `web-sim run network clearCookies` does,
|
|
39
|
+
* so it is logged the same way.
|
|
40
|
+
*/
|
|
41
|
+
export function CookiesButton({ host }: { host: HostStore }) {
|
|
42
|
+
const cookies = useCookies()
|
|
43
|
+
const { open, setOpen, root } = usePopover()
|
|
44
|
+
if (cookies === null) {
|
|
45
|
+
return null
|
|
46
|
+
}
|
|
47
|
+
const clear = (domain?: string) => {
|
|
48
|
+
if (host.plugins.plugins.some((plugin) => plugin.id === 'network')) {
|
|
49
|
+
void host.plugins.run('network', 'clearCookies', domain === undefined ? undefined : { domain })
|
|
50
|
+
} else {
|
|
51
|
+
void fetch(`${COOKIES_PATH}${domain === undefined ? '' : `?domain=${encodeURIComponent(domain)}`}`, { method: 'DELETE' })
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const domains = Object.entries(cookies)
|
|
55
|
+
const total = domains.reduce((sum, [, list]) => sum + list.length, 0)
|
|
56
|
+
const label = open ? 'Hide cookies' : `Cookies (${total})`
|
|
57
|
+
return (
|
|
58
|
+
<div className="ws-popover-anchor" ref={root}>
|
|
59
|
+
<button type="button" className="ws-icon-button" onClick={() => setOpen(!open)} data-tooltip={label} aria-label={label} aria-pressed={open} aria-expanded={open}>
|
|
60
|
+
<CookieIcon size={16} />
|
|
61
|
+
</button>
|
|
62
|
+
{open && (
|
|
63
|
+
<div className="ws-popover ws-cookies" role="dialog" aria-label="Cookies">
|
|
64
|
+
<div className="ws-popover-head">
|
|
65
|
+
<span className="ws-popover-title">Cookies · {total}</span>
|
|
66
|
+
<button type="button" className="ws-text-button" disabled={total === 0} onClick={() => clear()}>
|
|
67
|
+
Clear all
|
|
68
|
+
</button>
|
|
69
|
+
</div>
|
|
70
|
+
{domains.length === 0 ? (
|
|
71
|
+
<div className="ws-popover-empty">No cookies</div>
|
|
72
|
+
) : (
|
|
73
|
+
<ul className="ws-cookie-list">
|
|
74
|
+
{domains.map(([domain, list]) => (
|
|
75
|
+
<li key={domain}>
|
|
76
|
+
<div className="ws-cookie-text">
|
|
77
|
+
<span className="ws-cookie-domain">
|
|
78
|
+
{domain} <span className="ws-cookie-count">{list.length}</span>
|
|
79
|
+
</span>
|
|
80
|
+
<span className="ws-cookie-names">
|
|
81
|
+
{list.map((cookie) => (cookie.path === '/' ? cookie.name : `${cookie.name} (${cookie.path})`)).join(', ')}
|
|
82
|
+
</span>
|
|
83
|
+
</div>
|
|
84
|
+
<button type="button" className="ws-icon-button ws-cookie-clear" onClick={() => clear(domain)} data-tooltip={`Clear ${domain}`} aria-label={`Clear ${domain}`}>
|
|
85
|
+
<TrashIcon size={14} />
|
|
86
|
+
</button>
|
|
87
|
+
</li>
|
|
88
|
+
))}
|
|
89
|
+
</ul>
|
|
90
|
+
)}
|
|
91
|
+
</div>
|
|
92
|
+
)}
|
|
93
|
+
</div>
|
|
94
|
+
)
|
|
95
|
+
}
|