react-modal icon indicating copy to clipboard operation
react-modal copied to clipboard

onRequestClose doesn't appear to work correctly

Open asdcdow opened this issue 9 years ago • 8 comments

Based on the the documentation, it sounds like onRequestClose is supposed to be called anytime the modal is closed. However, it appears to be called any time the overlay is clicked and it is never called when the isOpen prop changes from true to false.

Here is code example. If you click on the overlay, you will end up Close handled in the console, even though the modal is still open. If you click the Close Modal button, Close handled doesn't end up in the console, even though the modal has closed.

asdcdow avatar Aug 28 '17 03:08 asdcdow

@asdcdow onRequestClose is also intended to be called from the overlay (so you can decide if the modal should close or not)...and this function is responsible to change the state for the modal.

onRequestClose={() => {
  console.log('Close handled');
  this.handleCloseModal();
}}

diasbruno avatar Aug 28 '17 13:08 diasbruno

In that case, you may want to change the documentation here. The description makes it sound like whatever is passed to on onRequestClose will be called right before the modal closes (no matter what triggers the closing). However, really it is called whenever the overlay is clicked (whether that will trigger a close or not). I would argue that name should also also be changed to something like onOverlayClick since it doesn't actually have anything to do with whether the modal is closing unless you explicitly add logic to do so.

asdcdow avatar Aug 28 '17 18:08 asdcdow

[Edit] @asdcdow Actually, it's really a callback for the overlay. Rename it would require a major version change, so better fix the documentation. Can you improve the documentation for this?

diasbruno avatar Aug 28 '17 22:08 diasbruno

onRequestClose is also called when ESC is pressed, so it's not only a callback for the overlay.

@asdcdow I think onRequestClose is pretty appropriate. The modal only speaks for itself. If you change isOpen from the outside, then it's not the modal itself requesting to close, therefore the modal shouldn't request anything.

gustavopch avatar Sep 07 '17 15:09 gustavopch

@shivashp has updated the documentation (thanks). Requesting your approvals, @asdcdow and @gustavopch, to close this issue.

diasbruno avatar Sep 18 '17 21:09 diasbruno

@diasbruno I think the docs are pretty clear now.

gustavopch avatar Sep 19 '17 00:09 gustavopch

Thanks for making the usage clearer

asdcdow avatar Sep 19 '17 01:09 asdcdow

I was using "reactstrap" instead of "react-modal", and was missing the toggle event on my Modal element. I had the toggle attribute on my ModalHeader, but not on my Modal tag.

Adding toggle={this.toggle} onto my Modal made the modal go away when clicking off of it , on all devices.

FranticRock avatar Apr 26 '20 04:04 FranticRock