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

[1.0.23] Modal - Dragging outside the modal closes it #712

Open
TimBroddin opened this issue Nov 22, 2024 · 0 comments
Open

[1.0.23] Modal - Dragging outside the modal closes it #712

TimBroddin opened this issue Nov 22, 2024 · 0 comments

Comments

@TimBroddin
Copy link

Quick summary

When dragging outside a modal (probably any dialog) it gets closed. The click event triggers on mouse up, and has the clientX & clientY from where the mouse was released.

Image

Workaround/fix

I was able to fix this by adding this code inside dialogable.js:

 if (this.options().clickOutside) {
            let isInteractingInside = false;

            // Track mousedown events inside the dialog
            this.el.addEventListener('mousedown', e => {
                if (e.target !== this.el) {
                    isInteractingInside = true;
                }
            });

            // Reset the tracking on mouseup
            window.addEventListener('mouseup', () => {
                setTimeout(() => {
                    isInteractingInside = false;
                }, 0);
            });

            // Clicking outside the dialog should close it...
            this.el.addEventListener('click', e => {
                // Clicking the ::backdrop pseudo-element is treated the same as clicking the <dialog> element itself
                // Therefore, we can dissregard clicks on any element other than the <dialog> element itself...
                if (e.target !== this.el) return

                // Don't close if the interaction started inside the modal
                if (isInteractingInside) return

                // Again, because we can't listen for clicks on ::backdrop, we have to test for intersection
                // between the click and the visible parts of the dialog elements...
                if (clickHappenedOutside(this.el, e)) {
                    this.hide()

                    e.preventDefault(); e.stopPropagation()
                }
            })
        }
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

1 participant