react-native-minimal-webview 0.0.0-stage → 1.0.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.
Files changed (118) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +150 -2
  3. package/android/build.gradle +26 -0
  4. package/android/consumer-rules.pro +4 -0
  5. package/android/src/main/AndroidManifest.xml +2 -0
  6. package/android/src/main/java/expo/modules/minimalwebview/Asks.kt +44 -0
  7. package/android/src/main/java/expo/modules/minimalwebview/DocumentEnd.kt +31 -0
  8. package/android/src/main/java/expo/modules/minimalwebview/Hooks.kt +487 -0
  9. package/android/src/main/java/expo/modules/minimalwebview/MinimalWebView.kt +507 -0
  10. package/android/src/main/java/expo/modules/minimalwebview/MinimalWebViewClients.kt +358 -0
  11. package/android/src/main/java/expo/modules/minimalwebview/MinimalWebViewModule.kt +245 -0
  12. package/android/src/main/java/expo/modules/minimalwebview/MinimalWebViewNavigation.kt +199 -0
  13. package/android/src/main/java/expo/modules/minimalwebview/MinimalWebViewScripts.kt +197 -0
  14. package/android/src/main/java/expo/modules/minimalwebview/NavigationRules.kt +113 -0
  15. package/android/src/main/java/expo/modules/minimalwebview/PlatformSettings.kt +303 -0
  16. package/android/src/main/java/expo/modules/minimalwebview/PullToRefresh.kt +76 -0
  17. package/android/src/main/java/expo/modules/minimalwebview/requests/Downloads.kt +259 -0
  18. package/android/src/main/java/expo/modules/minimalwebview/requests/FileUpload.kt +44 -0
  19. package/android/src/main/java/expo/modules/minimalwebview/requests/FullscreenVideo.kt +87 -0
  20. package/android/src/main/java/expo/modules/minimalwebview/requests/Permissions.kt +77 -0
  21. package/build/WebView.d.ts +96 -0
  22. package/build/WebView.d.ts.map +1 -0
  23. package/build/WebView.js +453 -0
  24. package/build/WebView.js.map +1 -0
  25. package/build/bridge/createBridge.d.ts +32 -0
  26. package/build/bridge/createBridge.d.ts.map +1 -0
  27. package/build/bridge/createBridge.js +137 -0
  28. package/build/bridge/createBridge.js.map +1 -0
  29. package/build/bridge/endpoint.d.ts +33 -0
  30. package/build/bridge/endpoint.d.ts.map +1 -0
  31. package/build/bridge/endpoint.js +105 -0
  32. package/build/bridge/endpoint.js.map +1 -0
  33. package/build/bridge/origins.d.ts +25 -0
  34. package/build/bridge/origins.d.ts.map +1 -0
  35. package/build/bridge/origins.js +59 -0
  36. package/build/bridge/origins.js.map +1 -0
  37. package/build/bridge/pageClient.generated.d.ts +3 -0
  38. package/build/bridge/pageClient.generated.d.ts.map +1 -0
  39. package/build/bridge/pageClient.generated.js +4 -0
  40. package/build/bridge/pageClient.generated.js.map +1 -0
  41. package/build/bridge/protocol.d.ts +50 -0
  42. package/build/bridge/protocol.d.ts.map +1 -0
  43. package/build/bridge/protocol.js +52 -0
  44. package/build/bridge/protocol.js.map +1 -0
  45. package/build/defaultNavigationRules.d.ts +15 -0
  46. package/build/defaultNavigationRules.d.ts.map +1 -0
  47. package/build/defaultNavigationRules.js +22 -0
  48. package/build/defaultNavigationRules.js.map +1 -0
  49. package/build/index.d.ts +31 -0
  50. package/build/index.d.ts.map +1 -0
  51. package/build/index.js +30 -0
  52. package/build/index.js.map +1 -0
  53. package/build/platform.generated.d.ts +701 -0
  54. package/build/platform.generated.d.ts.map +1 -0
  55. package/build/platform.generated.js +201 -0
  56. package/build/platform.generated.js.map +1 -0
  57. package/build/reactNativeWebViewDefaults.d.ts +21 -0
  58. package/build/reactNativeWebViewDefaults.d.ts.map +1 -0
  59. package/build/reactNativeWebViewDefaults.js +12 -0
  60. package/build/reactNativeWebViewDefaults.js.map +1 -0
  61. package/build/reactNativeWebViewMessages.d.ts +16 -0
  62. package/build/reactNativeWebViewMessages.d.ts.map +1 -0
  63. package/build/reactNativeWebViewMessages.js +24 -0
  64. package/build/reactNativeWebViewMessages.js.map +1 -0
  65. package/build/refStore.d.ts +32 -0
  66. package/build/refStore.d.ts.map +1 -0
  67. package/build/refStore.js +115 -0
  68. package/build/refStore.js.map +1 -0
  69. package/build/types.d.ts +1173 -0
  70. package/build/types.d.ts.map +1 -0
  71. package/build/types.defaults.d.ts +8 -0
  72. package/build/types.defaults.d.ts.map +1 -0
  73. package/build/types.defaults.js +17 -0
  74. package/build/types.defaults.js.map +1 -0
  75. package/build/types.js +2 -0
  76. package/build/types.js.map +1 -0
  77. package/expo-module.config.json +9 -0
  78. package/ios/Asks.swift +45 -0
  79. package/ios/DelegateProxy.swift +158 -0
  80. package/ios/DocumentEnd.swift +22 -0
  81. package/ios/DownloadName.swift +10 -0
  82. package/ios/Downloads.swift +51 -0
  83. package/ios/Hooks.swift +310 -0
  84. package/ios/Loads.swift +44 -0
  85. package/ios/MRWExceptions.h +9 -0
  86. package/ios/MRWExceptions.m +10 -0
  87. package/ios/MRWInvoke.h +38 -0
  88. package/ios/MRWInvoke.m +187 -0
  89. package/ios/MinimalWebView+Dialogs.swift +96 -0
  90. package/ios/MinimalWebView+Navigation.swift +243 -0
  91. package/ios/MinimalWebView+Scripts.swift +92 -0
  92. package/ios/MinimalWebView.docc/MinimalWebView.md +13 -0
  93. package/ios/MinimalWebView.podspec +32 -0
  94. package/ios/MinimalWebView.swift +513 -0
  95. package/ios/MinimalWebViewModule.swift +110 -0
  96. package/ios/NavigationRules.swift +123 -0
  97. package/ios/OriginRules.swift +36 -0
  98. package/ios/PlatformCompletions.generated.swift +39 -0
  99. package/ios/PlatformSettings.swift +252 -0
  100. package/ios/ReadAccess.swift +19 -0
  101. package/ios/Recovery.swift +25 -0
  102. package/ios/SharedCookies.swift +59 -0
  103. package/package.json +99 -4
  104. package/src/WebView.tsx +626 -0
  105. package/src/bridge/createBridge.ts +178 -0
  106. package/src/bridge/endpoint.ts +123 -0
  107. package/src/bridge/origins.ts +61 -0
  108. package/src/bridge/pageClient.generated.ts +5 -0
  109. package/src/bridge/pageClient.ts +67 -0
  110. package/src/bridge/protocol.ts +78 -0
  111. package/src/defaultNavigationRules.ts +24 -0
  112. package/src/index.ts +37 -0
  113. package/src/platform.generated.ts +693 -0
  114. package/src/reactNativeWebViewDefaults.ts +13 -0
  115. package/src/reactNativeWebViewMessages.ts +26 -0
  116. package/src/refStore.ts +165 -0
  117. package/src/types.defaults.ts +24 -0
  118. package/src/types.ts +1240 -0
