Skip to content

Commit 3efd6b6

Browse files
authored
fix: remove extra dual-mode images from lightbox (#1883)
1 parent b641b36 commit 3efd6b6

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

_javascript/modules/components/img-popup.js

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,44 @@
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+
let lightbox = GLightbox({ selector: `${selector}` });
12+
13+
if (
14+
(html.hasAttribute('data-mode') &&
15+
html.getAttribute('data-mode') === 'dark') ||
16+
(!html.hasAttribute('data-mode') &&
17+
window.matchMedia('(prefers-color-scheme: dark)').matches)
18+
) {
19+
selector = darkImages;
20+
}
21+
22+
function updateImages(event) {
23+
if (
24+
event.source === window &&
25+
event.data &&
26+
event.data.direction === ModeToggle.ID
27+
) {
28+
if (selector === lightImages) {
29+
selector = darkImages;
30+
} else {
31+
selector = lightImages;
32+
}
33+
}
34+
35+
lightbox.destroy();
36+
lightbox = GLightbox({ selector: `${selector}` });
37+
}
838

939
export function imgPopup() {
10-
if (document.getElementsByClassName(IMG_CLASS).length === 0) {
40+
if (document.querySelector(`${selector}`) === null) {
1141
return;
1242
}
1343

14-
GLightbox({ selector: `.${IMG_CLASS}` });
44+
if (document.getElementById('mode-toggle')) {
45+
window.addEventListener('message', updateImages);
46+
}
1547
}

0 commit comments

Comments
 (0)