Skip to content

Commit

Permalink
Removed Enter to launch fullscreen
Browse files Browse the repository at this point in the history
  • Loading branch information
marekdedic committed Oct 6, 2024
1 parent 4a22a2f commit 0418e52
Showing 1 changed file with 17 additions and 30 deletions.
47 changes: 17 additions & 30 deletions src/lib/keyboard-navigation.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,32 @@
import { fullscreenEnabled, toggleFullscreen } from "./fullscreen";

let keyHandler: ((e: KeyboardEvent) => void) | null = null;

function fullscreenKeyHandler(e: KeyboardEvent): void {
if (e.key === "Enter") {
e.preventDefault();
toggleFullscreen();
}
}

export function addKeyboardNavigation(
options: ILBOptions,
closeLightbox: () => void,
previousImage: () => void,
nextImage: () => void,
): void {
if (options.fullscreen && fullscreenEnabled) {
document.addEventListener("keypress", fullscreenKeyHandler);
}

if (options.enableKeyboard) {
keyHandler = (e): void => {
if (options.quitOnEscKey && e.key === "Escape") {
e.preventDefault();
closeLightbox();
}
if (e.key === "ArrowLeft") {
e.preventDefault();
previousImage();
}
if (e.key === "ArrowRight") {
e.preventDefault();
nextImage();
}
};
document.addEventListener("keyup", keyHandler);
if (!options.enableKeyboard) {
return;
}
keyHandler = (e): void => {
if (options.quitOnEscKey && e.key === "Escape") {
e.preventDefault();
closeLightbox();
}
if (e.key === "ArrowLeft") {
e.preventDefault();
previousImage();
}
if (e.key === "ArrowRight") {
e.preventDefault();
nextImage();
}
};
document.addEventListener("keyup", keyHandler);
}

export function removeKeyboardNavigation(): void {
document.removeEventListener("keypress", fullscreenKeyHandler);
if (keyHandler !== null) {
document.removeEventListener("keyup", keyHandler);
}
Expand Down

0 comments on commit 0418e52

Please sign in to comment.