import React, { useState, useRef, useEffect, HTMLAttributes } from "react"; import "./Notification.css"; import { renderProps, onLoad, getCN, get_typeName, isDevaice, getDevaice } from "../utils"; import { IconLoading } from "../../svg_icons/IconLoading"; export interface INotification extends HTMLAttributes { maxWidth?: number; loading?: false | "success" | "error" | "loading"; duration?: number; onClose?: boolean; onClosed?: () => void; device?: "desktop" | "mobile"; } function Notification({ children, maxWidth = window.innerWidth - 40, loading = false, duration = 3000, onClose, onClosed, device = getDevaice(), ...props }: INotification) { const [is_active, setIs_active] = useState(false); const ref = useRef(null); const [width, setWidth] = useState(40); const callbackClose = () => { const timerClose = setTimeout(() => { onClosed && onClosed(); clearTimeout(timerClose); }, 500) } useEffect(() => { if (loading == `success` || loading == `error`) { const timer = setTimeout(() => { setIs_active(false); callbackClose() clearTimeout(timer) }, 2500) } if (loading) return setWidth(40); }, [loading]) useEffect(() => { if (onClose) { setIs_active(false); callbackClose() } if (duration) { setTimeout(() => { setIs_active(false); callbackClose() }, duration); } if (!onClose) setIs_active(true); if (ref.current) { if (ref.current.clientWidth >= maxWidth) { setWidth(maxWidth); } else setWidth(ref.current.clientWidth); } }, [ref && ref.current && ref.current.clientWidth, onClose]); return ( { setIs_active(false); callbackClose() }} style={{ width: !!loading ? width : `auto`, }} className="MYUI-Notification__in" > {!loading && ( {children} )} {loading == `loading` && ( )} {loading == `success` && ( )} {loading == `error` && ( )} ); } export default Notification;