import React, { useMemo } from 'react'; import { useWindowDimensions } from 'react-native'; import { StyleSheet, View } from 'react-native'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; import { FadingView } from '../containers'; import { HeaderBottomBorder } from '../line'; import type { HeaderProps } from './types'; const MIN_CENTER_WIDTH_PRC = 0.4; const Header: React.FC = ({ showNavBar, headerStyle, headerLeft = null, headerLeftStyle, headerLeftFadesIn, headerCenter = null, headerCenterStyle, headerCenterFadesIn = true, headerRight = null, headerRightStyle, headerRightFadesIn, noBottomBorder = false, ignoreTopSafeArea = false, borderColor, initialBorderColor, borderWidth, SurfaceComponent, }) => { const { top } = useSafeAreaInsets(); const dimensions = useWindowDimensions(); const headerCenterExists = !!headerCenter; const { centerWidth, minSideHeaderWidth } = useMemo(() => { const _centerWidth = headerCenterExists ? MIN_CENTER_WIDTH_PRC * dimensions.width : 0; const _minSideHeaderWidth = (dimensions.width - _centerWidth) / 2; return { centerWidth: _centerWidth, minSideHeaderWidth: _minSideHeaderWidth }; }, [headerCenterExists, dimensions]); const noHeaderLeftRight = !headerLeft && !headerRight; return ( {SurfaceComponent && SurfaceComponent({ showNavBar })} {headerLeftFadesIn ? ( {headerLeft} ) : ( {headerLeft} )} {headerCenter && (headerCenterFadesIn ? ( {headerCenter} ) : ( {headerCenter} ))} {headerRightFadesIn ? ( {headerRight} ) : ( {headerRight} )} {!noBottomBorder && ( )} ); }; export default Header; const styles = StyleSheet.create({ container: { flexDirection: 'row', width: '100%', alignItems: 'center', justifyContent: 'flex-start', }, leftContainer: { flexDirection: 'row', paddingHorizontal: 8, paddingVertical: 4, justifyContent: 'flex-start', alignItems: 'center', overflow: 'hidden', }, centerContainer: { flex: 1, flexDirection: 'row', paddingHorizontal: 6, alignItems: 'center', justifyContent: 'center', }, rightContainer: { flexDirection: 'row-reverse', paddingVertical: 4, paddingHorizontal: 8, alignItems: 'center', justifyContent: 'flex-start', overflow: 'hidden', }, noFlex: { display: 'none' }, });