@onsvisual/svelte-components 1.1.44 → 1.1.45

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.
@@ -81,19 +81,25 @@
81
81
 
82
82
  let el; // Header HTML element
83
83
 
84
+ let url = null;
84
85
  let lang = "en";
85
86
  let baseurl = "https://www.ons.gov.uk";
86
87
  let baseother = "https://cy.ons.gov.uk";
87
88
  let path = "";
88
89
 
89
90
  function setPaths() {
90
- const url = page?.url || document.location;
91
+ url = page?.url || document.location;
91
92
  lang = url.host.startsWith("cy") ? "cy" : "en";
92
93
  baseurl = lang === "cy" ? "https://cy.ons.gov.uk" : "https://www.ons.gov.uk";
93
94
  baseother = lang === "cy" ? "https://www.ons.gov.uk" : "https://cy.ons.gov.uk";
94
95
  path = url.pathname;
95
96
  }
96
97
 
98
+ function getActiveLink(navLinks, path) {
99
+ const candidates = navLinks.filter((link) => path.endsWith(link.href));
100
+ return candidates.length ? candidates[candidates.length - 1] : null;
101
+ }
102
+
97
103
  const texts = {
98
104
  Home: "Hafan",
99
105
  Search: "Chwilio",
@@ -110,7 +116,8 @@
110
116
  };
111
117
 
112
118
  $: i18n = (text) => (lang === "cy" && texts[text] ? texts[text] : text);
113
- $: route = page?.subscribe ? $page.route?.id : page?.route?.id || "";
119
+ $: activeLink =
120
+ Array.isArray(navLinks) && url?.pathname ? getActiveLink(navLinks, url.pathname) : null;
114
121
 
115
122
  onMount(() => {
116
123
  setPaths();
@@ -220,7 +227,7 @@
220
227
  </div>
221
228
  {#if Array.isArray(navLinks)}
222
229
  <div class="ons-navigation-wrapper">
223
- <div class="ons-container ons-container--gutterless@2xs@l">
230
+ <Container {width}>
224
231
  <nav
225
232
  class="ons-navigation ons-navigation--main ons-js-navigation"
226
233
  id="main-nav"
@@ -231,14 +238,14 @@
231
238
  {#each navLinks as link (link.href)}
232
239
  <li
233
240
  class="ons-navigation__item"
234
- class:ons-navigation__item--active={route.endsWith(link.href)}
241
+ class:ons-navigation__item--active={activeLink?.href === link.href}
235
242
  >
236
243
  <a class="ons-navigation__link" href={link.href}> {link.label} </a>
237
244
  </li>
238
245
  {/each}
239
246
  </ul>
240
247
  </nav>
241
- </div>
248
+ </Container>
242
249
  </div>
243
250
  {/if}
244
251
  {/if}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onsvisual/svelte-components",
3
- "version": "1.1.44",
3
+ "version": "1.1.45",
4
4
  "scripts": {
5
5
  "dev": "vite dev",
6
6
  "build": "npm run build:package && npm run build:docs",