import React, { useEffect, useRef } from 'react'; import './VideoContextMenu.css'; function VideoContextMenu({ visible, position, onClose, onCopyVideoUrl, onCopyVideoUrlAtTime, onCopyEmbedCode }) { const menuRef = useRef(null); useEffect(() => { if (visible && menuRef.current) { // Position the menu menuRef.current.style.left = `${position.x}px`; menuRef.current.style.top = `${position.y}px`; // Adjust if menu goes off screen const rect = menuRef.current.getBoundingClientRect(); const windowWidth = window.innerWidth; const windowHeight = window.innerHeight; if (rect.right > windowWidth) { menuRef.current.style.left = `${position.x - rect.width}px`; } if (rect.bottom > windowHeight) { menuRef.current.style.top = `${position.y - rect.height}px`; } } }, [visible, position]); useEffect(() => { const handleClickOutside = (e) => { if (visible && menuRef.current && !menuRef.current.contains(e.target)) { onClose(); } }; const handleEscape = (e) => { if (e.key === 'Escape' && visible) { onClose(); } }; if (visible) { // Use capture phase to catch events earlier, before they can be stopped // Listen to both mousedown and click to ensure we catch all clicks document.addEventListener('mousedown', handleClickOutside, true); document.addEventListener('click', handleClickOutside, true); document.addEventListener('keydown', handleEscape); } return () => { document.removeEventListener('mousedown', handleClickOutside, true); document.removeEventListener('click', handleClickOutside, true); document.removeEventListener('keydown', handleEscape); }; }, [visible, onClose]); if (!visible) return null; return (
e.stopPropagation()}>
Copy video URL
Copy video URL at current time
Copy embed code
); } export default VideoContextMenu;