({
dialogState,
setDialogState,
})
| 12 | } |
| 13 | |
| 14 | const ImageModal: React.FC<ImageModalProps> = ({ |
| 15 | dialogState, |
| 16 | setDialogState, |
| 17 | }) => { |
| 18 | const [isCopied, setIsCopied] = useState(false) |
| 19 | |
| 20 | const [isDownloaded, setIsDownloaded] = useState(false) |
| 21 | |
| 22 | const { imgSrc, open, prompt } = dialogState |
| 23 | |
| 24 | const modalRef = useRef<HTMLDivElement>(null) |
| 25 | |
| 26 | useEffect(() => { |
| 27 | if (open) { |
| 28 | document.body.style.overflow = 'hidden' |
| 29 | document.addEventListener('click', handleClickOutside) |
| 30 | } else { |
| 31 | document.body.style.overflow = 'unset' |
| 32 | document.removeEventListener('click', handleClickOutside) |
| 33 | } |
| 34 | |
| 35 | return () => { |
| 36 | document.body.style.overflow = 'unset' |
| 37 | document.removeEventListener('click', handleClickOutside) |
| 38 | setIsCopied(false) |
| 39 | setIsDownloaded(false) |
| 40 | } |
| 41 | // eslint-disable-next-line react-hooks/exhaustive-deps |
| 42 | }, [open]) |
| 43 | |
| 44 | const clearModalContent = () => { |
| 45 | setTimeout(() => { |
| 46 | setDialogState({ |
| 47 | ...dialogState, |
| 48 | open: false, |
| 49 | imgSrc: '', |
| 50 | prompt: '', |
| 51 | }) |
| 52 | }, 500) |
| 53 | } |
| 54 | |
| 55 | const handleClickOutside = (event: MouseEvent) => { |
| 56 | if ( |
| 57 | modalRef.current && |
| 58 | !modalRef.current.contains(event.target as Node) |
| 59 | ) { |
| 60 | setDialogState({ |
| 61 | ...dialogState, |
| 62 | open: false, |
| 63 | }) |
| 64 | clearModalContent() |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | const handleClose = () => { |
| 69 | setDialogState({ ...dialogState, open: false }) |
| 70 | clearModalContent() |
| 71 | } |
nothing calls this directly
no test coverage detected