react-native-minimal-webview 1.2.0 → 1.3.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.
Files changed (98) hide show
  1. package/README.md +35 -31
  2. package/android/build.gradle +1 -1
  3. package/android/src/main/java/expo/modules/minimalwebview/Asks.kt +5 -11
  4. package/android/src/main/java/expo/modules/minimalwebview/Hooks.kt +17 -11
  5. package/android/src/main/java/expo/modules/minimalwebview/MinimalWebView.kt +83 -125
  6. package/android/src/main/java/expo/modules/minimalwebview/MinimalWebViewClients.kt +16 -31
  7. package/android/src/main/java/expo/modules/minimalwebview/MinimalWebViewModule.kt +18 -19
  8. package/android/src/main/java/expo/modules/minimalwebview/MinimalWebViewNavigation.kt +24 -38
  9. package/android/src/main/java/expo/modules/minimalwebview/MinimalWebViewScripts.kt +24 -36
  10. package/android/src/main/java/expo/modules/minimalwebview/NavigationRules.kt +19 -20
  11. package/android/src/main/java/expo/modules/minimalwebview/PlatformSettings.kt +41 -37
  12. package/android/src/main/java/expo/modules/minimalwebview/PullToRefresh.kt +14 -18
  13. package/android/src/main/java/expo/modules/minimalwebview/requests/Downloads.kt +12 -23
  14. package/android/src/main/java/expo/modules/minimalwebview/requests/FileUpload.kt +0 -1
  15. package/android/src/main/java/expo/modules/minimalwebview/requests/FullscreenVideo.kt +1 -4
  16. package/android/src/main/java/expo/modules/minimalwebview/requests/Permissions.kt +34 -24
  17. package/build/WebView.d.ts +11 -2
  18. package/build/WebView.d.ts.map +1 -1
  19. package/build/WebView.js +45 -71
  20. package/build/WebView.js.map +1 -1
  21. package/build/bridge/createBridge.d.ts +1 -5
  22. package/build/bridge/createBridge.d.ts.map +1 -1
  23. package/build/bridge/createBridge.js +3 -15
  24. package/build/bridge/createBridge.js.map +1 -1
  25. package/build/bridge/endpoint.d.ts +1 -2
  26. package/build/bridge/endpoint.d.ts.map +1 -1
  27. package/build/bridge/endpoint.js +5 -6
  28. package/build/bridge/endpoint.js.map +1 -1
  29. package/build/bridge/matchesOrigin.d.ts +11 -0
  30. package/build/bridge/matchesOrigin.d.ts.map +1 -0
  31. package/build/bridge/matchesOrigin.js +26 -0
  32. package/build/bridge/matchesOrigin.js.map +1 -0
  33. package/build/bridge/origins.d.ts +2 -13
  34. package/build/bridge/origins.d.ts.map +1 -1
  35. package/build/bridge/origins.js +1 -30
  36. package/build/bridge/origins.js.map +1 -1
  37. package/build/bridge/pageClient.generated.d.ts +1 -1
  38. package/build/bridge/pageClient.generated.d.ts.map +1 -1
  39. package/build/bridge/pageClient.generated.js +2 -2
  40. package/build/bridge/pageClient.generated.js.map +1 -1
  41. package/build/bridge/protocol.d.ts +3 -5
  42. package/build/bridge/protocol.d.ts.map +1 -1
  43. package/build/bridge/protocol.js +12 -14
  44. package/build/bridge/protocol.js.map +1 -1
  45. package/build/index.d.ts +1 -1
  46. package/build/index.js +1 -1
  47. package/build/navigationRules.d.ts +0 -5
  48. package/build/navigationRules.d.ts.map +1 -1
  49. package/build/navigationRules.js +3 -12
  50. package/build/navigationRules.js.map +1 -1
  51. package/build/reactNativeWebViewDefaults.d.ts +0 -4
  52. package/build/reactNativeWebViewDefaults.d.ts.map +1 -1
  53. package/build/reactNativeWebViewDefaults.js +0 -4
  54. package/build/reactNativeWebViewDefaults.js.map +1 -1
  55. package/build/reactNativeWebViewMessages.d.ts.map +1 -1
  56. package/build/reactNativeWebViewMessages.js +0 -1
  57. package/build/reactNativeWebViewMessages.js.map +1 -1
  58. package/build/refStore.d.ts +0 -2
  59. package/build/refStore.d.ts.map +1 -1
  60. package/build/refStore.js +0 -6
  61. package/build/refStore.js.map +1 -1
  62. package/build/types.d.ts +55 -39
  63. package/build/types.d.ts.map +1 -1
  64. package/ios/Asks.swift +6 -9
  65. package/ios/DelegateProxy.swift +4 -18
  66. package/ios/DownloadName.swift +0 -2
  67. package/ios/Downloads.swift +0 -2
  68. package/ios/Hooks.swift +4 -6
  69. package/ios/Loads.swift +37 -19
  70. package/ios/MinimalWebView+Dialogs.swift +5 -7
  71. package/ios/MinimalWebView+Navigation.swift +23 -49
  72. package/ios/MinimalWebView+Scripts.swift +10 -17
  73. package/ios/MinimalWebView.podspec +1 -2
  74. package/ios/MinimalWebView.swift +63 -128
  75. package/ios/MinimalWebViewModule.swift +1 -18
  76. package/ios/NavigationRules.swift +2 -9
  77. package/ios/OriginRules.swift +4 -13
  78. package/ios/PlatformSettings.swift +9 -30
  79. package/ios/ReadAccess.swift +0 -2
  80. package/ios/Records.swift +18 -0
  81. package/ios/Recovery.swift +2 -8
  82. package/ios/SharedCookies.swift +10 -25
  83. package/mocks/MinimalWebView.ts +5 -0
  84. package/package.json +11 -10
  85. package/src/WebView.tsx +58 -80
  86. package/src/bridge/createBridge.ts +5 -20
  87. package/src/bridge/endpoint.ts +6 -9
  88. package/src/bridge/matchesOrigin.ts +27 -0
  89. package/src/bridge/origins.ts +2 -30
  90. package/src/bridge/pageClient.generated.ts +2 -2
  91. package/src/bridge/pageClient.ts +1 -7
  92. package/src/bridge/protocol.ts +16 -18
  93. package/src/index.ts +1 -1
  94. package/src/navigationRules.ts +6 -12
  95. package/src/reactNativeWebViewDefaults.ts +0 -4
  96. package/src/reactNativeWebViewMessages.ts +0 -1
  97. package/src/refStore.ts +0 -7
  98. package/src/types.ts +55 -39
