-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathWebViewProvider.tsx
More file actions
74 lines (65 loc) · 1.71 KB
/
Copy pathWebViewProvider.tsx
File metadata and controls
74 lines (65 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import React, { createContext, useContext, useState } from "react";
import { StyleSheet, View, ViewStyle } from "react-native";
type WebViewContextType = {
mountWebView: (webView: React.ReactNode, webViewStyle: ViewStyle) => void;
unmountWebView: () => void;
isMounted: boolean;
};
// Type-safe default value
const defaultWebViewContext: WebViewContextType = {
mountWebView: () => {
if (__DEV__) {
console.warn("WebViewPortal used without Provider");
}
},
unmountWebView: () => {
if (__DEV__) {
console.warn("WebViewPortal used without Provider");
}
},
isMounted: false,
};
const WebViewContext = createContext<WebViewContextType>(defaultWebViewContext);
export const WebViewProvider = ({
children,
}: {
children: React.ReactNode;
}) => {
const [webView, setWebView] = useState<React.ReactNode>(null);
const [style, setStyle] = useState<ViewStyle>({});
const [isMounted, setIsMounted] = useState(false);
const mountWebView = (
webViewComponent: React.ReactNode,
webViewStyle: ViewStyle,
) => {
setWebView(webViewComponent);
setStyle(webViewStyle);
setIsMounted(true);
};
const unmountWebView = () => {
setIsMounted(false);
};
return (
<WebViewContext.Provider
value={{ mountWebView, unmountWebView, isMounted }}
>
{children}
<View
style={[style, isMounted ? styles.visible : styles.hidden]}
collapsable={false}
pointerEvents="box-none"
>
{webView}
</View>
</WebViewContext.Provider>
);
};
const styles = StyleSheet.create({
visible: {
display: "flex",
},
hidden: {
display: "none",
},
});
export const useWebViewPortal = () => useContext(WebViewContext);