From 988f55a277373d119a4c37e7d4cce7e681147326 Mon Sep 17 00:00:00 2001 From: Fernando Fleury Date: Mon, 17 Dec 2018 14:04:41 +0100 Subject: [PATCH] [added] Introduce onAfterClose callback prop (#724) --- docs/README.md | 4 ++++ specs/Modal.events.spec.js | 12 ++++++++++++ src/components/ModalPortal.js | 5 +++++ 3 files changed, 21 insertions(+) diff --git a/docs/README.md b/docs/README.md index a507114e..96ae2b68 100644 --- a/docs/README.md +++ b/docs/README.md @@ -31,6 +31,10 @@ import ReactModal from 'react-modal'; Function that will be run after the modal has opened. */ onAfterOpen={handleAfterOpenFunc} + /* + Function that will be run after the modal has closed. + */ + onAfterClose={handleAfterCloseFunc} /* Function that will be run when the modal is requested to be closed (either by clicking on overlay or pressing ESC) Note: It is not called if isOpen is changed by other means. diff --git a/specs/Modal.events.spec.js b/specs/Modal.events.spec.js index c002ff2d..299b1ba2 100644 --- a/specs/Modal.events.spec.js +++ b/specs/Modal.events.spec.js @@ -24,6 +24,18 @@ export default () => { afterOpenCallback.called.should.be.ok(); }); + it("should trigger the onAfterClose callback", () => { + const onAfterCloseCallback = sinon.spy(); + const modal = renderModal({ + isOpen: true, + onAfterClose: onAfterCloseCallback + }); + + modal.portal.close(); + + onAfterCloseCallback.called.should.be.ok(); + }); + it("keeps focus inside the modal when child has no tabbable elements", () => { let tabPrevented = false; const modal = renderModal({ isOpen: true }, "hello"); diff --git a/src/components/ModalPortal.js b/src/components/ModalPortal.js index 4b37fef5..371da3bf 100644 --- a/src/components/ModalPortal.js +++ b/src/components/ModalPortal.js @@ -43,6 +43,7 @@ export default class ModalPortal extends Component { ariaHideApp: PropTypes.bool, appElement: PropTypes.instanceOf(SafeHTMLElement), onAfterOpen: PropTypes.func, + onAfterClose: PropTypes.func, onRequestClose: PropTypes.func, closeTimeoutMS: PropTypes.number, shouldFocusAfterRender: PropTypes.bool, @@ -183,6 +184,10 @@ export default class ModalPortal extends Component { focusManager.popWithoutFocus(); } } + + if (this.props.onAfterClose) { + this.props.onAfterClose(); + } }; open = () => {