import { useEffect, useState } from 'react'; import VideoPlayhead from './Playhead'; import Box from '../Box'; import { useDefaultLabelContext } from '../contexts/DefaultLabelProvider'; import Icon from '../Icon'; import TapArea from '../TapArea'; import Text from '../Text'; import styles from '../Video.css'; type Props = { captionsButton: 'enabled' | 'disabled' | null; currentTime: number; duration: number; fullscreen: boolean; onCaptionsChange: (event: React.SyntheticEvent) => void; onFullscreenChange: () => void; onPause: (event: React.SyntheticEvent) => void; onPlay: (event: React.SyntheticEvent) => void; onPlayheadDown: ( event: React.MouseEvent | React.TouchEvent, ) => void; onPlayheadUp: ( event: React.MouseEvent | React.TouchEvent, ) => void; onVolumeChange: (event: React.SyntheticEvent) => void; playing: boolean; seek: (time: number) => void; volume: number; }; const timeToString = (time?: number) => { const rounded = Math.floor(time || 0); const minutes = Math.floor(rounded / 60); const seconds = rounded - minutes * 60; const minutesStr = minutes < 10 ? `0${minutes}` : minutes; const secondsStr = seconds < 10 ? `0${seconds}` : seconds; return `${minutesStr}:${secondsStr}`; }; function VideoControls({ captionsButton, currentTime, duration, fullscreen, onCaptionsChange, onFullscreenChange, onPause, onPlay, onPlayheadDown, onPlayheadUp, onVolumeChange, playing, seek, volume, }: Props) { const handleFullscreenChange = ({ event, }: { event: React.MouseEvent | React.KeyboardEvent; }) => { event.stopPropagation(); onFullscreenChange(); }; const handlePlayingChange = ({ event, }: { event: React.MouseEvent | React.KeyboardEvent; }) => { if (playing) { onPause(event); } else { onPlay(event); } }; const handleCaptionsChange = ({ event, }: { event: React.MouseEvent | React.KeyboardEvent; }) => { event.stopPropagation(); onCaptionsChange(event); }; const handleVolumeChange = ({ event, }: { event: React.MouseEvent | React.KeyboardEvent; }) => { onVolumeChange(event); }; const muted = volume === 0; const [showFullscreenButton, setShowFullscreenButton] = useState(false); const { accessibilityHideCaptionsLabel: defaultAccessibilityHideCaptionsLabel, accessibilityShowCaptionsLabel: defaultAccessibilityShowCaptionsLabel, accessibilityMaximizeLabel: defaultAccessibilityMaximizeLabel, accessibilityMinimizeLabel: defaultAccessibilityMinimizeLabel, accessibilityMuteLabel: defaultAccessibilityMuteLabel, accessibilityPauseLabel: defaultAccessibilityPauseLabel, accessibilityPlayLabel: defaultAccessibilityPlayLabel, accessibilityProgressLabel: defaultAccessibilityProgressLabel, accessibilityUnmuteLabel: defaultAccessibilityUnmuteLabel, } = useDefaultLabelContext('Video'); useEffect(() => { setShowFullscreenButton( typeof document !== 'undefined' && (!!document.fullscreenEnabled || // @ts-expect-error - TS2339 - Property 'webkitFullscreenEnabled' does not exist on type 'Document'. !!document.webkitFullscreenEnabled || // @ts-expect-error - TS2339 - Property 'mozFullScreenEnabled' does not exist on type 'Document'. !!document.mozFullScreenEnabled || // @ts-expect-error - TS2551 - Property 'msFullscreenEnabled' does not exist on type 'Document'. Did you mean 'fullscreenEnabled'? !!document.msFullscreenEnabled), ); }, []); return (
{captionsButton && ( {captionsButton === 'enabled' && ( )} )} {timeToString(currentTime)} {timeToString(duration)} {showFullscreenButton && ( )}
); } export default VideoControls;