Skip to content

Commit

Permalink
fix: remove extra dual-mode images from lightbox
Browse files Browse the repository at this point in the history
  • Loading branch information
cotes2020 committed Jul 29, 2024
1 parent b641b36 commit e77b932
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions _javascript/modules/components/img-popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,47 @@
* Dependencies: https://github.com/biati-digital/glightbox
*/

const IMG_CLASS = 'popup';
const html = document.documentElement;
const lightImages = '.popup:not(.dark)';
const darkImages = '.popup:not(.light)';
let selector = lightImages;

if (
(html.hasAttribute('data-mode') &&
html.getAttribute('data-mode') === 'dark') ||
(!html.hasAttribute('data-mode') &&
window.matchMedia('(prefers-color-scheme: dark)').matches)
) {
selector = darkImages;
}

function updateImages(event, lightbox) {
if (
event.source === window &&
event.data &&
event.data.direction === ModeToggle.ID
) {
if (selector === lightImages) {
selector = darkImages;
} else {
selector = lightImages;
}
}

lightbox.destroy();
lightbox = GLightbox({ selector: `${selector}` });
}

export function imgPopup() {
if (document.getElementsByClassName(IMG_CLASS).length === 0) {
if (document.querySelector(`${selector}`) === null) {
return;
}

GLightbox({ selector: `.${IMG_CLASS}` });
let lightbox = GLightbox({ selector: `${selector}` });

if (document.getElementById('mode-toggle')) {
window.addEventListener('message', (event) => {
updateImages(event, lightbox);
});
}
}

0 comments on commit e77b932

Please sign in to comment.