Skip to content

Commit

Permalink
Add logics for handling keyboard event
Browse files Browse the repository at this point in the history
Closes #5
  • Loading branch information
marshallku committed Nov 18, 2024
1 parent d4e2cc3 commit d2eb85d
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/zoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ const zoom = (image: HTMLImageElement, { background, useMaximumSize = true, onTr
const bg = document.createElement("div");
const imageClone = document.createElement("img");

const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === "Escape") {
removeImage();
}
};

const removeImage = () => {
bg.classList.remove("zoom-bg--reveal");
imageClone.style.transform = "";
Expand All @@ -37,6 +43,7 @@ const zoom = (image: HTMLImageElement, { background, useMaximumSize = true, onTr
imageClone.removeEventListener("click", removeImage);
window.removeEventListener("scroll", removeImage);
window.removeEventListener("resize", removeImage);
document.removeEventListener("keydown", handleKeyDown);
};

bg.classList.add("zoom-bg");
Expand Down Expand Up @@ -77,6 +84,9 @@ const zoom = (image: HTMLImageElement, { background, useMaximumSize = true, onTr
once: true,
passive: true,
});
document.addEventListener("keydown", handleKeyDown, {
passive: true,
});

document.body.append(bg, imageClone);

Expand Down

0 comments on commit d2eb85d

Please sign in to comment.