ahaha / src /ImageModal.jsx
zerinf
update logs, decisions, figures, layout
523133d
Raw History Blame Contribute Delete
2.1 kB
import { useEffect } from 'react';
import 'bootstrap/dist/css/bootstrap.min.css';
import './ImageModal.css';
function ImageModal({
modalState,
setModalState,
figures
}) {
const { show, currentIndex } = modalState;
const closeModal = () => setModalState({ ...modalState, show: false });
const prevImage = () => setModalState({
...modalState,
currentIndex: (currentIndex - 1 + figures.length) % figures.length
});
const nextImage = () => setModalState({
...modalState,
currentIndex: (currentIndex + 1) % figures.length
});
useEffect(() => {
const handleKeyDown = (e) => {
if (!show) return;
if (e.key === 'ArrowLeft') prevImage();
if (e.key === 'ArrowRight') nextImage();
if (e.key === 'Escape') closeModal();
};
window.addEventListener('keydown', handleKeyDown);
return () => window.removeEventListener('keydown', handleKeyDown);
}, [show, currentIndex, figures.length]);
if (!show || figures.length === 0) return null;
return (
<>
<div className="modal fade show d-block" tabIndex="-1" role="dialog" onClick={closeModal}>
<div className="modal-dialog modal-lg modal-dialog-centered" role="document" onClick={(e) => e.stopPropagation()}>
<div className="modal-content">
<div className="modal-body text-center">
<img
className="img-fluid"
src={`data:image/png;base64,${figures[currentIndex]}`}
alt={`Fig-${currentIndex}`}
/>
</div>
<div className="modal-footer justify-content-between">
<button className="btn btn-secondary" onClick={prevImage}>Prev</button>
<span>Image {currentIndex + 1}/{figures.length}</span>
<button className="btn btn-secondary" onClick={nextImage}>Next</button>
<button className="btn btn-danger" onClick={closeModal}>Close</button>
</div>
</div>
</div>
</div>
<div className="modal-backdrop fade show" />
</>
);
}
export default ImageModal;