Skip to content

Commit

Permalink
add optional onOutAnimationEnd hook
Browse files Browse the repository at this point in the history
  • Loading branch information
nickschot committed Jul 27, 2021
1 parent 428f91d commit 060bc26
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ this.modals.open(
// You most likely do not have to do this unless you absolutely
// can't have an animation ending in '-out'
animationKeyframesOutName: 'custom-animation-name-out',
// optional: a hook that is called when the closing animation of
// the modal has finished.
onOutAnimationEnd: () => {}
},
);
```
Expand Down
3 changes: 3 additions & 0 deletions addon/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export default class Modal {
_resolve(result) {
if (!this._deferredOutAnimation) {
set(this, '_deferredOutAnimation', defer());
if (this._options.onOutAnimationEnd) {
this._deferredOutAnimation.promise.then(() => this._options.onOutAnimationEnd()).catch(() => {});
}

this._result = result;
this._deferred.resolve(result);
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/services/modals-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,29 @@ module('Service | modals', function (hooks) {

assert.equal(modals.count, 0);
});

test('modals will call the optional onOutAnimationEnd hook when it is passed as an option', async function (assert) {
let modals = this.owner.lookup('service:modals');
let modal = modals.open(
'modal',
{},
{
onOutAnimationEnd: () => {
assert.step('animation ended');
},
},
);
assert.step('modal open');

modal._resolve();
assert.step('modal closing');

modal._remove();
assert.step('modal closed');

// we need to wait a microtick for the closing animation promise to be resolved
await Promise.resolve();

assert.verifySteps(['modal open', 'modal closing', 'modal closed', 'animation ended']);
});
});

0 comments on commit 060bc26

Please sign in to comment.