Skip to content

Commit 62d6742

Browse files
committed
Enable onAnimationModalInEnd callback
Closes #637
1 parent 12a82e7 commit 62d6742

File tree

3 files changed

+47
-26
lines changed

3 files changed

+47
-26
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
@@ -104,7 +104,11 @@ export default Component.extend({
104104
let isOutAnimation = animationName.substring(animationName.length - 4) === '-out';
105105
if (isOutAnimation) {
106106
this.modal._remove();
107+
108+
return;
107109
}
110+
111+
this.modal._options.onAnimationModalInEnd?.(animationName);
108112
};
109113

110114
let element = this._getElement();

tests/application/basics-test.js

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { visit, click, triggerKeyEvent, waitUntil } from '@ember/test-helpers';
22
import { setupApplicationTest } from 'ember-qunit';
33
import { module, test } from 'qunit';
44

5-
import sinon from 'sinon';
5+
import Modal1 from 'dummy/components/modal1';
66

77
import { setupPromiseModals } from 'ember-promise-modals/test-support';
88

@@ -71,6 +71,45 @@ module('Application | basics', function (hooks) {
7171
assert.dom('body', document).hasStyle({ overflow: 'visible' });
7272
});
7373

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

@@ -100,29 +139,4 @@ module('Application | basics', function (hooks) {
100139
foo: 'bar',
101140
});
102141
});
103-
104-
test('closing a modal will trigger the animation start on the `modals` service', async function (assert) {
105-
await visit('/');
106-
107-
let modalsService = this.owner.lookup('service:modals');
108-
let spy = sinon.spy(modalsService, '_onModalAnimationStart');
109-
110-
assert.dom('.epm-modal').doesNotExist();
111-
112-
await click('[data-test-show-modal]');
113-
114-
assert.dom('.epm-modal').exists();
115-
116-
await waitUntil(() => {
117-
let { opacity } = window.getComputedStyle(document.querySelector('.epm-backdrop'));
118-
return opacity === '1';
119-
});
120-
121-
assert.strictEqual(spy.callCount, 1, '_onModalAnimationStart is called when opening a modal');
122-
123-
await triggerKeyEvent(document, 'keydown', 'Escape');
124-
125-
assert.dom('.epm-modal').doesNotExist();
126-
assert.strictEqual(spy.callCount, 2, '_onModalAnimationStart is called again when closing it');
127-
});
128142
});

0 commit comments

Comments
 (0)