Header.tsx
5.47 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import React, {
useState,
useCallback,
useEffect,
useLayoutEffect,
} from 'react';
import URL from 'url-parse';
import { useToggle } from '@huse/boolean';
import RNCWebview from 'react-native-webview';
import {
DynamicStyleSheet,
useDynamicStyleSheet,
useDynamicValue,
} from 'react-native-dark-mode';
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
import * as Progress from 'react-native-progress';
import {
Platform,
SafeAreaView,
View,
Text,
TouchableOpacity,
StyleSheet,
Animated,
TextInput,
NativeSyntheticEvent,
TextInputSubmitEditingEventData,
LayoutAnimation,
} from 'react-native';
import { colorPreset } from '../../design';
import { WebviewState, WebviewActions, webActions } from './reducer';
const dynamicStyles = new DynamicStyleSheet({
headerContainer: {
backgroundColor: colorPreset.backgroundColor.primary,
borderBottomWidth: StyleSheet.hairlineWidth,
borderBottomColor: colorPreset.separator.opaque,
},
containerStyle: {
backgroundColor: colorPreset.backgroundColor.secondary,
borderRadius: 10,
margin: 10,
...Platform.select({
ios: {
height: 36,
marginTop: 4,
},
android: {
height: 36,
},
}),
overflow: 'hidden',
},
labelContainer: {
flexDirection: 'row',
flex: 1,
alignItems: 'center',
},
hostLabel: {
color: colorPreset.labelColor.primary,
flex: 1,
textAlign: 'center',
fontSize: 17,
},
refreshButton: {
width: 36,
height: 36,
justifyContent: 'center',
alignItems: 'center',
},
inputContainerStyle: {
backgroundColor: colorPreset.backgroundColor.secondary,
},
inputStyle: {
flex: 1,
paddingHorizontal: 16,
},
progressBar: {
position: 'absolute',
left: 0,
right: 0,
bottom: 0,
height: 2,
},
});
interface Props {
state: WebviewState;
dispatch: React.Dispatch<WebviewActions>;
webview: React.RefObject<RNCWebview>;
}
// EXPERIMENT
function WebviewHeader({ state, dispatch, webview }: Props) {
const [uri, setUri] = useState<URL>();
const [focused, toggleFocused] = useToggle(false);
const progressBarOpacity = new Animated.Value(1);
const { url, progress, loading } = state;
useEffect(() => {
const newUrl = new URL(url);
setUri(newUrl);
}, [url]);
useEffect(() => {
if (progress === 1) {
Animated.timing(progressBarOpacity, {
toValue: 0,
useNativeDriver: true,
duration: 1000,
}).start();
}
}, [progress, progressBarOpacity]);
const onSubmitEditing = useCallback(
(e: NativeSyntheticEvent<TextInputSubmitEditingEventData>) => {
toggleFocused();
dispatch(webActions.loadText(e.nativeEvent.text));
},
[toggleFocused, dispatch]
);
const onPressLoading = useCallback(() => {
if (loading) {
webview.current?.stopLoading();
} else {
webview.current?.reload();
}
}, [loading, webview]);
useLayoutEffect(() => {
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
}, [focused]);
const styles = useDynamicStyleSheet(dynamicStyles);
const redColor = useDynamicValue(colorPreset.rainbow.red);
const greenColor = useDynamicValue(colorPreset.rainbow.green);
const primaryLabelColor = useDynamicValue(colorPreset.labelColor.primary);
return (
<SafeAreaView style={styles.headerContainer}>
<View style={styles.containerStyle}>
{focused ? (
<View style={styles.labelContainer}>
<TextInput
defaultValue={url}
onSubmitEditing={onSubmitEditing}
style={styles.inputStyle}
underlineColorAndroid="transparent"
textContentType="URL"
selectTextOnFocus
keyboardType="url"
returnKeyType="go"
autoCapitalize="none"
autoCorrect={false}
autoFocus
onBlur={e => {
toggleFocused();
}}
clearButtonMode="while-editing"
/>
</View>
) : (
<TouchableOpacity
activeOpacity={1}
onPress={toggleFocused}
style={styles.labelContainer}
>
<View style={styles.refreshButton}>
<MaterialCommunityIcons
name={
uri?.protocol === 'https:'
? 'shield-outline'
: 'shield-off-outline'
}
color={uri?.protocol === 'https:' ? greenColor : redColor}
size={20}
/>
</View>
<Text style={styles.hostLabel}>
{uri?.hostname.replace(/^www\./, '')}
</Text>
<TouchableOpacity
onPress={onPressLoading}
style={styles.refreshButton}
>
<MaterialCommunityIcons
name={loading ? 'close' : 'refresh'}
color={primaryLabelColor}
size={20}
/>
</TouchableOpacity>
<Animated.View
style={[{ opacity: progressBarOpacity }, styles.progressBar]}
>
<Progress.Bar
progress={progress}
borderWidth={0}
borderRadius={0}
width={null}
useNativeDriver
/>
</Animated.View>
</TouchableOpacity>
)}
</View>
</SafeAreaView>
);
}
export default WebviewHeader;