Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close overlay on click outside image #205

Open
parad0xe opened this issue Nov 20, 2023 · 1 comment
Open

Close overlay on click outside image #205

parad0xe opened this issue Nov 20, 2023 · 1 comment

Comments

@parad0xe
Copy link

No integrated support for closing the viewer when clicking on the overlay.

Temporary fix proposal using an wrapper (In an ImageViewer.tsx file for example):

import { forwardRef, memo, useEffect, useRef, useState } from "react"
import Viewer from "react-viewer"
import ViewerProps from "react-viewer/lib/ViewerProps"

export const ImageViewer = memo(function ImageViewer(props: ViewerProps) {
    useEffect(() => {
        if(props.visible) {
            const handleDocumentClick = (element: PointerEvent) => {
                if((element.target as HTMLElement).parentElement.classList.contains("react-viewer")) {
                    props.onClose()
                }
            }

            document.addEventListener('click', handleDocumentClick)

            return () => {
                document.removeEventListener('click', handleDocumentClick)
            }
        }
    }, [props.visible])

    return <Viewer {...props} />
})
@kocunop
Copy link

kocunop commented Sep 20, 2024

use onMaskClick prop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants