Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5c5910f

Browse files
committedAug 18, 2024
fix: remove extra dual-mode images from lightbox (mmistakes#1883)
1 parent b641b36 commit 5c5910f

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed
 

‎_javascript/modules/components/img-popup.js

+38-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,47 @@
44
* Dependencies: https://github.com/biati-digital/glightbox
55
*/
66

7-
const IMG_CLASS = 'popup';
7+
const html = document.documentElement;
8+
const lightImages = '.popup:not(.dark)';
9+
const darkImages = '.popup:not(.light)';
10+
let selector = lightImages;
11+
12+
function updateImages(lightbox) {
13+
if (selector === lightImages) {
14+
selector = darkImages;
15+
} else {
16+
selector = lightImages;
17+
}
18+
19+
lightbox.destroy();
20+
lightbox = GLightbox({ selector: `${selector}` });
21+
}
822

923
export function imgPopup() {
10-
if (document.getElementsByClassName(IMG_CLASS).length === 0) {
24+
if (document.querySelector('.popup') === null) {
1125
return;
1226
}
1327

14-
GLightbox({ selector: `.${IMG_CLASS}` });
28+
if (
29+
(html.hasAttribute('data-mode') &&
30+
html.getAttribute('data-mode') === 'dark') ||
31+
(!html.hasAttribute('data-mode') &&
32+
window.matchMedia('(prefers-color-scheme: dark)').matches)
33+
) {
34+
selector = darkImages;
35+
}
36+
37+
let lightbox = GLightbox({ selector: `${selector}` });
38+
39+
if (document.getElementById('mode-toggle')) {
40+
window.addEventListener('message', (event) => {
41+
if (
42+
event.source === window &&
43+
event.data &&
44+
event.data.direction === ModeToggle.ID
45+
) {
46+
updateImages(lightbox);
47+
}
48+
});
49+
}
1550
}

0 commit comments

Comments
 (0)
Please sign in to comment.