package/README.md CHANGED
@@ -4,6 +4,7 @@ A React Native WebView that hands you the iOS and Android WebView APIs, instead
4
4
 
5
5
  It runs on iOS and Android as an Expo module.
6
6
 
7
+ [Get started](guides/getting-started.md) ·
7
8
  [Install](#install) ·
8
9
  [npm](https://www.npmjs.com/package/react-native-minimal-webview) ·
9
10
  [API reference](https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/) ·
@@ -37,6 +38,7 @@ import { WebView, type WebViewRef } from 'react-native-minimal-webview';
37
38
  export function Browser() {
38
39
  const ref = useRef<WebViewRef>(null);
39
40
  const pageDown = () => {
41
+ // Android's WebView.pageDown(boolean): a method string names one platform's method
40
42
  if (Platform.OS === 'android') ref.current?.call('webView.pageDown', [true]);
41
43
  };
42
44
  return (
@@ -54,7 +56,8 @@ export function Browser() {
54
56
  }
55
57
  ```
56
58
 
57
- A setting or method a newer SDK adds works without a release of this package: see
59
+ A setting a newer SDK adds goes under its root's `other` key, and `call` takes any public method, so neither waits for a
60
+ release of this package: see
58
61
  [`ios`](https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/interfaces/WebViewProps/#ios),
59
62
  [`android`](https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/interfaces/WebViewProps/#android) and
60
63
  [`call`](https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/type-aliases/WebViewRef/#call).
@@ -63,10 +66,13 @@ A setting or method a newer SDK adds works without a release of this package: se
63
66
 
64
67
  [How it works](guides/how-it-works.md) explains the parts. The core adds only what the platform APIs lack:
65
68
 
66
- - `navigationRules`: navigations decided natively, with a JS round trip only for an `ask` rule.
67
- - A typed promise bridge, open only to the bridge's `allowedOrigins`.
68
- - `useWebViewState(ref)`: the navigation state, kept natively.
69
- - `onPermissionRequest`: your app answers the page's permission requests (camera, microphone, location).
69
+ - [`navigationRules`](https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/interfaces/WebViewProps/#navigationrules): navigations decided
70
+ natively, with a JS round trip only where a prop asks for one.
71
+ - A typed promise bridge ([`createBridge`](https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/functions/createBridge/)): `window.nativeBridge` in
72
+ the page, open only to the bridge's [`allowedOrigins`](https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/type-aliases/BridgeOptions/#allowedorigins).
73
+ - [`useWebViewState(ref)`](https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/functions/useWebViewState/): the navigation state, kept natively.
74
+ - [`onPermissionRequest`](https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/interfaces/WebViewProps/#onpermissionrequest): your app answers the
75
+ page's permission requests (camera, microphone, location).
70
76
 
71
77
  The [migration guide](guides/migrating-from-react-native-webview.md) covers each react-native-webview prop that works differently here.
72
78
 
@@ -78,13 +84,34 @@ The [migration guide](guides/migrating-from-react-native-webview.md) covers each
78
84
 
79
85
  ## Install
80
86
 
87
+ [Get started](guides/getting-started.md) walks you through a new Expo app with a WebView on screen.
88
+
89
+ In an Expo app, install the package and expo-build-properties:
90
+
81
91
  ```sh
82
- npx expo install react-native-minimal-webview
92
+ npx expo install react-native-minimal-webview expo-build-properties
93
+ ```
94
+
95
+ Set the iOS and Android [minimums](#requirements) in `app.json`, before you rebuild: below iOS 17.0, the build leaves
96
+ the package's native module out, and `WebView` throws as it renders.
97
+
98
+ ```json
99
+ {
100
+ "expo": {
101
+ "plugins": [
102
+ [
103
+ "expo-build-properties",
104
+ { "ios": { "deploymentTarget": "17.0" }, "android": { "minSdkVersion": 29 } }
105
+ ]
106
+ ]
107
+ }
108
+ }
83
109
  ```
84
110
 
85
111
  Then rebuild the app (`npx expo run:ios`, `npx expo run:android`).
86
112
 
87
- A bare React Native app adds Expo modules first, then installs with its package manager:
113
+ A bare React Native app adds Expo modules first, then installs with its package manager. It sets the minimums in
114
+ `ios/Podfile` (`platform :ios, '17.0'`) and `android/build.gradle` (`minSdkVersion = 29`) before `pod-install`:
88
115
 
89
116
  ```sh
90
117
  npx install-expo-modules@latest
@@ -101,34 +128,11 @@ npx pod-install
101
128
  | Expo SDK | 53 (React Native 0.79, React 19) |
102
129
  | Architecture | [New architecture](https://reactnative.dev/architecture/landing-page) only |
103
130
 
104
- An Expo app sets the two minimums with expo-build-properties (`npx expo install expo-build-properties`) in `app.json`:
105
-
106
- ```json
107
- {
108
- "expo": {
109
- "plugins": [
110
- [
111
- "expo-build-properties",
112
- { "ios": { "deploymentTarget": "17.0" }, "android": { "minSdkVersion": 29 } }
113
- ]
114
- ]
115
- }
116
- }
117
- ```
118
-
119
131
  Some features need a newer Android System WebView: [which ones](guides/android-system-webview.md).
120
132
 
121
133
  ## Usage
122
134
 
123
- ```tsx
124
- import { WebView } from 'react-native-minimal-webview';
125
-
126
- export function Browser() {
127
- return <WebView source={{ uri: 'https://example.com' }} style={{ flex: 1 }} />;
128
- }
129
- ```
130
-
131
- Then pick what you need:
135
+ Pick what you need:
132
136
 
133
137
  | To | See |
134
138
  | --------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
@@ -4,7 +4,7 @@ plugins {
4
4
  }
5
5
 
6
6
  group = 'expo.modules.minimalwebview'
7
- version = '0.1.0' // expo-autolinking requires it
7
+ version = '0.1.0' // expo-module-gradle-plugin requires it
8
8
 
9
9
  android {
10
10
  namespace "expo.modules.minimalwebview"
@@ -1,22 +1,16 @@
1
1
  package expo.modules.minimalwebview
2
2
 
3
-
4
- /**
5
- * Every `ask` (a page request, a navigation request or a hook's) waits here, by id, until JS answers it through [answer]
6
- * or native gives up (null, and the core's `askCancelled` to JS): a Document end, a timeout or the platform. [send] puts an
7
- * event on the view's `onHookEvent`, which reaches the app's extensions' `onEvent`. Main thread only.
8
- */
3
+ /** Main thread only. */
9
4
  internal class Asks(private val send: (Map<String, Any?>) -> Unit) {
10
5
  private val pending = mutableMapOf<Int, (Any?) -> Unit>()
11
6
  private var nextId = 0
12
- /** The view closed: JS is unmounted, and a hook may still hold its context. */
13
7
  private var closed = false
14
8
 
15
- /** Sends the ask to JS and returns its id, for [cancel]. Once the view closed it answers null at once. */
16
- fun ask(key: String, name: String, payload: Map<String, Any?>, onAnswer: (Any?) -> Unit): Int {
9
+ /** Sends the ask to JS and returns its id, for [cancel]. Once the view closed it answers null at once and returns null. */
10
+ fun ask(key: String, name: String, payload: Map<String, Any?>, onAnswer: (Any?) -> Unit): Int? {
17
11
  if (closed) {
18
12
  onAnswer(null)
19
- return 0 // no ask: cancel(0) finds nothing
13
+ return null
20
14
  }
21
15
  val id = ++nextId
22
16
  pending[id] = onAnswer
@@ -36,7 +30,7 @@ internal class Asks(private val send: (Map<String, Any?>) -> Unit) {
36
30
  onAnswer(null)
37
31
  }
38
32
 
39
- /** Cancels every pending ask. At closing JS isn't told: it aborts its own handlers at unmount. */
33
+ /** Cancels every pending ask. At closing JS isn't told. */
40
34
  fun documentEnded(reason: DocumentEnd) {
41
35
  if (reason == DocumentEnd.CLOSING) closed = true
42
36
  pending.keys.toList().forEach { cancel(it, tell = reason != DocumentEnd.CLOSING) }
@@ -33,8 +33,16 @@ object MinimalWebViewHooks {
33
33
  val key: String,
34
34
  val webViewFactory: ((Context) -> WebView)?,
35
35
  val configure: ((WebView, WebViewContext) -> Unit)?,
36
+ val newWebViewsOnly: Boolean,
36
37
  val attach: (WebView, WebViewContext) -> Unit
37
- )
38
+ ) {
39
+ val whyNotLate get() = when {
40
+ configure != null -> "configure"
41
+ webViewFactory != null -> "a webViewFactory"
42
+ newWebViewsOnly -> "newWebViewsOnly"
43
+ else -> null
44
+ }
45
+ }
38
46
 
39
47
  internal val hooks = mutableListOf<Hook>()
40
48
 
@@ -64,6 +72,8 @@ object MinimalWebViewHooks {
64
72
  * @param configure runs on the main thread as soon as each WebView this hook is attached to, on or off, is made,
65
73
  * before the package sets it up ([WebViewCompat.setProfile][androidx.webkit.WebViewCompat.setProfile] goes here).
66
74
  * It gets the same context that [attach] gets next for that WebView, so state it makes can be keyed by the context.
75
+ * @param newWebViewsOnly true attaches the hook only when a WebView is created, never to one whose key turns on
76
+ * later, as a hook with [configure] or [webViewFactory] does.
67
77
  * @param attach runs on the main thread once the WebView has its settings and is in the view hierarchy, so what it
68
78
  * sets wins over those settings. Register what to run when that WebView goes away with
69
79
  * [WebViewContext.onDestroyed]. Document-start scripts you add here run before the package's, so
@@ -73,13 +83,14 @@ object MinimalWebViewHooks {
73
83
  key: String,
74
84
  webViewFactory: ((Context) -> WebView)? = null,
75
85
  configure: ((WebView, WebViewContext) -> Unit)? = null,
86
+ newWebViewsOnly: Boolean = false,
76
87
  attach: (WebView, WebViewContext) -> Unit
77
88
  ) {
78
89
  if (key == "core") {
79
90
  Log.e(TAG, "the hook key `core` is reserved: the hook is not registered")
80
91
  return
81
92
  }
82
- if (putHook(hooks, Hook(key, webViewFactory, configure, attach))) {
93
+ if (putHook(hooks, Hook(key, webViewFactory, configure, newWebViewsOnly, attach))) {
83
94
  Log.w(
84
95
  TAG,
85
96
  "a hook under `$key` replaces the one registered before: a JS reload registers every hook again; " +
@@ -228,8 +239,8 @@ interface WebViewContext {
228
239
  fun emit(name: String, payload: Map<String, Any?>)
229
240
 
230
241
  /**
231
- * Sends the event as [emit] does, and calls [onAnswer] with JS's answer: the extension's `onEvent` return value, a
232
- * promise awaited, in JSON types as [handle]'s arguments are.
242
+ * Sends the event as [emit] does, but to the extension's `onAsk` when it has one, and calls [onAnswer] with JS's
243
+ * answer: that handler's return value, a promise awaited, in JSON types as [handle]'s arguments are.
233
244
  *
234
245
  * It answers null when the handler throws or there is none, or when the Document ends first; there is no timeout. It
235
246
  * answers null at once while the hook is off, and after the context's attach ended. An ask made before the WebView's
@@ -310,9 +321,8 @@ class HookReply internal constructor(
310
321
  }
311
322
 
312
323
  /**
313
- * [HookContext]'s base: the hook's `hookOptions` value (null while its key is absent), its lists, and whether the core
314
- * routes them. Chromium's IO thread reads [routed] and [clients] (shouldInterceptRequest), so [options] and the detach
315
- * are volatile; the rest is main thread only.
324
+ * Chromium's IO thread reads [routed] and [clients] (shouldInterceptRequest), so [options] and the detach are volatile;
325
+ * the rest is main thread only.
316
326
  */
317
327
  internal open class Attachment(value: Any?) {
318
328
  @Volatile var options: Any? = value
@@ -325,7 +335,6 @@ internal open class Attachment(value: Any?) {
325
335
  // own changes write them.
326
336
  val clients: MutableList<WebViewClient> = CopyOnWriteArrayList()
327
337
  val chromeClients = mutableListOf<WebChromeClient>()
328
- // Copy-on-write too: the core iterates them live, and a listener may remove itself while it runs.
329
338
  val touchListeners: MutableList<View.OnTouchListener> = CopyOnWriteArrayList()
330
339
  val scrollListeners: MutableList<View.OnScrollChangeListener> = CopyOnWriteArrayList()
331
340
 
@@ -374,10 +383,8 @@ internal open class Attachment(value: Any?) {
374
383
  internal fun <T> routed(attachments: List<Attachment>, pick: (Attachment) -> List<T>) =
375
384
  attachments.filter { it.routed }.flatMap(pick)
376
385
 
377
- /** The core's [WebViewContext] for one hook's attach: the hook's key goes on its events and asks. */
378
386
  internal class HookContext(val hook: MinimalWebViewHooks.Hook, view: MinimalWebView) :
379
387
  Attachment(view.hookOptions[hook.key]), WebViewContext {
380
- /** Null once the attach ended. Main thread only. */
381
388
  private var view: MinimalWebView? = view
382
389
  val key get() = hook.key
383
390
  override val url get() = view?.url.orEmpty()
@@ -385,7 +392,6 @@ internal class HookContext(val hook: MinimalWebViewHooks.Hook, view: MinimalWebV
385
392
  private val destroyedHandlers = mutableListOf<(DocumentEnd) -> Unit>()
386
393
  val handlers = mutableMapOf<String, (List<Any?>, HookReply) -> Unit>()
387
394
 
388
- /** The attach ended: also drops the view and what the hook gave it, bar the [onDestroyed] handlers [destroyed] runs next. */
389
395
  override fun detach() {
390
396
  super.detach()
391
397
  view = null