Notification.js 2.36 KB
Newer Older
1
2
3
import React, { useEffect, useRef } from 'react';
import { Animated, Text, View, StyleSheet } from 'react-native';

Soo Hyun Kim's avatar
Soo Hyun Kim committed
4
const NotificationBox = (props) => {
5
6
7
    const opacity = useRef(new Animated.Value(0)).current;

    useEffect(() => {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
        Animated.sequence([
            Animated.timing(opacity, {
                toValue: 1,
                duration: 500,
                useNativeDriver: true,
            }),
            Animated.delay(2500),
            Animated.timing(opacity, {
                toValue: 0,
                duration: 500,
                useNativeDriver: true,
            }),
        ]).start(() => {
            props.onHide();
        })
    }, [])

    return (
        <Animated.View
            style={[
                {
                    opacity: opacity
                },
                {
                    transform: [{
                        translateY: opacity.interpolate({
                            inputRange: [0, 1],
                            outputRange: [-20, 0],
                        })
                    }],
                },
                style.msgBox
            ]}
        >
            <Text style={style.textStyle}>
                {props.message}
            </Text>
        </Animated.View>
    );
};
48

Soo Hyun Kim's avatar
Soo Hyun Kim committed
49
const Notification = (props) => {
50
51
52
53
54
55
56
    return (
        <View style={{
            position: 'absolute',
            top: 0,
            left: 0,
            right: 0,
        }}>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
            {console.log(props.notification)}
            {props.notification.map((notificationMsg) => (
                <NotificationBox
                    key={notificationMsg}
                    message={notificationMsg}
                    onHide={() => {
                        props.setNotification((prev) =>
                        prev.filter((currentNotification) => currentNotification !== notificationMsg )
                    )
                    }}
                />
            ))}
        </View>
    )
}
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

const style = StyleSheet.create({
    msgBox: {
        margin: 10,
        marginBottom: 5,
        backgroundColor: 'white',
        padding: 15,
        borderRadius: 4,
        shadowColor: 'black',
        shadowOffset: {
            width: 0,
            height: 3,
        },
        shadowOpacity: 0.3,
        shadowRadius: 5,
        elevation: 6,
    },
    textStyle: {
        fontSize: 20
    }
})

export default Notification