@vvedantb/native-ui 0.20.1 → 0.21.0-next-90c9e82619627a37726c0e2115cd6bbacfd21bcb
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/dist/webview/HtmlView.jsx +101 -83
- package/dist/webview/HtmlView.web.jsx +71 -30
- package/package.json +1 -1
- package/src/entries/webview.ts +1 -1
- package/src/webview/HtmlView.tsx +28 -2
- package/src/webview/HtmlView.web.tsx +20 -2
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { c as _c } from "react/compiler-runtime";
|
|
2
|
-
import { useEffect, useRef, useState } from "react";
|
|
2
|
+
import { useEffect, useImperativeHandle, useRef, useState } from "react";
|
|
3
3
|
import { View } from "react-native";
|
|
4
4
|
import { WebView } from "react-native-webview";
|
|
5
5
|
import { HTML_VIEW_HEIGHT_SCRIPT, htmlViewNavigation, htmlViewPostScript, parseHtmlViewHeight } from "./htmlView";
|
|
6
|
+
|
|
7
|
+
/** Sends the page a JSON message at once, as a `message` event (the iframe's `postMessage`). */
|
|
8
|
+
|
|
6
9
|
/**
|
|
7
10
|
* An HTML page in a sandboxed `react-native-webview`, as the web's sandboxed
|
|
8
11
|
* `srcdoc` iframe: transparent on the screen behind it, scripts on, and no
|
|
@@ -17,7 +20,7 @@ import { HTML_VIEW_HEIGHT_SCRIPT, htmlViewNavigation, htmlViewPostScript, parseH
|
|
|
17
20
|
* sandboxed iframe instead.
|
|
18
21
|
*/
|
|
19
22
|
export function HtmlView(t0) {
|
|
20
|
-
const $ = _c(
|
|
23
|
+
const $ = _c(33);
|
|
21
24
|
const {
|
|
22
25
|
html,
|
|
23
26
|
title,
|
|
@@ -26,144 +29,159 @@ export function HtmlView(t0) {
|
|
|
26
29
|
onOpenLink,
|
|
27
30
|
onContentHeight,
|
|
28
31
|
hostMessage,
|
|
32
|
+
onPageMessage,
|
|
33
|
+
ref: handle,
|
|
29
34
|
testID
|
|
30
35
|
} = t0;
|
|
31
36
|
const javaScriptEnabled = t1 === undefined ? true : t1;
|
|
32
37
|
const ref = useRef(null);
|
|
33
|
-
const [loaded, setLoaded] = useState(false);
|
|
34
38
|
let t2;
|
|
39
|
+
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
|
|
40
|
+
t2 = () => ({
|
|
41
|
+
postMessage: message => ref.current?.injectJavaScript(htmlViewPostScript(message))
|
|
42
|
+
});
|
|
43
|
+
$[0] = t2;
|
|
44
|
+
} else {
|
|
45
|
+
t2 = $[0];
|
|
46
|
+
}
|
|
47
|
+
useImperativeHandle(handle, t2);
|
|
48
|
+
const [loaded, setLoaded] = useState(false);
|
|
35
49
|
let t3;
|
|
36
|
-
|
|
37
|
-
|
|
50
|
+
let t4;
|
|
51
|
+
if ($[1] !== hostMessage || $[2] !== loaded) {
|
|
52
|
+
t3 = () => {
|
|
38
53
|
if (loaded && hostMessage) {
|
|
39
54
|
ref.current?.injectJavaScript(htmlViewPostScript(hostMessage));
|
|
40
55
|
}
|
|
41
56
|
};
|
|
42
|
-
|
|
43
|
-
$[
|
|
44
|
-
$[
|
|
45
|
-
$[2] = t2;
|
|
57
|
+
t4 = [loaded, hostMessage];
|
|
58
|
+
$[1] = hostMessage;
|
|
59
|
+
$[2] = loaded;
|
|
46
60
|
$[3] = t3;
|
|
61
|
+
$[4] = t4;
|
|
47
62
|
} else {
|
|
48
|
-
t2 = $[2];
|
|
49
63
|
t3 = $[3];
|
|
64
|
+
t4 = $[4];
|
|
50
65
|
}
|
|
51
|
-
useEffect(
|
|
52
|
-
let
|
|
53
|
-
if ($[
|
|
54
|
-
|
|
66
|
+
useEffect(t3, t4);
|
|
67
|
+
let t5;
|
|
68
|
+
if ($[5] !== onOpenLink) {
|
|
69
|
+
t5 = url => {
|
|
55
70
|
if (htmlViewNavigation(url, true) === "open") {
|
|
56
71
|
onOpenLink(url);
|
|
57
72
|
}
|
|
58
73
|
};
|
|
59
|
-
$[
|
|
60
|
-
$[
|
|
74
|
+
$[5] = onOpenLink;
|
|
75
|
+
$[6] = t5;
|
|
61
76
|
} else {
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
const openIfWeb = t4;
|
|
65
|
-
let t5;
|
|
66
|
-
if ($[6] !== height) {
|
|
67
|
-
t5 = {
|
|
68
|
-
height
|
|
69
|
-
};
|
|
70
|
-
$[6] = height;
|
|
71
|
-
$[7] = t5;
|
|
72
|
-
} else {
|
|
73
|
-
t5 = $[7];
|
|
77
|
+
t5 = $[6];
|
|
74
78
|
}
|
|
79
|
+
const openIfWeb = t5;
|
|
75
80
|
let t6;
|
|
76
|
-
if ($[
|
|
81
|
+
if ($[7] !== height) {
|
|
77
82
|
t6 = {
|
|
78
|
-
|
|
83
|
+
height
|
|
79
84
|
};
|
|
80
|
-
$[
|
|
81
|
-
$[
|
|
85
|
+
$[7] = height;
|
|
86
|
+
$[8] = t6;
|
|
82
87
|
} else {
|
|
83
|
-
t6 = $[
|
|
88
|
+
t6 = $[8];
|
|
84
89
|
}
|
|
85
90
|
let t7;
|
|
86
|
-
if ($[
|
|
87
|
-
t7 =
|
|
91
|
+
if ($[9] !== html) {
|
|
92
|
+
t7 = {
|
|
93
|
+
html
|
|
94
|
+
};
|
|
95
|
+
$[9] = html;
|
|
88
96
|
$[10] = t7;
|
|
89
97
|
} else {
|
|
90
98
|
t7 = $[10];
|
|
91
99
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
100
|
+
let t8;
|
|
101
|
+
if ($[11] === Symbol.for("react.memo_cache_sentinel")) {
|
|
102
|
+
t8 = ["*"];
|
|
103
|
+
$[11] = t8;
|
|
104
|
+
} else {
|
|
105
|
+
t8 = $[11];
|
|
106
|
+
}
|
|
107
|
+
const t9 = javaScriptEnabled ? HTML_VIEW_HEIGHT_SCRIPT : undefined;
|
|
108
|
+
let t10;
|
|
109
|
+
if ($[12] !== onContentHeight || $[13] !== onPageMessage) {
|
|
110
|
+
t10 = event => {
|
|
96
111
|
const reported = parseHtmlViewHeight(event.nativeEvent.data);
|
|
97
112
|
if (reported !== null) {
|
|
98
113
|
onContentHeight?.(reported);
|
|
114
|
+
} else {
|
|
115
|
+
onPageMessage?.(event.nativeEvent.data);
|
|
99
116
|
}
|
|
100
117
|
};
|
|
101
|
-
$[
|
|
102
|
-
$[
|
|
118
|
+
$[12] = onContentHeight;
|
|
119
|
+
$[13] = onPageMessage;
|
|
120
|
+
$[14] = t10;
|
|
103
121
|
} else {
|
|
104
|
-
|
|
122
|
+
t10 = $[14];
|
|
105
123
|
}
|
|
106
|
-
let
|
|
107
|
-
if ($[
|
|
108
|
-
|
|
124
|
+
let t11;
|
|
125
|
+
if ($[15] !== onOpenLink) {
|
|
126
|
+
t11 = request => {
|
|
109
127
|
const action = htmlViewNavigation(request.url, request.isTopFrame);
|
|
110
128
|
if (action === "open") {
|
|
111
129
|
onOpenLink(request.url);
|
|
112
130
|
}
|
|
113
131
|
return action === "load";
|
|
114
132
|
};
|
|
115
|
-
$[
|
|
116
|
-
$[14] = t10;
|
|
117
|
-
} else {
|
|
118
|
-
t10 = $[14];
|
|
119
|
-
}
|
|
120
|
-
let t11;
|
|
121
|
-
if ($[15] !== openIfWeb) {
|
|
122
|
-
t11 = event_0 => openIfWeb(event_0.nativeEvent.targetUrl);
|
|
123
|
-
$[15] = openIfWeb;
|
|
133
|
+
$[15] = onOpenLink;
|
|
124
134
|
$[16] = t11;
|
|
125
135
|
} else {
|
|
126
136
|
t11 = $[16];
|
|
127
137
|
}
|
|
128
138
|
let t12;
|
|
129
|
-
if ($[17]
|
|
130
|
-
t12 =
|
|
131
|
-
$[17] =
|
|
139
|
+
if ($[17] !== openIfWeb) {
|
|
140
|
+
t12 = event_0 => openIfWeb(event_0.nativeEvent.targetUrl);
|
|
141
|
+
$[17] = openIfWeb;
|
|
142
|
+
$[18] = t12;
|
|
132
143
|
} else {
|
|
133
|
-
t12 = $[
|
|
144
|
+
t12 = $[18];
|
|
134
145
|
}
|
|
135
146
|
let t13;
|
|
136
|
-
if ($[
|
|
137
|
-
t13 =
|
|
138
|
-
|
|
139
|
-
};
|
|
140
|
-
$[18] = t13;
|
|
147
|
+
if ($[19] === Symbol.for("react.memo_cache_sentinel")) {
|
|
148
|
+
t13 = () => setLoaded(true);
|
|
149
|
+
$[19] = t13;
|
|
141
150
|
} else {
|
|
142
|
-
t13 = $[
|
|
151
|
+
t13 = $[19];
|
|
143
152
|
}
|
|
144
153
|
let t14;
|
|
145
|
-
if ($[
|
|
146
|
-
t14 =
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
$[
|
|
150
|
-
$[22] = t6;
|
|
151
|
-
$[23] = t8;
|
|
152
|
-
$[24] = t9;
|
|
153
|
-
$[25] = title;
|
|
154
|
-
$[26] = t14;
|
|
154
|
+
if ($[20] === Symbol.for("react.memo_cache_sentinel")) {
|
|
155
|
+
t14 = {
|
|
156
|
+
backgroundColor: "transparent"
|
|
157
|
+
};
|
|
158
|
+
$[20] = t14;
|
|
155
159
|
} else {
|
|
156
|
-
t14 = $[
|
|
160
|
+
t14 = $[20];
|
|
157
161
|
}
|
|
158
162
|
let t15;
|
|
159
|
-
if ($[
|
|
160
|
-
t15 = <
|
|
161
|
-
$[
|
|
162
|
-
$[
|
|
163
|
-
$[
|
|
164
|
-
$[
|
|
163
|
+
if ($[21] !== javaScriptEnabled || $[22] !== t10 || $[23] !== t11 || $[24] !== t12 || $[25] !== t7 || $[26] !== t9 || $[27] !== title) {
|
|
164
|
+
t15 = <WebView ref={ref} source={t7} accessibilityLabel={title} originWhitelist={t8} javaScriptEnabled={javaScriptEnabled} injectedJavaScriptBeforeContentLoaded={t9} onMessage={t10} onShouldStartLoadWithRequest={t11} setSupportMultipleWindows={true} onOpenWindow={t12} onLoadEnd={t13} incognito={true} domStorageEnabled={false} thirdPartyCookiesEnabled={false} sharedCookiesEnabled={false} allowFileAccess={false} allowFileAccessFromFileURLs={false} allowUniversalAccessFromFileURLs={false} mixedContentMode="never" mediaPlaybackRequiresUserAction={true} nestedScrollEnabled={true} style={t14} />;
|
|
165
|
+
$[21] = javaScriptEnabled;
|
|
166
|
+
$[22] = t10;
|
|
167
|
+
$[23] = t11;
|
|
168
|
+
$[24] = t12;
|
|
169
|
+
$[25] = t7;
|
|
170
|
+
$[26] = t9;
|
|
171
|
+
$[27] = title;
|
|
172
|
+
$[28] = t15;
|
|
173
|
+
} else {
|
|
174
|
+
t15 = $[28];
|
|
175
|
+
}
|
|
176
|
+
let t16;
|
|
177
|
+
if ($[29] !== t15 || $[30] !== t6 || $[31] !== testID) {
|
|
178
|
+
t16 = <View testID={testID} style={t6}>{t15}</View>;
|
|
179
|
+
$[29] = t15;
|
|
180
|
+
$[30] = t6;
|
|
181
|
+
$[31] = testID;
|
|
182
|
+
$[32] = t16;
|
|
165
183
|
} else {
|
|
166
|
-
|
|
184
|
+
t16 = $[32];
|
|
167
185
|
}
|
|
168
|
-
return
|
|
186
|
+
return t16;
|
|
169
187
|
}
|
|
@@ -1,63 +1,104 @@
|
|
|
1
1
|
import { c as _c } from "react/compiler-runtime";
|
|
2
|
+
import { useEffect, useImperativeHandle, useRef } from "react";
|
|
2
3
|
import { View } from "react-native";
|
|
3
4
|
/**
|
|
4
5
|
* `HtmlView` on web (`react-native-webview` has no web build): the page in a
|
|
5
6
|
* sandboxed `srcdoc` iframe at `height`, scrolling inside. Scripts, forms and
|
|
6
7
|
* popups only, with no `allow-same-origin`. It does not report a height or
|
|
7
|
-
* deliver `hostMessage`; the native view does.
|
|
8
|
+
* deliver `hostMessage`; the native view does. The bridge works as on native:
|
|
9
|
+
* the page's `parent.postMessage` reaches `onPageMessage` (as JSON text), and
|
|
10
|
+
* the `ref` handle's `postMessage` reaches the page.
|
|
8
11
|
*/
|
|
9
12
|
export function HtmlView(t0) {
|
|
10
|
-
const $ = _c(
|
|
13
|
+
const $ = _c(15);
|
|
11
14
|
const {
|
|
12
15
|
html,
|
|
13
16
|
title,
|
|
14
17
|
height,
|
|
15
18
|
javaScriptEnabled: t1,
|
|
19
|
+
onPageMessage,
|
|
20
|
+
ref: handle,
|
|
16
21
|
testID
|
|
17
22
|
} = t0;
|
|
18
23
|
const javaScriptEnabled = t1 === undefined ? true : t1;
|
|
24
|
+
const frame = useRef(null);
|
|
19
25
|
let t2;
|
|
20
|
-
if ($[0]
|
|
21
|
-
t2 = {
|
|
26
|
+
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
|
|
27
|
+
t2 = () => ({
|
|
28
|
+
postMessage: message => frame.current?.contentWindow?.postMessage(JSON.parse(message), "*")
|
|
29
|
+
});
|
|
30
|
+
$[0] = t2;
|
|
31
|
+
} else {
|
|
32
|
+
t2 = $[0];
|
|
33
|
+
}
|
|
34
|
+
useImperativeHandle(handle, t2);
|
|
35
|
+
let t3;
|
|
36
|
+
let t4;
|
|
37
|
+
if ($[1] !== onPageMessage) {
|
|
38
|
+
t3 = () => {
|
|
39
|
+
if (!onPageMessage) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const listener = event => {
|
|
43
|
+
if (event.source !== frame.current?.contentWindow) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
onPageMessage(typeof event.data === "string" ? event.data : JSON.stringify(event.data));
|
|
47
|
+
};
|
|
48
|
+
window.addEventListener("message", listener);
|
|
49
|
+
return () => window.removeEventListener("message", listener);
|
|
50
|
+
};
|
|
51
|
+
t4 = [onPageMessage];
|
|
52
|
+
$[1] = onPageMessage;
|
|
53
|
+
$[2] = t3;
|
|
54
|
+
$[3] = t4;
|
|
55
|
+
} else {
|
|
56
|
+
t3 = $[2];
|
|
57
|
+
t4 = $[3];
|
|
58
|
+
}
|
|
59
|
+
useEffect(t3, t4);
|
|
60
|
+
let t5;
|
|
61
|
+
if ($[4] !== height) {
|
|
62
|
+
t5 = {
|
|
22
63
|
height
|
|
23
64
|
};
|
|
24
|
-
$[
|
|
25
|
-
$[
|
|
65
|
+
$[4] = height;
|
|
66
|
+
$[5] = t5;
|
|
26
67
|
} else {
|
|
27
|
-
|
|
68
|
+
t5 = $[5];
|
|
28
69
|
}
|
|
29
|
-
const
|
|
30
|
-
let
|
|
31
|
-
if ($[
|
|
32
|
-
|
|
70
|
+
const t6 = javaScriptEnabled ? "allow-scripts allow-forms allow-popups" : "";
|
|
71
|
+
let t7;
|
|
72
|
+
if ($[6] === Symbol.for("react.memo_cache_sentinel")) {
|
|
73
|
+
t7 = {
|
|
33
74
|
border: 0,
|
|
34
75
|
width: "100%",
|
|
35
76
|
height: "100%",
|
|
36
77
|
background: "transparent"
|
|
37
78
|
};
|
|
38
|
-
$[
|
|
79
|
+
$[6] = t7;
|
|
39
80
|
} else {
|
|
40
|
-
|
|
81
|
+
t7 = $[6];
|
|
41
82
|
}
|
|
42
|
-
let
|
|
43
|
-
if ($[
|
|
44
|
-
|
|
45
|
-
$[
|
|
46
|
-
$[
|
|
47
|
-
$[
|
|
48
|
-
$[
|
|
83
|
+
let t8;
|
|
84
|
+
if ($[7] !== html || $[8] !== t6 || $[9] !== title) {
|
|
85
|
+
t8 = <iframe ref={frame} title={title} srcDoc={html} sandbox={t6} style={t7} />;
|
|
86
|
+
$[7] = html;
|
|
87
|
+
$[8] = t6;
|
|
88
|
+
$[9] = title;
|
|
89
|
+
$[10] = t8;
|
|
49
90
|
} else {
|
|
50
|
-
|
|
91
|
+
t8 = $[10];
|
|
51
92
|
}
|
|
52
|
-
let
|
|
53
|
-
if ($[
|
|
54
|
-
|
|
55
|
-
$[
|
|
56
|
-
$[
|
|
57
|
-
$[
|
|
58
|
-
$[
|
|
93
|
+
let t9;
|
|
94
|
+
if ($[11] !== t5 || $[12] !== t8 || $[13] !== testID) {
|
|
95
|
+
t9 = <View testID={testID} style={t5}>{t8}</View>;
|
|
96
|
+
$[11] = t5;
|
|
97
|
+
$[12] = t8;
|
|
98
|
+
$[13] = testID;
|
|
99
|
+
$[14] = t9;
|
|
59
100
|
} else {
|
|
60
|
-
|
|
101
|
+
t9 = $[14];
|
|
61
102
|
}
|
|
62
|
-
return
|
|
103
|
+
return t9;
|
|
63
104
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vvedantb/native-ui",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.21.0-next-90c9e82619627a37726c0e2115cd6bbacfd21bcb",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Shared React Native / Expo UI primitives. Floating surfaces, forms, overlays, toasts, settings chrome, tab bar, AI chat parts, the family app marks, a NativeWind preset, the Expo web root and Clerk + Convex auth screens.",
|
|
6
6
|
"type": "module",
|
package/src/entries/webview.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// `@vvedantb/native-ui/webview`. Imports `react-native-webview` (an optional
|
|
2
2
|
// peer): the only entry that does, so an app without it bundles no WebView.
|
|
3
3
|
export { HtmlView } from "../webview/HtmlView";
|
|
4
|
-
export type { HtmlViewProps } from "../webview/HtmlView";
|
|
4
|
+
export type { HtmlViewHandle, HtmlViewProps } from "../webview/HtmlView";
|
|
5
5
|
export { HTML_VIEW_HEIGHT_SCRIPT, htmlViewNavigation, htmlViewPostScript, parseHtmlViewHeight } from "../webview/htmlView";
|
|
6
6
|
export type { HtmlViewNavigation } from "../webview/htmlView";
|
package/src/webview/HtmlView.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useEffect, useRef, useState } from "react";
|
|
1
|
+
import { useEffect, useImperativeHandle, useRef, useState, type Ref } from "react";
|
|
2
2
|
import { View } from "react-native";
|
|
3
3
|
import { WebView } from "react-native-webview";
|
|
4
4
|
import { HTML_VIEW_HEIGHT_SCRIPT, htmlViewNavigation, htmlViewPostScript, parseHtmlViewHeight } from "./htmlView";
|
|
@@ -21,9 +21,20 @@ export type HtmlViewProps = {
|
|
|
21
21
|
* loaded and again whenever it changes (a theme switch, say), without a reload.
|
|
22
22
|
*/
|
|
23
23
|
hostMessage?: string;
|
|
24
|
+
/**
|
|
25
|
+
* Any other message the page posts (`window.ReactNativeWebView.postMessage`
|
|
26
|
+
* here, `parent.postMessage` in the web iframe), as a string. Not the height
|
|
27
|
+
* reports, which go to `onContentHeight`.
|
|
28
|
+
*/
|
|
29
|
+
onPageMessage?: (data: string) => void;
|
|
30
|
+
/** `postMessage` sends the page one JSON message now, for a request and reply bridge. */
|
|
31
|
+
ref?: Ref<HtmlViewHandle>;
|
|
24
32
|
testID?: string;
|
|
25
33
|
};
|
|
26
34
|
|
|
35
|
+
/** Sends the page a JSON message at once, as a `message` event (the iframe's `postMessage`). */
|
|
36
|
+
export type HtmlViewHandle = { postMessage: (message: string) => void };
|
|
37
|
+
|
|
27
38
|
/**
|
|
28
39
|
* An HTML page in a sandboxed `react-native-webview`, as the web's sandboxed
|
|
29
40
|
* `srcdoc` iframe: transparent on the screen behind it, scripts on, and no
|
|
@@ -37,8 +48,22 @@ export type HtmlViewProps = {
|
|
|
37
48
|
* `react-native-webview` (an optional peer). On web, `HtmlView.web.tsx` draws a
|
|
38
49
|
* sandboxed iframe instead.
|
|
39
50
|
*/
|
|
40
|
-
export function HtmlView({
|
|
51
|
+
export function HtmlView({
|
|
52
|
+
html,
|
|
53
|
+
title,
|
|
54
|
+
height,
|
|
55
|
+
javaScriptEnabled = true,
|
|
56
|
+
onOpenLink,
|
|
57
|
+
onContentHeight,
|
|
58
|
+
hostMessage,
|
|
59
|
+
onPageMessage,
|
|
60
|
+
ref: handle,
|
|
61
|
+
testID,
|
|
62
|
+
}: HtmlViewProps) {
|
|
41
63
|
const ref = useRef<WebView>(null);
|
|
64
|
+
useImperativeHandle(handle, () => ({
|
|
65
|
+
postMessage: (message: string) => ref.current?.injectJavaScript(htmlViewPostScript(message)),
|
|
66
|
+
}));
|
|
42
67
|
const [loaded, setLoaded] = useState(false);
|
|
43
68
|
|
|
44
69
|
// The page is an external system: push each host message into it once it can listen.
|
|
@@ -65,6 +90,7 @@ export function HtmlView({ html, title, height, javaScriptEnabled = true, onOpen
|
|
|
65
90
|
onMessage={(event) => {
|
|
66
91
|
const reported = parseHtmlViewHeight(event.nativeEvent.data);
|
|
67
92
|
if (reported !== null) onContentHeight?.(reported);
|
|
93
|
+
else onPageMessage?.(event.nativeEvent.data);
|
|
68
94
|
}}
|
|
69
95
|
onShouldStartLoadWithRequest={(request) => {
|
|
70
96
|
const action = htmlViewNavigation(request.url, request.isTopFrame);
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { useEffect, useImperativeHandle, useRef } from "react";
|
|
1
2
|
import { View } from "react-native";
|
|
2
3
|
import type { HtmlViewProps } from "./HtmlView";
|
|
3
4
|
|
|
@@ -5,12 +6,29 @@ import type { HtmlViewProps } from "./HtmlView";
|
|
|
5
6
|
* `HtmlView` on web (`react-native-webview` has no web build): the page in a
|
|
6
7
|
* sandboxed `srcdoc` iframe at `height`, scrolling inside. Scripts, forms and
|
|
7
8
|
* popups only, with no `allow-same-origin`. It does not report a height or
|
|
8
|
-
* deliver `hostMessage`; the native view does.
|
|
9
|
+
* deliver `hostMessage`; the native view does. The bridge works as on native:
|
|
10
|
+
* the page's `parent.postMessage` reaches `onPageMessage` (as JSON text), and
|
|
11
|
+
* the `ref` handle's `postMessage` reaches the page.
|
|
9
12
|
*/
|
|
10
|
-
export function HtmlView({ html, title, height, javaScriptEnabled = true, testID }: HtmlViewProps) {
|
|
13
|
+
export function HtmlView({ html, title, height, javaScriptEnabled = true, onPageMessage, ref: handle, testID }: HtmlViewProps) {
|
|
14
|
+
const frame = useRef<HTMLIFrameElement>(null);
|
|
15
|
+
useImperativeHandle(handle, () => ({
|
|
16
|
+
postMessage: (message: string) => frame.current?.contentWindow?.postMessage(JSON.parse(message), "*"),
|
|
17
|
+
}));
|
|
18
|
+
// The page is an external system: its messages arrive on the window, from this frame only.
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
if (!onPageMessage) return;
|
|
21
|
+
const listener = (event: MessageEvent) => {
|
|
22
|
+
if (event.source !== frame.current?.contentWindow) return;
|
|
23
|
+
onPageMessage(typeof event.data === "string" ? event.data : JSON.stringify(event.data));
|
|
24
|
+
};
|
|
25
|
+
window.addEventListener("message", listener);
|
|
26
|
+
return () => window.removeEventListener("message", listener);
|
|
27
|
+
}, [onPageMessage]);
|
|
11
28
|
return (
|
|
12
29
|
<View testID={testID} style={{ height }}>
|
|
13
30
|
<iframe
|
|
31
|
+
ref={frame}
|
|
14
32
|
title={title}
|
|
15
33
|
srcDoc={html}
|
|
16
34
|
sandbox={javaScriptEnabled ? "allow-scripts allow-forms allow-popups" : ""}
|