xref: /MusicFree/src/components/base/appBar.tsx (revision 6cfecf1cdd150fc94c5ad42fede7d65068b9ea40)
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>;
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,
661119c2eaS猫头猫        containerStyle,
671119c2eaS猫头猫        contentStyle,
68e650bfb3S猫头猫        children,
69e650bfb3S猫头猫    } = props;
70e650bfb3S猫头猫
71e650bfb3S猫头猫    const colors = useColors();
72e650bfb3S猫头猫    const navigation = useNavigation();
73e650bfb3S猫头猫
74*6cfecf1cS猫头猫    const bgColor = color(colors.appBar ?? colors.primary)
75*6cfecf1cS猫头猫        .alpha(backgroundOpacity)
76*6cfecf1cS猫头猫        .toString();
77e650bfb3S猫头猫    const contentColor = _color ?? colors.headerText;
78e650bfb3S猫头猫
79e650bfb3S猫头猫    const [showMenu, setShowMenu] = useState(false);
80e650bfb3S猫头猫    const menuIconLayoutRef = useRef<LayoutRectangle>();
81e650bfb3S猫头猫    const scaleRate = useSharedValue(0);
82e650bfb3S猫头猫
83e650bfb3S猫头猫    useEffect(() => {
84e650bfb3S猫头猫        if (showMenu) {
85e650bfb3S猫头猫            scaleRate.value = withTiming(1, timingConfig);
86e650bfb3S猫头猫        } else {
87e650bfb3S猫头猫            scaleRate.value = withTiming(0, timingConfig);
88e650bfb3S猫头猫        }
89e650bfb3S猫头猫    }, [showMenu]);
90e650bfb3S猫头猫
91e650bfb3S猫头猫    const transformStyle = useAnimatedStyle(() => {
92e650bfb3S猫头猫        return {
93e650bfb3S猫头猫            opacity: scaleRate.value,
94e650bfb3S猫头猫        };
95e650bfb3S猫头猫    });
96e650bfb3S猫头猫
97e650bfb3S猫头猫    return (
98e650bfb3S猫头猫        <>
99e650bfb3S猫头猫            {withStatusBar ? <StatusBar backgroundColor={bgColor} /> : null}
1001119c2eaS猫头猫            <View
1011119c2eaS猫头猫                style={[
1021119c2eaS猫头猫                    styles.container,
1031119c2eaS猫头猫                    containerStyle,
1041119c2eaS猫头猫                    {backgroundColor: bgColor},
1051119c2eaS猫头猫                ]}>
106e650bfb3S猫头猫                <IconButton
107e650bfb3S猫头猫                    name="arrow-left"
108e650bfb3S猫头猫                    sizeType="normal"
109e650bfb3S猫头猫                    color={contentColor}
110e650bfb3S猫头猫                    style={globalStyle.notShrink}
111e650bfb3S猫头猫                    onPress={() => {
112e650bfb3S猫头猫                        navigation.goBack();
113e650bfb3S猫头猫                    }}
114e650bfb3S猫头猫                />
1151119c2eaS猫头猫                <View style={[globalStyle.grow, styles.content, contentStyle]}>
116e650bfb3S猫头猫                    {typeof children === 'string' ? (
117e650bfb3S猫头猫                        <ThemeText
118e650bfb3S猫头猫                            fontSize="title"
119e650bfb3S猫头猫                            fontWeight="bold"
120e650bfb3S猫头猫                            numberOfLines={1}
121e650bfb3S猫头猫                            color={
122e650bfb3S猫头猫                                titleTextOpacity !== 1
123e650bfb3S猫头猫                                    ? color(contentColor)
124e650bfb3S猫头猫                                          .alpha(titleTextOpacity)
125e650bfb3S猫头猫                                          .toString()
126e650bfb3S猫头猫                                    : contentColor
127e650bfb3S猫头猫                            }>
128e650bfb3S猫头猫                            {children}
129e650bfb3S猫头猫                        </ThemeText>
130e650bfb3S猫头猫                    ) : (
131e650bfb3S猫头猫                        children
132e650bfb3S猫头猫                    )}
133e650bfb3S猫头猫                </View>
134e650bfb3S猫头猫                {actions.map(action => (
135e650bfb3S猫头猫                    <IconButton
136e650bfb3S猫头猫                        name={action.icon}
137e650bfb3S猫头猫                        sizeType="normal"
138e650bfb3S猫头猫                        color={contentColor}
139e650bfb3S猫头猫                        style={[globalStyle.notShrink, styles.rightButton]}
140e650bfb3S猫头猫                        onPress={action.onPress}
141e650bfb3S猫头猫                    />
142e650bfb3S猫头猫                ))}
143e650bfb3S猫头猫                {menu?.length ? (
144e650bfb3S猫头猫                    <IconButton
145e650bfb3S猫头猫                        name="dots-vertical"
146e650bfb3S猫头猫                        sizeType="normal"
147e650bfb3S猫头猫                        onLayout={e => {
148e650bfb3S猫头猫                            menuIconLayoutRef.current = e.nativeEvent.layout;
149e650bfb3S猫头猫                        }}
150e650bfb3S猫头猫                        color={contentColor}
151e650bfb3S猫头猫                        style={[globalStyle.notShrink, styles.rightButton]}
152e650bfb3S猫头猫                        onPress={() => {
153e650bfb3S猫头猫                            setShowMenu(true);
154e650bfb3S猫头猫                        }}
155e650bfb3S猫头猫                    />
156e650bfb3S猫头猫                ) : null}
157e650bfb3S猫头猫            </View>
158e650bfb3S猫头猫            <Portal>
159e650bfb3S猫头猫                {showMenu ? (
160e650bfb3S猫头猫                    <TouchableWithoutFeedback
161e650bfb3S猫头猫                        onPress={() => {
162e650bfb3S猫头猫                            setShowMenu(false);
163e650bfb3S猫头猫                        }}>
164e650bfb3S猫头猫                        <View style={styles.blocker} />
165e650bfb3S猫头猫                    </TouchableWithoutFeedback>
166e650bfb3S猫头猫                ) : null}
167e650bfb3S猫头猫                <>
168e650bfb3S猫头猫                    <Animated.View
169e650bfb3S猫头猫                        pointerEvents={showMenu ? 'auto' : 'none'}
170e650bfb3S猫头猫                        style={[
171e650bfb3S猫头猫                            {
172e650bfb3S猫头猫                                borderBottomColor: colors.backdrop,
173e650bfb3S猫头猫                                left:
174e650bfb3S猫头猫                                    (menuIconLayoutRef.current?.x ?? 0) +
175e650bfb3S猫头猫                                    (menuIconLayoutRef.current?.width ?? 0) /
176e650bfb3S猫头猫                                        2 -
177e650bfb3S猫头猫                                    rpx(10),
178e650bfb3S猫头猫                                top:
179e650bfb3S猫头猫                                    (menuIconLayoutRef.current?.y ?? 0) +
180e650bfb3S猫头猫                                    (menuIconLayoutRef.current?.height ?? 0) +
181e650bfb3S猫头猫                                    (menuWithStatusBar
182e650bfb3S猫头猫                                        ? OriginalStatusBar.currentHeight ?? 0
183e650bfb3S猫头猫                                        : 0),
184e650bfb3S猫头猫                            },
185e650bfb3S猫头猫                            transformStyle,
186e650bfb3S猫头猫                            styles.bubbleCorner,
187e650bfb3S猫头猫                        ]}
188e650bfb3S猫头猫                    />
189e650bfb3S猫头猫                    <Animated.View
190e650bfb3S猫头猫                        pointerEvents={showMenu ? 'auto' : 'none'}
191e650bfb3S猫头猫                        style={[
192e650bfb3S猫头猫                            {
193*6cfecf1cS猫头猫                                backgroundColor: colors.background,
194e650bfb3S猫头猫                                right: rpx(24),
195e650bfb3S猫头猫                                top:
196e650bfb3S猫头猫                                    (menuIconLayoutRef.current?.y ?? 0) +
197e650bfb3S猫头猫                                    (menuIconLayoutRef.current?.height ?? 0) +
198e650bfb3S猫头猫                                    rpx(20) +
199e650bfb3S猫头猫                                    (menuWithStatusBar
200e650bfb3S猫头猫                                        ? OriginalStatusBar.currentHeight ?? 0
201e650bfb3S猫头猫                                        : 0),
202*6cfecf1cS猫头猫                                shadowColor: colors.shadow,
203e650bfb3S猫头猫                            },
204e650bfb3S猫头猫                            transformStyle,
205e650bfb3S猫头猫                            styles.menu,
206e650bfb3S猫头猫                        ]}>
2077a8d024eS猫头猫                        {menu.map(it =>
2087a8d024eS猫头猫                            it.show !== false ? (
2091119c2eaS猫头猫                                <ListItem
2101119c2eaS猫头猫                                    withHorizonalPadding
2111119c2eaS猫头猫                                    heightType="small"
212e650bfb3S猫头猫                                    onPress={() => {
213e650bfb3S猫头猫                                        it.onPress?.();
214e650bfb3S猫头猫                                        setShowMenu(false);
215e650bfb3S猫头猫                                    }}>
2161119c2eaS猫头猫                                    <ListItem.ListItemIcon icon={it.icon} />
2171119c2eaS猫头猫                                    <ListItem.Content title={it.title} />
2181119c2eaS猫头猫                                </ListItem>
2197a8d024eS猫头猫                            ) : null,
2207a8d024eS猫头猫                        )}
221e650bfb3S猫头猫                    </Animated.View>
222e650bfb3S猫头猫                </>
223e650bfb3S猫头猫            </Portal>
224e650bfb3S猫头猫        </>
225e650bfb3S猫头猫    );
226e650bfb3S猫头猫}
227e650bfb3S猫头猫
228e650bfb3S猫头猫const styles = StyleSheet.create({
229e650bfb3S猫头猫    container: {
230e650bfb3S猫头猫        width: '100%',
231e650bfb3S猫头猫        zIndex: 10000,
232e650bfb3S猫头猫        height: rpx(88),
233e650bfb3S猫头猫        flexDirection: 'row',
234e650bfb3S猫头猫        paddingHorizontal: rpx(24),
235e650bfb3S猫头猫    },
236e650bfb3S猫头猫    content: {
237e650bfb3S猫头猫        flexDirection: 'row',
238e650bfb3S猫头猫        flexBasis: 0,
239e650bfb3S猫头猫        alignItems: 'center',
240e650bfb3S猫头猫        paddingHorizontal: rpx(24),
241e650bfb3S猫头猫    },
242e650bfb3S猫头猫    rightButton: {
243e650bfb3S猫头猫        marginLeft: rpx(28),
244e650bfb3S猫头猫    },
245e650bfb3S猫头猫    blocker: {
246e650bfb3S猫头猫        position: 'absolute',
247e650bfb3S猫头猫        bottom: 0,
248e650bfb3S猫头猫        left: 0,
249e650bfb3S猫头猫        width: '100%',
250e650bfb3S猫头猫        height: '100%',
251e650bfb3S猫头猫        zIndex: 10010,
252e650bfb3S猫头猫    },
253e650bfb3S猫头猫    bubbleCorner: {
254e650bfb3S猫头猫        position: 'absolute',
255e650bfb3S猫头猫        borderColor: 'transparent',
256e650bfb3S猫头猫        borderWidth: rpx(10),
257e650bfb3S猫头猫        zIndex: 10012,
258e650bfb3S猫头猫        transformOrigin: 'right top',
259e650bfb3S猫头猫        opacity: 0,
260e650bfb3S猫头猫    },
261e650bfb3S猫头猫    menu: {
262e650bfb3S猫头猫        width: rpx(340),
263e650bfb3S猫头猫        maxHeight: rpx(600),
264e650bfb3S猫头猫        borderRadius: rpx(8),
265e650bfb3S猫头猫        zIndex: 10011,
266e650bfb3S猫头猫        position: 'absolute',
267e650bfb3S猫头猫        opacity: 0,
268e650bfb3S猫头猫        shadowOffset: {
269e650bfb3S猫头猫            width: 0,
270e650bfb3S猫头猫            height: 2,
271e650bfb3S猫头猫        },
272e650bfb3S猫头猫        shadowOpacity: 0.23,
273e650bfb3S猫头猫        shadowRadius: 2.62,
274e650bfb3S猫头猫        elevation: 4,
275e650bfb3S猫头猫    },
276e650bfb3S猫头猫});
277