@trycourier/courier-ui-toast 2.1.10 → 2.1.12

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,68 @@ 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
+ }
213
+ const TOAST_DISMISS_ANIMATION_MS = 300;
152
214
  const _CourierToastItem = class _CourierToastItem extends CourierBaseElement {
153
215
  constructor(props) {
154
216
  super();
@@ -160,50 +222,21 @@ const _CourierToastItem = class _CourierToastItem extends CourierBaseElement {
160
222
  __publicField(this, "_autoDismiss");
161
223
  /** The timeout before the toast item is auto-dismissed, applicable if _autoDismiss is true. */
162
224
  __publicField(this, "_autoDismissTimeoutMs");
163
- // Auto-dismiss countdown state. The countdown is paused while the cursor is
164
- // 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);
225
+ // Auto-dismiss countdown state. The countdown only runs while this item is the
226
+ // top of the stack and the cursor is away; otherwise it's paused and resumes
227
+ // from where it left off, so a toast is never dismissed before it's legible.
228
+ // Both conditions are tracked by the containing CourierToast — see
229
+ // CourierToast.refreshAutoDismissCountdowns.
230
+ __publicField(this, "_autoDismissTimeout", null);
231
+ /** The progress bar element, paused/resumed alongside the countdown to stay in sync. */
171
232
  __publicField(this, "_autoDismissBar", null);
172
233
  // Callbacks
173
234
  __publicField(this, "_onItemDismissedCallback", null);
174
235
  __publicField(this, "_onToastItemClickCallback", null);
175
236
  __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
237
  this._message = props.message;
204
238
  this._autoDismiss = props.autoDismiss;
205
239
  this._autoDismissTimeoutMs = props.autoDismissTimeoutMs;
206
- this._autoDismissRemainingMs = props.autoDismissTimeoutMs;
207
240
  this._themeManager = props.themeManager;
208
241
  this._themeSubscription = this._themeManager.subscribe((_) => {
209
242
  this.render();
@@ -253,7 +286,8 @@ const _CourierToastItem = class _CourierToastItem extends CourierBaseElement {
253
286
  * @param timeoutMs - the animation duration to fade out the toast item
254
287
  */
255
288
  dismiss(timeoutMs = _CourierToastItem.dismissAnimationTimeoutMs) {
256
- this.clearAutoDismissTimer();
289
+ var _a;
290
+ (_a = this._autoDismissTimeout) == null ? void 0 : _a.cancel();
257
291
  this.classList.add("dismissing");
258
292
  setTimeout(() => {
259
293
  this.remove();
@@ -262,34 +296,52 @@ const _CourierToastItem = class _CourierToastItem extends CourierBaseElement {
262
296
  }
263
297
  }, timeoutMs);
264
298
  }
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
- }
299
+ /**
300
+ * Pause the auto-dismiss countdown, banking the time already elapsed and
301
+ * freezing the progress bar so the two stay in sync.
302
+ *
303
+ * Called by the containing {@link CourierToast} while the cursor is over the
304
+ * toast stack, or while this item sits behind the top of the stack. No-op if
305
+ * this item doesn't auto-dismiss.
306
+ */
307
+ pauseAutoDismiss() {
308
+ var _a;
309
+ (_a = this._autoDismissTimeout) == null ? void 0 : _a.pause();
310
+ this.syncAutoDismissBarPlayState();
271
311
  }
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);
312
+ /**
313
+ * Resume a paused auto-dismiss countdown from where it left off.
314
+ *
315
+ * Called by the containing {@link CourierToast} once the cursor leaves the
316
+ * toast stack and this item is on top of it. No-op if this item doesn't
317
+ * auto-dismiss.
318
+ */
319
+ resumeAutoDismiss() {
320
+ var _a;
321
+ (_a = this._autoDismissTimeout) == null ? void 0 : _a.resume();
322
+ this.syncAutoDismissBarPlayState();
323
+ }
324
+ /** Freeze/unfreeze the progress bar to match the countdown. */
325
+ syncAutoDismissBarPlayState() {
326
+ var _a;
327
+ if (!this._autoDismissBar) {
328
+ return;
329
+ }
330
+ this._autoDismissBar.style.animationPlayState = ((_a = this._autoDismissTimeout) == null ? void 0 : _a.isPaused) ? "paused" : "running";
277
331
  }
278
332
  /** @override */
279
333
  onComponentMounted() {
280
334
  this.render();
281
335
  if (this._autoDismiss) {
282
- this.addEventListener("mouseenter", this.pauseAutoDismiss);
283
- this.addEventListener("mouseleave", this.resumeAutoDismiss);
284
- this.scheduleAutoDismiss();
336
+ this._autoDismissTimeout = new PausableTimeout(() => this.dismiss(), this._autoDismissTimeoutMs);
337
+ this._autoDismissTimeout.start();
285
338
  }
286
339
  }
287
340
  /** @override */
288
341
  onComponentUnmounted() {
342
+ var _a;
289
343
  this._themeSubscription.unsubscribe();
290
- this.clearAutoDismissTimer();
291
- this.removeEventListener("mouseenter", this.pauseAutoDismiss);
292
- this.removeEventListener("mouseleave", this.resumeAutoDismiss);
344
+ (_a = this._autoDismissTimeout) == null ? void 0 : _a.cancel();
293
345
  }
294
346
  get theme() {
295
347
  return this._themeManager.getTheme();
@@ -315,7 +367,7 @@ const _CourierToastItem = class _CourierToastItem extends CourierBaseElement {
315
367
  * styles as {@link CourierToastItem}s enter/exit.
316
368
  */
317
369
  render() {
318
- var _a, _b, _c, _d;
370
+ var _a, _b, _c, _d, _e;
319
371
  while (this.firstChild) {
320
372
  this.removeChild(this.firstChild);
321
373
  }
@@ -326,7 +378,7 @@ const _CourierToastItem = class _CourierToastItem extends CourierBaseElement {
326
378
  if (this._autoDismiss) {
327
379
  const autoDismiss = document.createElement("div");
328
380
  autoDismiss.classList.add("auto-dismiss");
329
- if (this._autoDismissPaused) {
381
+ if ((_a = this._autoDismissTimeout) == null ? void 0 : _a.isPaused) {
330
382
  autoDismiss.style.animationPlayState = "paused";
331
383
  }
332
384
  overflowHiddenContainer.append(autoDismiss);
@@ -342,8 +394,8 @@ const _CourierToastItem = class _CourierToastItem extends CourierBaseElement {
342
394
  }
343
395
  this.addEventListener("click", this.onClick);
344
396
  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
397
+ (_c = (_b = this.theme.item) == null ? void 0 : _b.dismissIcon) == null ? void 0 : _c.color,
398
+ (_e = (_d = this.theme.item) == null ? void 0 : _d.dismissIcon) == null ? void 0 : _e.svg
347
399
  );
348
400
  dismiss.classList.add("dismiss");
349
401
  dismiss.addEventListener("click", (event) => {
@@ -423,8 +475,8 @@ const _CourierToastItem = class _CourierToastItem extends CourierBaseElement {
423
475
  }
424
476
  }
425
477
  };
426
- /** The animation duration to fade out a dismissed toast before its element is removed. */
427
- __publicField(_CourierToastItem, "dismissAnimationTimeoutMs", 300);
478
+ /** How long the exit animation runs before a dismissed toast's element is removed. */
479
+ __publicField(_CourierToastItem, "dismissAnimationTimeoutMs", TOAST_DISMISS_ANIMATION_MS);
428
480
  let CourierToastItem = _CourierToastItem;
429
481
  registerElement(CourierToastItem);
430
482
  const _CourierToastDatastore = class _CourierToastDatastore {
@@ -588,6 +640,20 @@ const _CourierToast = class _CourierToast extends CourierBaseElement {
588
640
  __publicField(this, "_toastStyle");
589
641
  __publicField(this, "_authListener");
590
642
  __publicField(this, "_datastoreListener");
643
+ /**
644
+ * Whether the cursor is currently over the toast stack.
645
+ *
646
+ * Hover is tracked here — on the container — rather than per item, because
647
+ * the stacked items are one hover surface: moving between them (or a new
648
+ * toast arriving under the cursor) must not resume anyone's countdown.
649
+ */
650
+ __publicField(this, "_isHovered", false);
651
+ /**
652
+ * Auto-dismiss countdowns for custom toast items set via
653
+ * {@link CourierToast.setToastItem}. {@link CourierToastItem}s own their own
654
+ * countdown, so they're not tracked here.
655
+ */
656
+ __publicField(this, "_customItemAutoDismissTimeouts", /* @__PURE__ */ new WeakMap());
591
657
  // Consumer-provided options
592
658
  __publicField(this, "_autoDismiss", false);
593
659
  __publicField(this, "_autoDismissTimeoutMs", 5e3);
@@ -605,6 +671,14 @@ const _CourierToast = class _CourierToast extends CourierBaseElement {
605
671
  right: "30px",
606
672
  zIndex: 999
607
673
  });
674
+ __publicField(this, "onMouseEnter", () => {
675
+ this._isHovered = true;
676
+ this.refreshAutoDismissCountdowns();
677
+ });
678
+ __publicField(this, "onMouseLeave", () => {
679
+ this._isHovered = false;
680
+ this.refreshAutoDismissCountdowns();
681
+ });
608
682
  this._themeManager = (props == null ? void 0 : props.themeManager) ?? new CourierToastThemeManager(defaultLightTheme);
609
683
  this._themeSubscription = this._themeManager.subscribe((_) => {
610
684
  this.refreshStyles();
@@ -717,6 +791,7 @@ const _CourierToast = class _CourierToast extends CourierBaseElement {
717
791
  */
718
792
  dismissToastForMessage(message) {
719
793
  this.childNodes.forEach((node) => {
794
+ var _a;
720
795
  const nodeMessageId = node.dataset.courierMessageId;
721
796
  if (nodeMessageId !== message.messageId) {
722
797
  return;
@@ -724,7 +799,9 @@ const _CourierToast = class _CourierToast extends CourierBaseElement {
724
799
  if (node instanceof CourierToastItem) {
725
800
  node.dismiss();
726
801
  } else {
802
+ (_a = this._customItemAutoDismissTimeouts.get(node)) == null ? void 0 : _a.cancel();
727
803
  node.remove();
804
+ this.refreshAutoDismissCountdowns();
728
805
  }
729
806
  });
730
807
  }
@@ -733,6 +810,8 @@ const _CourierToast = class _CourierToast extends CourierBaseElement {
733
810
  */
734
811
  onComponentMounted() {
735
812
  this._toastStyle = injectGlobalStyle(_CourierToast.id, this.getStyles(this.theme));
813
+ this.addEventListener("mouseenter", this.onMouseEnter);
814
+ this.addEventListener("mouseleave", this.onMouseLeave);
736
815
  CourierToastDatastore.shared.addDatastoreListener(this._datastoreListener);
737
816
  Courier.shared.addAuthenticationListener(this.authChangedCallback.bind(this));
738
817
  CourierToastDatastore.shared.listenForMessages();
@@ -742,12 +821,47 @@ const _CourierToast = class _CourierToast extends CourierBaseElement {
742
821
  */
743
822
  onComponentUnmounted() {
744
823
  var _a, _b;
824
+ this.removeEventListener("mouseenter", this.onMouseEnter);
825
+ this.removeEventListener("mouseleave", this.onMouseLeave);
745
826
  this._datastoreListener.remove();
746
827
  (_a = this._authListener) == null ? void 0 : _a.remove();
747
828
  (_b = this._toastStyle) == null ? void 0 : _b.remove();
748
829
  this._themeManager.cleanup();
749
830
  this._themeSubscription.unsubscribe();
750
831
  }
832
+ /**
833
+ * Bring every item's countdown in line with the current stack: only the top
834
+ * item counts down, and nothing counts down while the cursor is over the stack.
835
+ *
836
+ * Items behind the top one are frozen where they are and pick up the rest of
837
+ * their countdown once they surface. Only the top toast is legible — the ones
838
+ * behind it are scaled down with their content transparent — so letting them
839
+ * all count down at once would expire the whole stack together and the user
840
+ * would never get to read anything but the newest toast.
841
+ *
842
+ * Call this after anything that changes which item is on top: an item arriving,
843
+ * an item leaving, or hover starting/ending.
844
+ */
845
+ refreshAutoDismissCountdowns() {
846
+ const items = Array.from(this.children);
847
+ const topItem = items[items.length - 1];
848
+ items.forEach((item) => {
849
+ this.setItemAutoDismissPaused(
850
+ item,
851
+ /* paused= */
852
+ this._isHovered || item !== topItem
853
+ );
854
+ });
855
+ }
856
+ /** Pause or resume a single item's auto-dismiss countdown. */
857
+ setItemAutoDismissPaused(item, paused) {
858
+ if (item instanceof CourierToastItem) {
859
+ paused ? item.pauseAutoDismiss() : item.resumeAutoDismiss();
860
+ return;
861
+ }
862
+ const customItemTimeout = this._customItemAutoDismissTimeouts.get(item);
863
+ paused ? customItemTimeout == null ? void 0 : customItemTimeout.pause() : customItemTimeout == null ? void 0 : customItemTimeout.resume();
864
+ }
751
865
  /**
752
866
  * Lifecycle callback invoked when an observed attribute changes.
753
867
  *
@@ -805,7 +919,9 @@ const _CourierToast = class _CourierToast extends CourierBaseElement {
805
919
  CourierToastDatastore.shared.listenForMessages();
806
920
  }
807
921
  removeAllItems() {
922
+ var _a;
808
923
  while (this.firstChild) {
924
+ (_a = this._customItemAutoDismissTimeouts.get(this.firstChild)) == null ? void 0 : _a.cancel();
809
925
  this.removeChild(this.firstChild);
810
926
  }
811
927
  }
@@ -814,6 +930,7 @@ const _CourierToast = class _CourierToast extends CourierBaseElement {
814
930
  toastItem.dataset.courierMessageId = message.messageId;
815
931
  this.appendChild(toastItem);
816
932
  this.resizeContainerToHeight(this.topStackItemHeight);
933
+ this.refreshAutoDismissCountdowns();
817
934
  return toastItem;
818
935
  }
819
936
  createToastItem(message) {
@@ -831,6 +948,7 @@ const _CourierToast = class _CourierToast extends CourierBaseElement {
831
948
  });
832
949
  item.onItemDismissed((_) => {
833
950
  this.resizeContainerToHeight(this.topStackItemHeight);
951
+ this.refreshAutoDismissCountdowns();
834
952
  });
835
953
  if (this._customToastItemContent) {
836
954
  item.setToastItemContent(this._customToastItemContent);
@@ -853,9 +971,12 @@ const _CourierToast = class _CourierToast extends CourierBaseElement {
853
971
  autoDismissTimeoutMs: this._autoDismissTimeoutMs
854
972
  });
855
973
  if (this._autoDismiss) {
856
- setTimeout(() => {
857
- this.removeChild(customItem);
974
+ const autoDismissTimeout = new PausableTimeout(() => {
975
+ customItem.remove();
976
+ this.refreshAutoDismissCountdowns();
858
977
  }, this._autoDismissTimeoutMs);
978
+ this._customItemAutoDismissTimeouts.set(customItem, autoDismissTimeout);
979
+ autoDismissTimeout.start();
859
980
  }
860
981
  customItem.addEventListener("click", () => {
861
982
  if (this._onItemClick) {
@@ -936,7 +1057,7 @@ const _CourierToast = class _CourierToast extends CourierBaseElement {
936
1057
  }
937
1058
 
938
1059
  ${CourierToastItem.id}.dismissing {
939
- animation: courier-toast-hide 0.3s ease-in-out forwards;
1060
+ animation: courier-toast-hide ${TOAST_DISMISS_ANIMATION_MS}ms ease-out forwards;
940
1061
  }
941
1062
 
942
1063
  @keyframes courier-toast-show {
@@ -950,15 +1071,25 @@ const _CourierToast = class _CourierToast extends CourierBaseElement {
950
1071
  }
951
1072
  }
952
1073
 
1074
+ /* Lift the toast back out the way it came in rather than blinking it out
1075
+ in place, which reads as the toast leaving instead of vanishing. The
1076
+ toast is anchored to the top of the viewport, so "out" is up: a full
1077
+ translateY of its own height, eased so most of the travel happens
1078
+ early and the tail is imperceptible. */
1079
+ /* Shrink the toast slightly as it fades so it reads as receding rather
1080
+ than blinking out, but keep it where it sits — moving it competes with
1081
+ the toast underneath sliding up to take its place. The stack's own
1082
+ horizontal scale is preserved so a toast dismissed from behind the top
1083
+ one doesn't jump to full width on its way out. */
953
1084
  @keyframes courier-toast-hide {
954
1085
  0% {
955
1086
  opacity: 1;
956
- transform: scaleX(var(--scale, 1));
1087
+ transform: scale(1) scaleX(var(--scale, 1));
957
1088
  }
958
1089
 
959
1090
  100% {
960
1091
  opacity: 0;
961
- transform: scaleX(var(--scale, 1));
1092
+ transform: scale(0.92) scaleX(var(--scale, 1));
962
1093
  }
963
1094
  }
964
1095
 
@@ -1129,7 +1260,7 @@ class CourierToastDatastoreEvents {
1129
1260
  }
1130
1261
  }
1131
1262
  Courier.shared.courierUserAgentName = "courier-ui-toast";
1132
- Courier.shared.courierUserAgentVersion = "2.1.10";
1263
+ Courier.shared.courierUserAgentVersion = "2.1.12";
1133
1264
  export {
1134
1265
  Courier2 as Courier,
1135
1266
  CourierToast,