@4site/engrid-scripts 0.27.3 → 0.27.4

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.
@@ -3,7 +3,7 @@ export declare class FrequencyUpsellModal extends Modal {
3
3
  private readonly upsellOptions;
4
4
  private _amountWithFees;
5
5
  private _upsellAmountWithFees;
6
- constructor(upsellOptions: FrequencyUpsellOptions);
6
+ constructor(upsellOptions: FrequencyUpsellOptions, onDismiss?: () => void);
7
7
  set amountWithFees(value: number);
8
8
  set upsellAmountWithFees(value: number);
9
9
  updateModalContent(): void;
@@ -5,11 +5,12 @@
5
5
  */
6
6
  import { ENGrid, Modal } from ".";
7
7
  export class FrequencyUpsellModal extends Modal {
8
- constructor(upsellOptions) {
8
+ constructor(upsellOptions, onDismiss) {
9
9
  super({
10
10
  onClickOutside: "bounce",
11
11
  customClass: `engrid--frequency-upsell-modal ${upsellOptions.customClass}`,
12
- showCloseX: false,
12
+ showCloseX: upsellOptions.showCloseX,
13
+ onDismiss,
13
14
  });
14
15
  this._amountWithFees = 0;
15
16
  this._upsellAmountWithFees = 0;
@@ -31,6 +31,11 @@ export declare class FrequencyUpsell {
31
31
  */
32
32
  getUpsellAmount(withFee: boolean): number;
33
33
  private addEventListeners;
34
+ /**
35
+ * Handle the modal being dismissed via the X button, Esc key, or click-outside.
36
+ * This always counts as a decline (onDecline fires either way).
37
+ */
38
+ private handleModalDismiss;
34
39
  /**
35
40
  * Create the frequency field for the upsell, if it does not exist on the page already
36
41
  * This is required by DonationFrequency to set the frequency
@@ -23,7 +23,7 @@ export class FrequencyUpsell {
23
23
  }
24
24
  this.options = this.selectOptions(window.EngridFrequencyUpsell);
25
25
  this.logger.log("FrequencyUpsell initialized", this.options);
26
- this.upsellModal = new FrequencyUpsellModal(this.options);
26
+ this.upsellModal = new FrequencyUpsellModal(this.options, () => this.handleModalDismiss());
27
27
  this.createFrequencyField();
28
28
  this.addEventListeners();
29
29
  }
@@ -144,6 +144,20 @@ export class FrequencyUpsell {
144
144
  return true;
145
145
  });
146
146
  }
147
+ /**
148
+ * Handle the modal being dismissed via the X button, Esc key, or click-outside.
149
+ * This always counts as a decline (onDecline fires either way).
150
+ */
151
+ handleModalDismiss() {
152
+ this.logger.log("Frequency upsell modal dismissed (declined)");
153
+ this.options.onDecline();
154
+ if (this.options.submitOnClose) {
155
+ this._form.submitForm();
156
+ }
157
+ else {
158
+ this._form.dispatchError();
159
+ }
160
+ }
147
161
  /**
148
162
  * Create the frequency field for the upsell, if it does not exist on the page already
149
163
  * This is required by DonationFrequency to set the frequency
@@ -10,6 +10,8 @@ export interface FrequencyUpsellOptions {
10
10
  onOpen: () => void;
11
11
  onAccept: () => void;
12
12
  onDecline: () => void;
13
+ showCloseX: boolean;
14
+ submitOnClose: boolean;
13
15
  }
14
16
  export interface FrequencyUpsellABTestConfig {
15
17
  abTest: true;
@@ -10,4 +10,6 @@ export const FrequencyUpsellOptionsDefaults = {
10
10
  onOpen: () => { },
11
11
  onAccept: () => { },
12
12
  onDecline: () => { },
13
+ showCloseX: false,
14
+ submitOnClose: false,
13
15
  };
package/dist/modal.d.ts CHANGED
@@ -4,6 +4,8 @@ interface ModalOptions {
4
4
  closeButtonLabel?: string;
5
5
  customClass?: string;
6
6
  showCloseX?: boolean;
7
+ closeOnEsc?: boolean;
8
+ onDismiss?: () => void;
7
9
  }
8
10
  export declare abstract class Modal {
9
11
  modalContent: NodeListOf<Element> | HTMLElement | String;
@@ -13,7 +15,14 @@ export declare abstract class Modal {
13
15
  protected constructor(options: ModalOptions);
14
16
  private createModal;
15
17
  private addEventListeners;
18
+ /**
19
+ * Generic entry point for dismissing the modal.
20
+ * Fires the onDismiss callback before closing, so consumers can react to the modal being
21
+ * dismissed rather than closed via their own explicit button logic.
22
+ */
23
+ private dismiss;
16
24
  private focusTrapHandler;
25
+ private escKeyHandler;
17
26
  open(): void;
18
27
  close(): void;
19
28
  getModalContent(): NodeListOf<Element> | HTMLElement | String;
package/dist/modal.js CHANGED
@@ -8,6 +8,8 @@ export class Modal {
8
8
  closeButtonLabel: "Okay!",
9
9
  customClass: "",
10
10
  showCloseX: true,
11
+ closeOnEsc: true,
12
+ onDismiss: () => { },
11
13
  };
12
14
  this.focusTrapHandler = (e) => {
13
15
  const modalElement = this.modal;
@@ -33,6 +35,11 @@ export class Modal {
33
35
  }
34
36
  }
35
37
  };
38
+ this.escKeyHandler = (e) => {
39
+ if (e.key === "Escape" && this.options.closeOnEsc) {
40
+ this.dismiss();
41
+ }
42
+ };
36
43
  this.options = Object.assign(Object.assign({}, this.defaultOptions), options);
37
44
  this.modalContent = this.getModalContent();
38
45
  this.createModal();
@@ -83,7 +90,7 @@ export class Modal {
83
90
  button.classList.add("engrid-modal__button");
84
91
  button.textContent = this.options.closeButtonLabel;
85
92
  button.addEventListener("click", () => {
86
- this.close();
93
+ this.dismiss();
87
94
  });
88
95
  modalBody === null || modalBody === void 0 ? void 0 : modalBody.appendChild(button);
89
96
  }
@@ -93,13 +100,13 @@ export class Modal {
93
100
  var _a, _b, _c, _d, _e;
94
101
  // Close event on top X
95
102
  (_b = (_a = this.modal) === null || _a === void 0 ? void 0 : _a.querySelector(".engrid-modal__close")) === null || _b === void 0 ? void 0 : _b.addEventListener("click", () => {
96
- this.close();
103
+ this.dismiss();
97
104
  });
98
105
  // Bounce scale when clicking outside of modal
99
106
  (_d = (_c = this.modal) === null || _c === void 0 ? void 0 : _c.querySelector(".engrid-modal__overlay")) === null || _d === void 0 ? void 0 : _d.addEventListener("click", (event) => {
100
107
  if (event.target === event.currentTarget) {
101
108
  if (this.options.onClickOutside === "close") {
102
- this.close();
109
+ this.dismiss();
103
110
  }
104
111
  else if (this.options.onClickOutside === "bounce") {
105
112
  const modal = document.querySelector(".engrid-modal");
@@ -115,25 +122,37 @@ export class Modal {
115
122
  const closeEls = (_e = this.modal) === null || _e === void 0 ? void 0 : _e.querySelectorAll(".modal__close");
116
123
  closeEls === null || closeEls === void 0 ? void 0 : closeEls.forEach((el) => {
117
124
  el.addEventListener("click", () => {
118
- this.close();
125
+ this.dismiss();
119
126
  });
120
127
  });
121
128
  }
129
+ /**
130
+ * Generic entry point for dismissing the modal.
131
+ * Fires the onDismiss callback before closing, so consumers can react to the modal being
132
+ * dismissed rather than closed via their own explicit button logic.
133
+ */
134
+ dismiss() {
135
+ var _a, _b;
136
+ (_b = (_a = this.options).onDismiss) === null || _b === void 0 ? void 0 : _b.call(_a);
137
+ this.close();
138
+ }
122
139
  open() {
123
- var _a, _b, _c, _d;
140
+ var _a, _b, _c, _d, _e;
124
141
  ENGrid.setBodyData("has-lightbox", "true");
125
142
  (_a = this.modal) === null || _a === void 0 ? void 0 : _a.classList.remove("modal--hidden");
126
143
  (_b = this.modal) === null || _b === void 0 ? void 0 : _b.removeAttribute("aria-hidden");
127
144
  const container = (_c = this.modal) === null || _c === void 0 ? void 0 : _c.querySelector(".engrid-modal__container");
128
145
  container === null || container === void 0 ? void 0 : container.focus({ preventScroll: true });
129
146
  (_d = this.modal) === null || _d === void 0 ? void 0 : _d.addEventListener("keydown", this.focusTrapHandler);
147
+ (_e = this.modal) === null || _e === void 0 ? void 0 : _e.addEventListener("keydown", this.escKeyHandler);
130
148
  }
131
149
  close() {
132
- var _a, _b, _c;
150
+ var _a, _b, _c, _d;
133
151
  ENGrid.setBodyData("has-lightbox", false);
134
152
  (_a = this.modal) === null || _a === void 0 ? void 0 : _a.classList.add("modal--hidden");
135
153
  (_b = this.modal) === null || _b === void 0 ? void 0 : _b.setAttribute("aria-hidden", "true");
136
154
  (_c = this.modal) === null || _c === void 0 ? void 0 : _c.removeEventListener("keydown", this.focusTrapHandler);
155
+ (_d = this.modal) === null || _d === void 0 ? void 0 : _d.removeEventListener("keydown", this.escKeyHandler);
137
156
  }
138
157
  getModalContent() {
139
158
  return "<h1>Default Modal Content</h1>";
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const AppVersion = "0.27.3";
1
+ export declare const AppVersion = "0.27.4";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const AppVersion = "0.27.3";
1
+ export const AppVersion = "0.27.4";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@4site/engrid-scripts",
3
- "version": "0.27.3",
3
+ "version": "0.27.4",
4
4
  "description": "Scripts for the ENGRID Project",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -25,7 +25,7 @@
25
25
  "strongly-typed-events": "^2.0.9"
26
26
  },
27
27
  "homepage": "https://github.com/4site-interactive-studios/engrid#readme",
28
- "gitHead": "b117056008fd80443843cefd5178ad9942a59843",
28
+ "gitHead": "e01ad2b4ea0b53edda39388f6597976e33e5f129",
29
29
  "devDependencies": {
30
30
  "nodemon": "^2.0.20"
31
31
  }