@webview-bridge/react-native 1.6.1 → 1.6.2
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.
|
@@ -2,16 +2,91 @@ import type { Bridge, BridgeStore, KeyOfOrString, Parser, ParserSchema } from ".
|
|
|
2
2
|
import React from "react";
|
|
3
3
|
import type { BridgeWebView } from "./types/webview";
|
|
4
4
|
export type CreateWebViewArgs<BridgeObject extends Bridge, PostMessageSchema extends ParserSchema<any>> = {
|
|
5
|
+
/**
|
|
6
|
+
* The bridge object to be used in the WebView.
|
|
7
|
+
* @example
|
|
8
|
+
import { createWebView, bridge } from "@webview-bridge/react-native";
|
|
9
|
+
import InAppBrowser from "react-native-inappbrowser-reborn";
|
|
10
|
+
|
|
11
|
+
// Register functions in the bridge object in your React Native code
|
|
12
|
+
export const appBridge = bridge({
|
|
13
|
+
async getMessage() {
|
|
14
|
+
return "Hello, I'm native";
|
|
15
|
+
},
|
|
16
|
+
async sum(a: number, b: number) {
|
|
17
|
+
return a + b;
|
|
18
|
+
},
|
|
19
|
+
async openInAppBrowser(url: string) {
|
|
20
|
+
if (await InAppBrowser.isAvailable()) {
|
|
21
|
+
await InAppBrowser.open(url);
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
// ... Add more functions as needed
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
export const { WebView } = createWebView({
|
|
28
|
+
bridge: appBridge,
|
|
29
|
+
debug: true, // Enable console.log visibility in the native WebView
|
|
30
|
+
});
|
|
31
|
+
*/
|
|
5
32
|
bridge: BridgeStore<BridgeObject>;
|
|
33
|
+
/**
|
|
34
|
+
* If `true`, the console.log visibility in the WebView is enabled.
|
|
35
|
+
* @default false
|
|
36
|
+
*/
|
|
6
37
|
debug?: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Set the timeout in milliseconds for the response from the web method.
|
|
40
|
+
* @default 2000
|
|
41
|
+
*/
|
|
7
42
|
responseTimeout?: number;
|
|
43
|
+
/**
|
|
44
|
+
* The schema for the postMessage method.
|
|
45
|
+
* @link https://gronxb.github.io/webview-bridge/using-a-post-message.html
|
|
46
|
+
*/
|
|
8
47
|
postMessageSchema?: PostMessageSchema;
|
|
48
|
+
/**
|
|
49
|
+
* Callback function when a method that is not defined in the bridge is called.
|
|
50
|
+
* @link https://gronxb.github.io/webview-bridge/backward-compatibility/new-method.html#react-native-part
|
|
51
|
+
*/
|
|
9
52
|
fallback?: (method: keyof BridgeObject) => void;
|
|
10
53
|
};
|
|
11
54
|
export type WebMethod<T> = T & {
|
|
12
55
|
isReady: boolean;
|
|
13
56
|
};
|
|
57
|
+
/**
|
|
58
|
+
* Create a WebView component that can communicate with the bridge.
|
|
59
|
+
* @link https://gronxb.github.io/webview-bridge/getting-started.html
|
|
60
|
+
* @example
|
|
61
|
+
import { createWebView, bridge } from "@webview-bridge/react-native";
|
|
62
|
+
import InAppBrowser from "react-native-inappbrowser-reborn";
|
|
63
|
+
|
|
64
|
+
// Register functions in the bridge object in your React Native code
|
|
65
|
+
export const appBridge = bridge({
|
|
66
|
+
async getMessage() {
|
|
67
|
+
return "Hello, I'm native";
|
|
68
|
+
},
|
|
69
|
+
async sum(a: number, b: number) {
|
|
70
|
+
return a + b;
|
|
71
|
+
},
|
|
72
|
+
async openInAppBrowser(url: string) {
|
|
73
|
+
if (await InAppBrowser.isAvailable()) {
|
|
74
|
+
await InAppBrowser.open(url);
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
// ... Add more functions as needed
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
export const { WebView } = createWebView({
|
|
81
|
+
bridge: appBridge,
|
|
82
|
+
debug: true, // Enable console.log visibility in the native WebView
|
|
83
|
+
});
|
|
84
|
+
*/
|
|
14
85
|
export declare const createWebView: <BridgeObject extends Bridge, PostMessageSchema extends ParserSchema<any>>({ bridge, debug, responseTimeout, postMessageSchema, fallback, }: CreateWebViewArgs<BridgeObject, PostMessageSchema>) => {
|
|
86
|
+
/**
|
|
87
|
+
* Sends an event from React Native to the Web.
|
|
88
|
+
* @link https://gronxb.github.io/webview-bridge/using-a-post-message.html
|
|
89
|
+
*/
|
|
15
90
|
postMessage: <EventName extends KeyOfOrString<PostMessageSchema>, Args extends Parser<PostMessageSchema, EventName>>(eventName: EventName, args: Args) => void;
|
|
16
91
|
WebView: React.ForwardRefExoticComponent<import("react-native-webview/lib/WebViewTypes").IOSWebViewProps & import("react-native-webview/lib/WebViewTypes").AndroidWebViewProps & import("react-native-webview/lib/WebViewTypes").WindowsWebViewProps & React.RefAttributes<BridgeWebView>>;
|
|
17
92
|
/**
|
|
@@ -33,6 +33,34 @@ const react_native_webview_1 = __importDefault(require("react-native-webview"));
|
|
|
33
33
|
const bridge_1 = require("./integrations/bridge");
|
|
34
34
|
const console_1 = require("./integrations/console");
|
|
35
35
|
const handleRegisterWebMethod_1 = require("./integrations/handleRegisterWebMethod");
|
|
36
|
+
/**
|
|
37
|
+
* Create a WebView component that can communicate with the bridge.
|
|
38
|
+
* @link https://gronxb.github.io/webview-bridge/getting-started.html
|
|
39
|
+
* @example
|
|
40
|
+
import { createWebView, bridge } from "@webview-bridge/react-native";
|
|
41
|
+
import InAppBrowser from "react-native-inappbrowser-reborn";
|
|
42
|
+
|
|
43
|
+
// Register functions in the bridge object in your React Native code
|
|
44
|
+
export const appBridge = bridge({
|
|
45
|
+
async getMessage() {
|
|
46
|
+
return "Hello, I'm native";
|
|
47
|
+
},
|
|
48
|
+
async sum(a: number, b: number) {
|
|
49
|
+
return a + b;
|
|
50
|
+
},
|
|
51
|
+
async openInAppBrowser(url: string) {
|
|
52
|
+
if (await InAppBrowser.isAvailable()) {
|
|
53
|
+
await InAppBrowser.open(url);
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
// ... Add more functions as needed
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
export const { WebView } = createWebView({
|
|
60
|
+
bridge: appBridge,
|
|
61
|
+
debug: true, // Enable console.log visibility in the native WebView
|
|
62
|
+
});
|
|
63
|
+
*/
|
|
36
64
|
const createWebView = ({ bridge, debug, responseTimeout = 2000, postMessageSchema, fallback, }) => {
|
|
37
65
|
const WebMethod = {
|
|
38
66
|
current: {
|
|
@@ -47,6 +75,10 @@ const createWebView = ({ bridge, debug, responseTimeout = 2000, postMessageSchem
|
|
|
47
75
|
}
|
|
48
76
|
});
|
|
49
77
|
return {
|
|
78
|
+
/**
|
|
79
|
+
* Sends an event from React Native to the Web.
|
|
80
|
+
* @link https://gronxb.github.io/webview-bridge/using-a-post-message.html
|
|
81
|
+
*/
|
|
50
82
|
postMessage: (eventName, args) => {
|
|
51
83
|
let _args = args;
|
|
52
84
|
if (postMessageSchema) {
|
package/package.json
CHANGED