import React, { useCallback, useRef, useState } from "react"; import { useVideoPlayerState } from "../VideoContext"; interface BackdropControlProps { children?: React.ReactNode; } // TODO add double click to toggle fullscreen export function BackdropControl(props: BackdropControlProps) { const { videoState } = useVideoPlayerState(); const [moved, setMoved] = useState(false); const timeout = useRef | null>(null); const clickareaRef = useRef(null); const handleMouseMove = useCallback(() => { setMoved(true); if (timeout.current) clearTimeout(timeout.current); timeout.current = setTimeout(() => { setMoved(false); timeout.current = null; }, 3000); }, [timeout, setMoved]); const handleMouseLeave = useCallback(() => { setMoved(false); }, [setMoved]); const handleClick = useCallback( (e: React.MouseEvent) => { if (!clickareaRef.current || clickareaRef.current !== e.target) return; if (videoState.isPlaying) videoState.pause(); else videoState.play(); }, [videoState, clickareaRef] ); const showUI = moved || videoState.isPaused; return (
{showUI ? props.children : null}
); }