Skip to content

Commit

Permalink
Added beforeClose option to modals
Browse files Browse the repository at this point in the history
We have unsaved changes confirmation modals that open when a close of another modal (typically attached to a route) is attempted. When that occurs we want to stop the original modal from closing until the confirmation modal promise is complete.

- when `closeModal()` is called, check if `beforeClose()` was passed in as an option, if it was then call it and await the result, if the result is `false` then the close is cancelled
  • Loading branch information
kevinansfield committed Mar 11, 2022
1 parent e302f7f commit a36814e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -4,3 +4,4 @@ Not recommended for general use, this package will be removed if upstream become

Custom changes:
- added `.finally()` to modal instances so the Promise-like interface is more complete. Useful for cleanup after a modal is resolved/rejected without duplication
- added `beforeClose()` as an option that can be passed when opening a modal, if the function returns `false` then the modal close will be aborted
9 changes: 8 additions & 1 deletion addon/components/modal.js
Expand Up @@ -95,7 +95,14 @@ export default Component.extend({
this._super(...arguments);
},

closeModal(result) {
async closeModal(result) {
if (this.modal._options.beforeClose) {
let allowClose = await this.modal._options.beforeClose(result);
if (allowClose === false) {
return;
}
}

// Trigger out animation
set(this, 'animatingOutClass', this.outAnimationClass);

Expand Down

0 comments on commit a36814e

Please sign in to comment.