Skip to content

Commit

Permalink
Merge pull request #34 from TrellisCommerce/feature/TECH-778
Browse files Browse the repository at this point in the history
TECH-778 added custom lazy loading functionality for promo images
  • Loading branch information
cmegalo authored Sep 16, 2024
2 parents 483a188 + 16c556f commit a63dce3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
40 changes: 35 additions & 5 deletions assets/details-disclosure.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ class DetailsDisclosure extends HTMLElement {
constructor() {
super();
this.mainDetailsToggle = this.querySelector('details');
this.content = this.mainDetailsToggle.querySelector('summary').nextElementSibling;
this.content =
this.mainDetailsToggle.querySelector('summary').nextElementSibling;

this.mainDetailsToggle.addEventListener('focusout', this.onFocusOut.bind(this));
this.mainDetailsToggle.addEventListener(
'focusout',
this.onFocusOut.bind(this),
);
this.mainDetailsToggle.addEventListener('toggle', this.onToggle.bind(this));
}

Expand All @@ -26,7 +30,9 @@ class DetailsDisclosure extends HTMLElement {

close() {
this.mainDetailsToggle.removeAttribute('open');
this.mainDetailsToggle.querySelector('summary').setAttribute('aria-expanded', false);
this.mainDetailsToggle
.querySelector('summary')
.setAttribute('aria-expanded', false);
}
}

Expand All @@ -36,16 +42,40 @@ class HeaderMenu extends DetailsDisclosure {
constructor() {
super();
this.header = document.querySelector('.header-wrapper');
this.megaMenu = document.querySelector('.mega-menu');
}

onToggle() {
if (this.megaMenu) {
var parentLinks = document.querySelectorAll('details.mega-menu');
parentLinks.forEach(function (parentLink) {
loadImages(parentLink);
});
function loadImages(menuItem) {
var images = menuItem.querySelectorAll('.custom-lazyload');
if (!images.length) return;
images.forEach(function (image) {
if (image.dataset.src) {
image.src = image.dataset.src;
image.removeAttribute('data-src');
image.classList.remove('custom-lazyload');
}
});
}
}

if (!this.header) return;
this.header.preventHide = this.mainDetailsToggle.open;

if (document.documentElement.style.getPropertyValue('--header-bottom-position-desktop') !== '') return;
if (
document.documentElement.style.getPropertyValue(
'--header-bottom-position-desktop',
) !== ''
)
return;
document.documentElement.style.setProperty(
'--header-bottom-position-desktop',
`${Math.floor(this.header.getBoundingClientRect().bottom)}px`
`${Math.floor(this.header.getBoundingClientRect().bottom)}px`,
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions snippets/header-mega-menu.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@
<img
width='160'
height='160'
class='twcss-aspect-square twcss-object-cover'
src='{{ block.settings.promotional_image | image_url }}'
class='twcss-aspect-square twcss-object-cover custom-lazyload'
data-src='{{ block.settings.promotional_image | image_url }}'
alt='{{ block.settings.promotional_image.alt | escape }}'
>

Check warning on line 102 in snippets/header-mega-menu.liquid

View workflow job for this annotation

GitHub Actions / Theme Check Report

snippets/header-mega-menu.liquid#L96-L102

[ImgLazyLoading] Use loading="eager" for images visible in the viewport on load and loading="lazy" for others
{%- endif -%}
Expand Down

0 comments on commit a63dce3

Please sign in to comment.