import React, {useState, useEffect} from 'react'; import {View, Text, StyleSheet, StatusBar, TouchableOpacity, ViewStyle, TextStyle} from 'react-native'; import {Size, getStatusBarHeight} from './_util'; import {Icon} from '.'; import {$Colors, $Theme} from './style'; export type NavBarProp = { style?: ViewStyle; TextStyle?: TextStyle; title?: string; onBack?: () => void; left?: JSX.Element | null; middle?: JSX.Element; right?: JSX.Element | null; scrollY?: number; breakPoint?: number; barStyle?: 'dark-content' | 'light-content'; navigation?: any; onBreak?: (isBreak: boolean) => void; shadow?: boolean; float?: boolean; }; const statusBarHeight = getStatusBarHeight(); const navBarHeight = 44; export const NavBarHeight = statusBarHeight + navBarHeight; const NavBar: React.FC = ({ scrollY = 0, barStyle = 'dark-content', left, middle, right, title = '', style, TextStyle, onBreak, breakPoint, onBack, navigation, shadow = true, float = false, }) => { const $textColor = TextStyle ? TextStyle.color || $Colors.normalText : $Colors.normalText; const [TextColor, setTextColor] = useState($textColor); const [invert, setInvert] = useState(false); useEffect(() => { if (breakPoint) { setInvert(scrollY > breakPoint); } }, [scrollY]); useEffect(() => { if (TextStyle && TextStyle.color) { setTextColor($textColor); } else { setTextColor(invert ? $Colors.normalText : $Colors.white); } onBreak && onBreak(invert); }, [invert]); const handleBack = () => { if (onBack) { onBack(); } else { if (navigation) { if (navigation.canGoBack()) { navigation.goBack(); } else { navigation.navigate('Root'); } } } }; return ( {left === null || !navigation ? null : left ? ( {left} ) : ( )} {middle ? middle : {title}} {right} ); }; export default NavBar; const styles = StyleSheet.create({ wrapper: { width: Size.width, left: 0, top: 0, backgroundColor: $Colors.white, paddingTop: statusBarHeight, zIndex: 99, }, float: { position: 'absolute', }, toolBar: { width: Size.width, left: 0, ...$Theme.flexRow, height: navBarHeight, }, left: { flex: 1, position: 'relative', height: navBarHeight, }, backBtn: { flex: 1, height: navBarHeight, position: 'absolute', left: 10, top: 0, ...$Theme.flexRowLeft, }, middle: { flex: 8, height: navBarHeight, ...$Theme.flexRow, }, right: { flex: 1, position: 'relative', height: navBarHeight, ...$Theme.flexRowRight, }, rightSlot: { position: 'absolute', right: 10, top: 0, height: navBarHeight, ...$Theme.flexRowRight, }, title: { fontSize: 16, fontWeight: 'bold', }, text: { color: $Colors.normalText, }, });