Skip to content

Commit 8b0230f

Browse files
committed
Enable keyboard navigation inside lightbox with arrow left and right
1 parent 679e75b commit 8b0230f

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Dialog change log
55

66
## 3.0.0 / ????-??-??
77

8+
* Enabled keyboard navigation inside lightbox with arrow left and right
9+
(@thekid)
810
* Merged PR #71: Aggregate weather for entries when importing. This uses
911
the free https://open-meteo.com/en/docs/historical-weather-api API
1012
(@thekid)

src/main/js/lightbox.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,29 @@ class Lightbox {
2424

2525
/** Attach all of the given elements to open the lightbox specified by the given DOM element */
2626
attach(selector, $target) {
27-
$target.addEventListener('click', e => $target.close());
28-
selector.forEach($link => $link.addEventListener('click', e => {
29-
e.preventDefault();
30-
this.#open($target, $link);
31-
}));
27+
$target.addEventListener('click', e => {
28+
$target.close();
29+
});
30+
$target.addEventListener('keydown', e => {
31+
let offset;
32+
switch (e.key) {
33+
case 'ArrowLeft': offset = parseInt(e.target.dataset.offset) - 1; break;
34+
case 'ArrowRight': offset = parseInt(e.target.dataset.offset) + 1; break;
35+
default: return;
36+
}
37+
38+
e.stopPropagation();
39+
selector.item(offset)?.dispatchEvent(new Event('click'));
40+
});
41+
42+
let i = 0;
43+
for (const $link of selector) {
44+
const offset = i++;
45+
$link.addEventListener('click', e => {
46+
e.preventDefault();
47+
$target.dataset.offset = offset;
48+
this.#open($target, $link);
49+
});
50+
}
3251
}
3352
}

0 commit comments

Comments
 (0)