@trycourier/courier-ui-inbox 1.0.13-beta → 1.0.15

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
@@ -1218,7 +1218,7 @@ const _CourierInboxDatastore = class _CourierInboxDatastore {
1218
1218
  return;
1219
1219
  }
1220
1220
  try {
1221
- message.opened = (/* @__PURE__ */ new Date()).toISOString();
1221
+ snapshot.message.opened = (/* @__PURE__ */ new Date()).toISOString();
1222
1222
  this.applyMessageSnapshot(snapshot);
1223
1223
  if (canCallApi) {
1224
1224
  await ((_a = Courier.shared.client) == null ? void 0 : _a.inbox.open({ messageId: message.messageId }));
@@ -1598,15 +1598,19 @@ class CourierInboxListItem extends CourierBaseElement {
1598
1598
  // Touch gestures
1599
1599
  __publicField(this, "_longPressTimeout", null);
1600
1600
  __publicField(this, "_isLongPress", false);
1601
+ // Intersection Observer
1602
+ __publicField(this, "_observer");
1601
1603
  // Callbacks
1602
1604
  __publicField(this, "onItemClick", null);
1603
1605
  __publicField(this, "onItemLongPress", null);
1604
1606
  __publicField(this, "onItemActionClick", null);
1607
+ __publicField(this, "onItemVisible", null);
1605
1608
  this._canClick = canClick;
1606
1609
  this._themeManager = themeManager;
1607
1610
  this._theme = themeManager.getTheme();
1608
1611
  this._isMobile = "ontouchstart" in window;
1609
1612
  this.render();
1613
+ this._setupIntersectionObserver();
1610
1614
  }
1611
1615
  static get id() {
1612
1616
  return "courier-inbox-list-item";
@@ -1652,6 +1656,26 @@ class CourierInboxListItem extends CourierBaseElement {
1652
1656
  this.classList.add("clickable");
1653
1657
  }
1654
1658
  }
1659
+ _setupIntersectionObserver() {
1660
+ if (typeof window === "undefined" || typeof IntersectionObserver === "undefined") {
1661
+ return;
1662
+ }
1663
+ if (this._observer) {
1664
+ this._observer.disconnect();
1665
+ }
1666
+ this._observer = new IntersectionObserver((entries) => {
1667
+ entries.forEach((entry) => {
1668
+ if (entry.intersectionRatio === 1 && this.onItemVisible && this._message) {
1669
+ this.onItemVisible(this._message);
1670
+ }
1671
+ });
1672
+ }, { threshold: 1 });
1673
+ this._observer.observe(this);
1674
+ }
1675
+ onComponentUnmounted() {
1676
+ var _a;
1677
+ (_a = this._observer) == null ? void 0 : _a.disconnect();
1678
+ }
1655
1679
  static getStyles(theme2) {
1656
1680
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z;
1657
1681
  const list = (_a = theme2.inbox) == null ? void 0 : _a.list;
@@ -1824,9 +1848,6 @@ class CourierInboxListItem extends CourierBaseElement {
1824
1848
  }
1825
1849
  });
1826
1850
  }
1827
- setOnLongPress(cb) {
1828
- this.onItemLongPress = cb;
1829
- }
1830
1851
  // Helpers
