Skip to content

Commit

Permalink
feat: use prefers redueced motion to control the autoplay
Browse files Browse the repository at this point in the history
  • Loading branch information
shsteimer committed May 7, 2024
1 parent 3144dbf commit 6b650dc
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions blocks/video/video.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* https://www.hlx.live/developer/block-collection/video
*/

const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)');

function embedYoutube(url, autoplay, background) {
const usp = new URLSearchParams(url.search);
let suffix = '';
Expand Down Expand Up @@ -62,7 +64,7 @@ function getVideoElement(source, autoplay, background) {
video.removeAttribute('controls');
video.addEventListener('canplay', () => {
video.muted = true;
video.play();
if (autoplay) video.play();
});
}

Expand Down Expand Up @@ -123,11 +125,12 @@ export default async function decorate(block) {
block.append(wrapper);
}

if (!placeholder || (autoplay)) {
if (!placeholder || autoplay) {
const observer = new IntersectionObserver((entries) => {
if (entries.some((e) => e.isIntersecting)) {
observer.disconnect();
loadVideoEmbed(block, link, !!placeholder || autoplay, autoplay);
const playOnLoad = autoplay && !prefersReducedMotion.matches;
loadVideoEmbed(block, link, playOnLoad, autoplay);
}
});
observer.observe(block);
Expand Down

0 comments on commit 6b650dc

Please sign in to comment.