EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
Toast.jsx
Go to the documentation of this file.
1import { useEffect } from 'react';
2import './Toast.css';
3
4export default function Toast({ message, type = 'info', duration = 5000, onClose }) {
5 useEffect(() => {
6 if (duration > 0) {
7 const timer = setTimeout(() => {
8 onClose();
9 }, duration);
10 return () => clearTimeout(timer);
11 }
12 }, [duration, onClose]);
13
14 const getIcon = () => {
15 switch (type) {
16 case 'success':
17 return 'check_circle';
18 case 'error':
19 return 'error';
20 case 'warning':
21 return 'warning';
22 case 'info':
23 default:
24 return 'info';
25 }
26 };
27
28 return (
29 <div className={`toast toast-${type}`}>
30 <span className="material-symbols-outlined toast-icon">{getIcon()}</span>
31 <span className="toast-message">{message}</span>
32 <button className="toast-close" onClick={onClose}>
33 <span className="material-symbols-outlined">close</span>
34 </button>
35 </div>
36 );
37}