1e650bfb3S猫头猫import React, {ReactNode, useEffect, useRef, useState} from 'react'; 2e650bfb3S猫头猫import { 3e650bfb3S猫头猫 LayoutRectangle, 4e650bfb3S猫头猫 StyleSheet, 5e650bfb3S猫头猫 TouchableWithoutFeedback, 6e650bfb3S猫头猫 View, 7e650bfb3S猫头猫 StatusBar as OriginalStatusBar, 81119c2eaS猫头猫 StyleProp, 91119c2eaS猫头猫 ViewStyle, 10e650bfb3S猫头猫} from 'react-native'; 11e650bfb3S猫头猫import rpx from '@/utils/rpx'; 12e650bfb3S猫头猫import useColors from '@/hooks/useColors'; 13e650bfb3S猫头猫import StatusBar from './statusBar'; 14e650bfb3S猫头猫import color from 'color'; 15e650bfb3S猫头猫import IconButton from './iconButton'; 16e650bfb3S猫头猫import globalStyle from '@/constants/globalStyle'; 17e650bfb3S猫头猫import ThemeText from './themeText'; 18e650bfb3S猫头猫import {useNavigation} from '@react-navigation/native'; 19e650bfb3S猫头猫import Animated, { 20e650bfb3S猫头猫 Easing, 21e650bfb3S猫头猫 useAnimatedStyle, 22e650bfb3S猫头猫 useSharedValue, 23e650bfb3S猫头猫 withTiming, 24e650bfb3S猫头猫} from 'react-native-reanimated'; 25e650bfb3S猫头猫import Portal from './portal'; 261119c2eaS猫头猫import ListItem from './listItem'; 27e650bfb3S猫头猫 28e650bfb3S猫头猫interface IAppBarProps { 29e650bfb3S猫头猫 backgroundOpacity?: number; 30e650bfb3S猫头猫 titleTextOpacity?: number; 31e650bfb3S猫头猫 withStatusBar?: boolean; 32e650bfb3S猫头猫 color?: string; 33e650bfb3S猫头猫 actions?: Array<{ 34e650bfb3S猫头猫 icon: string; 35e650bfb3S猫头猫 onPress?: () => void; 36e650bfb3S猫头猫 }>; 37e650bfb3S猫头猫 menu?: Array<{ 38e650bfb3S猫头猫 icon: string; 39e650bfb3S猫头猫 title: string; 407a8d024eS猫头猫 show?: boolean; 41e650bfb3S猫头猫 onPress?: () => void; 42e650bfb3S猫头猫 }>; 43e650bfb3S猫头猫 menuWithStatusBar?: boolean; 44e650bfb3S猫头猫 children?: string | ReactNode; 451119c2eaS猫头猫 containerStyle?: StyleProp<ViewStyle>; 461119c2eaS猫头猫 contentStyle?: StyleProp<ViewStyle>; 47a27adc20S猫头猫 actionComponent?: ReactNode; 48e650bfb3S猫头猫} 49e650bfb3S猫头猫 50e650bfb3S猫头猫const ANIMATION_EASING: Animated.EasingFunction = Easing.out(Easing.exp); 51e650bfb3S猫头猫const ANIMATION_DURATION = 500; 52e650bfb3S猫头猫 53e650bfb3S猫头猫const timingConfig = { 54e650bfb3S猫头猫 duration: ANIMATION_DURATION, 55e650bfb3S猫头猫 easing: ANIMATION_EASING, 56e650bfb3S猫头猫}; 57e650bfb3S猫头猫 58e650bfb3S猫头猫export default function AppBar(props: IAppBarProps) { 59e650bfb3S猫头猫 const { 60e650bfb3S猫头猫 backgroundOpacity = 1, 61e650bfb3S猫头猫 titleTextOpacity = 1, 62e650bfb3S猫头猫 withStatusBar, 63e650bfb3S猫头猫 color: _color, 64e650bfb3S猫头猫 actions = [], 65e650bfb3S猫头猫 menu = [], 66e650bfb3S猫头猫 menuWithStatusBar = true, 671119c2eaS猫头猫 containerStyle, 681119c2eaS猫头猫 contentStyle, 69e650bfb3S猫头猫 children, 70a27adc20S猫头猫 actionComponent, 71e650bfb3S猫头猫 } = props; 72e650bfb3S猫头猫 73e650bfb3S猫头猫 const colors = useColors(); 74e650bfb3S猫头猫 const navigation = useNavigation(); 75e650bfb3S猫头猫 766cfecf1cS猫头猫 const bgColor = color(colors.appBar ?? colors.primary) 776cfecf1cS猫头猫 .alpha(backgroundOpacity) 786cfecf1cS猫头猫 .toString(); 79277c5280S猫头猫 const contentColor = _color ?? colors.appBarText; 80e650bfb3S猫头猫 81e650bfb3S猫头猫 const [showMenu, setShowMenu] = useState(false); 82e650bfb3S猫头猫 const menuIconLayoutRef = useRef<LayoutRectangle>(); 83e650bfb3S猫头猫 const scaleRate = useSharedValue(0); 84e650bfb3S猫头猫 85e650bfb3S猫头猫 useEffect(() => { 86e650bfb3S猫头猫 if (showMenu) { 87e650bfb3S猫头猫 scaleRate.value = withTiming(1, timingConfig); 88e650bfb3S猫头猫 } else { 89e650bfb3S猫头猫 scaleRate.value = withTiming(0, timingConfig); 90e650bfb3S猫头猫 } 91e650bfb3S猫头猫 }, [showMenu]); 92e650bfb3S猫头猫 93e650bfb3S猫头猫 const transformStyle = useAnimatedStyle(() => { 94e650bfb3S猫头猫 return { 95e650bfb3S猫头猫 opacity: scaleRate.value, 96e650bfb3S猫头猫 }; 97e650bfb3S猫头猫 }); 98e650bfb3S猫头猫 99e650bfb3S猫头猫 return ( 100e650bfb3S猫头猫 <> 101e650bfb3S猫头猫 {withStatusBar ? <StatusBar backgroundColor={bgColor} /> : null} 1021119c2eaS猫头猫 <View 1031119c2eaS猫头猫 style={[ 1041119c2eaS猫头猫 styles.container, 1051119c2eaS猫头猫 containerStyle, 1061119c2eaS猫头猫 {backgroundColor: bgColor}, 1071119c2eaS猫头猫 ]}> 108e650bfb3S猫头猫 <IconButton 109e650bfb3S猫头猫 name="arrow-left" 110e650bfb3S猫头猫 sizeType="normal" 111e650bfb3S猫头猫 color={contentColor} 112e650bfb3S猫头猫 style={globalStyle.notShrink} 113e650bfb3S猫头猫 onPress={() => { 114e650bfb3S猫头猫 navigation.goBack(); 115e650bfb3S猫头猫 }} 116e650bfb3S猫头猫 /> 1171119c2eaS猫头猫 <View style={[globalStyle.grow, styles.content, contentStyle]}> 118e650bfb3S猫头猫 {typeof children === 'string' ? ( 119e650bfb3S猫头猫 <ThemeText 120e650bfb3S猫头猫 fontSize="title" 121e650bfb3S猫头猫 fontWeight="bold" 122e650bfb3S猫头猫 numberOfLines={1} 123e650bfb3S猫头猫 color={ 124e650bfb3S猫头猫 titleTextOpacity !== 1 125e650bfb3S猫头猫 ? color(contentColor) 126e650bfb3S猫头猫 .alpha(titleTextOpacity) 127e650bfb3S猫头猫 .toString() 128e650bfb3S猫头猫 : contentColor 129e650bfb3S猫头猫 }> 130e650bfb3S猫头猫 {children} 131e650bfb3S猫头猫 </ThemeText> 132e650bfb3S猫头猫 ) : ( 133e650bfb3S猫头猫 children 134e650bfb3S猫头猫 )} 135e650bfb3S猫头猫 </View> 136*f643c584S猫头猫 {actions.map((action, index) => ( 137e650bfb3S猫头猫 <IconButton 138*f643c584S猫头猫 key={index} 139e650bfb3S猫头猫 name={action.icon} 140e650bfb3S猫头猫 sizeType="normal" 141e650bfb3S猫头猫 color={contentColor} 142e650bfb3S猫头猫 style={[globalStyle.notShrink, styles.rightButton]} 143e650bfb3S猫头猫 onPress={action.onPress} 144e650bfb3S猫头猫 /> 145e650bfb3S猫头猫 ))} 146a27adc20S猫头猫 {actionComponent ?? null} 147e650bfb3S猫头猫 {menu?.length ? ( 148e650bfb3S猫头猫 <IconButton 149e650bfb3S猫头猫 name="dots-vertical" 150e650bfb3S猫头猫 sizeType="normal" 151e650bfb3S猫头猫 onLayout={e => { 152e650bfb3S猫头猫 menuIconLayoutRef.current = e.nativeEvent.layout; 153e650bfb3S猫头猫 }} 154e650bfb3S猫头猫 color={contentColor} 155e650bfb3S猫头猫 style={[globalStyle.notShrink, styles.rightButton]} 156e650bfb3S猫头猫 onPress={() => { 157e650bfb3S猫头猫 setShowMenu(true); 158e650bfb3S猫头猫 }} 159e650bfb3S猫头猫 /> 160e650bfb3S猫头猫 ) : null} 161e650bfb3S猫头猫 </View> 162e650bfb3S猫头猫 <Portal> 163e650bfb3S猫头猫 {showMenu ? ( 164e650bfb3S猫头猫 <TouchableWithoutFeedback 165e650bfb3S猫头猫 onPress={() => { 166e650bfb3S猫头猫 setShowMenu(false); 167e650bfb3S猫头猫 }}> 168e650bfb3S猫头猫 <View style={styles.blocker} /> 169e650bfb3S猫头猫 </TouchableWithoutFeedback> 170e650bfb3S猫头猫 ) : null} 171e650bfb3S猫头猫 <> 172e650bfb3S猫头猫 <Animated.View 173e650bfb3S猫头猫 pointerEvents={showMenu ? 'auto' : 'none'} 174e650bfb3S猫头猫 style={[ 175e650bfb3S猫头猫 { 176ab55f125S猫头猫 borderBottomColor: colors.background, 177e650bfb3S猫头猫 left: 178e650bfb3S猫头猫 (menuIconLayoutRef.current?.x ?? 0) + 179e650bfb3S猫头猫 (menuIconLayoutRef.current?.width ?? 0) / 180e650bfb3S猫头猫 2 - 181e650bfb3S猫头猫 rpx(10), 182e650bfb3S猫头猫 top: 183e650bfb3S猫头猫 (menuIconLayoutRef.current?.y ?? 0) + 184e650bfb3S猫头猫 (menuIconLayoutRef.current?.height ?? 0) + 185e650bfb3S猫头猫 (menuWithStatusBar 186e650bfb3S猫头猫 ? OriginalStatusBar.currentHeight ?? 0 187e650bfb3S猫头猫 : 0), 188e650bfb3S猫头猫 }, 189e650bfb3S猫头猫 transformStyle, 190e650bfb3S猫头猫 styles.bubbleCorner, 191e650bfb3S猫头猫 ]} 192e650bfb3S猫头猫 /> 193e650bfb3S猫头猫 <Animated.View 194e650bfb3S猫头猫 pointerEvents={showMenu ? 'auto' : 'none'} 195e650bfb3S猫头猫 style={[ 196e650bfb3S猫头猫 { 1976cfecf1cS猫头猫 backgroundColor: colors.background, 198e650bfb3S猫头猫 right: rpx(24), 199e650bfb3S猫头猫 top: 200e650bfb3S猫头猫 (menuIconLayoutRef.current?.y ?? 0) + 201e650bfb3S猫头猫 (menuIconLayoutRef.current?.height ?? 0) + 202e650bfb3S猫头猫 rpx(20) + 203e650bfb3S猫头猫 (menuWithStatusBar 204e650bfb3S猫头猫 ? OriginalStatusBar.currentHeight ?? 0 205e650bfb3S猫头猫 : 0), 2066cfecf1cS猫头猫 shadowColor: colors.shadow, 207e650bfb3S猫头猫 }, 208e650bfb3S猫头猫 transformStyle, 209e650bfb3S猫头猫 styles.menu, 210e650bfb3S猫头猫 ]}> 2117a8d024eS猫头猫 {menu.map(it => 2127a8d024eS猫头猫 it.show !== false ? ( 2131119c2eaS猫头猫 <ListItem 214*f643c584S猫头猫 key={it.title} 2151119c2eaS猫头猫 withHorizonalPadding 2161119c2eaS猫头猫 heightType="small" 217e650bfb3S猫头猫 onPress={() => { 218e650bfb3S猫头猫 it.onPress?.(); 219e650bfb3S猫头猫 setShowMenu(false); 220e650bfb3S猫头猫 }}> 2211119c2eaS猫头猫 <ListItem.ListItemIcon icon={it.icon} /> 2221119c2eaS猫头猫 <ListItem.Content title={it.title} /> 2231119c2eaS猫头猫 </ListItem> 2247a8d024eS猫头猫 ) : null, 2257a8d024eS猫头猫 )} 226e650bfb3S猫头猫 </Animated.View> 227e650bfb3S猫头猫 </> 228e650bfb3S猫头猫 </Portal> 229e650bfb3S猫头猫 </> 230e650bfb3S猫头猫 ); 231e650bfb3S猫头猫} 232e650bfb3S猫头猫 233e650bfb3S猫头猫const styles = StyleSheet.create({ 234e650bfb3S猫头猫 container: { 235e650bfb3S猫头猫 width: '100%', 236e650bfb3S猫头猫 zIndex: 10000, 237e650bfb3S猫头猫 height: rpx(88), 238e650bfb3S猫头猫 flexDirection: 'row', 239e650bfb3S猫头猫 paddingHorizontal: rpx(24), 240e650bfb3S猫头猫 }, 241e650bfb3S猫头猫 content: { 242e650bfb3S猫头猫 flexDirection: 'row', 243e650bfb3S猫头猫 flexBasis: 0, 244e650bfb3S猫头猫 alignItems: 'center', 245e650bfb3S猫头猫 paddingHorizontal: rpx(24), 246e650bfb3S猫头猫 }, 247e650bfb3S猫头猫 rightButton: { 248e650bfb3S猫头猫 marginLeft: rpx(28), 249e650bfb3S猫头猫 }, 250e650bfb3S猫头猫 blocker: { 251e650bfb3S猫头猫 position: 'absolute', 252e650bfb3S猫头猫 bottom: 0, 253e650bfb3S猫头猫 left: 0, 254e650bfb3S猫头猫 width: '100%', 255e650bfb3S猫头猫 height: '100%', 256e650bfb3S猫头猫 zIndex: 10010, 257e650bfb3S猫头猫 }, 258e650bfb3S猫头猫 bubbleCorner: { 259e650bfb3S猫头猫 position: 'absolute', 260e650bfb3S猫头猫 borderColor: 'transparent', 261e650bfb3S猫头猫 borderWidth: rpx(10), 262e650bfb3S猫头猫 zIndex: 10012, 263e650bfb3S猫头猫 transformOrigin: 'right top', 264e650bfb3S猫头猫 opacity: 0, 265e650bfb3S猫头猫 }, 266e650bfb3S猫头猫 menu: { 267e650bfb3S猫头猫 width: rpx(340), 268e650bfb3S猫头猫 maxHeight: rpx(600), 269e650bfb3S猫头猫 borderRadius: rpx(8), 270e650bfb3S猫头猫 zIndex: 10011, 271e650bfb3S猫头猫 position: 'absolute', 272e650bfb3S猫头猫 opacity: 0, 273e650bfb3S猫头猫 shadowOffset: { 274e650bfb3S猫头猫 width: 0, 275e650bfb3S猫头猫 height: 2, 276e650bfb3S猫头猫 }, 277e650bfb3S猫头猫 shadowOpacity: 0.23, 278e650bfb3S猫头猫 shadowRadius: 2.62, 279e650bfb3S猫头猫 elevation: 4, 280e650bfb3S猫头猫 }, 281e650bfb3S猫头猫}); 282