ember-promise-modals 3.0.1 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,6 +13,7 @@ export default Component.extend({
13
13
 
14
14
  tagName: '',
15
15
  outAnimationClass: 'epm-out',
16
+ animatingClass: '',
16
17
 
17
18
  modals: service(),
18
19
 
@@ -20,11 +21,13 @@ export default Component.extend({
20
21
 
21
22
  modalElementId: null,
22
23
  focusTrapOptions: null,
24
+ _animationEnd: null,
23
25
 
24
26
  init() {
25
27
  this._super(...arguments);
26
28
 
27
29
  set(this, 'modalElementId', guidFor(this));
30
+
28
31
  this.modal._componentInstance = this;
29
32
 
30
33
  let { focusTrapOptions: globalFocusTrapOptions } = this.modals;
@@ -38,79 +41,121 @@ export default Component.extend({
38
41
  didInsertElement() {
39
42
  this._super(...arguments);
40
43
 
41
- let element = document.getElementById(this.modalElementId);
44
+ this._addFocusTrap();
45
+ this._addAnimationListeners();
42
46
 
43
- if (this.focusTrapOptions) {
44
- let options = {
45
- ...this.focusTrapOptions,
46
- fallbackFocus: `#${this.modalElementId}`,
47
- onPostDeactivate: (...args) => {
48
- this.focusTrapOptions.onPostDeactivate?.(...args);
47
+ // animating in starts when the element is added to the DOM. Inform the service about it.
48
+ this.modals._onModalAnimationStart();
49
+ },
49
50
 
50
- if (this.isDestroyed || this.isDestroying) {
51
- return;
52
- }
51
+ willDestroyElement() {
52
+ this.destroyModal();
53
53
 
54
- this.closeModal();
55
- },
56
- };
54
+ this._super(...arguments);
55
+ },
57
56
 
58
- this.focusTrap = createFocusTrap(element, options);
59
- this.focusTrap.activate();
60
- }
57
+ _getElement() {
58
+ return document.getElementById(this.modalElementId);
59
+ },
61
60
 
62
- this.fadeOutEnd = ({ target, animationName }) => {
63
- this.modals._onModalAnimationEnd();
61
+ _addFocusTrap() {
62
+ let element = this._getElement();
64
63
 
65
- let isntTarget = target !== element;
66
- let animationEndsWrong = animationName.substring(animationName.length - 4) !== '-out';
64
+ if (!this.focusTrapOptions) {
65
+ return;
66
+ }
67
67
 
68
- if (isntTarget || animationEndsWrong) {
69
- return;
70
- }
68
+ let options = {
69
+ ...this.focusTrapOptions,
70
+ fallbackFocus: element,
71
+ onDeactivate: (...args) => {
72
+ this.focusTrapOptions.onDeactivate?.(...args);
73
+
74
+ if (this.isDestroyed || this.isDestroying) {
75
+ return;
76
+ }
71
77
 
72
- this.modal._remove();
78
+ this.closeModal();
79
+ },
73
80
  };
74
81
 
75
- this.modals._onModalAnimationStart();
76
- element.addEventListener('animationend', this.fadeOutEnd);
77
- set(this, 'animatingOutClass', '');
82
+ this.focusTrap = createFocusTrap(element, options);
83
+ this.focusTrap.activate();
78
84
  },
79
85
 
80
- willDestroyElement() {
81
- if (this.focusTrap) {
82
- this.focusTrap.deactivate({ onDeactivate: null });
86
+ _removeFocusTrap(onDeactivate = this.focusTrapOptions?.onDeactivate) {
87
+ if (!this.focusTrap) {
88
+ return;
83
89
  }
84
90
 
85
- if (this.fadeOutEnd) {
86
- let element = document.getElementById(this.modalElementId);
91
+ this.focusTrap.deactivate({ onDeactivate });
92
+ },
93
+
94
+ _addAnimationListeners() {
95
+ this._animationEnd = ({ target, animationName }) => {
96
+ // ignore animations bubbling up
97
+ if (target !== this._getElement()) {
98
+ return;
99
+ }
100
+
101
+ // An animation has ended, inform the modals service
102
+ this.modals._onModalAnimationEnd();
87
103
 
88
- if (element) {
89
- element.removeEventListener('animationend', this.fadeOutEnd);
90
- // make sure that we remove the modal, also resolving the test waiter
104
+ let isOutAnimation = animationName.substring(animationName.length - 4) === '-out';
105
+ if (isOutAnimation) {
91
106
  this.modal._remove();
92
107
  }
108
+ };
109
+
110
+ let element = this._getElement();
111
+ if (element) {
112
+ element.addEventListener('animationend', this._animationEnd);
113
+ }
114
+ },
115
+
116
+ _removeAnimationListeners() {
117
+ if (!this._animationEnd) {
118
+ return;
93
119
  }
94
120
 
95
- this._super(...arguments);
121
+ let element = this._getElement();
122
+ if (element) {
123
+ element.removeEventListener('animationend', this._animationEnd);
124
+ }
125
+
126
+ this._animationEnd = null;
96
127
  },
97
128
 
98
- closeModal(result) {
99
- // Trigger out animation
100
- set(this, 'animatingOutClass', this.outAnimationClass);
129
+ destroyModal() {
130
+ // Remove the focus trap without triggering the optional onDeactivate() hook
131
+ this._removeFocusTrap(null);
132
+
133
+ // Remove the animation listeners
134
+ this._removeAnimationListeners();
101
135
 
102
- if (this.focusTrap) {
103
- this.focusTrap.deactivate({
104
- onDeactivate: this.focusTrapOptions.onDeactivate,
105
- });
136
+ // make sure that we remove the modal, also resolving the test waiter
137
+ this.modal._remove();
138
+ },
139
+
140
+ closeModal(result) {
141
+ if (this.animatingClass !== '') {
142
+ return;
106
143
  }
107
144
 
145
+ this.modals._onModalAnimationStart();
146
+
147
+ // This triggers the out animation, which in turn will remove the modal after it completes
148
+ set(this, 'animatingClass', this.outAnimationClass);
149
+
108
150
  this.modal._resolve(result);
109
151
  },
110
152
 
111
153
  actions: {
112
154
  close(result) {
113
155
  this.closeModal(result);
156
+
157
+ // Remove the focus trap, triggering the onDeactivate() hook
158
+ this._removeFocusTrap();
114
159
  },
115
160
  },
116
161
  });
@@ -0,0 +1,33 @@
1
+ import Helper from '@ember/component/helper';
2
+ import { inject as service } from '@ember/service';
3
+
4
+ export default class OpenModalHelper extends Helper {
5
+ @service modals;
6
+
7
+ modal = null;
8
+
9
+ compute([modalComponent, data, options], { close }) {
10
+ this.showModal(modalComponent, data, options, close);
11
+ }
12
+
13
+ async showModal(modalComponent, data, options, close) {
14
+ this.modal = this.modals.open(modalComponent, data, options);
15
+
16
+ let success = await this.modal;
17
+
18
+ this.modal = null;
19
+
20
+ if (close) {
21
+ close(success);
22
+ }
23
+ }
24
+
25
+ hideModal() {
26
+ this.modal?.close();
27
+ }
28
+
29
+ willDestroy() {
30
+ this.hideModal();
31
+ super.willDestroy(...arguments);
32
+ }
33
+ }
package/addon/modal.js CHANGED
@@ -13,16 +13,16 @@ import { defer } from 'rsvp';
13
13
  /**
14
14
  * @class Modal
15
15
  * @param {Service} service
16
- * @param {String} name
16
+ * @param {String} componentClass
17
17
  * @param {any} data
18
18
  * @param {ModalOptions} options
19
19
  * @method close - closes the modal
20
20
  * @method then - resolves when the modal is closed
21
21
  */
22
22
  export default class Modal {
23
- constructor(service, name, data, options = {}) {
23
+ constructor(service, componentClass, data, options = {}) {
24
24
  this._service = service;
25
- this._name = name;
25
+ this._componentClass = componentClass;
26
26
  this._data = data;
27
27
  this._options = {
28
28
  className: '',
@@ -54,18 +54,28 @@ export default class Modal {
54
54
  return this._deferred.promise.then(onFulfilled, onRejected);
55
55
  }
56
56
 
57
- _resolve(result) {
58
- if (!this._deferredOutAnimation) {
59
- set(this, '_deferredOutAnimation', defer());
60
- if (this._options.onAnimationModalOutEnd) {
61
- this._deferredOutAnimation.promise.then(() => this._options.onAnimationModalOutEnd()).catch(() => {});
62
- }
57
+ _destroy() {
58
+ if (!this._componentInstance) {
59
+ return;
60
+ }
63
61
 
64
- this._result = result;
65
- this._deferred.resolve(result);
62
+ this._componentInstance.destroyModal();
63
+ }
66
64
 
67
- waitForPromise(this._deferredOutAnimation.promise);
65
+ _resolve(result) {
66
+ if (this._deferredOutAnimation) {
67
+ return;
68
68
  }
69
+
70
+ set(this, '_deferredOutAnimation', defer());
71
+ if (this._options.onAnimationModalOutEnd) {
72
+ this._deferredOutAnimation.promise.then(() => this._options.onAnimationModalOutEnd()).catch(() => {});
73
+ }
74
+
75
+ this._result = result;
76
+ this._deferred.resolve(result);
77
+
78
+ waitForPromise(this._deferredOutAnimation.promise);
69
79
  }
70
80
 
71
81
  _remove() {
@@ -1,4 +1,5 @@
1
1
  import { A } from '@ember/array';
2
+ import { deprecate } from '@ember/debug';
2
3
  import { computed } from '@ember/object';
3
4
  import { alias } from '@ember/object/computed';
4
5
  import Service from '@ember/service';
@@ -20,23 +21,41 @@ export default Service.extend({
20
21
  if (this.focusTrapOptions !== null) {
21
22
  this.focusTrapOptions = {
22
23
  ...(this.focusTrapOptions ?? {}),
23
- clickOutsideDeactivates: this.focusTrapOptions?.clickOutsideDeactivate ?? true,
24
+ clickOutsideDeactivates: this.focusTrapOptions?.clickOutsideDeactivates ?? true,
24
25
  };
25
26
  }
26
27
  },
27
28
 
28
29
  willDestroy() {
29
30
  this._onLastModalRemoved();
31
+ this._onModalAnimationEnd();
32
+ this._destroyModals();
33
+
34
+ this._super(...arguments);
30
35
  },
31
36
 
32
37
  /**
33
- * @param {string} name component path
38
+ * @param {*} componentClass component class
34
39
  * @param {*} data passed to the component template
35
40
  * @param {ModalOptions} options applied to the modal
36
41
  * @returns {Modal}
37
42
  */
38
- open(name, data, options) {
39
- let modal = new Modal(this, name, data, options);
43
+ open(componentClass, data, options) {
44
+ deprecate(
45
+ `Ember Promise Modals: For extended compatibility with Embroider and its tree-shaking ability, you need to import and pass in the component class of the modal component you want to display instead of "${componentClass}".`,
46
+ typeof componentClass !== 'string',
47
+ {
48
+ id: 'ember-promise-modals.modals-from-string',
49
+ until: '5.0.0',
50
+ for: 'ember-promise-modals',
51
+ since: {
52
+ enabled: '3.1.0',
53
+ },
54
+ url: 'https://github.com/mainmatter/ember-promise-modals/blob/main/DEPRECATIONS.md#ember-promise-modalsmodals-from-string',
55
+ },
56
+ );
57
+
58
+ let modal = new Modal(this, componentClass, data, options);
40
59
 
41
60
  this._stack.pushObject(modal);
42
61
 
@@ -47,6 +66,12 @@ export default Service.extend({
47
66
  return modal;
48
67
  },
49
68
 
69
+ _destroyModals() {
70
+ this._stack.forEach(modal => {
71
+ modal._destroy();
72
+ });
73
+ },
74
+
50
75
  _onFirstModalAdded() {
51
76
  document.body.classList.add('epm-scrolling-disabled');
52
77
  },
@@ -17,14 +17,14 @@
17
17
 
18
18
  @media (prefers-reduced-motion: reduce) {
19
19
  :root {
20
- --epm-animation-backdrop-in-duration: 0s;
21
- --epm-animation-backdrop-out-duration: 0s;
22
- --epm-animation-modal-in-duration: 0s;
23
- --epm-animation-modal-out-duration: 0s;
24
- --epm-animation-backdrop-in-delay: 0s;
25
- --epm-animation-backdrop-out-delay: 0s;
26
- --epm-animation-modal-in-delay: 0s;
27
- --epm-animation-modal-out-delay: 0s;
20
+ --epm-animation-backdrop-in-duration: 0.001s;
21
+ --epm-animation-backdrop-out-duration: 0.001s;
22
+ --epm-animation-modal-in-duration: 0.001s;
23
+ --epm-animation-modal-out-duration: 0.001s;
24
+ --epm-animation-backdrop-in-delay: 0.001s;
25
+ --epm-animation-backdrop-out-delay: 0.001s;
26
+ --epm-animation-modal-in-delay: 0.001s;
27
+ --epm-animation-modal-out-delay: 0.001s;
28
28
  }
29
29
  }
30
30
 
@@ -1,6 +1,6 @@
1
1
  {{!-- template-lint-disable no-action --}}
2
- <div id={{this.modalElementId}} class="epm-modal {{this.optionsClassName}} {{this.animatingOutClass}}">
3
- {{#let (component (ensure-safe-component @modal._name) data=@modal._data close=(action "close")) as |ModalComponent|}}
2
+ <div id={{this.modalElementId}} class="epm-modal {{this.optionsClassName}} {{this.animatingClass}}">
3
+ {{#let (component (ensure-safe-component @modal._componentClass) data=@modal._data close=(action "close")) as |ModalComponent|}}
4
4
  <ModalComponent />
5
5
  {{/let}}
6
6
  </div>
@@ -1,30 +1,45 @@
1
+ import { settled } from '@ember/test-helpers';
2
+
3
+ function setMinimum(propertyName) {
4
+ document.documentElement.style.setProperty(propertyName, '0.001s');
5
+ }
6
+
7
+ function unsetMinimum(propertyName) {
8
+ document.documentElement.style.removeProperty(propertyName);
9
+ }
10
+
1
11
  export function setupPromiseModals(hooks) {
2
12
  hooks.beforeEach(function () {
3
- document.documentElement.style.setProperty('--epm-animation-backdrop-in-duration', `0.00001s`);
4
- document.documentElement.style.setProperty('--epm-animation-backdrop-out-duration', `0.00001s`);
5
- document.documentElement.style.setProperty('--epm-animation-backdrop-in-delay', `0.00001s`);
6
- document.documentElement.style.setProperty('--epm-animation-backdrop-out-delay', `0.00001s`);
7
- document.documentElement.style.setProperty('--epm-animation-modal-in-duration', `0.00001s`);
8
- document.documentElement.style.setProperty('--epm-animation-modal-out-duration', `0.00001s`);
9
- document.documentElement.style.setProperty('--epm-animation-modal-in-delay', `0.00001s`);
10
- document.documentElement.style.setProperty('--epm-animation-modal-out-delay', `0.00001s`);
13
+ setMinimum('--epm-animation-backdrop-in-duration');
14
+ setMinimum('--epm-animation-backdrop-out-duration');
15
+ setMinimum('--epm-animation-backdrop-in-delay');
16
+ setMinimum('--epm-animation-backdrop-out-delay');
17
+ setMinimum('--epm-animation-modal-in-duration');
18
+ setMinimum('--epm-animation-modal-out-duration');
19
+ setMinimum('--epm-animation-modal-in-delay');
20
+ setMinimum('--epm-animation-modal-out-delay');
11
21
 
12
22
  this.modals = this.owner.lookup('service:modals');
13
23
  });
14
24
 
15
- hooks.afterEach(function () {
25
+ hooks.afterEach(async function () {
16
26
  // be sure to close all modals after a test
17
27
  if (this.modals) {
18
- this.modals._stack.forEach(modal => modal.close());
28
+ this.modals._destroyModals();
19
29
  }
20
30
 
21
- document.documentElement.style.removeProperty('--epm-animation-backdrop-in-duration', `0.00001s`);
22
- document.documentElement.style.removeProperty('--epm-animation-backdrop-out-duration', `0.00001s`);
23
- document.documentElement.style.removeProperty('--epm-animation-backdrop-in-delay', `0.00001s`);
24
- document.documentElement.style.removeProperty('--epm-animation-backdrop-out-delay', `0.00001s`);
25
- document.documentElement.style.removeProperty('--epm-animation-modal-in-duration', `0.00001s`);
26
- document.documentElement.style.removeProperty('--epm-animation-modal-out-duration', `0.00001s`);
27
- document.documentElement.style.removeProperty('--epm-animation-modal-in-delay', `0.00001s`);
28
- document.documentElement.style.removeProperty('--epm-animation-modal-out-delay', `0.00001s`);
31
+ delete this.modals;
32
+
33
+ unsetMinimum('--epm-animation-backdrop-in-duration');
34
+ unsetMinimum('--epm-animation-backdrop-out-duration');
35
+ unsetMinimum('--epm-animation-backdrop-in-delay');
36
+ unsetMinimum('--epm-animation-backdrop-out-delay');
37
+ unsetMinimum('--epm-animation-modal-in-duration');
38
+ unsetMinimum('--epm-animation-modal-out-duration');
39
+ unsetMinimum('--epm-animation-modal-in-delay');
40
+ unsetMinimum('--epm-animation-modal-out-delay');
41
+
42
+ // wait for any registered test waiters to settle
43
+ await settled();
29
44
  });
30
45
  }
@@ -0,0 +1 @@
1
+ export { default } from 'ember-promise-modals/helpers/open-modal';
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "ember-promise-modals",
3
- "version": "3.0.1",
3
+ "version": "4.1.0",
4
4
  "description": "The better way to handle modals in your Ember.js apps.",
5
5
  "keywords": [
6
6
  "ember-addon"
7
7
  ],
8
- "repository": "https://github.com/simplabs/ember-promise-modals",
8
+ "repository": "https://github.com/mainmatter/ember-promise-modals",
9
9
  "license": "MIT",
10
- "author": "simplabs GmbH",
10
+ "author": "Mainmatter GmbH",
11
11
  "directories": {
12
12
  "doc": "doc",
13
13
  "test": "tests"
@@ -29,61 +29,63 @@
29
29
  "test:ember-compatibility": "ember try:each"
30
30
  },
31
31
  "dependencies": {
32
- "@ember/test-waiters": "^3.0.1",
33
- "@embroider/util": "^1.6.0",
32
+ "@ember/test-waiters": "^3.0.2",
33
+ "@embroider/util": "^1.7.1",
34
34
  "broccoli-funnel": "^3.0.8",
35
35
  "broccoli-merge-trees": "^4.2.0",
36
36
  "broccoli-postcss": "^6.0.1",
37
- "ember-auto-import": "^2.4.1",
37
+ "ember-auto-import": "^2.4.2",
38
38
  "ember-cli-babel": "^7.26.11",
39
39
  "ember-cli-htmlbars": "^6.0.1",
40
- "focus-trap": "^6.7.3",
41
- "postcss-preset-env": "^7.4.3"
40
+ "focus-trap": "^6.9.3",
41
+ "postcss-preset-env": "^7.6.0"
42
42
  },
43
43
  "devDependencies": {
44
- "@babel/eslint-parser": "7.17.0",
44
+ "@babel/eslint-parser": "7.23.10",
45
45
  "@ember/optional-features": "2.0.0",
46
- "@ember/test-helpers": "2.7.0",
47
- "@embroider/test-setup": "1.6.0",
46
+ "@ember/string": "3.1.1",
47
+ "@ember/test-helpers": "2.9.4",
48
+ "@embroider/macros": "1.13.4",
49
+ "@embroider/test-setup": "3.0.3",
50
+ "@release-it-plugins/lerna-changelog": "5.0.0",
48
51
  "broccoli-asset-rev": "3.0.0",
49
- "ember-cli": "4.3.0",
50
- "ember-cli-dependency-checker": "3.3.0",
52
+ "ember-cli": "4.12.2",
53
+ "ember-cli-dependency-checker": "3.3.2",
51
54
  "ember-cli-inject-live-reload": "2.1.0",
52
55
  "ember-cli-sri": "2.1.1",
53
56
  "ember-cli-terser": "4.0.2",
54
57
  "ember-disable-prototype-extensions": "1.1.3",
55
- "ember-export-application-global": "2.0.1",
56
58
  "ember-load-initializers": "2.1.2",
57
59
  "ember-maybe-import-regenerator": "1.0.0",
58
- "ember-qunit": "5.1.5",
59
- "ember-resolver": "8.0.3",
60
- "ember-source": "4.3.0",
60
+ "ember-qunit": "8.0.2",
61
+ "ember-resolver": "8.1.0",
62
+ "ember-source": "4.12.4",
61
63
  "ember-source-channel-url": "3.0.0",
62
- "ember-template-lint": "4.3.0",
64
+ "ember-template-lint": "5.13.0",
63
65
  "ember-try": "2.0.0",
64
- "eslint": "8.13.0",
65
- "eslint-config-prettier": "8.5.0",
66
- "eslint-plugin-ember": "10.6.0",
67
- "eslint-plugin-import-helpers": "1.2.1",
66
+ "eslint": "8.56.0",
67
+ "eslint-config-prettier": "8.10.0",
68
+ "eslint-plugin-ember": "11.12.0",
69
+ "eslint-plugin-import-helpers": "1.3.1",
68
70
  "eslint-plugin-node": "11.1.0",
69
- "eslint-plugin-prettier": "4.0.0",
70
- "eslint-plugin-promise": "6.0.0",
71
- "eslint-plugin-qunit": "7.2.0",
71
+ "eslint-plugin-prettier": "4.2.1",
72
+ "eslint-plugin-promise": "6.1.1",
73
+ "eslint-plugin-qunit": "7.3.4",
72
74
  "loader.js": "4.7.0",
73
75
  "npm-run-all": "4.1.5",
74
- "prettier": "2.6.2",
75
- "qunit": "2.18.1",
76
+ "prettier": "2.8.8",
77
+ "qunit": "2.20.0",
76
78
  "qunit-console-grouper": "0.3.0",
77
79
  "qunit-dom": "2.0.0",
78
- "release-it": "14.14.2",
79
- "release-it-lerna-changelog": "4.0.1",
80
- "webpack": "5.72.0"
80
+ "release-it": "15.11.0",
81
+ "sinon": "15.2.0",
82
+ "webpack": "5.90.0"
81
83
  },
82
84
  "engines": {
83
- "node": "12.* || >= 14.*"
85
+ "node": "12.* || 14.* || >= 16.*"
84
86
  },
85
87
  "changelog": {
86
- "repo": "simplabs/ember-promise-modals",
88
+ "repo": "mainmatter/ember-promise-modals",
87
89
  "labels": {
88
90
  "breaking": ":boom: Breaking Change",
89
91
  "enhancement": ":rocket: Enhancement",
@@ -102,6 +104,7 @@
102
104
  "peerDependencyRules": {
103
105
  "ignoreMissing": [
104
106
  "@babel/core",
107
+ "mustache",
105
108
  "postcss",
106
109
  "webpack"
107
110
  ]