Skip to content

Commit

Permalink
chore: refactor how autiplay interacts with placeholder, especailly f…
Browse files Browse the repository at this point in the history
…or LCP
  • Loading branch information
shsteimer committed May 7, 2024
1 parent 98b7b86 commit a119a95
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 40 deletions.
8 changes: 6 additions & 2 deletions blocks/video/video.css
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
.video {
width: unset;
text-align: center;
max-width: 800px;
margin: 32px auto;
}

.video.lazy-loading {
.video[data-embed-is-loaded="false"]:not(.placeholder) {
/* reserve an approximate space to avoid extensive layout shifts */
aspect-ratio: 16 / 9;
}
Expand All @@ -31,6 +30,11 @@
position: relative;
}

.video[data-embed-is-loaded="true"] .video-placeholder {
visibility: hidden;
height: 0;
}

.video .video-placeholder > * {
display: flex;
align-items: center;
Expand Down
83 changes: 45 additions & 38 deletions blocks/video/video.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
* https://www.hlx.live/developer/block-collection/video
*/

function embedYoutube(url, replacePlaceholder, autoplay) {
function embedYoutube(url, autoplay, background) {
const usp = new URLSearchParams(url.search);
let suffix = '';
if (replacePlaceholder || autoplay) {
if (background || autoplay) {
const suffixParams = {
autoplay: '1',
mute: autoplay ? '1' : '0',
controls: autoplay ? '0' : '1',
disablekb: autoplay ? '1' : '0',
loop: autoplay ? '1' : '0',
playsinline: autoplay ? '1' : '0',
autoplay: autoplay ? '1' : '0',
mute: background ? '1' : '0',
controls: background ? '0' : '1',
disablekb: background ? '1' : '0',
loop: background ? '1' : '0',
playsinline: background ? '1' : '0',
};
suffix = `&${Object.entries(suffixParams).map(([k, v]) => `${k}=${encodeURIComponent(v)}`).join('&')}`;
}
Expand All @@ -32,13 +32,13 @@ function embedYoutube(url, replacePlaceholder, autoplay) {
return temp.children.item(0);
}

function embedVimeo(url, replacePlaceholder, autoplay) {
function embedVimeo(url, autoplay, background) {
const [, video] = url.pathname.split('/');
let suffix = '';
if (replacePlaceholder || autoplay) {
if (background || autoplay) {
const suffixParams = {
autoplay: '1',
background: autoplay ? '1' : '0',
autoplay: autoplay ? '1' : '0',
background: background ? '1' : '0',
};
suffix = `?${Object.entries(suffixParams).map(([k, v]) => `${k}=${encodeURIComponent(v)}`).join('&')}`;
}
Expand All @@ -52,22 +52,20 @@ function embedVimeo(url, replacePlaceholder, autoplay) {
return temp.children.item(0);
}

function getVideoElement(source, replacePlaceholder, autoplay) {
function getVideoElement(source, autoplay, background) {
const video = document.createElement('video');
video.setAttribute('controls', '');
video.dataset.loading = 'true';
video.addEventListener('loadedmetadata', () => delete video.dataset.loading);
if (autoplay || replacePlaceholder) {
video.setAttribute('autoplay', '');
if (autoplay) {
if (autoplay) video.setAttribute('autoplay', '');
if (background) {
video.setAttribute('loop', '');

Check failure on line 62 in blocks/video/video.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 4 spaces but found 6
video.setAttribute('playsinline', '');

Check failure on line 63 in blocks/video/video.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 4 spaces but found 6
video.removeAttribute('controls');

Check failure on line 64 in blocks/video/video.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 4 spaces but found 6
video.addEventListener('canplay', () => {

Check failure on line 65 in blocks/video/video.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 4 spaces but found 6
video.muted = true;

Check failure on line 66 in blocks/video/video.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 6 spaces but found 8
video.play();

Check failure on line 67 in blocks/video/video.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 6 spaces but found 8
});

Check failure on line 68 in blocks/video/video.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 4 spaces but found 6
}
}

const sourceEl = document.createElement('source');
Expand All @@ -78,50 +76,59 @@ function getVideoElement(source, replacePlaceholder, autoplay) {
return video;
}

const loadVideoEmbed = (block, link, replacePlaceholder, autoplay) => {
if (block.dataset.embedIsLoaded) {

Check failure on line 79 in blocks/video/video.js

View workflow job for this annotation

GitHub Actions / build

More than 1 blank line not allowed
const loadVideoEmbed = (block, link, autoplay, background) => {
if (block.dataset.embedIsLoaded === 'true') {
return;
}
const url = new URL(link);

const isYoutube = link.includes('youtube') || link.includes('youtu.be');
const isVimeo = link.includes('vimeo');
const isMp4 = link.includes('.mp4');

let embedEl;
if (isYoutube) {
embedEl = embedYoutube(url, replacePlaceholder, autoplay);
block.append(embedYoutube(url, autoplay, background));
block.dataset.embedIsLoaded = true;
} else if (isVimeo) {
embedEl = embedVimeo(url, replacePlaceholder, autoplay);
} else if (isMp4) {
embedEl = getVideoElement(link, replacePlaceholder, autoplay);
block.append(embedVimeo(url, autoplay, background));
block.dataset.embedIsLoaded = true;
} else {
const videoEl = getVideoElement(link, autoplay, background);
block.append(videoEl);
videoEl.addEventListener('canplay', () => {
block.dataset.embedIsLoaded = true;
});
}
block.replaceChildren(embedEl);

block.dataset.embedIsLoaded = true;
};

export default async function decorate(block) {
const placeholder = block.querySelector('picture');
const link = block.querySelector('a').href;
block.textContent = '';
block.dataset.embedIsLoaded = false;

if (placeholder) {
const autoplay = block.classList.contains('autoplay');
if (!!placeholder) {

Check failure on line 111 in blocks/video/video.js

View workflow job for this annotation

GitHub Actions / build

Redundant double negation
block.classList.add('placeholder');
const wrapper = document.createElement('div');
wrapper.className = 'video-placeholder';
wrapper.innerHTML = '<div class="video-placeholder-play"><button type="button" title="Play"></button></div>';
wrapper.prepend(placeholder);
wrapper.addEventListener('click', () => {
loadVideoEmbed(block, link, true, false);
});
wrapper.append(placeholder);

if (!autoplay) {
wrapper.insertAdjacentHTML('beforeend',

Check failure on line 118 in blocks/video/video.js

View workflow job for this annotation

GitHub Actions / build

Expected newline after '('
'<div class="video-placeholder-play"><button type="button" title="Play"></button></div>');
wrapper.addEventListener('click', () => {
loadVideoEmbed(block, link, true, false);
});
}
block.append(wrapper);
} else {
block.classList.add('lazy-loading');
}

if (!placeholder || (autoplay)) {
const observer = new IntersectionObserver((entries) => {
if (entries.some((e) => e.isIntersecting)) {
observer.disconnect();
loadVideoEmbed(block, link, false, block.classList.contains('autoplay'));
block.classList.remove('lazy-loading');
loadVideoEmbed(block, link, !!placeholder || autoplay, autoplay);
}
});
observer.observe(block);
Expand Down

0 comments on commit a119a95

Please sign in to comment.