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