import { useEffect, useRef } from "react"; import { useVideoPlayerState } from "../VideoContext"; interface Props { children?: React.ReactNode; id?: string; className?: string; } // TODO store popout in router history so you can press back to yeet // TODO add transition export function VideoPopout(props: Props) { const { videoState } = useVideoPlayerState(); const popoutRef = useRef(null); const isOpen = videoState.popout === props.id; useEffect(() => { if (!isOpen) return; const popoutEl = popoutRef.current; function windowClick(e: MouseEvent) { const rect = popoutEl?.getBoundingClientRect(); if (rect) { if ( e.pageX >= rect.x && e.pageX <= rect.x + rect.width && e.pageY >= rect.y && e.pageY <= rect.y + rect.height ) { // inside bounding box of popout return; } } videoState.closePopout(); } window.addEventListener("click", windowClick); return () => { window.removeEventListener("click", windowClick); }; }, [isOpen, videoState]); return (
{isOpen ? props.children : null}
); }