@@ -8,29 +8,29 @@ import { useShouldAdapt } from '../lib/useShouldAdapt';
8
8
import { useConfig } from './ConfigContext' ;
9
9
import { LoopVideoPlayer } from './LoopVideoPlayer' ;
10
10
11
- const videoContainerStyles = ( height : number , width : number ) => css `
11
+ const videoContainerStyles = css `
12
12
z-index : ${ getZIndex ( 'loop-video-container' ) } ;
13
13
position : relative;
14
- height : ${ height } px;
15
- width : ${ width } px;
16
14
` ;
17
15
18
16
type Props = {
19
17
src : string ;
20
18
videoId : string ;
21
19
width ?: number ;
22
20
height ?: number ;
21
+ thumbnailImage : string ;
22
+ fallbackImageComponent : JSX . Element ;
23
23
hasAudio ?: boolean ;
24
- fallbackImage : JSX . Element ;
25
24
} ;
26
25
27
26
export const LoopVideo = ( {
28
27
src,
29
28
videoId,
30
29
width = 600 ,
31
30
height = 360 ,
31
+ thumbnailImage,
32
+ fallbackImageComponent,
32
33
hasAudio = true ,
33
- fallbackImage,
34
34
} : Props ) => {
35
35
const adapted = useShouldAdapt ( ) ;
36
36
const { renderingTarget } = useConfig ( ) ;
@@ -39,6 +39,7 @@ export const LoopVideo = ({
39
39
const [ isPlaying , setIsPlaying ] = useState ( false ) ;
40
40
const [ isMuted , setIsMuted ] = useState ( true ) ;
41
41
const [ currentTime , setCurrentTime ] = useState ( 0 ) ;
42
+ const [ prefersReducedMotion , setPrefersReducedMotion ] = useState ( false ) ;
42
43
/**
43
44
* Keep a track of whether the video has been in view. We only want to
44
45
* pause the video if it has been in view.
@@ -57,11 +58,17 @@ export const LoopVideo = ({
57
58
if ( ! vidRef . current ) return ;
58
59
59
60
if ( isInView ) {
60
- if ( ! hasBeenInView ) {
61
- // When the video first comes into view, it should autoplay
62
- setIsPlaying ( true ) ;
63
- void vidRef . current . play ( ) ;
61
+ // We only want to autoplay the first time the video comes into view.
62
+ if ( hasBeenInView ) return ;
63
+
64
+ if ( window . matchMedia ( '(prefers-reduced-motion: reduce)' ) . matches ) {
65
+ setPrefersReducedMotion ( true ) ;
66
+ return ;
64
67
}
68
+
69
+ setIsPlaying ( true ) ;
70
+ void vidRef . current . play ( ) ;
71
+
65
72
setHasBeenInView ( true ) ;
66
73
}
67
74
@@ -73,7 +80,7 @@ export const LoopVideo = ({
73
80
74
81
if ( renderingTarget !== 'Web' ) return null ;
75
82
76
- if ( adapted ) return fallbackImage ;
83
+ if ( adapted ) return fallbackImageComponent ;
77
84
78
85
const handleClick = ( event : React . SyntheticEvent ) => {
79
86
event . preventDefault ( ) ;
@@ -141,18 +148,14 @@ export const LoopVideo = ({
141
148
const AudioIcon = isMuted ? SvgAudioMute : SvgAudio ;
142
149
143
150
return (
144
- < div
145
- className = "loop-video-container"
146
- ref = { setNode }
147
- css = { videoContainerStyles ( height , width ) }
148
- >
151
+ < div ref = { setNode } css = { videoContainerStyles } >
149
152
< LoopVideoPlayer
150
153
src = { src }
151
154
videoId = { videoId }
152
155
width = { width }
153
156
height = { height }
154
157
hasAudio = { hasAudio }
155
- fallbackImage = { fallbackImage }
158
+ fallbackImageComponent = { fallbackImageComponent }
156
159
currentTime = { currentTime }
157
160
setCurrentTime = { setCurrentTime }
158
161
ref = { vidRef }
@@ -166,6 +169,9 @@ export const LoopVideo = ({
166
169
handleKeyDown = { handleKeyDown }
167
170
onError = { onError }
168
171
AudioIcon = { AudioIcon }
172
+ thumbnailImage = {
173
+ prefersReducedMotion ? thumbnailImage : undefined
174
+ }
169
175
/>
170
176
</ div >
171
177
) ;
0 commit comments