-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Toasts.js
116 lines (106 loc) · 2.51 KB
/
Toasts.js
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
105
106
107
108
109
110
111
112
113
114
115
116
import React from 'react'
function Toast(props) {
const [display, on] = React.useState(true)
function off() {
return on(false)
}
React.useEffect(() => {
if (props.timeout) {
const to = setTimeout(off, props.timeout)
return () => clearTimeout(to)
}
}, [props.timeout])
return (
<div className={`toast mb2 ${display ? '' : 'out'}`}>
<div className="toast-content">
{props.children}
{props.closable && (
<button className="close" onClick={off}>
×
</button>
)}
</div>
<style jsx>
{`
@keyframes in {
from {
transform: translateX(16rem);
}
to {
transform: translateX(0rem);
}
}
@keyframes out {
from {
transform: translateX(0rem);
}
97% {
transform: translateX(20rem);
}
to {
transform: translateX(20rem);
display: none;
height: 0;
padding: 0;
margin: 0;
}
}
.toast {
padding: 8px 16px;
color: #fff;
border: 1px solid #fff;
border-radius: 2px;
background: #000;
font-size: 14px;
animation-name: in;
animation-duration: 600ms;
animation-direction: forwards;
animation-fill-mode: both;
animation-timing-function: ease-out;
}
.toast.out {
animation-name: out;
}
.toast-content {
display: flex;
align-items: center;
}
.toast :global(.close) {
padding-left: 0;
padding-right: 0;
background: transparent;
color: white;
border: none;
font-size: 100%;
margin-left: 1rem;
text-decoration: none;
cursor: pointer;
}
`}
</style>
</div>
)
}
function ToastContainer(props) {
return (
<div className="toast">
{props.toasts
.slice()
.reverse()
.map(toast => (
<Toast key={toast.children} {...toast} />
))}
<style jsx>
{`
.toast {
position: fixed;
z-index: 9999;
top: 5rem;
right: 1rem;
}
`}
</style>
</div>
)
}
export default ToastContainer