1import React, { useEffect } from "react";
2import ReactDOM from "react-dom";
3import "./Styles/_toast.scss";
4
5export default function Toast({ children, ...props }) {
6 const { title, message, timeout, isOpen, callback } = props;
7
8 useEffect(() => {
9 if (isOpen) {
10 const timer = setTimeout(() => {
11 callback();
12 }, timeout);
13 return () => {
14 clearTimeout(timer);
15 };
16 }
17 }, []);
18
19 return ReactDOM.createPortal(
20 <div className={`Toast ${isOpen ? "active" : ""}`}>
21 <span className="Toast-Close" onMouseDown={callback}>
22 Close
23 </span>
24 <h2>{title}</h2>
25 <p>{message}</p>
26 </div>,
27 document.getElementById("portals")
28 );
29}
30
31Toast.defaultProps = {
32 title: "Title",
33 message: "Message",
34 timeout: 2000,
35 isOpen: false,
36 callback: () => { },
37};