xref: /MusicFree/src/components/base/appBar.tsx (revision 1119c2ea435417cd5c53719f91691ff2b1aa8670)
1e650bfb3S猫头猫import React, {ReactNode, useEffect, useRef, useState} from 'react';
2e650bfb3S猫头猫import {
3e650bfb3S猫头猫    LayoutRectangle,
4e650bfb3S猫头猫    StyleSheet,
5e650bfb3S猫头猫    TouchableWithoutFeedback,
6e650bfb3S猫头猫    View,
7e650bfb3S猫头猫    StatusBar as OriginalStatusBar,
8*1119c2eaS猫头猫    StyleProp,
9*1119c2eaS猫头猫    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';
26*1119c2eaS猫头猫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;
45*1119c2eaS猫头猫    containerStyle?: StyleProp<ViewStyle>;
46*1119c2eaS猫头猫    contentStyle?: StyleProp<ViewStyle>;
47e650bfb3S猫头猫}
48e650bfb3S猫头猫
49e650bfb3S猫头猫const ANIMATION_EASING: Animated.EasingFunction = Easing.out(Easing.exp);
50e650bfb3S猫头猫const ANIMATION_DURATION = 500;
51e650bfb3S猫头猫
52e650bfb3S猫头猫const timingConfig = {
53e650bfb3S猫头猫    duration: ANIMATION_DURATION,
54e650bfb3S猫头猫    easing: ANIMATION_EASING,
55e650bfb3S猫头猫};
56e650bfb3S猫头猫
57e650bfb3S猫头猫export default function AppBar(props: IAppBarProps) {
58e650bfb3S猫头猫    const {
59e650bfb3S猫头猫        backgroundOpacity = 1,
60e650bfb3S猫头猫        titleTextOpacity = 1,
61e650bfb3S猫头猫        withStatusBar,
62e650bfb3S猫头猫        color: _color,
63e650bfb3S猫头猫        actions = [],
64e650bfb3S猫头猫        menu = [],
65e650bfb3S猫头猫        menuWithStatusBar = true,
66*1119c2eaS猫头猫        containerStyle,
67*1119c2eaS猫头猫        contentStyle,
68e650bfb3S猫头猫        children,
69e650bfb3S猫头猫    } = props;
70e650bfb3S猫头猫
71e650bfb3S猫头猫    const colors = useColors();
72e650bfb3S猫头猫    const navigation = useNavigation();
73e650bfb3S猫头猫
74e650bfb3S猫头猫    const bgColor = color(colors.primary).alpha(backgroundOpacity).toString();
75e650bfb3S猫头猫    const contentColor = _color ?? colors.headerText;
76e650bfb3S猫头猫
77e650bfb3S猫头猫    const [showMenu, setShowMenu] = useState(false);
78e650bfb3S猫头猫    const menuIconLayoutRef = useRef<LayoutRectangle>();
79e650bfb3S猫头猫    const scaleRate = useSharedValue(0);
80e650bfb3S猫头猫
81e650bfb3S猫头猫    useEffect(() => {
82e650bfb3S猫头猫        if (showMenu) {
83e650bfb3S猫头猫            scaleRate.value = withTiming(1, timingConfig);
84e650bfb3S猫头猫        } else {
85e650bfb3S猫头猫            scaleRate.value = withTiming(0, timingConfig);
86e650bfb3S猫头猫        }
87e650bfb3S猫头猫    }, [showMenu]);
88e650bfb3S猫头猫
89e650bfb3S猫头猫    const transformStyle = useAnimatedStyle(() => {
90e650bfb3S猫头猫        return {
91e650bfb3S猫头猫            opacity: scaleRate.value,
92e650bfb3S猫头猫        };
93e650bfb3S猫头猫    });
94e650bfb3S猫头猫
95e650bfb3S猫头猫    return (
96e650bfb3S猫头猫        <>
97e650bfb3S猫头猫            {withStatusBar ? <StatusBar backgroundColor={bgColor} /> : null}
98*1119c2eaS猫头猫            <View
99*1119c2eaS猫头猫                style={[
100*1119c2eaS猫头猫                    styles.container,
101*1119c2eaS猫头猫                    containerStyle,
102*1119c2eaS猫头猫                    {backgroundColor: bgColor},
103*1119c2eaS猫头猫                ]}>
104e650bfb3S猫头猫                <IconButton
105e650bfb3S猫头猫                    name="arrow-left"
106e650bfb3S猫头猫                    sizeType="normal"
107e650bfb3S猫头猫                    color={contentColor}
108e650bfb3S猫头猫                    style={globalStyle.notShrink}
109e650bfb3S猫头猫                    onPress={() => {
110e650bfb3S猫头猫                        navigation.goBack();
111e650bfb3S猫头猫                    }}
112e650bfb3S猫头猫                />
113*1119c2eaS猫头猫                <View style={[globalStyle.grow, styles.content, contentStyle]}>
114e650bfb3S猫头猫                    {typeof children === 'string' ? (
115e650bfb3S猫头猫                        <ThemeText
116e650bfb3S猫头猫                            fontSize="title"
117e650bfb3S猫头猫                            fontWeight="bold"
118e650bfb3S猫头猫                            numberOfLines={1}
119e650bfb3S猫头猫                            color={
120e650bfb3S猫头猫                                titleTextOpacity !== 1
121e650bfb3S猫头猫                                    ? color(contentColor)
122e650bfb3S猫头猫                                          .alpha(titleTextOpacity)
123e650bfb3S猫头猫                                          .toString()
124e650bfb3S猫头猫                                    : contentColor
125e650bfb3S猫头猫                            }>
126e650bfb3S猫头猫                            {children}
127e650bfb3S猫头猫                        </ThemeText>
128e650bfb3S猫头猫                    ) : (
129e650bfb3S猫头猫                        children
130e650bfb3S猫头猫                    )}
131e650bfb3S猫头猫                </View>
132e650bfb3S猫头猫                {actions.map(action => (
133e650bfb3S猫头猫                    <IconButton
134e650bfb3S猫头猫                        name={action.icon}
135e650bfb3S猫头猫                        sizeType="normal"
136e650bfb3S猫头猫                        color={contentColor}
137e650bfb3S猫头猫                        style={[globalStyle.notShrink, styles.rightButton]}
138e650bfb3S猫头猫                        onPress={action.onPress}
139e650bfb3S猫头猫                    />
140e650bfb3S猫头猫                ))}
141e650bfb3S猫头猫                {menu?.length ? (
142e650bfb3S猫头猫                    <IconButton
143e650bfb3S猫头猫                        name="dots-vertical"
144e650bfb3S猫头猫                        sizeType="normal"
145e650bfb3S猫头猫                        onLayout={e => {
146e650bfb3S猫头猫                            menuIconLayoutRef.current = e.nativeEvent.layout;
147e650bfb3S猫头猫                        }}
148e650bfb3S猫头猫                        color={contentColor}
149e650bfb3S猫头猫                        style={[globalStyle.notShrink, styles.rightButton]}
150e650bfb3S猫头猫                        onPress={() => {
151e650bfb3S猫头猫                            setShowMenu(true);
152e650bfb3S猫头猫                        }}
153e650bfb3S猫头猫                    />
154e650bfb3S猫头猫                ) : null}
155e650bfb3S猫头猫            </View>
156e650bfb3S猫头猫            <Portal>
157e650bfb3S猫头猫                {showMenu ? (
158e650bfb3S猫头猫                    <TouchableWithoutFeedback
159e650bfb3S猫头猫                        onPress={() => {
160e650bfb3S猫头猫                            setShowMenu(false);
161e650bfb3S猫头猫                        }}>
162e650bfb3S猫头猫                        <View style={styles.blocker} />
163e650bfb3S猫头猫                    </TouchableWithoutFeedback>
164e650bfb3S猫头猫                ) : null}
165e650bfb3S猫头猫                <>
166e650bfb3S猫头猫                    <Animated.View
167e650bfb3S猫头猫                        pointerEvents={showMenu ? 'auto' : 'none'}
168e650bfb3S猫头猫                        style={[
169e650bfb3S猫头猫                            {
170e650bfb3S猫头猫                                borderBottomColor: colors.backdrop,
171e650bfb3S猫头猫                                left:
172e650bfb3S猫头猫                                    (menuIconLayoutRef.current?.x ?? 0) +
173e650bfb3S猫头猫                                    (menuIconLayoutRef.current?.width ?? 0) /
174e650bfb3S猫头猫                                        2 -
175e650bfb3S猫头猫                                    rpx(10),
176e650bfb3S猫头猫                                top:
177e650bfb3S猫头猫                                    (menuIconLayoutRef.current?.y ?? 0) +
178e650bfb3S猫头猫                                    (menuIconLayoutRef.current?.height ?? 0) +
179e650bfb3S猫头猫                                    (menuWithStatusBar
180e650bfb3S猫头猫                                        ? OriginalStatusBar.currentHeight ?? 0
181e650bfb3S猫头猫                                        : 0),
182e650bfb3S猫头猫                            },
183e650bfb3S猫头猫                            transformStyle,
184e650bfb3S猫头猫                            styles.bubbleCorner,
185e650bfb3S猫头猫                        ]}
186e650bfb3S猫头猫                    />
187e650bfb3S猫头猫                    <Animated.View
188e650bfb3S猫头猫                        pointerEvents={showMenu ? 'auto' : 'none'}
189e650bfb3S猫头猫                        style={[
190e650bfb3S猫头猫                            {
191e650bfb3S猫头猫                                backgroundColor: colors.headerText,
192e650bfb3S猫头猫                                right: rpx(24),
193e650bfb3S猫头猫                                top:
194e650bfb3S猫头猫                                    (menuIconLayoutRef.current?.y ?? 0) +
195e650bfb3S猫头猫                                    (menuIconLayoutRef.current?.height ?? 0) +
196e650bfb3S猫头猫                                    rpx(20) +
197e650bfb3S猫头猫                                    (menuWithStatusBar
198e650bfb3S猫头猫                                        ? OriginalStatusBar.currentHeight ?? 0
199e650bfb3S猫头猫                                        : 0),
200e650bfb3S猫头猫                            },
201e650bfb3S猫头猫                            transformStyle,
202e650bfb3S猫头猫                            styles.menu,
203e650bfb3S猫头猫                        ]}>
2047a8d024eS猫头猫                        {menu.map(it =>
2057a8d024eS猫头猫                            it.show !== false ? (
206*1119c2eaS猫头猫                                <ListItem
207*1119c2eaS猫头猫                                    withHorizonalPadding
208*1119c2eaS猫头猫                                    heightType="small"
209e650bfb3S猫头猫                                    onPress={() => {
210e650bfb3S猫头猫                                        it.onPress?.();
211e650bfb3S猫头猫                                        setShowMenu(false);
212e650bfb3S猫头猫                                    }}>
213*1119c2eaS猫头猫                                    <ListItem.ListItemIcon icon={it.icon} />
214*1119c2eaS猫头猫                                    <ListItem.Content title={it.title} />
215*1119c2eaS猫头猫                                </ListItem>
2167a8d024eS猫头猫                            ) : null,
2177a8d024eS猫头猫                        )}
218e650bfb3S猫头猫                    </Animated.View>
219e650bfb3S猫头猫                </>
220e650bfb3S猫头猫            </Portal>
221e650bfb3S猫头猫        </>
222e650bfb3S猫头猫    );
223e650bfb3S猫头猫}
224e650bfb3S猫头猫
225e650bfb3S猫头猫const styles = StyleSheet.create({
226e650bfb3S猫头猫    container: {
227e650bfb3S猫头猫        width: '100%',
228e650bfb3S猫头猫        zIndex: 10000,
229e650bfb3S猫头猫        height: rpx(88),
230e650bfb3S猫头猫        flexDirection: 'row',
231e650bfb3S猫头猫        paddingHorizontal: rpx(24),
232e650bfb3S猫头猫    },
233e650bfb3S猫头猫    content: {
234e650bfb3S猫头猫        flexDirection: 'row',
235e650bfb3S猫头猫        flexBasis: 0,
236e650bfb3S猫头猫        alignItems: 'center',
237e650bfb3S猫头猫        paddingHorizontal: rpx(24),
238e650bfb3S猫头猫    },
239e650bfb3S猫头猫    rightButton: {
240e650bfb3S猫头猫        marginLeft: rpx(28),
241e650bfb3S猫头猫    },
242e650bfb3S猫头猫    blocker: {
243e650bfb3S猫头猫        position: 'absolute',
244e650bfb3S猫头猫        bottom: 0,
245e650bfb3S猫头猫        left: 0,
246e650bfb3S猫头猫        width: '100%',
247e650bfb3S猫头猫        height: '100%',
248e650bfb3S猫头猫        zIndex: 10010,
249e650bfb3S猫头猫    },
250e650bfb3S猫头猫    bubbleCorner: {
251e650bfb3S猫头猫        position: 'absolute',
252e650bfb3S猫头猫        borderColor: 'transparent',
253e650bfb3S猫头猫        borderWidth: rpx(10),
254e650bfb3S猫头猫        zIndex: 10012,
255e650bfb3S猫头猫        transformOrigin: 'right top',
256e650bfb3S猫头猫        opacity: 0,
257e650bfb3S猫头猫    },
258e650bfb3S猫头猫    menu: {
259e650bfb3S猫头猫        width: rpx(340),
260e650bfb3S猫头猫        maxHeight: rpx(600),
261e650bfb3S猫头猫        borderRadius: rpx(8),
262e650bfb3S猫头猫        zIndex: 10011,
263e650bfb3S猫头猫        position: 'absolute',
264e650bfb3S猫头猫        opacity: 0,
265e650bfb3S猫头猫        shadowColor: '#000',
266e650bfb3S猫头猫        shadowOffset: {
267e650bfb3S猫头猫            width: 0,
268e650bfb3S猫头猫            height: 2,
269e650bfb3S猫头猫        },
270e650bfb3S猫头猫        shadowOpacity: 0.23,
271e650bfb3S猫头猫        shadowRadius: 2.62,
272e650bfb3S猫头猫        elevation: 4,
273e650bfb3S猫头猫    },
274e650bfb3S猫头猫});
275