How to prevent a simple React modal from being closed if you either start or end the click event inside the modal?

Ryan Peschel

I've been looking at a lot of modals made in React, and a lot of them seem to have this problem, so I have put together a simple example in jsFiddle to demonstrate the problem.

Code from the snippet:

handleOutsideClick = e => {
    if (e.target.id === "modal-view-info") {
      this.props.closeSelf();
    }
}

Basically, it works (clicking outside the modal closes it). The problem is that I want to make it so that the modal only closes if the click event starts and finishes outside the modal. That is, if you start the mousedown outside the modal but drag it inside the modal for the corresponding mouseup, the modal should not close. Similarly, if you start the click inside the modal and end it outside the modal, it should not close.

I've been messing with it for a while, and I believe the problem is that browsers consider the e.Target from a click event to be the parent element. That is, if you start clicking inside a child element, but then release the mouse button inside a parent element, the e.Target will be the parent element. e.Target will also be the parent element in the opposite case. This is making things tricky.

Is it possible to modify the snippet to incorporate this functionality? I'm already using this modal code all over my codebase so I'd prefer to just fix this, rather than go with a completely different design.

jered

Don't listen for a click event on the entire modal. Instead, put a hidden div behind the modal that will capture clicks that are only outside the modal.

CSS

.modal-close {
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
}

Component

class Modal extends React.Component {
  constructor(props) {
    super(props);

    }

  render() {
    return (
      <div>
        <div id="modal-view-info" className="modal" >
          <div className="modal-close" onClick={this.props.closeSelf} />
          <div className="modal-content">
            Hello how are you doing?
            Well I hope!
          </div>
        </div>
      </div>
    )
  }
}

https://jsfiddle.net/vdwr7Lg5/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Twitter Bootstrap / jQuery - How to temporarily prevent the modal from being closed?

Click event inside of a Modal - test

Prevent Boostrap modal closing after LinkButton Click inside modal

How to populate modal on event click?

How to prevent modal event listener from affecting other items?

react-responsive-modal : how to prevent closing of modal when we click outside the div

Prevent keyboard dismiss React Native inside Modal

Prevent keypress event listener from being triggered while bootstrap modal is open

Prevent AngularJS modal from closing based on logic inside the modal controller

How can I prevent the modal window from closing in React?

How to prevent modal click-through?

How can you prevent a click from firing at the end of a long press?

How to prevent page from scrolling to top after css3 modal is closed?

Modal Button Click Event

Button Click Event from inside a modal not firing when using angular.js and ui.bootstrap

How do you prevent Modal dialog from closing when users clicks outside of the modal dialog box In JQuery UI?

Catch event click on button inside a modal created by jquery

get active tab id in button click event inside a bootstrap modal

ng-click inside a form modal doesn't fire event

How to trigger API inside React Modal only when Modal is opened?

Prevent input inside a bootstrap modal losing focus when click on modal's scrollbar

Prevent modal from scrolling

Bootstrap Modal - catch link click inside modal?

How to listen to bootstrap modal close event in react

Prevent text to overrun inside modal

how to render a modal inside root element in react?

How to prevent from the keydown event of the enter key in the document when modal is open?

bootstrap how to detect which button closed a modal dialog on 'hidden_modal_bs' event func?

Prevent bootstrap modal from closing on submit button click