xref: /MusicFree/src/components/base/appBar.tsx (revision f2a4767ceca3f373f366c86f5089baa71d86479d)
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猫头猫    titleTextOpacity?: number;
30e650bfb3S猫头猫    withStatusBar?: boolean;
31e650bfb3S猫头猫    color?: string;
32e650bfb3S猫头猫    actions?: Array<{
33e650bfb3S猫头猫        icon: string;
34e650bfb3S猫头猫        onPress?: () => void;
35e650bfb3S猫头猫    }>;
36e650bfb3S猫头猫    menu?: Array<{
37e650bfb3S猫头猫        icon: string;
38e650bfb3S猫头猫        title: string;
397a8d024eS猫头猫        show?: boolean;
40e650bfb3S猫头猫        onPress?: () => void;
41e650bfb3S猫头猫    }>;
42e650bfb3S猫头猫    menuWithStatusBar?: boolean;
43e650bfb3S猫头猫    children?: string | ReactNode;
441119c2eaS猫头猫    containerStyle?: StyleProp<ViewStyle>;
451119c2eaS猫头猫    contentStyle?: StyleProp<ViewStyle>;
46a27adc20S猫头猫    actionComponent?: ReactNode;
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猫头猫        titleTextOpacity = 1,
60e650bfb3S猫头猫        withStatusBar,
61e650bfb3S猫头猫        color: _color,
62e650bfb3S猫头猫        actions = [],
63e650bfb3S猫头猫        menu = [],
64e650bfb3S猫头猫        menuWithStatusBar = true,
651119c2eaS猫头猫        containerStyle,
661119c2eaS猫头猫        contentStyle,
67e650bfb3S猫头猫        children,
68a27adc20S猫头猫        actionComponent,
69e650bfb3S猫头猫    } = props;
70e650bfb3S猫头猫
71e650bfb3S猫头猫    const colors = useColors();
72e650bfb3S猫头猫    const navigation = useNavigation();
73e650bfb3S猫头猫
74*f2a4767cS猫头猫    const bgColor = color(colors.appBar ?? colors.primary).toString();
75277c5280S猫头猫    const contentColor = _color ?? colors.appBarText;
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}
981119c2eaS猫头猫            <View
991119c2eaS猫头猫                style={[
1001119c2eaS猫头猫                    styles.container,
1011119c2eaS猫头猫                    containerStyle,
1021119c2eaS猫头猫                    {backgroundColor: bgColor},
1031119c2eaS猫头猫                ]}>
104e650bfb3S猫头猫                <IconButton
105e650bfb3S猫头猫                    name="arrow-left"
106e650bfb3S猫头猫                    sizeType="normal"
107e650bfb3S猫头猫                    color={contentColor}
108e650bfb3S猫头猫                    style={globalStyle.notShrink}
109e650bfb3S猫头猫                    onPress={() => {
110e650bfb3S猫头猫                        navigation.goBack();
111e650bfb3S猫头猫                    }}
112e650bfb3S猫头猫                />
1131119c2eaS猫头猫                <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>
132f643c584S猫头猫                {actions.map((action, index) => (
133e650bfb3S猫头猫                    <IconButton
134f643c584S猫头猫                        key={index}
135e650bfb3S猫头猫                        name={action.icon}
136e650bfb3S猫头猫                        sizeType="normal"
137e650bfb3S猫头猫                        color={contentColor}
138e650bfb3S猫头猫                        style={[globalStyle.notShrink, styles.rightButton]}
139e650bfb3S猫头猫                        onPress={action.onPress}
140e650bfb3S猫头猫                    />
141e650bfb3S猫头猫                ))}
142a27adc20S猫头猫                {actionComponent ?? null}
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猫头猫                            {
172ab55f125S猫头猫                                borderBottomColor: colors.background,
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猫头猫                            {
1936cfecf1cS猫头猫                                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),
2026cfecf1cS猫头猫                                shadowColor: colors.shadow,
203e650bfb3S猫头猫                            },
204e650bfb3S猫头猫                            transformStyle,
205e650bfb3S猫头猫                            styles.menu,
206e650bfb3S猫头猫                        ]}>
2077a8d024eS猫头猫                        {menu.map(it =>
2087a8d024eS猫头猫                            it.show !== false ? (
2091119c2eaS猫头猫                                <ListItem
210f643c584S猫头猫                                    key={it.title}
2111119c2eaS猫头猫                                    withHorizonalPadding
2121119c2eaS猫头猫                                    heightType="small"
213e650bfb3S猫头猫                                    onPress={() => {
214e650bfb3S猫头猫                                        it.onPress?.();
215e650bfb3S猫头猫                                        setShowMenu(false);
216e650bfb3S猫头猫                                    }}>
2171119c2eaS猫头猫                                    <ListItem.ListItemIcon icon={it.icon} />
2181119c2eaS猫头猫                                    <ListItem.Content title={it.title} />
2191119c2eaS猫头猫                                </ListItem>
2207a8d024eS猫头猫                            ) : null,
2217a8d024eS猫头猫                        )}
222e650bfb3S猫头猫                    </Animated.View>
223e650bfb3S猫头猫                </>
224e650bfb3S猫头猫            </Portal>
225e650bfb3S猫头猫        </>
226e650bfb3S猫头猫    );
227e650bfb3S猫头猫}
228e650bfb3S猫头猫
229e650bfb3S猫头猫const styles = StyleSheet.create({
230e650bfb3S猫头猫    container: {
231e650bfb3S猫头猫        width: '100%',
232e650bfb3S猫头猫        zIndex: 10000,
233e650bfb3S猫头猫        height: rpx(88),
234e650bfb3S猫头猫        flexDirection: 'row',
235e650bfb3S猫头猫        paddingHorizontal: rpx(24),
236e650bfb3S猫头猫    },
237e650bfb3S猫头猫    content: {
238e650bfb3S猫头猫        flexDirection: 'row',
239e650bfb3S猫头猫        flexBasis: 0,
240e650bfb3S猫头猫        alignItems: 'center',
241e650bfb3S猫头猫        paddingHorizontal: rpx(24),
242e650bfb3S猫头猫    },
243e650bfb3S猫头猫    rightButton: {
244e650bfb3S猫头猫        marginLeft: rpx(28),
245e650bfb3S猫头猫    },
246e650bfb3S猫头猫    blocker: {
247e650bfb3S猫头猫        position: 'absolute',
248e650bfb3S猫头猫        bottom: 0,
249e650bfb3S猫头猫        left: 0,
250e650bfb3S猫头猫        width: '100%',
251e650bfb3S猫头猫        height: '100%',
252e650bfb3S猫头猫        zIndex: 10010,
253e650bfb3S猫头猫    },
254e650bfb3S猫头猫    bubbleCorner: {
255e650bfb3S猫头猫        position: 'absolute',
256e650bfb3S猫头猫        borderColor: 'transparent',
257e650bfb3S猫头猫        borderWidth: rpx(10),
258e650bfb3S猫头猫        zIndex: 10012,
259e650bfb3S猫头猫        transformOrigin: 'right top',
260e650bfb3S猫头猫        opacity: 0,
261e650bfb3S猫头猫    },
262e650bfb3S猫头猫    menu: {
263e650bfb3S猫头猫        width: rpx(340),
264e650bfb3S猫头猫        maxHeight: rpx(600),
265e650bfb3S猫头猫        borderRadius: rpx(8),
266e650bfb3S猫头猫        zIndex: 10011,
267e650bfb3S猫头猫        position: 'absolute',
268e650bfb3S猫头猫        opacity: 0,
269e650bfb3S猫头猫        shadowOffset: {
270e650bfb3S猫头猫            width: 0,
271e650bfb3S猫头猫            height: 2,
272e650bfb3S猫头猫        },
273e650bfb3S猫头猫        shadowOpacity: 0.23,
274e650bfb3S猫头猫        shadowRadius: 2.62,
275e650bfb3S猫头猫        elevation: 4,
276e650bfb3S猫头猫    },
277e650bfb3S猫头猫});
278