navigate modal images with left / right / up / down keys

This commit is contained in:
Martti Malmi 2023-10-05 14:44:39 +03:00
parent 7587db9c8b
commit 11c32c2745

View File

@ -1,5 +1,5 @@
import "./SpotlightMedia.css";
import { useMemo, useState } from "react";
import { useEffect, useMemo, useState } from "react";
import Modal from "Element/Modal";
import Icon from "Icons/Icon";
import { ProxyImg } from "Element/ProxyImg";
@ -17,6 +17,24 @@ export function SpotlightMedia(props: SpotlightMediaProps) {
return props.images.at(idx % props.images.length);
}, [idx, props]);
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
switch (e.key) {
case "ArrowLeft":
case "ArrowUp":
dec();
break;
case "ArrowRight":
case "ArrowDown":
inc();
break;
}
};
document.addEventListener("keydown", handleKeyDown);
return () => document.removeEventListener("keydown", handleKeyDown);
}, []);
function dec() {
setIdx(s => {
if (s - 1 === -1) {
@ -36,6 +54,7 @@ export function SpotlightMedia(props: SpotlightMediaProps) {
}
});
}
return (
<div className="spotlight">
<ProxyImg src={image} />