Skip to content

Commit d375abf

Browse files
committed
adding fixed-height variant to embed block
1 parent 142196c commit d375abf

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

blocks/embed/embed.css

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
.embed {
2-
width: unset;
3-
text-align: center;
4-
max-width: 800px;
5-
margin: 32px auto;
6-
}
2+
width: 100%;
3+
max-width: 100%;
4+
margin: 32px auto;
5+
text-align: center;
6+
}
77

88
.embed > div {
99
display: flex;

blocks/embed/embed.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,35 @@
66

77
import { loadScript } from '../../scripts/aem.js';
88

9-
const getDefaultEmbed = (url) => `<div style="left: 0; width: 100%; height: 0; position: relative; padding-bottom: 56.25%;">
9+
const getEmbedContainerStyle = (fixedHeight) => (fixedHeight
10+
? `left: 0; width: 100%; height: ${fixedHeight}px; position: relative;`
11+
: 'left: 0; width: 100%; height: 0; position: relative; padding-bottom: 56.25%;');
12+
13+
const getDefaultEmbed = (url, fixedHeight) => `<div style="${getEmbedContainerStyle(fixedHeight)}">
1014
<iframe src="${url.href}" style="border: 0; top: 0; left: 0; width: 100%; height: 100%; position: absolute;" allowfullscreen=""
1115
scrolling="no" allow="encrypted-media" title="Content from ${url.hostname}" loading="lazy">
1216
</iframe>
1317
</div>`;
1418

15-
const embedYoutube = (url, autoplay) => {
19+
const embedYoutube = (url, autoplay, fixedHeight) => {
1620
const usp = new URLSearchParams(url.search);
1721
const suffix = autoplay ? '&muted=1&autoplay=1' : '';
1822
let vid = usp.get('v') ? encodeURIComponent(usp.get('v')) : '';
1923
const embed = url.pathname;
2024
if (url.origin.includes('youtu.be')) {
2125
[, vid] = url.pathname.split('/');
2226
}
23-
const embedHTML = `<div style="left: 0; width: 100%; height: 0; position: relative; padding-bottom: 56.25%;">
27+
const embedHTML = `<div style="${getEmbedContainerStyle(fixedHeight)}">
2428
<iframe src="https://www.youtube.com${vid ? `/embed/${vid}?rel=0&v=${vid}${suffix}` : embed}" style="border: 0; top: 0; left: 0; width: 100%; height: 100%; position: absolute;"
2529
allow="autoplay; fullscreen; picture-in-picture; encrypted-media; accelerometer; gyroscope; picture-in-picture" allowfullscreen="" scrolling="no" title="Content from Youtube" loading="lazy"></iframe>
2630
</div>`;
2731
return embedHTML;
2832
};
2933

30-
const embedVimeo = (url, autoplay) => {
34+
const embedVimeo = (url, autoplay, fixedHeight) => {
3135
const [, video] = url.pathname.split('/');
3236
const suffix = autoplay ? '?muted=1&autoplay=1' : '';
33-
const embedHTML = `<div style="left: 0; width: 100%; height: 0; position: relative; padding-bottom: 56.25%;">
37+
const embedHTML = `<div style="${getEmbedContainerStyle(fixedHeight)}">
3438
<iframe src="https://player.vimeo.com/video/${video}${suffix}"
3539
style="border: 0; top: 0; left: 0; width: 100%; height: 100%; position: absolute;"
3640
frameborder="0" allow="autoplay; fullscreen; picture-in-picture" allowfullscreen
@@ -45,7 +49,17 @@ const embedTwitter = (url) => {
4549
return embedHTML;
4650
};
4751

48-
const loadEmbed = (block, link, autoplay) => {
52+
const getFixedHeight = (block) => {
53+
const fixedHeightClass = [...block.classList]
54+
.find((className) => /^(?:fixedheight-|fixed-height-|h-)?\d+(?:px)?$/i.test(className));
55+
if (!fixedHeightClass) return null;
56+
57+
const match = fixedHeightClass.match(/(\d+)/);
58+
const value = match ? Number(match[1]) : NaN;
59+
return Number.isFinite(value) && value > 0 ? value : null;
60+
};
61+
62+
const loadEmbed = (block, link, autoplay, fixedHeight) => {
4963
if (block.classList.contains('embed-is-loaded')) {
5064
return;
5165
}
@@ -68,10 +82,10 @@ const loadEmbed = (block, link, autoplay) => {
6882
const config = EMBEDS_CONFIG.find((e) => e.match.some((match) => link.includes(match)));
6983
const url = new URL(link);
7084
if (config) {
71-
block.innerHTML = config.embed(url, autoplay);
85+
block.innerHTML = config.embed(url, autoplay, fixedHeight);
7286
block.classList = `block embed embed-${config.match[0]}`;
7387
} else {
74-
block.innerHTML = getDefaultEmbed(url);
88+
block.innerHTML = getDefaultEmbed(url, fixedHeight);
7589
block.classList = 'block embed';
7690
}
7791
block.classList.add('embed-is-loaded');
@@ -80,22 +94,27 @@ const loadEmbed = (block, link, autoplay) => {
8094
export default function decorate(block) {
8195
const placeholder = block.querySelector('picture');
8296
const link = block.querySelector('a').href;
97+
const fixedHeight = getFixedHeight(block);
8398
block.textContent = '';
8499

85100
if (placeholder) {
86101
const wrapper = document.createElement('div');
87102
wrapper.className = 'embed-placeholder';
103+
if (fixedHeight) {
104+
wrapper.style.aspectRatio = 'auto';
105+
wrapper.style.height = `${fixedHeight}px`;
106+
}
88107
wrapper.innerHTML = '<div class="embed-placeholder-play"><button type="button" title="Play"></button></div>';
89108
wrapper.prepend(placeholder);
90109
wrapper.addEventListener('click', () => {
91-
loadEmbed(block, link, true);
110+
loadEmbed(block, link, true, fixedHeight);
92111
});
93112
block.append(wrapper);
94113
} else {
95114
const observer = new IntersectionObserver((entries) => {
96115
if (entries.some((e) => e.isIntersecting)) {
97116
observer.disconnect();
98-
loadEmbed(block, link);
117+
loadEmbed(block, link, false, fixedHeight);
99118
}
100119
});
101120
observer.observe(block);

0 commit comments

Comments
 (0)