| 14 | } |
| 15 | |
| 16 | export default function ImagePreview({ current, images, onClick }: Props) { |
| 17 | const [index, setIndex] = useState(images.indexOf(current)); |
| 18 | useEffect(() => { |
| 19 | if (document.body) { |
| 20 | document.body.classList.add(styles.overflow); |
| 21 | } |
| 22 | }, []); |
| 23 | |
| 24 | function moveLeft() { |
| 25 | setIndex((index) => { |
| 26 | if (index === 0) { |
| 27 | return images.length - 1; |
| 28 | } |
| 29 | return index - 1; |
| 30 | }); |
| 31 | } |
| 32 | |
| 33 | function moveRight() { |
| 34 | setIndex((index) => { |
| 35 | if (index === images.length - 1) { |
| 36 | return 0; |
| 37 | } |
| 38 | return index + 1; |
| 39 | }); |
| 40 | } |
| 41 | |
| 42 | useKeyboard( |
| 43 | { |
| 44 | onKeyUp(event: KeyboardEvent) { |
| 45 | switch (event.key) { |
| 46 | case 'ArrowLeft': |
| 47 | return moveLeft(); |
| 48 | case 'ArrowRight': |
| 49 | return moveRight(); |
| 50 | } |
| 51 | }, |
| 52 | }, |
| 53 | [images] |
| 54 | ); |
| 55 | |
| 56 | const image = images[index]; |
| 57 | |
| 58 | return ( |
| 59 | <Portal id="preview-portal"> |
| 60 | <Preview |
| 61 | onClick={(event) => { |
| 62 | event.preventDefault(); |
| 63 | event.stopPropagation(); |
| 64 | onClick(); |
| 65 | if (document.body) { |
| 66 | document.body.classList.remove(styles.overflow); |
| 67 | } |
| 68 | }} |
| 69 | > |
| 70 | <img className={styles.preview} src={image} /> |
| 71 | {images.length > 1 && ( |
| 72 | <nav |
| 73 | className={styles.options} |