Skip to content

Commit

Permalink
[added] Introduce onAfterClose callback prop (reactjs#724)
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandofleury authored and diasbruno committed Dec 17, 2018
1 parent d4a8a32 commit 988f55a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions specs/Modal.events.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
5 changes: 5 additions & 0 deletions src/components/ModalPortal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -183,6 +184,10 @@ export default class ModalPortal extends Component {
focusManager.popWithoutFocus();
}
}

if (this.props.onAfterClose) {
this.props.onAfterClose();
}
};

open = () => {
Expand Down

0 comments on commit 988f55a

Please sign in to comment.