1831
1852
  _getMenuOptions() {
1832
1853
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
@@ -1904,6 +1925,9 @@ class CourierInboxListItem extends CourierBaseElement {
1904
1925
  setOnItemLongPress(cb) {
1905
1926
  this.onItemLongPress = cb;
1906
1927
  }
1928
+ setOnItemVisible(cb) {
1929
+ this.onItemVisible = cb;
1930
+ }
1907
1931
  // Content rendering
1908
1932
  _updateContent() {
1909
1933
  var _a, _b, _c;
@@ -2203,6 +2227,21 @@ class CourierInboxPaginationListItem extends CourierBaseElement {
2203
2227
  }
2204
2228
  }
2205
2229
  registerElement(CourierInboxPaginationListItem);
2230
+ function markAsRead(message) {
2231
+ return CourierInboxDatastore.shared.readMessage({ message });
2232
+ }
2233
+ function markAsUnread(message) {
2234
+ return CourierInboxDatastore.shared.unreadMessage({ message });
2235
+ }
2236
+ function clickMessage(message) {
2237
+ return CourierInboxDatastore.shared.clickMessage({ message });
2238
+ }
2239
+ function archiveMessage(message) {
2240
+ return CourierInboxDatastore.shared.archiveMessage({ message });
2241
+ }
2242
+ function openMessage(message) {
2243
+ return CourierInboxDatastore.shared.openMessage({ message });
2244
+ }
2206
2245
  class CourierInboxList extends CourierBaseElement {
2207
2246
  constructor(props) {
2208
2247
  super();
@@ -2460,6 +2499,7 @@ class CourierInboxList extends CourierBaseElement {
2460
2499
  var _a2;
2461
2500
  return (_a2 = this._onMessageLongPress) == null ? void 0 : _a2.call(this, message2, index);
2462
2501
  });
2502
+ listItem.setOnItemVisible((message2) => this.openVisibleMessage(message2));
2463
2503
  list.appendChild(listItem);
2464
2504
  });
2465
2505
  if (this._canPaginate) {
@@ -2474,6 +2514,12 @@ class CourierInboxList extends CourierBaseElement {
2474
2514
  list.appendChild(paginationItem);
2475
2515
  }
2476
2516
  }
2517
+ async openVisibleMessage(message) {
2518
+ try {
2519
+ await openMessage(message);
2520
+ } catch (error) {
2521
+ }
2522
+ }
2477
2523
  // Factories
2478
2524
  setLoadingStateFactory(factory) {
2479
2525
  this._loadingStateFactory = factory;
@@ -3902,6 +3948,7 @@ class CourierInbox extends CourierBaseElement {
3902
3948
  // State
3903
3949
  __publicField(this, "_currentFeed", "inbox");
3904
3950
  // Theming
3951
+ // Theme manager instance for handling theming logic
3905
3952
  __publicField(this, "_themeManager");
3906
3953
  // Components
3907
3954
  __publicField(this, "_inboxStyle");
@@ -3928,15 +3975,32 @@ class CourierInbox extends CourierBaseElement {
3928
3975
  static get id() {
3929
3976
  return "courier-inbox";
3930
3977
  }
3978
+ /** Returns the current feed type. */
3979
+ get currentFeed() {
3980
+ return this._currentFeed;
3981
+ }
3982
+ /** Returns the current theme object. */
3931
3983
  get theme() {
3932
3984
  return this._themeManager.getTheme();
3933
3985
  }
3986
+ /**
3987
+ * Set the light theme for the inbox.
3988
+ * @param theme The light theme object to set.
3989
+ */
3934
3990
  setLightTheme(theme2) {
3935
3991
  this._themeManager.setLightTheme(theme2);
3936
3992
  }
3993
+ /**
3994
+ * Set the dark theme for the inbox.
3995
+ * @param theme The dark theme object to set.
3996
+ */
3937
3997
  setDarkTheme(theme2) {
3938
3998
  this._themeManager.setDarkTheme(theme2);
3939
3999
  }
4000
+ /**
4001
+ * Set the theme mode (light/dark/system).
4002
+ * @param mode The theme mode to set.
4003
+ */
3940
4004
  setMode(mode) {
3941
4005
  this._themeManager.setMode(mode);
3942
4006
  }
@@ -4094,49 +4158,95 @@ class CourierInbox extends CourierBaseElement {
4094
4158
  }
4095
4159
  `;
4096
4160
  }
4161
+ /**
4162
+ * Sets a custom header factory for the inbox.
4163
+ * @param factory - A function that returns an HTMLElement to render as the header.
4164
+ */
4097
4165
  setHeader(factory) {
4098
4166
  this._headerFactory = factory;
4099
4167
  this.updateHeader();
4100
4168
  }
4169
+ /**
4170
+ * Removes the custom header factory from the inbox, reverting to the default header.
4171
+ */
4101
4172
  removeHeader() {
4102
4173
  this._headerFactory = null;
4103
4174
  this.updateHeader();
4104
4175
  }
4176
+ /**
4177
+ * Sets a custom loading state factory for the inbox list.
4178
+ * @param factory - A function that returns an HTMLElement to render as the loading state.
4179
+ */
4105
4180
  setLoadingState(factory) {
4106
4181
  var _a;
4107
4182
  (_a = this._list) == null ? void 0 : _a.setLoadingStateFactory(factory);
4108
4183
  }
4184
+ /**
4185
+ * Sets a custom empty state factory for the inbox list.
4186
+ * @param factory - A function that returns an HTMLElement to render as the empty state.
4187
+ */
4109
4188
  setEmptyState(factory) {
4110
4189
  var _a;
4111
4190
  (_a = this._list) == null ? void 0 : _a.setEmptyStateFactory(factory);
4112
4191
  }
4192
+ /**
4193
+ * Sets a custom error state factory for the inbox list.
4194
+ * @param factory - A function that returns an HTMLElement to render as the error state.
4195
+ */
4113
4196
  setErrorState(factory) {
4114
4197
  var _a;
4115
4198
  (_a = this._list) == null ? void 0 : _a.setErrorStateFactory(factory);
4116
4199
  }
4200
+ /**
4201
+ * Sets a custom list item factory for the inbox list.
4202
+ * @param factory - A function that returns an HTMLElement to render as a list item.
4203
+ */
4117
4204
  setListItem(factory) {
4118
4205
  var _a;
4119
4206
  (_a = this._list) == null ? void 0 : _a.setListItemFactory(factory);
4120
4207
  }
4208
+ /**
4209
+ * Sets a custom pagination item factory for the inbox list.
4210
+ * @param factory - A function that returns an HTMLElement to render as a pagination item.
4211
+ */
4121
4212
  setPaginationItem(factory) {
4122
4213
  var _a;
4123
4214
  (_a = this._list) == null ? void 0 : _a.setPaginationItemFactory(factory);
4124
4215
  }
4216
+ /**
4217
+ * Registers a handler for message click events.
4218
+ * @param handler - A function to be called when a message is clicked.
4219
+ */
4125
4220
  onMessageClick(handler) {
4126
4221
  var _a;
4127
4222
  this._onMessageClick = handler;
4128
4223
  (_a = this._list) == null ? void 0 : _a.setCanClickListItems(handler !== void 0);
4129
4224
  }
4225
+ /**
4226
+ * Registers a handler for message action click events.
4227
+ * @param handler - A function to be called when a message action is clicked.
4228
+ */
4130
4229
  onMessageActionClick(handler) {
4131
4230
  this._onMessageActionClick = handler;
4132
4231
  }
4232
+ /**
4233
+ * Registers a handler for message long press events.
4234
+ * @param handler - A function to be called when a message is long-pressed.
4235
+ */
4133
4236
  onMessageLongPress(handler) {
4134
4237
  var _a;
4135
4238
  this._onMessageLongPress = handler;
4136
4239
  (_a = this._list) == null ? void 0 : _a.setCanLongPressListItems(handler !== void 0);
4137
4240
  }
4241
+ /**
4242
+ * Sets the feed type for the inbox (e.g., "inbox" or "archive").
4243
+ * @param feedType - The feed type to display.
4244
+ */
4138
4245
  setFeedType(feedType) {
4139
4246
  var _a;
4247
+ if (this._currentFeed === feedType) {
4248
+ return;
4249
+ }
4140
4250
  this._currentFeed = feedType;
4141
4251
  (_a = this._list) == null ? void 0 : _a.setFeedType(feedType);
4142
4252
  this.updateHeader();
@@ -4168,6 +4278,9 @@ class CourierInbox extends CourierBaseElement {
4168
4278
  await CourierInboxDatastore.shared.load(props);
4169
4279
  await CourierInboxDatastore.shared.listenForUpdates();
4170
4280
  }
4281
+ /**
4282
+ * Forces a reload of the inbox data, bypassing the cache.
4283
+ */
4171
4284
  refresh() {
4172
4285
  this.load({
4173
4286
  canUseCache: false
@@ -4317,10 +4430,10 @@ class CourierInboxPopupMenu extends CourierBaseElement {
4317
4430
  __publicField(this, "_width", "440px");
4318
4431
  __publicField(this, "_height", "440px");
4319
4432
  __publicField(this, "_popupAlignment", "top-left");
4320
- __publicField(this, "_top", "40px");
4321
- __publicField(this, "_right", "0");
4322
- __publicField(this, "_bottom", "40px");
4323
- __publicField(this, "_left", "0");
4433
+ __publicField(this, "_top");
4434
+ __publicField(this, "_right");
4435
+ __publicField(this, "_bottom");
4436
+ __publicField(this, "_left");
4324
4437
  // Theming
4325
4438
  __publicField(this, "_themeManager", new CourierInboxThemeManager(defaultLightTheme));
4326
4439
  // Components
@@ -4352,15 +4465,33 @@ class CourierInboxPopupMenu extends CourierBaseElement {
4352
4465
  static get id() {
4353
4466
  return "courier-inbox-popup-menu";
4354
4467
  }
4468
+ /** Returns the current theme object. */
4355
4469
  get theme() {
4356
4470
  return this._themeManager.getTheme();
4357
4471
  }
4472
+ /** Returns the current feed type. */
4473
+ get currentFeed() {
4474
+ var _a;
4475
+ return ((_a = this._inbox) == null ? void 0 : _a.currentFeed) ?? "inbox";
4476
+ }
4477
+ /**
4478
+ * Set the light theme for the popup menu.
4479
+ * @param theme The light theme object to set.
4480
+ */
4358
4481
  setLightTheme(theme2) {
4359
4482
  this._themeManager.setLightTheme(theme2);
4360
4483
  }
4484
+ /**
4485
+ * Set the dark theme for the popup menu.
4486
+ * @param theme The dark theme object to set.
4487
+ */
4361
4488
  setDarkTheme(theme2) {
4362
4489
  this._themeManager.setDarkTheme(theme2);
4363
4490
  }
4491
+ /**
4492
+ * Set the theme mode (light/dark/system).
4493
+ * @param mode The theme mode to set.
4494
+ */
4364
4495
  setMode(mode) {
4365
4496
  this._themeManager.setMode(mode);
4366
4497
  }
@@ -4483,9 +4614,17 @@ class CourierInboxPopupMenu extends CourierBaseElement {
4483
4614
  break;
4484
4615
  }
4485
4616
  }
4617
+ /**
4618
+ * Called when the unread count changes.
4619
+ * @param _ The new unread count (unused).
4620
+ */
4486
4621
  onUnreadCountChange(_) {
4487
4622
  this.render();
4488
4623
  }
4624
+ /**
4625
+ * Set a handler for message click events.
4626
+ * @param handler The function to call when a message is clicked.
4627
+ */
4489
4628
  onMessageClick(handler) {
4490
4629
  var _a;
4491
4630
  (_a = this._inbox) == null ? void 0 : _a.onMessageClick((props) => {
@@ -4495,6 +4634,10 @@ class CourierInboxPopupMenu extends CourierBaseElement {
4495
4634
  this.closePopup();
4496
4635
  });
4497
4636
  }
4637
+ /**
4638
+ * Set a handler for message action click events.
4639
+ * @param handler The function to call when a message action is clicked.
4640
+ */
4498
4641
  onMessageActionClick(handler) {
4499
4642
  var _a;
4500
4643
  (_a = this._inbox) == null ? void 0 : _a.onMessageActionClick((props) => {
@@ -4504,6 +4647,10 @@ class CourierInboxPopupMenu extends CourierBaseElement {
4504
4647
  this.closePopup();
4505
4648
  });
4506
4649
  }
4650
+ /**
4651
+ * Set a handler for message long press events.
4652
+ * @param handler The function to call when a message is long pressed.
4653
+ */
4507
4654
  onMessageLongPress(handler) {
4508
4655
  var _a;
4509
4656
  (_a = this._inbox) == null ? void 0 : _a.onMessageLongPress((props) => {
@@ -4537,39 +4684,39 @@ class CourierInboxPopupMenu extends CourierBaseElement {
4537
4684
  this._popup.style.transform = "";
4538
4685
  switch (this._popupAlignment) {
4539
4686
  case "top-right":
4540
- this._popup.style.top = this._top;
4541
- this._popup.style.right = this._right;
4687
+ this._popup.style.top = this._top ?? "40px";
4688
+ this._popup.style.right = this._right ?? "0px";
4542
4689
  break;
4543
4690
  case "top-left":
4544
- this._popup.style.top = this._top;
4545
- this._popup.style.left = this._left;
4691
+ this._popup.style.top = this._top ?? "40px";
4692
+ this._popup.style.left = this._left ?? "0px";
4546
4693
  break;
4547
4694
  case "top-center":
4548
- this._popup.style.top = this._top;
4695
+ this._popup.style.top = this._top ?? "40px";
4549
4696
  this._popup.style.left = "50%";
4550
4697
  this._popup.style.transform = "translateX(-50%)";
4551
4698
  break;
4552
4699
  case "bottom-right":
4553
- this._popup.style.bottom = this._bottom;
4554
- this._popup.style.right = this._right;
4700
+ this._popup.style.bottom = this._bottom ?? "40px";
4701
+ this._popup.style.right = this._right ?? "0px";
4555
4702
  break;
4556
4703
  case "bottom-left":
4557
- this._popup.style.bottom = this._bottom;
4558
- this._popup.style.left = this._left;
4704
+ this._popup.style.bottom = this._bottom ?? "40px";
4705
+ this._popup.style.left = this._left ?? "0px";
4559
4706
  break;
4560
4707
  case "bottom-center":
4561
- this._popup.style.bottom = this._bottom;
4708
+ this._popup.style.bottom = this._bottom ?? "40px";
4562
4709
  this._popup.style.left = "50%";
4563
4710
  this._popup.style.transform = "translateX(-50%)";
4564
4711
  break;
4565
4712
  case "center-right":
4566
4713
  this._popup.style.top = "50%";
4567
- this._popup.style.right = this._right;
4714
+ this._popup.style.right = this._right ?? "40px";
4568
4715
  this._popup.style.transform = "translateY(-50%)";
4569
4716
  break;
4570
4717
  case "center-left":
4571
4718
  this._popup.style.top = "50%";
4572
- this._popup.style.left = this._left;
4719
+ this._popup.style.left = this._left ?? "40px";
4573
4720
  this._popup.style.transform = "translateY(-50%)";
4574
4721
  break;
4575
4722
  case "center-center":
@@ -4579,21 +4726,37 @@ class CourierInboxPopupMenu extends CourierBaseElement {
4579
4726
  break;
4580
4727
  }
4581
4728
  }
4729
+ /**
4730
+ * Toggle the popup menu open/closed.
4731
+ * @param event The click event that triggered the toggle.
4732
+ */
4582
4733
  togglePopup(event) {
4583
4734
  event.stopPropagation();
4584
4735
  if (!this._popup) return;
4585
4736
  const isVisible = this._popup.style.display === "block";
4586
4737
  this._popup.style.display = isVisible ? "none" : "block";
4587
4738
  }
4739
+ /**
4740
+ * Close the popup menu.
4741
+ */
4588
4742
  closePopup() {
4589
4743
  if (!this._popup) return;
4590
4744
  this._popup.style.display = "none";
4591
4745
  }
4746
+ /**
4747
+ * Set the content of the popup inbox.
4748
+ * @param element The HTMLElement to set as the content.
4749
+ */
4592
4750
  setContent(element) {
4593
4751
  if (!this._inbox) return;
4594
4752
  this._inbox.innerHTML = "";
4595
4753
  this._inbox.appendChild(element);
4596
4754
  }
4755
+ /**
4756
+ * Set the size of the popup menu.
4757
+ * @param width The width to set.
4758
+ * @param height The height to set.
4759
+ */
4597
4760
  setSize(width, height) {
4598
4761
  this._width = width;
4599
4762
  this._height = height;
@@ -4601,6 +4764,10 @@ class CourierInboxPopupMenu extends CourierBaseElement {
4601
4764
  this._popup.style.width = width;
4602
4765
  this._popup.style.height = height;
4603
4766
  }
4767
+ /**
4768
+ * Set the popup alignment/position.
4769
+ * @param position The alignment/position to set.
4770
+ */
4604
4771
  setPosition(position) {
4605
4772
  var _a, _b;
4606
4773
  if (this.isValidPosition(position)) {
@@ -4610,39 +4777,74 @@ class CourierInboxPopupMenu extends CourierBaseElement {
4610
4777
  (_b = (_a = Courier.shared.client) == null ? void 0 : _a.options.logger) == null ? void 0 : _b.error(`Invalid position: ${position}`);
4611
4778
  }
4612
4779
  }
4780
+ /**
4781
+ * Set the feed type for the inbox.
4782
+ * @param feedType The feed type to set.
4783
+ */
4613
4784
  setFeedType(feedType) {
4614
4785
  var _a;
4615
4786
  (_a = this._inbox) == null ? void 0 : _a.setFeedType(feedType);
4616
4787
  }
4617
4788
  // Factory methods
4789
+ /**
4790
+ * Set a custom header factory for the inbox.
4791
+ * @param factory The factory function for the header.
4792
+ */
4618
4793
  setHeader(factory) {
4619
4794
  var _a;
4620
4795
  (_a = this._inbox) == null ? void 0 : _a.setHeader(factory);
4621
4796
  }
4797
+ /**
4798
+ * Remove the custom header from the inbox.
4799
+ */
4622
4800
  removeHeader() {
4623
4801
  var _a;
4624
4802
  (_a = this._inbox) == null ? void 0 : _a.removeHeader();
4625
4803
  }
4804
+ /**
4805
+ * Set a custom loading state factory for the inbox.
4806
+ * @param factory The factory function for the loading state.
4807
+ */
4626
4808
  setLoadingState(factory) {
4627
4809
  var _a;
4628
4810
  (_a = this._inbox) == null ? void 0 : _a.setLoadingState(factory);
4629
4811
  }
4812
+ /**
4813
+ * Set a custom empty state factory for the inbox.
4814
+ * @param factory The factory function for the empty state.
4815
+ */
4630
4816
  setEmptyState(factory) {
4631
4817
  var _a;
4632
4818
  (_a = this._inbox) == null ? void 0 : _a.setEmptyState(factory);
4633
4819
  }
4820
+ /**
4821
+ * Set a custom error state factory for the inbox.
4822
+ * @param factory The factory function for the error state.
4823
+ */
4634
4824
  setErrorState(factory) {
4635
4825
  var _a;
4636
4826
  (_a = this._inbox) == null ? void 0 : _a.setErrorState(factory);
4637
4827
  }
4828
+ /**
4829
+ * Set a custom list item factory for the inbox.
4830
+ * @param factory The factory function for the list item.
4831
+ */
4638
4832
  setListItem(factory) {
4639
4833
  var _a;
4640
4834
  (_a = this._inbox) == null ? void 0 : _a.setListItem(factory);
4641
4835
  }
4836
+ /**
4837
+ * Set a custom pagination item factory for the inbox.
4838
+ * @param factory The factory function for the pagination item.
4839
+ */
4642
4840
  setPaginationItem(factory) {
4643
4841
  var _a;
4644
4842
  (_a = this._inbox) == null ? void 0 : _a.setPaginationItem(factory);
4645
4843
  }
4844
+ /**
4845
+ * Set a custom menu button factory for the popup trigger.
4846
+ * @param factory The factory function for the menu button.
4847
+ */
4646
4848
  setMenuButton(factory) {
4647
4849
  this._popupMenuButtonFactory = factory;
4648
4850
  this.render();
@@ -4664,21 +4866,6 @@ class CourierInboxPopupMenu extends CourierBaseElement {
4664
4866
  }
4665
4867
  }
4666
4868
  registerElement(CourierInboxPopupMenu);
4667
- function markAsRead(message) {
4668
- return CourierInboxDatastore.shared.readMessage({ message });
4669
- }
4670
- function markAsUnread(message) {
4671
- return CourierInboxDatastore.shared.unreadMessage({ message });
4672
- }
4673
- function clickMessage(message) {
4674
- return CourierInboxDatastore.shared.clickMessage({ message });
4675
- }
4676
- function archiveMessage(message) {
4677
- return CourierInboxDatastore.shared.archiveMessage({ message });
4678
- }
4679
- function openMessage(message) {
4680
- return CourierInboxDatastore.shared.openMessage({ message });
4681
- }
4682
4869
  class CourierInboxDatastoreEvents {
4683
4870
  onDataSetChange(_, __) {
4684
4871
  }