import React, { useEffect, useState, useContext } from 'react'; import { createPortal } from 'react-dom'; import Icon from '../Icon'; import './Toast.scss'; import Button from '../Button'; import classNames from 'classnames'; import { ToasterProps } from './types'; import Typography from '../Typography'; import { ThemeContext } from '../ThemeProvider/ThemeProvider'; const Toaster: React.FC = ({ isOpen = false, variant = 'success', toastTitle = 'Success', onCancelClick = () => {}, onConfirmation = () => {}, toastMessage = 'Hello this is child element', displayDuration = 5000, confirmationText = 'Confirm', zIndex = 99, }) => { const [isExiting, setIsExiting] = useState(false); const iconMap = { success: 'success', info: 'info', warning: 'warning', danger: 'error', confirm: 'delete', }; const getToasterIcon = ( variant: 'success' | 'warning' | 'danger' | 'info' | 'confirm' ) => { const name = iconMap[variant]; return ; }; const themeContext = useContext(ThemeContext); const currentTheme = themeContext?.currentTheme; useEffect(() => { if (isOpen && variant !== 'confirm') { const timer = setTimeout(() => { setIsExiting(true); setTimeout(onCancelClick, 500); // Duration of exit animation }, displayDuration); return () => { clearTimeout(timer); setIsExiting(false); }; } return () => {}; }, [isOpen]); const handleClose = () => { setIsExiting(true); setTimeout(() => { onCancelClick(); setIsExiting(false); }, 500); }; if (!isOpen && !isExiting) return null; return createPortal(
{getToasterIcon(variant)}
{toastTitle} {toastMessage} {variant === 'confirm' && (
)}
, document.body ); }; export default Toaster;