@@ -0,0 +1,26 @@
1
+ import { Platform } from 'react-native';
2
+
3
+ import type { InjectedScript } from './types';
4
+
5
+ /**
6
+ * Gives pages react-native-webview's messaging over the bridge event `rnw`; pass it in `injectedScripts`.
7
+ *
8
+ * - The page's `window.ReactNativeWebView.postMessage(data)` arrives as `String(data)`.
9
+ * - What you emit as `rnw` reaches the page as a `message` event on `window` on iOS and on `document` on Android, as
10
+ * react-native-webview does.
11
+ * - There is no `injectedObjectJson`: `window.ReactNativeWebView` has `postMessage` only.
12
+ *
13
+ * It needs the `bridge` prop: only the main frame of the pages in `allowedOrigins` gets `window.ReactNativeWebView`.
14
+ * Its `origins` are `['*']`, since the bridge already scopes it.
15
+ * @see [Messaging example](https://rasmuskard.github.io/react-native-minimal-webview/guides/migrating-from-react-native-webview/#postmessage-and-onmessage)
16
+ * @group Scripts
17
+ */
18
+ export const reactNativeWebViewMessages: InjectedScript = {
19
+ // Runs after the bridge's own script, so `window.nativeBridge` already exists where the bridge is installed.
20
+ code: `if (window.nativeBridge) {
21
+ window.ReactNativeWebView = { postMessage: (data) => window.nativeBridge.emit('rnw', String(data)) };
22
+ window.nativeBridge.on('rnw', (data) =>
23
+ ${Platform.OS === 'ios' ? 'window' : 'document'}.dispatchEvent(new MessageEvent('message', { data })));
24
+ }`,
25
+ origins: ['*'],
26
+ };
@@ -0,0 +1,165 @@
1
+ // The ref store: each ref object's navigation state and the WebView handle attached to it, behind createWebViewRef,
2
+ // useWebViewState and WebView's `ref`.
3
+ import {
4
+ type Ref,
5
+ type RefObject,
6
+ useImperativeHandle,
7
+ useLayoutEffect,
8
+ useMemo,
9
+ useRef,
10
+ useSyncExternalStore,
11
+ } from 'react';
12
+
13
+ import type { NavigationState, WebViewRef, WebViewRefObject } from './types';
14
+
15
+ /** An empty `url`, so a stale URL never looks valid while no WebView is mounted. */
16
+ const INITIAL_STATE: NavigationState = {
17
+ url: '',
18
+ pendingUrl: null,
19
+ title: '',
20
+ loading: false,
21
+ progress: 0,
22
+ canGoBack: false,
23
+ canGoForward: false,
24
+ outcome: null,
25
+ };
26
+
27
+ /** A mounted WebView's hold on its ref: it passes on the WebView's state, and lets go at unmount. */
28
+ type Attachment = { update(state: NavigationState): void; detach(): void };
29
+ type Adapter = {
30
+ nav: WebViewRefObject;
31
+ attach(handle: WebViewRef, state: NavigationState): Attachment;
32
+ };
33
+
34
+ /** The navigation object behind each ref object: created by createWebViewRef, or made on first use for a plain ref. */
35
+ const adapters = new WeakMap<object, Adapter>();
36
+
37
+ function adapterFor(ref: object) {
38
+ let adapter = adapters.get(ref);
39
+ if (!adapter) adapters.set(ref, (adapter = createAdapter()));
40
+ return adapter;
41
+ }
42
+
43
+ function createAdapter(): Adapter {
44
+ let attached: WebViewRef | null = null;
45
+ let state = INITIAL_STATE;
46
+ const listeners = new Set<() => void>();
47
+ const set = (next: NavigationState) => {
48
+ state = next;
49
+ listeners.forEach((listener) => listener());
50
+ };
51
+ const nav: WebViewRefObject = {
52
+ get current() {
53
+ return attached;
54
+ },
55
+ getState: () => state,
56
+ subscribe(listener) {
57
+ listeners.add(listener);
58
+ return () => void listeners.delete(listener);
59
+ },
60
+ };
61
+ return {
62
+ nav,
63
+ // Last attached wins, like a React ref; an earlier instance's later updates and detach are ignored.
64
+ attach(handle, initial) {
65
+ if (__DEV__ && attached) {
66
+ console.warn(
67
+ '[react-native-minimal-webview] This ref is already attached to a mounted WebView; the new one replaces it. Give each WebView its own ref.'
68
+ );
69
+ }
70
+ attached = handle;
71
+ set(initial);
72
+ return {
73
+ update: (next) => attached === handle && set(next),
74
+ detach() {
75
+ if (attached !== handle) return;
76
+ attached = null;
77
+ set(INITIAL_STATE);
78
+ },
79
+ };
80
+ },
81
+ };
82
+ }
83
+
84
+ /**
85
+ * A ref to pass as a WebView's `ref`, which a module or a store can hold, so code outside React can read the state and
86
+ * call the WebView.
87
+ * @see [Outside React example](https://rasmuskard.github.io/react-native-minimal-webview/guides/examples/#outside-react)
88
+ * @group WebView
89
+ */
90
+ export function createWebViewRef(): WebViewRefObject {
91
+ const adapter = createAdapter();
92
+ adapters.set(adapter.nav, adapter);
93
+ return adapter.nav;
94
+ }
95
+
96
+ /**
97
+ * Attaches `handle` to `ref`. A callback or plain ref gets it through `useImperativeHandle`; a `createWebViewRef` ref
98
+ * serves it as its own `current`. Any ref object also gets the navigation state through its adapter.
99
+ * Returns a function that records a new state, passes it to the attached ref and returns the previous state.
100
+ */
101
+ export function useAttach(
102
+ ref: Ref<WebViewRef> | undefined,
103
+ handle: WebViewRef
104
+ ): (state: NavigationState) => NavigationState {
105
+ const last = useRef(INITIAL_STATE);
106
+ const attachment = useRef<Attachment>(undefined);
107
+ const adapter = ref && typeof ref === 'object' ? adapterFor(ref) : undefined;
108
+ useImperativeHandle(adapter?.nav === ref ? undefined : ref, () => handle, [handle]);
109
+ useLayoutEffect(() => {
110
+ if (!adapter) return undefined;
111
+ const attached = adapter.attach(handle, last.current);
112
+ attachment.current = attached;
113
+ return () => {
114
+ attached.detach();
115
+ attachment.current = undefined;
116
+ };
117
+ }, [adapter, handle]);
118
+ return (state) => {
119
+ const was = last.current;
120
+ last.current = state;
121
+ attachment.current?.update(state);
122
+ return was;
123
+ };
124
+ }
125
+
126
+ /**
127
+ * The navigation state of the WebView mounted with `ref` (a `useRef`, `createRef` or `createWebViewRef` one);
128
+ * re-renders on every change, `progress` included, and follows a remount.
129
+ * @see [Navigation state example](https://rasmuskard.github.io/react-native-minimal-webview/guides/examples/#navigation-state)
130
+ * @group Navigation
131
+ */
132
+ export function useWebViewState(ref: RefObject<WebViewRef | null>): NavigationState;
133
+ /**
134
+ * What `select` returns for the navigation state of the WebView mounted with `ref`; re-renders only when that changes
135
+ * (by `Object.is`), and follows a remount.
136
+ *
137
+ * `select` must be pure: it can run more than once for a state, and it can be a new function at each render. A
138
+ * `select` that returns a new object or array each time re-renders on every state change, because `Object.is` never
139
+ * matches it.
140
+ */
141
+ export function useWebViewState<T>(
142
+ ref: RefObject<WebViewRef | null>,
143
+ select: (state: NavigationState) => T
144
+ ): T;
145
+ export function useWebViewState<T>(
146
+ ref: RefObject<WebViewRef | null>,
147
+ select?: (state: NavigationState) => T
148
+ ): NavigationState | T {
149
+ const { nav } = adapterFor(ref);
150
+ const getSnapshot = useMemo<() => NavigationState | T>(
151
+ () => (select ? selectFrom(nav.getState, select) : nav.getState),
152
+ [nav, select]
153
+ );
154
+ return useSyncExternalStore(nav.subscribe, getSnapshot);
155
+ }
156
+
157
+ /** `select` of the current state, run again only for another state: a snapshot is the same value until a change. */
158
+ function selectFrom<T>(getState: () => NavigationState, select: (state: NavigationState) => T) {
159
+ let last: { state: NavigationState; selected: T } | undefined;
160
+ return () => {
161
+ const state = getState();
162
+ if (last?.state !== state) last = { state, selected: select(state) };
163
+ return last.selected;
164
+ };
165
+ }
@@ -0,0 +1,24 @@
1
+ // Generated by tsdoc-defaults from the @default/@defaultValue tags in ./types.ts. Do not edit.
2
+ import type { InjectedScript, BridgeOptions, IOSSettings, WebViewProps } from './types';
3
+
4
+ export const DEFAULTS: {
5
+ InjectedScript: Required<Pick<InjectedScript, 'allFrames'>>;
6
+ BridgeOptions: Required<Pick<BridgeOptions, 'timeoutMs'>>;
7
+ IOSSettings: Required<Pick<IOSSettings, 'configuration' | 'webView'>>;
8
+ WebViewProps: Required<Pick<WebViewProps, 'pullToRefresh' | 'sharedCookies'>>;
9
+ } = {
10
+ InjectedScript: {
11
+ allFrames: false,
12
+ },
13
+ BridgeOptions: {
14
+ timeoutMs: 10000,
15
+ },
16
+ IOSSettings: {
17
+ configuration: { allowsInlineMediaPlayback: true },
18
+ webView: { inspectable: __DEV__ },
19
+ },
20
+ WebViewProps: {
21
+ pullToRefresh: false,
22
+ sharedCookies: false,
23
+ },
24
+ };