@trycourier/courier-ui-toast 2.1.10 → 2.1.11

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.
package/dist/index.mjs CHANGED
@@ -149,6 +149,67 @@ class CourierToastThemeManager extends CourierThemeManager {
149
149
  };
150
150
  }
151
151
  }
152
+ class PausableTimeout {
153
+ constructor(callback, delayMs) {
154
+ __publicField(this, "_callback");
155
+ /** Time left on the countdown, updated each time it's paused. */
156
+ __publicField(this, "_remainingMs");
157
+ __publicField(this, "_timerId", null);
158
+ /** When the currently-running countdown was (re)started. */
159
+ __publicField(this, "_startedAt", 0);
160
+ __publicField(this, "_isPaused", false);
161
+ /** Set once the callback has fired (or the countdown was cancelled). */
162
+ __publicField(this, "_isDone", false);
163
+ this._callback = callback;
164
+ this._remainingMs = delayMs;
165
+ }
166
+ /** Whether the countdown is currently paused. */
167
+ get isPaused() {
168
+ return this._isPaused;
169
+ }
170
+ /** Start the countdown for whatever time is left on it. */
171
+ start() {
172
+ if (this._isDone) {
173
+ return;
174
+ }
175
+ this.clear();
176
+ this._isPaused = false;
177
+ this._startedAt = Date.now();
178
+ this._timerId = setTimeout(() => {
179
+ this._timerId = null;
180
+ this._isDone = true;
181
+ this._callback();
182
+ }, this._remainingMs);
183
+ }
184
+ /** Pause the countdown, banking the time that has already elapsed. */
185
+ pause() {
186
+ if (this._isDone || this._isPaused || this._timerId === null) {
187
+ return;
188
+ }
189
+ this._remainingMs = Math.max(0, this._remainingMs - (Date.now() - this._startedAt));
190
+ this._isPaused = true;
191
+ this.clear();
192
+ }
193
+ /** Resume a paused countdown from where it left off. */
194
+ resume() {
195
+ if (this._isDone || !this._isPaused) {
196
+ return;
197
+ }
198
+ this.start();
199
+ }
200
+ /** Cancel the countdown for good; it can't be started again. */
201
+ cancel() {
202
+ this._isPaused = false;
203
+ this._isDone = true;
204
+ this.clear();
205
+ }
206
+ clear() {
207
+ if (this._timerId !== null) {
208
+ clearTimeout(this._timerId);
209
+ this._timerId = null;
210
+ }
211
+ }
212
+ }
152
213
  const _CourierToastItem = class _CourierToastItem extends CourierBaseElement {
153
214
  constructor(props) {
154
215
  super();
@@ -162,48 +223,19 @@ const _CourierToastItem = class _CourierToastItem extends CourierBaseElement {
162
223
  __publicField(this, "_autoDismissTimeoutMs");
163
224
  // Auto-dismiss countdown state. The countdown is paused while the cursor is
164
225
  // over the toast and resumed (from where it left off) when the cursor leaves,
165
- // so a user reading the toast is never rushed. The JS timer and the CSS
166
- // progress bar are paused/resumed together to stay in sync.
167
- __publicField(this, "_autoDismissTimerId", null);
168
- __publicField(this, "_autoDismissRemainingMs");
169
- __publicField(this, "_autoDismissStartedAt", 0);
170
- __publicField(this, "_autoDismissPaused", false);
226
+ // so a user reading the toast is never rushed. Hover is tracked by the
227
+ // containing CourierToast, which pauses/resumes every item in the stack —
228
+ // see CourierToast.setAutoDismissPaused.
229
+ __publicField(this, "_autoDismissTimeout", null);
230
+ /** The progress bar element, paused/resumed alongside the countdown to stay in sync. */
171
231
  __publicField(this, "_autoDismissBar", null);
172
232
  // Callbacks
173
233
  __publicField(this, "_onItemDismissedCallback", null);
174
234
  __publicField(this, "_onToastItemClickCallback", null);
175
235
  __publicField(this, "_onToastItemActionClickCallback", null);
176
- /**
177
- * Pause the countdown while the cursor is over the toast: cancel the timer,
178
- * bank the time already elapsed, and freeze the progress bar.
179
- */
180
- __publicField(this, "pauseAutoDismiss", () => {
181
- if (!this._autoDismiss || this._autoDismissPaused) {
182
- return;
183
- }
184
- this._autoDismissPaused = true;
185
- this.clearAutoDismissTimer();
186
- const elapsed = Date.now() - this._autoDismissStartedAt;
187
- this._autoDismissRemainingMs = Math.max(0, this._autoDismissRemainingMs - elapsed);
188
- if (this._autoDismissBar) {
189
- this._autoDismissBar.style.animationPlayState = "paused";
190
- }
191
- });
192
- /** Resume the countdown from where it was paused once the cursor leaves. */
193
- __publicField(this, "resumeAutoDismiss", () => {
194
- if (!this._autoDismiss || !this._autoDismissPaused) {
195
- return;
196
- }
197
- this._autoDismissPaused = false;
198
- if (this._autoDismissBar) {
199
- this._autoDismissBar.style.animationPlayState = "running";
200
- }
201
- this.scheduleAutoDismiss();
202
- });
203
236
  this._message = props.message;
204
237
  this._autoDismiss = props.autoDismiss;
205
238
  this._autoDismissTimeoutMs = props.autoDismissTimeoutMs;
206
- this._autoDismissRemainingMs = props.autoDismissTimeoutMs;
207
239
  this._themeManager = props.themeManager;
208
240
  this._themeSubscription = this._themeManager.subscribe((_) => {
209
241
  this.render();
@@ -253,7 +285,8 @@ const _CourierToastItem = class _CourierToastItem extends CourierBaseElement {
253
285
  * @param timeoutMs - the animation duration to fade out the toast item
254
286
  */
255
287
  dismiss(timeoutMs = _CourierToastItem.dismissAnimationTimeoutMs) {
256
- this.clearAutoDismissTimer();
288
+ var _a;
289
+ (_a = this._autoDismissTimeout) == null ? void 0 : _a.cancel();
257
290
  this.classList.add("dismissing");
258
291
  setTimeout(() => {
259
292
  this.remove();
@@ -262,34 +295,50 @@ const _CourierToastItem = class _CourierToastItem extends CourierBaseElement {
262
295
  }
263
296
  }, timeoutMs);
264
297
  }
265
- /** Clears the pending auto-dismiss timer, if any. */
266
- clearAutoDismissTimer() {
267
- if (this._autoDismissTimerId !== null) {
268
- clearTimeout(this._autoDismissTimerId);
269
- this._autoDismissTimerId = null;
270
- }
298
+ /**
299
+ * Pause the auto-dismiss countdown, banking the time already elapsed and
300
+ * freezing the progress bar so the two stay in sync.
301
+ *
302
+ * Called by the containing {@link CourierToast} while the cursor is over the
303
+ * toast stack. No-op if this item doesn't auto-dismiss.
304
+ */
305
+ pauseAutoDismiss() {
306
+ var _a;
307
+ (_a = this._autoDismissTimeout) == null ? void 0 : _a.pause();
308
+ this.syncAutoDismissBarPlayState();
271
309
  }
272
- /** (Re)start the auto-dismiss timer for whatever time is left on the countdown. */
273
- scheduleAutoDismiss() {
274
- this.clearAutoDismissTimer();
275
- this._autoDismissStartedAt = Date.now();
276
- this._autoDismissTimerId = setTimeout(() => this.dismiss(), this._autoDismissRemainingMs);
310
+ /**
311
+ * Resume a paused auto-dismiss countdown from where it left off.
312
+ *
313
+ * Called by the containing {@link CourierToast} once the cursor leaves the
314
+ * toast stack. No-op if this item doesn't auto-dismiss.
315
+ */
316
+ resumeAutoDismiss() {
317
+ var _a;
318
+ (_a = this._autoDismissTimeout) == null ? void 0 : _a.resume();
319
+ this.syncAutoDismissBarPlayState();
320
+ }
321
+ /** Freeze/unfreeze the progress bar to match the countdown. */
322
+ syncAutoDismissBarPlayState() {
323
+ var _a;
324
+ if (!this._autoDismissBar) {
325
+ return;
326
+ }
327
+ this._autoDismissBar.style.animationPlayState = ((_a = this._autoDismissTimeout) == null ? void 0 : _a.isPaused) ? "paused" : "running";
277
328
  }
278
329
  /** @override */
279
330
  onComponentMounted() {
280
331
  this.render();
281
332
  if (this._autoDismiss) {
282
- this.addEventListener("mouseenter", this.pauseAutoDismiss);
283
- this.addEventListener("mouseleave", this.resumeAutoDismiss);
284
- this.scheduleAutoDismiss();
333
+ this._autoDismissTimeout = new PausableTimeout(() => this.dismiss(), this._autoDismissTimeoutMs);
334
+ this._autoDismissTimeout.start();
285
335
  }
286
336
  }
287
337
  /** @override */
288
338
  onComponentUnmounted() {
339
+ var _a;
289
340
  this._themeSubscription.unsubscribe();
290
- this.clearAutoDismissTimer();
291
- this.removeEventListener("mouseenter", this.pauseAutoDismiss);
292
- this.removeEventListener("mouseleave", this.resumeAutoDismiss);
341
+ (_a = this._autoDismissTimeout) == null ? void 0 : _a.cancel();
293
342
  }
294
343
  get theme() {
295
344
  return this._themeManager.getTheme();
@@ -315,7 +364,7 @@ const _CourierToastItem = class _CourierToastItem extends CourierBaseElement {
315
364
  * styles as {@link CourierToastItem}s enter/exit.
316
365
  */
317
366
  render() {
318
- var _a, _b, _c, _d;
367
+ var _a, _b, _c, _d, _e;
319
368
  while (this.firstChild) {
320
369
  this.removeChild(this.firstChild);
321
370
  }
@@ -326,7 +375,7 @@ const _CourierToastItem = class _CourierToastItem extends CourierBaseElement {
326
375
  if (this._autoDismiss) {
327
376
  const autoDismiss = document.createElement("div");
328
377
  autoDismiss.classList.add("auto-dismiss");
329
- if (this._autoDismissPaused) {
378
+ if ((_a = this._autoDismissTimeout) == null ? void 0 : _a.isPaused) {
330
379
  autoDismiss.style.animationPlayState = "paused";
331
380
  }
332
381
  overflowHiddenContainer.append(autoDismiss);
@@ -342,8 +391,8 @@ const _CourierToastItem = class _CourierToastItem extends CourierBaseElement {
342
391
  }
343
392
  this.addEventListener("click", this.onClick);
344
393
  const dismiss = new CourierIcon(
345
- (_b = (_a = this.theme.item) == null ? void 0 : _a.dismissIcon) == null ? void 0 : _b.color,
346
- (_d = (_c = this.theme.item) == null ? void 0 : _c.dismissIcon) == null ? void 0 : _d.svg
394
+ (_c = (_b = this.theme.item) == null ? void 0 : _b.dismissIcon) == null ? void 0 : _c.color,
395
+ (_e = (_d = this.theme.item) == null ? void 0 : _d.dismissIcon) == null ? void 0 : _e.svg
347
396
  );
348
397
  dismiss.classList.add("dismiss");
349
398
  dismiss.addEventListener("click", (event) => {
@@ -588,6 +637,20 @@ const _CourierToast = class _CourierToast extends CourierBaseElement {
588
637
  __publicField(this, "_toastStyle");
589
638
  __publicField(this, "_authListener");
590
639
  __publicField(this, "_datastoreListener");
640
+ /**
641
+ * Whether the cursor is currently over the toast stack.
642
+ *
643
+ * Hover is tracked here — on the container — rather than per item, because
644
+ * the stacked items are one hover surface: moving between them (or a new
645
+ * toast arriving under the cursor) must not resume anyone's countdown.
646
+ */
647
+ __publicField(this, "_isHovered", false);
648
+ /**
649
+ * Auto-dismiss countdowns for custom toast items set via
650
+ * {@link CourierToast.setToastItem}. {@link CourierToastItem}s own their own
651
+ * countdown, so they're not tracked here.
652
+ */
653
+ __publicField(this, "_customItemAutoDismissTimeouts", /* @__PURE__ */ new WeakMap());
591
654
  // Consumer-provided options
592
655
  __publicField(this, "_autoDismiss", false);
593
656
  __publicField(this, "_autoDismissTimeoutMs", 5e3);
@@ -605,6 +668,14 @@ const _CourierToast = class _CourierToast extends CourierBaseElement {
605
668
  right: "30px",
606
669
  zIndex: 999
607
670
  });
671
+ __publicField(this, "onMouseEnter", () => {
672
+ this._isHovered = true;
673
+ this.setAutoDismissPaused(true);
674
+ });
675
+ __publicField(this, "onMouseLeave", () => {
676
+ this._isHovered = false;
677
+ this.setAutoDismissPaused(false);
678
+ });
608
679
  this._themeManager = (props == null ? void 0 : props.themeManager) ?? new CourierToastThemeManager(defaultLightTheme);
609
680
  this._themeSubscription = this._themeManager.subscribe((_) => {
610
681
  this.refreshStyles();
@@ -717,6 +788,7 @@ const _CourierToast = class _CourierToast extends CourierBaseElement {
717
788
  */
718
789
  dismissToastForMessage(message) {
719
790
  this.childNodes.forEach((node) => {
791
+ var _a;
720
792
  const nodeMessageId = node.dataset.courierMessageId;
721
793
  if (nodeMessageId !== message.messageId) {
722
794
  return;
@@ -724,6 +796,7 @@ const _CourierToast = class _CourierToast extends CourierBaseElement {
724
796
  if (node instanceof CourierToastItem) {
725
797
  node.dismiss();
726
798
  } else {
799
+ (_a = this._customItemAutoDismissTimeouts.get(node)) == null ? void 0 : _a.cancel();
727
800
  node.remove();
728
801
  }
729
802
  });
@@ -733,6 +806,8 @@ const _CourierToast = class _CourierToast extends CourierBaseElement {
733
806
  */
734
807
  onComponentMounted() {
735
808
  this._toastStyle = injectGlobalStyle(_CourierToast.id, this.getStyles(this.theme));
809
+ this.addEventListener("mouseenter", this.onMouseEnter);
810
+ this.addEventListener("mouseleave", this.onMouseLeave);
736
811
  CourierToastDatastore.shared.addDatastoreListener(this._datastoreListener);
737
812
  Courier.shared.addAuthenticationListener(this.authChangedCallback.bind(this));
738
813
  CourierToastDatastore.shared.listenForMessages();
@@ -742,12 +817,27 @@ const _CourierToast = class _CourierToast extends CourierBaseElement {
742
817
  */
743
818
  onComponentUnmounted() {
744
819
  var _a, _b;
820
+ this.removeEventListener("mouseenter", this.onMouseEnter);
821
+ this.removeEventListener("mouseleave", this.onMouseLeave);
745
822
  this._datastoreListener.remove();
746
823
  (_a = this._authListener) == null ? void 0 : _a.remove();
747
824
  (_b = this._toastStyle) == null ? void 0 : _b.remove();
748
825
  this._themeManager.cleanup();
749
826
  this._themeSubscription.unsubscribe();
750
827
  }
828
+ /** Pause or resume the auto-dismiss countdown of every item in the stack. */
829
+ setAutoDismissPaused(paused) {
830
+ Array.from(this.children).forEach((child) => this.setItemAutoDismissPaused(child, paused));
831
+ }
832
+ /** Pause or resume a single item's auto-dismiss countdown. */
833
+ setItemAutoDismissPaused(item, paused) {
834
+ if (item instanceof CourierToastItem) {
835
+ paused ? item.pauseAutoDismiss() : item.resumeAutoDismiss();
836
+ return;
837
+ }
838
+ const customItemTimeout = this._customItemAutoDismissTimeouts.get(item);
839
+ paused ? customItemTimeout == null ? void 0 : customItemTimeout.pause() : customItemTimeout == null ? void 0 : customItemTimeout.resume();
840
+ }
751
841
  /**
752
842
  * Lifecycle callback invoked when an observed attribute changes.
753
843
  *
@@ -805,7 +895,9 @@ const _CourierToast = class _CourierToast extends CourierBaseElement {
805
895
  CourierToastDatastore.shared.listenForMessages();
806
896
  }
807
897
  removeAllItems() {
898
+ var _a;
808
899
  while (this.firstChild) {
900
+ (_a = this._customItemAutoDismissTimeouts.get(this.firstChild)) == null ? void 0 : _a.cancel();
809
901
  this.removeChild(this.firstChild);
810
902
  }
811
903
  }
@@ -814,6 +906,13 @@ const _CourierToast = class _CourierToast extends CourierBaseElement {
814
906
  toastItem.dataset.courierMessageId = message.messageId;
815
907
  this.appendChild(toastItem);
816
908
  this.resizeContainerToHeight(this.topStackItemHeight);
909
+ if (this._isHovered) {
910
+ this.setItemAutoDismissPaused(
911
+ toastItem,
912
+ /* paused= */
913
+ true
914
+ );
915
+ }
817
916
  return toastItem;
818
917
  }
819
918
  createToastItem(message) {
@@ -853,9 +952,9 @@ const _CourierToast = class _CourierToast extends CourierBaseElement {
853
952
  autoDismissTimeoutMs: this._autoDismissTimeoutMs
854
953
  });
855
954
  if (this._autoDismiss) {
856
- setTimeout(() => {
857
- this.removeChild(customItem);
858
- }, this._autoDismissTimeoutMs);
955
+ const autoDismissTimeout = new PausableTimeout(() => customItem.remove(), this._autoDismissTimeoutMs);
956
+ this._customItemAutoDismissTimeouts.set(customItem, autoDismissTimeout);
957
+ autoDismissTimeout.start();
859
958
  }
860
959
  customItem.addEventListener("click", () => {
861
960
  if (this._onItemClick) {
@@ -1129,7 +1228,7 @@ class CourierToastDatastoreEvents {
1129
1228
  }
1130
1229
  }
1131
1230
  Courier.shared.courierUserAgentName = "courier-ui-toast";
1132
- Courier.shared.courierUserAgentVersion = "2.1.10";
1231
+ Courier.shared.courierUserAgentVersion = "2.1.11";
1133
1232
  export {
1134
1233
  Courier2 as Courier,
1135
1234
  CourierToast,