2022-05-07 12:00:00 +00:00
|
|
|
import { Dispatch, SetStateAction, useCallback } from "react";
|
|
|
|
import { useHotkeys } from "react-hotkeys-hook";
|
|
|
|
import Img from "./Img";
|
|
|
|
import Button from "./Inputs/Button";
|
|
|
|
import Popup from "./Popup";
|
2022-03-18 00:54:10 +00:00
|
|
|
|
2022-04-03 08:34:21 +00:00
|
|
|
interface Props {
|
2022-03-18 00:54:10 +00:00
|
|
|
setState:
|
2022-03-27 15:01:14 +00:00
|
|
|
| Dispatch<SetStateAction<boolean | undefined>>
|
|
|
|
| Dispatch<SetStateAction<boolean>>;
|
2022-03-18 00:54:10 +00:00
|
|
|
state: boolean;
|
2022-03-18 12:51:48 +00:00
|
|
|
images: string[];
|
|
|
|
index: number;
|
|
|
|
setIndex: Dispatch<SetStateAction<number>>;
|
2022-04-03 08:34:21 +00:00
|
|
|
}
|
2022-03-18 00:54:10 +00:00
|
|
|
|
2022-04-03 08:34:21 +00:00
|
|
|
export default function LightBox(props: Props): JSX.Element {
|
2022-03-18 12:51:48 +00:00
|
|
|
const { state, setState, images, index, setIndex } = props;
|
2022-05-07 12:00:00 +00:00
|
|
|
const handlePrevious = useCallback(() => {
|
|
|
|
setIndex((previousIndex) => (previousIndex > 0 ? previousIndex - 1 : 0));
|
|
|
|
}, [setIndex]);
|
|
|
|
|
|
|
|
const handleNext = useCallback(() => {
|
|
|
|
setIndex((previousIndex) =>
|
|
|
|
previousIndex < images.length - 1 ? previousIndex + 1 : images.length - 1
|
|
|
|
);
|
|
|
|
}, [images.length, setIndex]);
|
|
|
|
|
|
|
|
useHotkeys("left", handlePrevious);
|
|
|
|
useHotkeys("right", handleNext);
|
2022-03-18 13:01:25 +00:00
|
|
|
|
2022-03-18 00:54:10 +00:00
|
|
|
return (
|
2022-03-18 12:51:48 +00:00
|
|
|
<>
|
|
|
|
{state && (
|
2022-05-07 12:00:00 +00:00
|
|
|
<Popup setState={setState} state={state} fillViewport>
|
|
|
|
<div
|
|
|
|
className="grid grid-cols-[4em,1fr,4em] place-items-center
|
|
|
|
gap-4 w-full h-full overflow-hidden"
|
|
|
|
>
|
|
|
|
<div>
|
|
|
|
{index > 0 && (
|
|
|
|
<Button onClick={handlePrevious}>
|
|
|
|
<span className="material-icons">chevron_left</span>
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<Img className="max-h-full" image={images[index]} />
|
|
|
|
|
|
|
|
<div>
|
|
|
|
{index < images.length - 1 && (
|
|
|
|
<Button onClick={handleNext}>
|
|
|
|
<span className="material-icons">chevron_right</span>
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Popup>
|
2022-03-18 12:51:48 +00:00
|
|
|
)}
|
|
|
|
</>
|
2022-03-18 00:54:10 +00:00
|
|
|
);
|
|
|
|
}
|