Files
notesnook/apps/mobile/src/components/header/index.js

171 lines
4.2 KiB
JavaScript
Raw Normal View History

2020-09-14 23:00:34 +05:00
import React from 'react';
2020-07-26 13:32:17 +05:00
import {
ActivityIndicator,
Platform,
2020-09-07 23:39:57 +05:00
StyleSheet,
2020-07-26 13:32:17 +05:00
TouchableOpacity,
View,
} from 'react-native';
2020-01-18 01:04:33 +05:00
import * as Animatable from 'react-native-animatable';
2020-09-14 23:00:34 +05:00
import {useSafeAreaInsets} from 'react-native-safe-area-context';
2020-02-11 20:07:36 +05:00
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
2020-07-26 13:32:17 +05:00
import {SIZE} from '../../common/common';
import {useTracked} from '../../provider';
2020-09-14 23:00:34 +05:00
import {eSendEvent} from '../../services/eventManager';
2020-05-10 22:16:55 +05:00
import NavigationService from '../../services/NavigationService';
2020-09-14 23:00:34 +05:00
import {useHideHeader} from '../../utils/hooks';
2020-07-26 13:32:17 +05:00
import {sideMenuRef} from '../../utils/refs';
import {DDS, w} from '../../utils/utils';
import {HeaderMenu} from './HeaderMenu';
import {HeaderTitle} from './HeaderTitle';
2020-05-10 22:16:55 +05:00
2020-05-11 01:02:11 +05:00
export const Header = ({showSearch, root}) => {
2020-01-17 21:26:01 +05:00
const [state, dispatch] = useTracked();
2020-09-14 23:00:34 +05:00
const {colors, syncing} = state;
2020-05-10 22:16:55 +05:00
2020-05-11 01:02:11 +05:00
let headerState = root ? state.headerState : state.indHeaderState;
2020-05-10 22:16:55 +05:00
2020-09-14 23:00:34 +05:00
const insets = useSafeAreaInsets();
2020-09-07 19:19:00 +05:00
const hideHeader = useHideHeader();
2020-05-10 22:16:55 +05:00
2020-09-07 19:19:00 +05:00
const onLeftButtonPress = () => {
if (!headerState.canGoBack) {
2020-09-21 13:01:37 +05:00
NavigationService.openDrawer();
2020-09-07 19:19:00 +05:00
return;
}
2020-09-14 23:00:34 +05:00
NavigationService.goBack();
2020-09-07 19:19:00 +05:00
};
2019-11-29 11:31:43 +05:00
return (
2020-01-27 14:01:24 +05:00
<View
2020-09-07 19:19:00 +05:00
style={[
styles.container,
{
2020-09-14 23:00:34 +05:00
marginTop: insets.top,
2020-09-07 19:19:00 +05:00
backgroundColor: colors.bg,
2020-09-14 23:00:34 +05:00
overflow: 'hidden',
2020-09-07 19:19:00 +05:00
},
]}>
2020-04-25 11:52:08 +05:00
<Animatable.View
transition={['opacity']}
duration={300}
2020-09-07 19:19:00 +05:00
style={[
styles.loadingContainer,
{
opacity: syncing ? 1 : 0,
},
]}>
2020-04-25 11:52:08 +05:00
<View
2020-09-07 19:19:00 +05:00
style={[
styles.loadingInnerContainer,
{
backgroundColor: colors.bg,
},
]}
2020-04-25 11:52:08 +05:00
/>
<ActivityIndicator size={25} color={colors.accent} />
</Animatable.View>
2020-09-07 19:19:00 +05:00
<View style={styles.leftBtnContainer}>
2020-09-07 21:23:38 +05:00
{!DDS.isTab ? (
2019-12-06 18:13:15 +05:00
<TouchableOpacity
2020-04-19 21:27:03 +05:00
hitSlop={{top: 20, bottom: 20, left: 50, right: 40}}
2020-09-07 19:19:00 +05:00
onPress={onLeftButtonPress}
style={styles.leftBtn}>
2020-09-07 23:39:57 +05:00
<Icon
2019-12-11 15:20:18 +05:00
style={{
2020-09-07 19:19:00 +05:00
marginLeft: headerState.canGoBack ? -5 : 0,
2019-12-11 15:20:18 +05:00
}}
2020-09-07 19:19:00 +05:00
color={colors.heading}
name={headerState.canGoBack ? 'arrow-left' : 'menu'}
size={SIZE.xxxl}
2019-12-07 12:05:15 +05:00
/>
2019-12-06 18:13:15 +05:00
</TouchableOpacity>
2020-09-06 16:10:34 +05:00
) : undefined}
2019-12-06 18:13:15 +05:00
2020-09-07 17:18:03 +05:00
{Platform.OS === 'android' ? <HeaderTitle root={root} /> : null}
2019-12-06 18:13:15 +05:00
</View>
2020-09-07 19:19:00 +05:00
{Platform.OS !== 'android' ? <HeaderTitle root={root} /> : null}
2020-09-06 16:10:34 +05:00
2020-09-07 19:19:00 +05:00
<View style={styles.rightBtnContainer}>
2020-01-01 00:04:59 +05:00
<Animatable.View
transition="opacity"
useNativeDriver={true}
2020-01-01 00:04:59 +05:00
duration={500}
style={{
2020-05-10 22:16:55 +05:00
opacity: hideHeader ? 1 : 0,
2020-01-01 00:04:59 +05:00
}}>
<TouchableOpacity
2020-07-26 13:32:17 +05:00
onPress={() => {
2020-09-07 17:18:03 +05:00
if (!hideHeader) return;
2020-07-26 13:32:17 +05:00
setHideHeader(false);
eSendEvent('showSearch');
}}
2020-09-07 19:19:00 +05:00
style={styles.rightBtn}>
<Icon name={'magnify'} size={SIZE.xl} color={colors.pri} />
2020-01-01 00:04:59 +05:00
</TouchableOpacity>
</Animatable.View>
2020-04-25 11:52:08 +05:00
2020-05-10 22:16:55 +05:00
<HeaderMenu />
2020-01-01 00:04:59 +05:00
</View>
2020-01-27 14:01:24 +05:00
</View>
2019-11-29 11:31:43 +05:00
);
};
2020-09-07 19:19:00 +05:00
const styles = StyleSheet.create({
container: {
flexDirection: 'row',
zIndex: 11,
height: 50,
marginBottom: 10,
justifyContent: 'center',
alignItems: 'center',
paddingHorizontal: 12,
width: '100%',
},
loadingContainer: {
alignSelf: 'center',
alignItems: 'center',
justifyContent: 'center',
zIndex: 999,
left: w / 2 - 20,
top: -20,
width: 40,
height: 40,
position: 'absolute',
},
loadingInnerContainer: {
width: 40,
height: 20,
position: 'absolute',
zIndex: 10,
top: 0,
},
leftBtnContainer: {
flexDirection: 'row',
justifyContent: 'flex-start',
alignItems: 'center',
position: 'absolute',
left: 12,
},
leftBtn: {
justifyContent: 'center',
alignItems: 'flex-start',
height: 40,
width: 60,
},
rightBtnContainer: {
flexDirection: 'row',
alignItems: 'center',
position: 'absolute',
right: 12,
},
rightBtn: {
justifyContent: 'center',
alignItems: 'flex-end',
height: 40,
width: 50,
paddingRight: 0,
},
});