-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathToast.js
More file actions
104 lines (89 loc) · 2.87 KB
/
Toast.js
File metadata and controls
104 lines (89 loc) · 2.87 KB
1
2
3
4
5
6
7
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import React from 'react';
import PropTypes from 'prop-types';
import assign from 'object-assign';
import {defaults} from '../defaults';
import stylesheet from '../stylesheet';
/* React Notification Component */
class Toast extends React.Component {
static propTypes = {
text: PropTypes.oneOfType([
PropTypes.string, PropTypes.element
]),
timeout: PropTypes.number,
type: PropTypes.string,
color: PropTypes.oneOfType([
PropTypes.object,
PropTypes.string
]),
style: PropTypes.oneOfType([
PropTypes.object,
PropTypes.bool
]),
stylesheet: PropTypes.object
};
state = {
containerStyle: this.props.stylesheet?.container ?? stylesheet.styles.container
};
getToastStyle() {
let {type, color, stylesheet: customStyle} = this.props;
let {styles} = {...stylesheet, ...customStyle};
let contentStyle = {};
/* If type is set, merge toast action styles with base */
switch (type) {
case 'success':
case 'error':
case 'warning':
case 'info':
contentStyle = assign({}, styles.content, defaults.colors[type]);
break;
case 'custom': {
const customStyle = {
backgroundColor: color.background,
color: color.text
};
contentStyle = assign({}, styles.content, customStyle);
}
break;
default:
contentStyle = assign({}, styles.content);
break;
}
return contentStyle;
}
animateState() {
let {styles} = {...stylesheet, ...customStyle};
// Show
setTimeout(() => {
this.updateStyle(styles.show);
}, 100); // wait 100ms after the component is called to animate toast.
// Timeout -1 displays toast as a persistent notification
if (this.props.timeout === -1) {
return;
}
// Hide after timeout
setTimeout(() => {
this.updateStyle(styles.hide);
}, this.props.timeout);
}
// Updates the style of the container with styles for a state (hide/show).
// This triggers animations.
updateStyle(stateStyle) {
let {styles} = {...stylesheet, ...customStyle};
this.setState({containerStyle: assign({}, styles.container, stateStyle)});
}
componentDidMount() {
this.animateState();
}
render() {
let {text} = this.props;
let {containerStyle} = this.state;
return (
<div className="toast-notification" style={containerStyle}>
<span style={this.getToastStyle()}>
{text}
</span>
</div>
);
}
}
export default Toast;