Sha256: 21e74ecabc8e67f4d2f65145e8a5c8af910b53d8115fcaa50e319020c4806170
Contents?: true
Size: 1.28 KB
Versions: 4
Compression:
Stored size: 1.28 KB
Contents
import React, { useEffect, useRef, useState } from "react"; import useToastStore from "../stores/useToastStore"; interface Props { error: string; notice: string; } export default function Toast(props: Props) { const [fadeout, setFadeout] = useState(false); const { toasts, error, notice, next } = useToastStore((state) => state); const timerRef = useRef<ReturnType<typeof setTimeout>>(null); const toast = toasts[0]; useEffect(() => { if (props.error) { error(props.error); } if (props.notice) { notice(props.notice); } }, [props.error, props.notice]); useEffect(() => { setFadeout(false); if (toast && !timerRef.current) { timerRef.current = setTimeout(() => { setFadeout(true); timerRef.current = setTimeout(() => { timerRef.current = null; setFadeout(false); next(); }, 500); }, 4000); } return () => { clearTimeout(timerRef.current); }; }, [toast]); const classNames = ["toast"]; if (toast) { classNames.push(toast.type); if (fadeout) { classNames.push("fadeout"); } } return ( <div className="toast-wrapper" aria-live="polite"> {toast && <div className={classNames.join(" ")}>{toast.message}</div>} </div> ); }
Version data entries
4 entries across 4 versions & 1 rubygems