Skip to content

Commit 1966f34

Browse files
authored
Merge pull request #13850 from guardian/doml/loop-video-keyboard
Loop video - extra keyboard options
2 parents d113a72 + e2600cd commit 1966f34

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

dotcom-rendering/src/components/LoopVideo.importable.tsx

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export const LoopVideo = ({
5858
if (!vidRef.current) return;
5959

6060
if (isInView) {
61-
// We only want to autoplay the first time the video comes into view.
61+
// We only autoplay the first time the video comes into view.
6262
if (hasBeenInView) return;
6363

6464
if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
@@ -82,19 +82,31 @@ export const LoopVideo = ({
8282

8383
if (adapted) return fallbackImageComponent;
8484

85-
const handleClick = (event: React.SyntheticEvent) => {
86-
event.preventDefault();
85+
const playVideo = () => {
86+
if (!vidRef.current) return;
87+
setIsPlaying(true);
88+
void vidRef.current.play();
89+
};
90+
91+
const pauseVideo = () => {
8792
if (!vidRef.current) return;
93+
setIsPlaying(false);
94+
void vidRef.current.pause();
95+
};
8896

97+
const playPauseVideo = () => {
8998
if (isPlaying) {
90-
setIsPlaying(false);
91-
void vidRef.current.pause();
99+
pauseVideo();
92100
} else {
93-
setIsPlaying(true);
94-
void vidRef.current.play();
101+
playVideo();
95102
}
96103
};
97104

105+
const handleClick = (event: React.SyntheticEvent) => {
106+
event.preventDefault();
107+
playPauseVideo();
108+
};
109+
98110
const onError = () => {
99111
window.guardian.modules.sentry.reportError(
100112
new Error(`Loop video could not be played. source: ${src}`),
@@ -134,14 +146,20 @@ export const LoopVideo = ({
134146
switch (event.key) {
135147
case 'Enter':
136148
case ' ':
137-
handleClick(event);
149+
playPauseVideo();
150+
break;
151+
case 'Escape':
152+
pauseVideo();
138153
break;
139154
case 'ArrowRight':
140155
seekForward();
141156
break;
142157
case 'ArrowLeft':
143158
seekBackward();
144159
break;
160+
case 'm':
161+
setIsMuted(!isMuted);
162+
break;
145163
}
146164
};
147165

0 commit comments

Comments
 (0)