import { useEffect, useRef, useState } from "react"; import styles from "./styles.module.scss"; interface IVideoOverlayProps { isShow: boolean; setIsShow: React.Dispatch>; animationDuration?: number; children?: React.ReactNode; } function VideoOverlay({ isShow, setIsShow, animationDuration = 5000, children, }: IVideoOverlayProps) { const timeoutRef = useRef(); const [animationDurationLocal, setAnimationDurationLocal] = useState(animationDuration); const click = () => { if (timeoutRef.current) clearTimeout(timeoutRef.current); setAnimationDurationLocal((prev) => prev + animationDuration); timeoutRef.current = setTimeout(() => { setIsShow(false); }, animationDuration); }; useEffect(() => { if (!isShow) return; console.log("start "); timeoutRef.current = setTimeout(() => { setIsShow(false); }, animationDuration); return () => { if (timeoutRef.current) clearTimeout(timeoutRef.current); }; }, [animationDuration, isShow, setIsShow]); return ( <> {isShow && (
{children}
)} ); } export default VideoOverlay;