@triptease/tt-navbar 0.0.17 → 0.0.19

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/web/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @triptease/tt-navbar v0.0.17
2
+ * @triptease/tt-navbar v0.0.19
3
3
  */
4
4
  var __defProp = Object.defineProperty;
5
5
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -1064,6 +1064,48 @@ var TtNavbar = class extends i4 {
1064
1064
  constructor() {
1065
1065
  super(...arguments);
1066
1066
  this.sidebarOpen = true;
1067
+ /*
1068
+ * Set the active state for the current page.
1069
+ *
1070
+ * This function iterates over all nav links and compares the current URL path with the href of each link.
1071
+ * If the current URL path starts with the href of a link, the link is marked as active. If multiple links
1072
+ * share the same prefix, the longest prefix is used as it is more specific.
1073
+ *
1074
+ * Example:
1075
+ * - Current URL: /channels/123
1076
+ * - Nav links:
1077
+ * - /channels
1078
+ * - /channels/123
1079
+ * - /channels/456
1080
+ *
1081
+ * Loop 1: currentPath = /channels/123, linkPath = /channels, bestMatch = /channels
1082
+ * Loop 2: currentPath = /channels/123, linkPath = /channels/123, bestMatch = /channels/123
1083
+ * Loop 3: currentPath = /channels/123, linkPath = /channels/456, bestMatch = /channels/123
1084
+ * Result: /channels/123 is marked as active
1085
+ *
1086
+ */
1087
+ this.setActiveState = () => {
1088
+ const currentPath = window.location.pathname;
1089
+ let bestMatch;
1090
+ let bestMatchLength = 0;
1091
+ for (const link of this.allNavLinks) {
1092
+ link.classList.remove("current-page");
1093
+ if (link.hasAttribute("aria-current")) {
1094
+ link.attributes.removeNamedItem("aria-current");
1095
+ }
1096
+ const linkPath = new URL(link.href).pathname;
1097
+ if (currentPath.startsWith(linkPath)) {
1098
+ if (linkPath.length > bestMatchLength) {
1099
+ bestMatch = link;
1100
+ bestMatchLength = linkPath.length;
1101
+ }
1102
+ }
1103
+ }
1104
+ if (bestMatch) {
1105
+ bestMatch.classList.add("current-page");
1106
+ bestMatch.setAttribute("aria-current", "page");
1107
+ }
1108
+ };
1067
1109
  this.closeAllDetails = (element) => {
1068
1110
  this.allDetailsElements.forEach((detail) => {
1069
1111
  if (element && !detail.contains(element)) {
@@ -1096,9 +1138,13 @@ var TtNavbar = class extends i4 {
1096
1138
  this.onAnchorClick = (e8) => {
1097
1139
  if (this.navigate) {
1098
1140
  this.navigate(e8);
1141
+ this.setActiveState();
1099
1142
  }
1100
1143
  };
1101
1144
  }
1145
+ firstUpdated() {
1146
+ this.setActiveState();
1147
+ }
1102
1148
  render() {
1103
1149
  return x`
1104
1150
  <nav id=${this.id} class="${this.sidebarOpen ? "" : "sidebar-closed"}">
@@ -1284,12 +1330,15 @@ __decorateClass([
1284
1330
  __decorateClass([
1285
1331
  n4({ type: String, attribute: "client-key" })
1286
1332
  ], TtNavbar.prototype, "clientKey", 2);
1287
- __decorateClass([
1288
- r5()
1289
- ], TtNavbar.prototype, "sidebarOpen", 2);
1290
1333
  __decorateClass([
1291
1334
  r6("details")
1292
1335
  ], TtNavbar.prototype, "allDetailsElements", 2);
1336
+ __decorateClass([
1337
+ r6("a")
1338
+ ], TtNavbar.prototype, "allNavLinks", 2);
1339
+ __decorateClass([
1340
+ r5()
1341
+ ], TtNavbar.prototype, "sidebarOpen", 2);
1293
1342
  export {
1294
1343
  TtNavbar
1295
1344
  };