Skip to content

Commit

Permalink
v2-hero block
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatan-lledo-netcentric committed Nov 8, 2024
1 parent 19fd547 commit 9b358f5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
11 changes: 11 additions & 0 deletions blocks/v2-hero/v2-hero.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* stylelint-disable selector-class-pattern */
.section .v2-hero-wrapper {
padding: 0;
margin: 0;
width: 100%;
max-width: 100%;
}

.v2-hero__container, .v2-hero__container img {
width: 100%;
}
29 changes: 29 additions & 0 deletions blocks/v2-hero/v2-hero.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const blockName = 'v2-hero';

export default function decorate(block) {
const pictures = block.querySelectorAll('picture');
const imgParent = pictures[0].parentElement;
const newPicture = document.createElement('picture');
const mediaQueries = ['(max-width: 749px)', '(min-width: 750px)'];
const MQ = window.matchMedia('(min-width: 750px)');
const img = document.createElement('img');

imgParent.classList.add(`${blockName}__container`);

mediaQueries.forEach((mediaQuery, index) => {
const source = document.createElement('source');
source.media = mediaQuery;
source.srcset = pictures[index].querySelector('img').src;
newPicture.appendChild(source);
if (MQ.matches) {
img.src = pictures[index].querySelector('img').src;
img.alt = pictures[index].querySelector('img').alt;
}
});
newPicture.appendChild(img);

// Remove the old pictures
pictures.forEach((picture) => picture.remove());

imgParent.appendChild(newPicture);
}
1 change: 1 addition & 0 deletions scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function buildHeroBlock(main) {
const picture = main.querySelector('picture');
// eslint-disable-next-line no-bitwise
if (h1 && picture && (h1.compareDocumentPosition(picture) & Node.DOCUMENT_POSITION_PRECEDING)) {
if (h1.closest('.v2-hero')) return;
const section = document.createElement('div');
section.append(buildBlock('hero', { elems: [picture, h1] }));
main.prepend(section);
Expand Down

0 comments on commit 9b358f5

Please sign in to comment.