import React, { useState, useRef, useCallback } from 'react' import { usePlayerContext } from '../../contexts/PlayerContext' import { VolumeUpIcon, VolumeDownIcon, VolumeMuteIcon } from '../../icons' import './VolumeControl.css' export const VolumeControl: React.FC = () => { const { videoState, setVolume, toggleMute } = usePlayerContext() const [showSlider, setShowSlider] = useState(false) const timeoutRef = useRef() const handleMouseEnter = useCallback(() => { if (timeoutRef.current) { window.clearTimeout(timeoutRef.current) } setShowSlider(true) }, []) const handleMouseLeave = useCallback(() => { timeoutRef.current = window.setTimeout(() => { setShowSlider(false) }, 300) }, []) const handleSliderChange = useCallback( (e: React.ChangeEvent) => { const volume = parseFloat(e.target.value) setVolume(volume) }, [setVolume] ) const VolumeIcon = videoState.muted ? VolumeMuteIcon : videoState.volume > 0.5 ? VolumeUpIcon : VolumeDownIcon return (
) }