|
| 1 | +import { css } from '@emotion/react'; |
| 2 | +import { log } from '@guardian/libs'; |
| 3 | +import { SvgAudio, SvgAudioMute } from '@guardian/source/react-components'; |
| 4 | +import { useEffect, useRef, useState } from 'react'; |
| 5 | +import { getZIndex } from '../lib/getZIndex'; |
| 6 | +import { useIsInView } from '../lib/useIsInView'; |
| 7 | +import { useShouldAdapt } from '../lib/useShouldAdapt'; |
| 8 | +import { useConfig } from './ConfigContext'; |
| 9 | +import { LoopVideoPlayer } from './LoopVideoPlayer'; |
| 10 | + |
| 11 | +const videoContainerStyles = (height: number, width: number) => css` |
| 12 | + z-index: ${getZIndex('loop-video-container')}; |
| 13 | + position: relative; |
| 14 | + height: ${height}px; |
| 15 | + width: ${width}px; |
| 16 | +`; |
| 17 | + |
| 18 | +type Props = { |
| 19 | + src: string; |
| 20 | + videoId: string; |
| 21 | + width?: number; |
| 22 | + height?: number; |
| 23 | + hasAudio?: boolean; |
| 24 | + fallbackImage: JSX.Element; |
| 25 | +}; |
| 26 | + |
| 27 | +export const LoopVideo = ({ |
| 28 | + src, |
| 29 | + videoId, |
| 30 | + width = 600, |
| 31 | + height = 360, |
| 32 | + hasAudio = true, |
| 33 | + fallbackImage, |
| 34 | +}: Props) => { |
| 35 | + const adapted = useShouldAdapt(); |
| 36 | + const { renderingTarget } = useConfig(); |
| 37 | + const vidRef = useRef<HTMLVideoElement>(null); |
| 38 | + const [isPlayable, setIsPlayable] = useState(false); |
| 39 | + const [isPlaying, setIsPlaying] = useState(false); |
| 40 | + const [isMuted, setIsMuted] = useState(true); |
| 41 | + const [currentTime, setCurrentTime] = useState(0); |
| 42 | + /** |
| 43 | + * Keep a track of whether the video has been in view. We only want to |
| 44 | + * pause the video if it has been in view. |
| 45 | + */ |
| 46 | + const [hasBeenInView, setHasBeenInView] = useState(false); |
| 47 | + |
| 48 | + const [isInView, setNode] = useIsInView({ |
| 49 | + repeat: true, |
| 50 | + threshold: 0.5, |
| 51 | + }); |
| 52 | + |
| 53 | + /** |
| 54 | + * Pause the video when the user scrolls past it. |
| 55 | + */ |
| 56 | + useEffect(() => { |
| 57 | + if (!vidRef.current) return; |
| 58 | + |
| 59 | + if (isInView) { |
| 60 | + if (!hasBeenInView) { |
| 61 | + // When the video first comes into view, it should autoplay |
| 62 | + setIsPlaying(true); |
| 63 | + void vidRef.current.play(); |
| 64 | + } |
| 65 | + setHasBeenInView(true); |
| 66 | + } |
| 67 | + |
| 68 | + if (!isInView && hasBeenInView && isPlayable && isPlaying) { |
| 69 | + setIsPlaying(false); |
| 70 | + void vidRef.current.pause(); |
| 71 | + } |
| 72 | + }, [isInView, hasBeenInView, isPlayable, isPlaying]); |
| 73 | + |
| 74 | + if (renderingTarget !== 'Web') return null; |
| 75 | + |
| 76 | + if (adapted) return fallbackImage; |
| 77 | + |
| 78 | + const handleClick = (event: React.SyntheticEvent) => { |
| 79 | + event.preventDefault(); |
| 80 | + if (!vidRef.current) return; |
| 81 | + |
| 82 | + if (isPlaying) { |
| 83 | + setIsPlaying(false); |
| 84 | + void vidRef.current.pause(); |
| 85 | + } else { |
| 86 | + setIsPlaying(true); |
| 87 | + void vidRef.current.play(); |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + const onError = () => { |
| 92 | + window.guardian.modules.sentry.reportError( |
| 93 | + new Error(`Loop video could not be played. source: ${src}`), |
| 94 | + 'loop-video', |
| 95 | + ); |
| 96 | + log('dotcom', `Loop video could not be played. source: ${src}`); |
| 97 | + }; |
| 98 | + |
| 99 | + const seekForward = () => { |
| 100 | + if (vidRef.current) { |
| 101 | + const newTime = Math.min( |
| 102 | + vidRef.current.currentTime + 1, |
| 103 | + vidRef.current.duration, |
| 104 | + ); |
| 105 | + |
| 106 | + vidRef.current.currentTime = newTime; |
| 107 | + setCurrentTime(newTime); |
| 108 | + } |
| 109 | + }; |
| 110 | + |
| 111 | + const seekBackward = () => { |
| 112 | + if (vidRef.current) { |
| 113 | + // Allow the user to cycle to the end of the video using the arrow keys |
| 114 | + const newTime = |
| 115 | + (((vidRef.current.currentTime - 1) % vidRef.current.duration) + |
| 116 | + vidRef.current.duration) % |
| 117 | + vidRef.current.duration; |
| 118 | + |
| 119 | + vidRef.current.currentTime = newTime; |
| 120 | + setCurrentTime(newTime); |
| 121 | + } |
| 122 | + }; |
| 123 | + |
| 124 | + const handleKeyDown = ( |
| 125 | + event: React.KeyboardEvent<HTMLVideoElement>, |
| 126 | + ): void => { |
| 127 | + switch (event.key) { |
| 128 | + case 'Enter': |
| 129 | + case ' ': |
| 130 | + handleClick(event); |
| 131 | + break; |
| 132 | + case 'ArrowRight': |
| 133 | + seekForward(); |
| 134 | + break; |
| 135 | + case 'ArrowLeft': |
| 136 | + seekBackward(); |
| 137 | + break; |
| 138 | + } |
| 139 | + }; |
| 140 | + |
| 141 | + const AudioIcon = isMuted ? SvgAudioMute : SvgAudio; |
| 142 | + |
| 143 | + return ( |
| 144 | + <div |
| 145 | + className="loop-video-container" |
| 146 | + ref={setNode} |
| 147 | + css={videoContainerStyles(height, width)} |
| 148 | + > |
| 149 | + <LoopVideoPlayer |
| 150 | + src={src} |
| 151 | + videoId={videoId} |
| 152 | + width={width} |
| 153 | + height={height} |
| 154 | + hasAudio={hasAudio} |
| 155 | + fallbackImage={fallbackImage} |
| 156 | + currentTime={currentTime} |
| 157 | + setCurrentTime={setCurrentTime} |
| 158 | + ref={vidRef} |
| 159 | + isPlayable={isPlayable} |
| 160 | + setIsPlayable={setIsPlayable} |
| 161 | + isPlaying={isPlaying} |
| 162 | + setIsPlaying={setIsPlaying} |
| 163 | + isMuted={isMuted} |
| 164 | + setIsMuted={setIsMuted} |
| 165 | + handleClick={handleClick} |
| 166 | + handleKeyDown={handleKeyDown} |
| 167 | + onError={onError} |
| 168 | + AudioIcon={AudioIcon} |
| 169 | + /> |
| 170 | + </div> |
| 171 | + ); |
| 172 | +}; |
0 commit comments