Skip to content

Commit 8f42e40

Browse files
committed
Use space bar & touch "clicks" to play/pause video
1 parent 21c6773 commit 8f42e40

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

src/main/js/lightbox.js

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class Lightbox {
6060
} else {
6161
$meta.className = 'meta for-video';
6262
$display.className = 'display is-video';
63-
$display.innerHTML = `<video playsinline poster="${$item.poster}" width="100%">${$item.innerHTML}</video>`;
63+
$display.innerHTML = `<video controls playsinline preload="metadata" poster="${$item.poster}" width="100%">${$item.innerHTML}</video>`;
6464

6565
// Simulate :playing pseudo-class
6666
const $video = $display.querySelector('video');
@@ -76,6 +76,13 @@ class Lightbox {
7676
$target.focus();
7777
}
7878

79+
#activate($target) {
80+
const $video = $target.querySelector('video');
81+
if ($video) {
82+
$video.paused ? $video.play() : $video.pause();
83+
}
84+
}
85+
7986
/** Attach all of the given elements to open the lightbox specified by the given DOM element */
8087
attach(selector, $target) {
8188
$target.addEventListener('click', e => {
@@ -87,17 +94,17 @@ class Lightbox {
8794
this.#replace && clearTimeout(this.#replace);
8895

8996
let offset;
90-
switch (e.key) {
91-
case 'Home': offset = 0; break;
92-
case 'End': offset = selector.length - 1; break;
93-
case 'ArrowLeft': offset = parseInt($target.dataset.offset) - 1; break;
94-
case 'ArrowRight': offset = parseInt($target.dataset.offset) + 1; break;
97+
switch (e.code) {
98+
case 'Home': this.#open($target, selector, 0); break;
99+
case 'End': this.#open($target, selector, selector.length - 1); break;
100+
case 'ArrowLeft': this.#open($target, selector, parseInt($target.dataset.offset) - 1); break;
101+
case 'ArrowRight': this.#open($target, selector, parseInt($target.dataset.offset) + 1); break;
102+
case 'Space': this.#activate($target); break;
95103
default: return;
96104
}
97105

98106
e.preventDefault();
99107
e.stopPropagation();
100-
this.#open($target, selector, offset);
101108
});
102109

103110
// Swipe left and right
@@ -127,27 +134,18 @@ class Lightbox {
127134
$target.querySelector('.next').style.visibility = null;
128135
$target.querySelector('.display').style.transform = null;
129136

130-
// Swipe was mostly vertical, ignore
131-
if (Math.abs(width) <= Math.abs(height)) return;
132-
133-
let offset;
134-
if (width > threshold) {
135-
offset = parseInt($target.dataset.offset) - 1;
136-
} else if (width < -threshold) {
137-
offset = parseInt($target.dataset.offset) + 1;
137+
e.stopPropagation();
138+
if (Math.abs(width) <= threshold && Math.abs(height) <= threshold) {
139+
this.#activate($target);
140+
} else if (Math.abs(width) <= Math.abs(height)) {
141+
// Swipe was mostly vertical, ignore
138142
} else {
139-
return;
143+
this.#open($target, selector, parseInt($target.dataset.offset) - Math.sign(width));
140144
}
141-
142-
e.stopPropagation();
143-
this.#open($target, selector, offset);
144145
});
145146

146147
$target.querySelector('.display').addEventListener('click', e => {
147148
e.stopPropagation();
148-
if (e.target instanceof HTMLVideoElement) {
149-
e.target.paused ? e.target.play() : e.target.pause();
150-
}
151149
});
152150
$target.addEventListener('close', e => {
153151
$target.querySelector('video')?.pause();

0 commit comments

Comments
 (0)