import React from 'react'; import { createPortal } from 'react-dom'; interface DesktopSpeedMenuProps { showSpeedMenu: boolean; playbackRate: number; speeds: number[]; onSpeedChange: (speed: number) => void; onToggleSpeedMenu: () => void; onMouseEnter: () => void; onMouseLeave: () => void; containerRef: React.RefObject; } export function DesktopSpeedMenu({ showSpeedMenu, playbackRate, speeds, onSpeedChange, onToggleSpeedMenu, onMouseEnter, onMouseLeave, containerRef }: DesktopSpeedMenuProps) { const buttonRef = React.useRef(null); const [menuPosition, setMenuPosition] = React.useState({ top: 0, left: 0 }); const [isFullscreen, setIsFullscreen] = React.useState(false); React.useEffect(() => { const updateFullscreen = () => { setIsFullscreen(!!document.fullscreenElement); }; document.addEventListener('fullscreenchange', updateFullscreen); updateFullscreen(); return () => document.removeEventListener('fullscreenchange', updateFullscreen); }, []); React.useEffect(() => { if (showSpeedMenu && buttonRef.current && containerRef.current) { const buttonRect = buttonRef.current.getBoundingClientRect(); const containerRect = containerRef.current.getBoundingClientRect(); setMenuPosition({ top: buttonRect.bottom - containerRect.top + 10, left: buttonRect.right - containerRect.left }); } }, [showSpeedMenu, containerRef]); // Auto-close menu on scroll React.useEffect(() => { if (!showSpeedMenu) return; const handleScroll = () => { if (showSpeedMenu) { onToggleSpeedMenu(); } }; window.addEventListener('scroll', handleScroll, { passive: true }); return () => window.removeEventListener('scroll', handleScroll); }, [showSpeedMenu, onToggleSpeedMenu]); const handleToggle = () => { if (!showSpeedMenu && buttonRef.current && containerRef.current) { const buttonRect = buttonRef.current.getBoundingClientRect(); const containerRect = containerRef.current.getBoundingClientRect(); setMenuPosition({ top: buttonRect.bottom - containerRect.top + 10, left: buttonRect.right - containerRect.left }); } onToggleSpeedMenu(); }; const MenuContent = (
{speeds.map((speed) => ( ))}
); return (
{/* Speed Menu (Portal) */} {showSpeedMenu && typeof document !== 'undefined' && createPortal(MenuContent, containerRef.current || document.body)}
); }