mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import React from 'react'
2import {
3 StyleProp,
4 StyleSheet,
5 TouchableOpacity,
6 View,
7 ViewStyle,
8} from 'react-native'
9import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'
10import {useNavigation} from '@react-navigation/native'
11
12import {usePalette} from '#/lib/hooks/usePalette'
13import {useWebMediaQueries} from '#/lib/hooks/useWebMediaQueries'
14import {NavigationProp} from '#/lib/routes/types'
15import {isWeb} from '#/platform/detection'
16import {useSetDrawerOpen} from '#/state/shell'
17import {Menu_Stroke2_Corner0_Rounded as Menu} from '#/components/icons/Menu'
18import {CenteredView} from './Views'
19
20const BACK_HITSLOP = {left: 20, top: 20, right: 50, bottom: 20}
21
22export function SimpleViewHeader({
23 showBackButton = true,
24 style,
25 children,
26}: React.PropsWithChildren<{
27 showBackButton?: boolean
28 style?: StyleProp<ViewStyle>
29}>) {
30 const pal = usePalette('default')
31 const setDrawerOpen = useSetDrawerOpen()
32 const navigation = useNavigation<NavigationProp>()
33 const {isMobile} = useWebMediaQueries()
34 const canGoBack = navigation.canGoBack()
35
36 const onPressBack = React.useCallback(() => {
37 if (navigation.canGoBack()) {
38 navigation.goBack()
39 } else {
40 navigation.navigate('Home')
41 }
42 }, [navigation])
43
44 const onPressMenu = React.useCallback(() => {
45 setDrawerOpen(true)
46 }, [setDrawerOpen])
47
48 const Container = isMobile ? View : CenteredView
49 return (
50 <Container
51 style={[
52 styles.header,
53 isMobile && styles.headerMobile,
54 isWeb && styles.headerWeb,
55 pal.view,
56 style,
57 ]}>
58 {showBackButton ? (
59 <TouchableOpacity
60 testID="viewHeaderDrawerBtn"
61 onPress={canGoBack ? onPressBack : onPressMenu}
62 hitSlop={BACK_HITSLOP}
63 style={canGoBack ? styles.backBtn : styles.backBtnWide}
64 accessibilityRole="button"
65 accessibilityLabel={canGoBack ? 'Back' : 'Menu'}
66 accessibilityHint="">
67 {canGoBack ? (
68 <FontAwesomeIcon
69 size={18}
70 icon="angle-left"
71 style={[styles.backIcon, pal.text]}
72 />
73 ) : (
74 <Menu size="lg" style={[{marginTop: 4}, pal.textLight]} />
75 )}
76 </TouchableOpacity>
77 ) : null}
78 {children}
79 </Container>
80 )
81}
82
83const styles = StyleSheet.create({
84 header: {
85 flexDirection: 'row',
86 alignItems: 'center',
87 paddingHorizontal: 18,
88 paddingVertical: 12,
89 width: '100%',
90 },
91 headerMobile: {
92 paddingHorizontal: 12,
93 paddingVertical: 10,
94 },
95 headerWeb: {
96 // @ts-ignore web-only
97 position: 'sticky',
98 top: 0,
99 zIndex: 1,
100 },
101 backBtn: {
102 width: 30,
103 height: 30,
104 },
105 backBtnWide: {
106 width: 30,
107 height: 30,
108 paddingLeft: 4,
109 marginRight: 4,
110 },
111 backIcon: {
112 marginTop: 6,
113 },
114})