Skip to content

Commit 79efb4a

Browse files
kyletsangTheSharpieOne
authored andcommitted
fix(Modal): Prevent static backdrop animation when modal clicked (#1755)
1 parent ce67559 commit 79efb4a

2 files changed

Lines changed: 91 additions & 3 deletions

File tree

‎src/Modal.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,14 @@ class Modal extends React.Component {
209209
if (e.target === this._mouseDownElement) {
210210
e.stopPropagation();
211211

212-
if (this.props.backdrop === 'static') {
212+
const backdrop = this._dialog ? this._dialog.parentNode : null;
213+
214+
if (backdrop && e.target === backdrop && this.props.backdrop === 'static') {
213215
this.handleStaticBackdropAnimation();
214216
}
215217

216218
if (!this.props.isOpen || this.props.backdrop !== true) return;
217219

218-
const backdrop = this._dialog ? this._dialog.parentNode : null;
219-
220220
if (backdrop && e.target === backdrop && this.props.toggle) {
221221
this.props.toggle(e);
222222
}

‎src/__tests__/Modal.spec.js‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,94 @@ describe('Modal', () => {
657657
wrapper.unmount();
658658
});
659659

660+
it('should animate when backdrop is "static" and escape key pressed', () => {
661+
isOpen = true;
662+
const wrapper = didMount(
663+
<Modal isOpen={isOpen} toggle={toggle} backdrop="static">
664+
<button id="clicker">Does Nothing</button>
665+
</Modal>
666+
);
667+
const spy = jest.spyOn(wrapper.instance(), 'handleStaticBackdropAnimation');
668+
669+
jest.runTimersToTime(300);
670+
671+
expect(isOpen).toBe(true);
672+
expect(document.getElementsByClassName('modal').length).toBe(1);
673+
674+
const escapeKeyUpEvent = {
675+
keyCode: keyCodes.esc,
676+
preventDefault: jest.fn(() => {}),
677+
stopPropagation: jest.fn(() => {}),
678+
};
679+
680+
wrapper.instance().handleEscape(escapeKeyUpEvent);
681+
expect(spy).toHaveBeenCalled();
682+
683+
wrapper.unmount();
684+
});
685+
686+
it('should animate when backdrop is "static" and backdrop is clicked', () => {
687+
isOpen = true;
688+
const wrapper = didMount(
689+
<Modal isOpen={isOpen} toggle={toggle} backdrop="static">
690+
<button id="clicker">Does Nothing</button>
691+
</Modal>
692+
);
693+
694+
const spy = jest.spyOn(wrapper.instance(), 'handleStaticBackdropAnimation');
695+
696+
jest.runTimersToTime(300);
697+
698+
expect(isOpen).toBe(true);
699+
expect(document.getElementsByClassName('modal').length).toBe(1);
700+
701+
const modal = document.getElementsByClassName('modal')[0];
702+
703+
const mouseDownEvent = document.createEvent('MouseEvents');
704+
mouseDownEvent.initEvent('mousedown', true, true);
705+
modal.dispatchEvent(mouseDownEvent);
706+
707+
const clickEvent = document.createEvent('MouseEvents');
708+
clickEvent.initEvent('click', true, true);
709+
modal.dispatchEvent(clickEvent);
710+
711+
jest.runTimersToTime(300);
712+
expect(spy).toHaveBeenCalled();
713+
714+
wrapper.unmount();
715+
});
716+
717+
it('should not animate when backdrop is "static" and modal is clicked', () => {
718+
isOpen = true;
719+
const wrapper = didMount(
720+
<Modal isOpen={isOpen} toggle={toggle} backdrop="static">
721+
<button id="clicker">Does Nothing</button>
722+
</Modal>
723+
);
724+
725+
const spy = jest.spyOn(wrapper.instance(), 'handleStaticBackdropAnimation');
726+
727+
jest.runTimersToTime(300);
728+
729+
expect(isOpen).toBe(true);
730+
expect(document.getElementsByClassName('modal').length).toBe(1);
731+
732+
const modalDialog = document.getElementsByClassName('modal-dialog')[0];
733+
734+
const mouseDownEvent = document.createEvent('MouseEvents');
735+
mouseDownEvent.initEvent('mousedown', true, true);
736+
modalDialog.dispatchEvent(mouseDownEvent);
737+
738+
const clickEvent = document.createEvent('MouseEvents');
739+
clickEvent.initEvent('click', true, true);
740+
modalDialog.dispatchEvent(clickEvent);
741+
742+
jest.runTimersToTime(300);
743+
expect(spy).not.toHaveBeenCalled();
744+
745+
wrapper.unmount();
746+
});
747+
660748
it('should destroy this._element', () => {
661749
isOpen = true;
662750
const wrapper = didMount(

0 commit comments

Comments
 (0)