Skip to content

Commit 17ec421

Browse files
committed
Enable onAnimationModalInEnd callback
Closes #637
1 parent e65f524 commit 17ec421

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ this.modals.open(
141141
// optional: a hook that is called when the closing animation of
142142
// the modal (so not the backdrop) has finished.
143143
onAnimationModalOutEnd: () => {},
144+
// optional: a hook that is called when the opening animation of
145+
// the modal (so not the backdrop) has finished.
146+
onAnimationModalInEnd: () => {},
144147
},
145148
);
146149
```

addon/components/modal.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ export default Component.extend({
7474

7575
if (isOutAninmation) {
7676
this.removeModal();
77+
78+
return;
7779
}
80+
81+
this.modal.onAnimationModalInEnd(animationName);
7882
};
7983

8084
this.modals._onModalAnimationStart();

addon/modal.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export default class Modal {
2727
this._options = {
2828
className: '',
2929
onAnimationModalOutEnd: undefined,
30+
onAnimationModalInEnd: undefined,
3031
...options,
3132
};
3233
this._result = undefined;
@@ -39,6 +40,14 @@ export default class Modal {
3940
return this._result;
4041
}
4142

43+
onAnimationModalInEnd() {
44+
if (!this._options.onAnimationModalInEnd) {
45+
return;
46+
}
47+
48+
return this._options.onAnimationModalInEnd(...arguments);
49+
}
50+
4251
@computed('_deferredOutAnimation')
4352
get isClosing() {
4453
return Boolean(this._deferredOutAnimation);

tests/application/basics-test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { visit, click, triggerKeyEvent, waitUntil } from '@ember/test-helpers';
22
import { setupApplicationTest } from 'ember-qunit';
33
import { module, test } from 'qunit';
4+
import Modal1 from 'dummy/components/modal1';
45

56
import { setupPromiseModals } from 'ember-promise-modals/test-support';
67

@@ -69,6 +70,45 @@ module('Application | basics', function (hooks) {
6970
assert.dom('body', document).hasStyle({ overflow: 'visible' });
7071
});
7172

73+
test('opening a modal calls onAnimationModal*End once the animation ends', async function (assert) {
74+
await visit('/');
75+
76+
assert.dom('.epm-backdrop').doesNotExist();
77+
assert.dom('.epm-modal').doesNotExist();
78+
79+
const applicationController = this.owner.lookup('controller:application');
80+
const modalsService = applicationController.modals;
81+
const showModal = applicationController.actions.showModal;
82+
83+
applicationController.actions.showModal = () => {
84+
assert.step('open');
85+
86+
modalsService.open(Modal1, {}, {
87+
onAnimationModalInEnd: () => {
88+
assert.step('onAnimationModalInEnd');
89+
},
90+
onAnimationModalOutEnd: () => {
91+
assert.step('onAnimationModalOutEnd');
92+
},
93+
});
94+
}
95+
96+
await click('[data-test-show-modal]');
97+
await waitUntil(() => !document.body.classList.contains('epm-animating') );
98+
await click('.epm-backdrop');
99+
100+
assert.dom('.epm-backdrop').doesNotExist();
101+
assert.dom('.epm-modal').doesNotExist();
102+
103+
assert.verifySteps([
104+
'open',
105+
'onAnimationModalInEnd',
106+
'onAnimationModalOutEnd',
107+
]);
108+
109+
applicationController.actions.showModal = showModal;
110+
});
111+
72112
test('pressing the Escape keyboard button closes the modal', async function (assert) {
73113
await visit('/');
74114

0 commit comments

Comments
 (0)