-
Notifications
You must be signed in to change notification settings - Fork 0
/
Toast.js
80 lines (73 loc) · 2.98 KB
/
Toast.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
import { useEffect, useRef, useState } from "react";
import { CSSTransition } from 'react-transition-group';
import { XIcon } from '@heroicons/react/solid';
export default function Toast(props) {
const {
children,
color = 'indigo',
duration
} = props;
const [isVisible, setIsVisible] = useState(true);
const containerRef = useRef(null);
const closeButtonDurationRef = useRef(null);
let containerColorClassNames = 'bg-indigo-500 text-white';
let closeButtonColorClassNames = 'border-indigo-600 hover:bg-indigo-600 hover:border-indigo-700';
if (color === 'red') {
containerColorClassNames = 'bg-red-500 text-white';
closeButtonColorClassNames = 'border-red-600 hover:bg-red-600 hover:border-red-700';
} else if (color === 'green') {
containerColorClassNames = 'bg-green-500 text-white';
closeButtonColorClassNames = 'border-green-600 hover:bg-green-600 hover:border-green-700';
}
useEffect(() => {
if (duration) {
setTimeout(() => {
setIsVisible(false);
}, duration);
}
}, [duration]);
return (
<div className="fixed bottom-0 right-0">
<CSSTransition
in={ isVisible }
timeout={ 500 }
classNames="transition__toast"
nodeRef={ containerRef }
appear
>
<div
ref={ containerRef }
className={ `transition__toast mr-4 mb-4 rounded-md flex overflow-hidden ${containerColorClassNames}` }
>
<button
className={`
relative p-4 border-r transition-all
${closeButtonColorClassNames}
`}
onClick={ () => setIsVisible(false) }
>
<XIcon className="w-5 h-5 relative z-10" />
{ Boolean(duration || true) && (
<CSSTransition
in={ Boolean(duration) }
timeout={ duration }
nodeRef={ closeButtonDurationRef }
classNames="transition__toast__duration"
appear
>
<div
ref={ closeButtonDurationRef }
className="transition__toast__duration absolute top-0 left-0 h-full bg-green-600"
style={{ transition: `all ${duration}ms linear`}}
/>
</CSSTransition>
) }
</button>
<div className="p-4">
{ children }
</div>
</div>
</CSSTransition>
</div>
);
}