xref: /MusicFree/src/components/base/appBar.tsx (revision ab55f125072c3b77549324c638fbca1fe4561337)
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();
79e650bfb3S猫头猫    const contentColor = _color ?? colors.headerText;
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>
136e650bfb3S猫头猫                {actions.map(action => (
137e650bfb3S猫头猫                    <IconButton
138e650bfb3S猫头猫                        name={action.icon}
139e650bfb3S猫头猫                        sizeType="normal"
140e650bfb3S猫头猫                        color={contentColor}
141e650bfb3S猫头猫                        style={[globalStyle.notShrink, styles.rightButton]}
142e650bfb3S猫头猫                        onPress={action.onPress}
143e650bfb3S猫头猫                    />
144e650bfb3S猫头猫                ))}
145a27adc20S猫头猫                {actionComponent ?? null}
146e650bfb3S猫头猫                {menu?.length ? (
147e650bfb3S猫头猫                    <IconButton
148e650bfb3S猫头猫                        name="dots-vertical"
149e650bfb3S猫头猫                        sizeType="normal"
150e650bfb3S猫头猫                        onLayout={e => {
151e650bfb3S猫头猫                            menuIconLayoutRef.current = e.nativeEvent.layout;
152e650bfb3S猫头猫                        }}
153e650bfb3S猫头猫                        color={contentColor}
154e650bfb3S猫头猫                        style={[globalStyle.notShrink, styles.rightButton]}
155e650bfb3S猫头猫                        onPress={() => {
156e650bfb3S猫头猫                            setShowMenu(true);
157e650bfb3S猫头猫                        }}
158e650bfb3S猫头猫                    />
159e650bfb3S猫头猫                ) : null}
160e650bfb3S猫头猫            </View>
161e650bfb3S猫头猫            <Portal>
162e650bfb3S猫头猫                {showMenu ? (
163e650bfb3S猫头猫                    <TouchableWithoutFeedback
164e650bfb3S猫头猫                        onPress={() => {
165e650bfb3S猫头猫                            setShowMenu(false);
166e650bfb3S猫头猫                        }}>
167e650bfb3S猫头猫                        <View style={styles.blocker} />
168e650bfb3S猫头猫                    </TouchableWithoutFeedback>
169e650bfb3S猫头猫                ) : null}
170e650bfb3S猫头猫                <>
171e650bfb3S猫头猫                    <Animated.View
172e650bfb3S猫头猫                        pointerEvents={showMenu ? 'auto' : 'none'}
173e650bfb3S猫头猫                        style={[
174e650bfb3S猫头猫                            {
175*ab55f125S猫头猫                                borderBottomColor: colors.background,
176e650bfb3S猫头猫                                left:
177e650bfb3S猫头猫                                    (menuIconLayoutRef.current?.x ?? 0) +
178e650bfb3S猫头猫                                    (menuIconLayoutRef.current?.width ?? 0) /
179e650bfb3S猫头猫                                        2 -
180e650bfb3S猫头猫                                    rpx(10),
181e650bfb3S猫头猫                                top:
182e650bfb3S猫头猫                                    (menuIconLayoutRef.current?.y ?? 0) +
183e650bfb3S猫头猫                                    (menuIconLayoutRef.current?.height ?? 0) +
184e650bfb3S猫头猫                                    (menuWithStatusBar
185e650bfb3S猫头猫                                        ? OriginalStatusBar.currentHeight ?? 0
186e650bfb3S猫头猫                                        : 0),
187e650bfb3S猫头猫                            },
188e650bfb3S猫头猫                            transformStyle,
189e650bfb3S猫头猫                            styles.bubbleCorner,
190e650bfb3S猫头猫                        ]}
191e650bfb3S猫头猫                    />
192e650bfb3S猫头猫                    <Animated.View
193e650bfb3S猫头猫                        pointerEvents={showMenu ? 'auto' : 'none'}
194e650bfb3S猫头猫                        style={[
195e650bfb3S猫头猫                            {
1966cfecf1cS猫头猫                                backgroundColor: colors.background,
197e650bfb3S猫头猫                                right: rpx(24),
198e650bfb3S猫头猫                                top:
199e650bfb3S猫头猫                                    (menuIconLayoutRef.current?.y ?? 0) +
200e650bfb3S猫头猫                                    (menuIconLayoutRef.current?.height ?? 0) +
201e650bfb3S猫头猫                                    rpx(20) +
202e650bfb3S猫头猫                                    (menuWithStatusBar
203e650bfb3S猫头猫                                        ? OriginalStatusBar.currentHeight ?? 0
204e650bfb3S猫头猫                                        : 0),
2056cfecf1cS猫头猫                                shadowColor: colors.shadow,
206e650bfb3S猫头猫                            },
207e650bfb3S猫头猫                            transformStyle,
208e650bfb3S猫头猫                            styles.menu,
209e650bfb3S猫头猫                        ]}>
2107a8d024eS猫头猫                        {menu.map(it =>
2117a8d024eS猫头猫                            it.show !== false ? (
2121119c2eaS猫头猫                                <ListItem
2131119c2eaS猫头猫                                    withHorizonalPadding
2141119c2eaS猫头猫                                    heightType="small"
215e650bfb3S猫头猫                                    onPress={() => {
216e650bfb3S猫头猫                                        it.onPress?.();
217e650bfb3S猫头猫                                        setShowMenu(false);
218e650bfb3S猫头猫                                    }}>
2191119c2eaS猫头猫                                    <ListItem.ListItemIcon icon={it.icon} />
2201119c2eaS猫头猫                                    <ListItem.Content title={it.title} />
2211119c2eaS猫头猫                                </ListItem>
2227a8d024eS猫头猫                            ) : null,
2237a8d024eS猫头猫                        )}
224e650bfb3S猫头猫                    </Animated.View>
225e650bfb3S猫头猫                </>
226e650bfb3S猫头猫            </Portal>
227e650bfb3S猫头猫        </>
228e650bfb3S猫头猫    );
229e650bfb3S猫头猫}
230e650bfb3S猫头猫
231e650bfb3S猫头猫const styles = StyleSheet.create({
232e650bfb3S猫头猫    container: {
233e650bfb3S猫头猫        width: '100%',
234e650bfb3S猫头猫        zIndex: 10000,
235e650bfb3S猫头猫        height: rpx(88),
236e650bfb3S猫头猫        flexDirection: 'row',
237e650bfb3S猫头猫        paddingHorizontal: rpx(24),
238e650bfb3S猫头猫    },
239e650bfb3S猫头猫    content: {
240e650bfb3S猫头猫        flexDirection: 'row',
241e650bfb3S猫头猫        flexBasis: 0,
242e650bfb3S猫头猫        alignItems: 'center',
243e650bfb3S猫头猫        paddingHorizontal: rpx(24),
244e650bfb3S猫头猫    },
245e650bfb3S猫头猫    rightButton: {
246e650bfb3S猫头猫        marginLeft: rpx(28),
247e650bfb3S猫头猫    },
248e650bfb3S猫头猫    blocker: {
249e650bfb3S猫头猫        position: 'absolute',
250e650bfb3S猫头猫        bottom: 0,
251e650bfb3S猫头猫        left: 0,
252e650bfb3S猫头猫        width: '100%',
253e650bfb3S猫头猫        height: '100%',
254e650bfb3S猫头猫        zIndex: 10010,
255e650bfb3S猫头猫    },
256e650bfb3S猫头猫    bubbleCorner: {
257e650bfb3S猫头猫        position: 'absolute',
258e650bfb3S猫头猫        borderColor: 'transparent',
259e650bfb3S猫头猫        borderWidth: rpx(10),
260e650bfb3S猫头猫        zIndex: 10012,
261e650bfb3S猫头猫        transformOrigin: 'right top',
262e650bfb3S猫头猫        opacity: 0,
263e650bfb3S猫头猫    },
264e650bfb3S猫头猫    menu: {
265e650bfb3S猫头猫        width: rpx(340),
266e650bfb3S猫头猫        maxHeight: rpx(600),
267e650bfb3S猫头猫        borderRadius: rpx(8),
268e650bfb3S猫头猫        zIndex: 10011,
269e650bfb3S猫头猫        position: 'absolute',
270e650bfb3S猫头猫        opacity: 0,
271e650bfb3S猫头猫        shadowOffset: {
272e650bfb3S猫头猫            width: 0,
273e650bfb3S猫头猫            height: 2,
274e650bfb3S猫头猫        },
275e650bfb3S猫头猫        shadowOpacity: 0.23,
276e650bfb3S猫头猫        shadowRadius: 2.62,
277e650bfb3S猫头猫        elevation: 4,
278e650bfb3S猫头猫    },
279e650bfb3S猫头猫});
280