Skip to content

Commit 6b650dc

Browse files
committed
feat: use prefers redueced motion to control the autoplay
1 parent 3144dbf commit 6b650dc

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

blocks/video/video.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* https://www.hlx.live/developer/block-collection/video
55
*/
66

7+
const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)');
8+
79
function embedYoutube(url, autoplay, background) {
810
const usp = new URLSearchParams(url.search);
911
let suffix = '';
@@ -62,7 +64,7 @@ function getVideoElement(source, autoplay, background) {
6264
video.removeAttribute('controls');
6365
video.addEventListener('canplay', () => {
6466
video.muted = true;
65-
video.play();
67+
if (autoplay) video.play();
6668
});
6769
}
6870

@@ -123,11 +125,12 @@ export default async function decorate(block) {
123125
block.append(wrapper);
124126
}
125127

126-
if (!placeholder || (autoplay)) {
128+
if (!placeholder || autoplay) {
127129
const observer = new IntersectionObserver((entries) => {
128130
if (entries.some((e) => e.isIntersecting)) {
129131
observer.disconnect();
130-
loadVideoEmbed(block, link, !!placeholder || autoplay, autoplay);
132+
const playOnLoad = autoplay && !prefersReducedMotion.matches;
133+
loadVideoEmbed(block, link, playOnLoad, autoplay);
131134
}
132135
});
133136
observer.observe(block);

0 commit comments

Comments
 (0)