Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TECH-778 added custom lazy loading functionality for promo images #34

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -93,13 +93,13 @@
{%- if block.settings.promotional_index == parent_index -%}
<div class='twcss-w-full twcss-max-w-64 twcss-flex twcss-flex-col twcss-gap-4'>
{%- if block.settings.promotional_headline -%}
<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 -%}
{%- if block.settings.promotional_headline -%}
<div class='twcss-text-large'>
Expand Down
Loading