forked from ilkerkesici/react-native-beauty-webview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
152 lines (142 loc) · 4.36 KB
/
index.js
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import React, { useState } from 'react';
import { SafeAreaView, StyleSheet, Modal } from 'react-native';
import { WebView } from 'react-native-webview';
import { Header } from './components/Header';
import Progress from './components/Progress';
import { colors } from './res';
const BeautyWebView = ({
visible,
onPressClose,
backgroundColor,
headerContent, // 'dark' || 'light', default 'dark'
headerBackground, // default #fff
url, // Required
progressColor,
progressHeight,
loadingText,
copyLinkTitle,
openBrowserTitle,
extraMenuItems,
animationType,
progressBarType, // 'normal' || 'background'
onLoadEnd,
onLoadStart,
navigationVisible,
closeIcon,
menuIcon,
onGoBack,
onGoForward,
incognito
}) => {
const [progressRef, setProgressRef] = useState(null);
const [backgroundProgressRef, setBackgroundProgressRef] = useState(null);
const [title, setTitle] = useState(loadingText);
const [backQueue, setBackQueue] = useState([]);
const [forwardQueue, setForwardQueue] = useState([]);
const [currentUrl, setCurrentUrl] = useState(url);
const onProgress = (progress) => {
progressRef?.startAnimation(progress);
progressBarType === 'background' && backgroundProgressRef?.startAnimation(progress);
};
const onNavigationStateChange = (event) => {
if (currentUrl === event.url) return;
backQueue.push(currentUrl);
setBackQueue(backQueue);
onGoForward && onGoForward();
setCurrentUrl(event.url);
}
const onPressBack = () => {
if (backQueue.length == 0) return;
const newUrl = backQueue[backQueue.length - 1];
forwardQueue.push(currentUrl);
setForwardQueue(forwardQueue);
onGoBack && onGoBack();
backQueue.pop();
setBackQueue(backQueue);
setCurrentUrl(newUrl);
}
const onPressForward = () => {
if (forwardQueue.length == 0) return;
const newUrl = forwardQueue[forwardQueue.length - 1];
backQueue.push(currentUrl);
setBackQueue(backQueue);
forwardQueue.pop();
setForwardQueue(forwardQueue);
setCurrentUrl(newUrl);
onGoForward && onGoForward();
}
const onClose = () => {
onPressClose && onPressClose();
setTimeout(() => {
setBackQueue([]);
setForwardQueue([]);
setCurrentUrl(url);
}, 200);
}
return (
<Modal visible={visible} transparent={false} animationType={animationType}>
<SafeAreaView style={[styles.container, { backgroundColor: backgroundColor }]}>
<Header
backgroundColor={headerBackground}
contentType={headerContent}
title={title}
url={currentUrl}
onPressClose={onClose}
copyLinkTitle={copyLinkTitle}
openBrowserTitle={openBrowserTitle}
extraMenuItems={extraMenuItems}
backgroundProgressRefOnChange={setBackgroundProgressRef}
navigationVisible={navigationVisible}
canForward={forwardQueue.length > 0}
canback={backQueue.length > 0}
onPressBack={onPressBack}
onPressForward={onPressForward}
closeIcon={closeIcon}
menuIcon={menuIcon}
/>
{
progressBarType === 'normal' &&
<Progress
height={progressHeight}
color={progressColor}
ref={(progress) => setProgressRef(progress)}
/>
}
<WebView
source={{ uri: currentUrl }}
onLoadProgress={({ nativeEvent }) => {
let loadingProgress = nativeEvent.progress;
onProgress(loadingProgress);
}}
injectedJavaScript="window.ReactNativeWebView.postMessage(document.title)"
onMessage={event => setTitle(event.nativeEvent.data)}
onLoadEnd={onLoadEnd}
onLoadStart={onLoadStart}
onNavigationStateChange={onNavigationStateChange}
incognito={incognito}
/>
</SafeAreaView>
</Modal>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
alignSelf: 'stretch',
},
});
BeautyWebView.defaultProps = {
transparent: true,
backgroundColor: colors.defaultBackground,
headerContent: 'dark',
headerBackground: colors.defaultBackground,
progressColor: colors.progress,
progressHeight: 4,
loadingText: 'Loading...',
copyLinkTitle: 'Copy Link',
openBrowserTitle: 'Open on Browser',
animationType: "slide",
progressBarType: "normal",
navigationVisible: true
}
export default BeautyWebView;