@onsvisual/svelte-components 1.1.42 → 1.1.44

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.
@@ -25,6 +25,19 @@
25
25
 
26
26
  <Story name="Compact with page title" args={{ compact: true, title: "Page title" }} />
27
27
 
28
+ <Story
29
+ name="Compact with page title and nav links"
30
+ args={{
31
+ compact: true,
32
+ title: "Page title",
33
+ navLinks: [
34
+ { label: "Home", href: "" },
35
+ { label: "Some page", href: "#0" },
36
+ { label: "Another page", href: "#1" }
37
+ ]
38
+ }}
39
+ />
40
+
28
41
  <Story name="Compact dark theme" args={{ compact: true, title: "Page title", theme: "dark" }} />
29
42
 
30
43
  <Story name="Legacy header" args={{ legacy: true }} />
@@ -1,5 +1,6 @@
1
1
  <script>
2
2
  import { onMount, getContext } from "svelte";
3
+ import initNav from "./nav.js";
3
4
  import Theme from "../Theme/Theme.svelte";
4
5
  import Container from "../Container/Container.svelte";
5
6
  import SkipLink from "../SkipLink/SkipLink.svelte";
@@ -7,8 +8,6 @@
7
8
  import HeaderNavCompact from "./HeaderNavCompact.svelte";
8
9
  import HeaderNavLegacy from "./HeaderNavLegacy.svelte";
9
10
 
10
- const page = getContext("page");
11
-
12
11
  /**
13
12
  * Optional product title
14
13
  * @type {string|null}
@@ -64,11 +63,23 @@
64
63
  * @type {boolean}
65
64
  */
66
65
  export let legacy = false;
66
+ /**
67
+ * Optional: Pass the "page" store from "$app/state" in SvelteKit (gets read from context by default if it exists)
68
+ * @type {any}
69
+ */
70
+ export let page = getContext("page");
67
71
  /**
68
72
  * Anchor link to skip to main body content (default "#main")
69
73
  * @type {string|null}
70
74
  */
71
75
  export let skipHref = "#main";
76
+ /**
77
+ * Optional: Nav links below title (only works in combination with title block). An array of links in the format {label, href}
78
+ * @type {{label: string, href: string}[]|null}
79
+ */
80
+ export let navLinks = null;
81
+
82
+ let el; // Header HTML element
72
83
 
73
84
  let lang = "en";
74
85
  let baseurl = "https://www.ons.gov.uk";
@@ -99,9 +110,18 @@
99
110
  };
100
111
 
101
112
  $: i18n = (text) => (lang === "cy" && texts[text] ? texts[text] : text);
113
+ $: route = page?.subscribe ? $page.route?.id : page?.route?.id || "";
102
114
 
103
115
  onMount(() => {
104
116
  setPaths();
117
+
118
+ if ((!compact && !legacy) || (title && Array.isArray(navLinks))) {
119
+ const hasBodyClass = "className" in document.body || {};
120
+ const bodyClassString = document.body?.className || "";
121
+ if (hasBodyClass && !bodyClassString.includes("ons-js-enabled"))
122
+ document.body.className = bodyClassString + " ons-js-enabled";
123
+ initNav(el?.parentElement || document);
124
+ }
105
125
  });
106
126
  </script>
107
127
 
@@ -110,6 +130,7 @@
110
130
  class:ons-header--basic={!compact && !legacy}
111
131
  class:ons-header__full={width === "full"}
112
132
  role="banner"
133
+ bind:this={el}
113
134
  >
114
135
  {#if skipHref}
115
136
  <SkipLink href={skipHref} />
@@ -166,9 +187,60 @@
166
187
  <div class="ons-header__title">{title}</div>
167
188
  {/if}
168
189
  </div>
190
+ {#if Array.isArray(navLinks)}
191
+ <div class="ons-grid__col ons-col-auto ons-u-flex-no-shrink ons-u-d-no@l">
192
+ <button
193
+ type="submit"
194
+ class="ons-btn ons-u-ml-2xs ons-u-d-no ons-js-navigation-button ons-u-d-no@l ons-btn--mobile ons-btn--ghost"
195
+ aria-label="Toggle main menu"
196
+ aria-controls="main-nav"
197
+ aria-expanded="false"
198
+ >
199
+ <span class="ons-btn__inner"
200
+ ><span class="ons-btn__text">Menu</span><svg
201
+ class="ons-icon ons-u-ml-2xs"
202
+ viewBox="0 0 8 13"
203
+ xmlns="http://www.w3.org/2000/svg"
204
+ focusable="false"
205
+ fill="currentColor"
206
+ role="img"
207
+ aria-hidden="true"
208
+ >
209
+ <path
210
+ d="M5.74,14.28l-.57-.56a.5.5,0,0,1,0-.71h0l5-5-5-5a.5.5,0,0,1,0-.71h0l.57-.56a.5.5,0,0,1,.71,0h0l5.93,5.93a.5.5,0,0,1,0,.7L6.45,14.28a.5.5,0,0,1-.71,0Z"
211
+ transform="translate(-5.02 -1.59)"
212
+ />
213
+ </svg></span
214
+ >
215
+ </button>
216
+ </div>
217
+ {/if}
169
218
  </div>
170
219
  </Container>
171
220
  </div>
221
+ {#if Array.isArray(navLinks)}
222
+ <div class="ons-navigation-wrapper">
223
+ <div class="ons-container ons-container--gutterless@2xs@l">
224
+ <nav
225
+ class="ons-navigation ons-navigation--main ons-js-navigation"
226
+ id="main-nav"
227
+ aria-label="Main menu"
228
+ data-analytics="header-navigation"
229
+ >
230
+ <ul class="ons-navigation__list">
231
+ {#each navLinks as link (link.href)}
232
+ <li
233
+ class="ons-navigation__item"
234
+ class:ons-navigation__item--active={route.endsWith(link.href)}
235
+ >
236
+ <a class="ons-navigation__link" href={link.href}> {link.label} </a>
237
+ </li>
238
+ {/each}
239
+ </ul>
240
+ </nav>
241
+ </div>
242
+ </div>
243
+ {/if}
172
244
  {/if}
173
245
  </Theme>
174
246
  </header>
@@ -9,11 +9,16 @@ export default class Header extends SvelteComponentTyped<{
9
9
  title?: string | null | undefined;
10
10
  skipHref?: string | null | undefined;
11
11
  compact?: boolean | undefined;
12
+ page?: any;
12
13
  headerBorder?: boolean | undefined;
13
14
  menuBorder?: boolean | undefined;
14
15
  bilingual?: boolean | undefined;
15
16
  titleHref?: string | null | undefined;
16
17
  legacy?: boolean | undefined;
18
+ navLinks?: {
19
+ label: string;
20
+ href: string;
21
+ }[] | null | undefined;
17
22
  }, {
18
23
  [evt: string]: CustomEvent<any>;
19
24
  }, {
@@ -33,11 +38,16 @@ declare const __propDef: {
33
38
  title?: string | null | undefined;
34
39
  skipHref?: string | null | undefined;
35
40
  compact?: boolean | undefined;
41
+ page?: any;
36
42
  headerBorder?: boolean | undefined;
37
43
  menuBorder?: boolean | undefined;
38
44
  bilingual?: boolean | undefined;
39
45
  titleHref?: string | null | undefined;
40
46
  legacy?: boolean | undefined;
47
+ navLinks?: {
48
+ label: string;
49
+ href: string;
50
+ }[] | null | undefined;
41
51
  };
42
52
  events: {
43
53
  [evt: string]: CustomEvent<any>;
@@ -1,7 +1,5 @@
1
1
  <script>
2
- import { onMount } from "svelte";
3
2
  import Container from "../Container/Container.svelte";
4
- import initNav from "./nav.js";
5
3
 
6
4
  export let width = "wide";
7
5
  export let headerBorder = false;
@@ -14,8 +12,6 @@
14
12
  export let path = "";
15
13
  export let i18n = (text) => text;
16
14
 
17
- let el; // Header HTML element
18
-
19
15
  const menu = {
20
16
  main: [
21
17
  {
@@ -239,17 +235,9 @@
239
235
  ]
240
236
  };
241
237
  const columns = [[menu.topics[0]], [menu.topics[1], menu.topics[2]], [menu.topics[3]]];
242
-
243
- onMount(() => {
244
- const hasBodyClass = "className" in document.body || {};
245
- const bodyClassString = document.body?.className || "";
246
- if (hasBodyClass && !bodyClassString.includes("ons-js-enabled"))
247
- document.body.className = bodyClassString + " ons-js-enabled";
248
- initNav(el?.parentElement || document);
249
- });
250
238
  </script>
251
239
 
252
- <div class="ons-header__top" class:ons-header--border={headerBorder} bind:this={el}>
240
+ <div class="ons-header__top" class:ons-header--border={headerBorder}>
253
241
  <Container {width}>
254
242
  <div
255
243
  class="ons-header__grid-top ons-grid ons-grid-flex ons-grid-flex--between ons-grid-flex--vertical-center ons-grid-flex--no-wrap ons-grid--gutterless"
@@ -17,7 +17,6 @@
17
17
 
18
18
  const callback = (entries, observer) => {
19
19
  entries.forEach((entry) => {
20
- console.log(entry);
21
20
  let intersecting = entry.isIntersecting;
22
21
  if (!entered && intersecting) {
23
22
  observer.unobserve(entry.target);
@@ -24,11 +24,9 @@
24
24
 
25
25
  function mouseOver() {
26
26
  isHovered = true;
27
- console.log("hovered");
28
27
  }
29
28
  function mouseLeave() {
30
29
  isHovered = false;
31
- console.log("unhovered");
32
30
  }
33
31
  </script>
34
32
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onsvisual/svelte-components",
3
- "version": "1.1.42",
3
+ "version": "1.1.44",
4
4
  "scripts": {
5
5
  "dev": "vite dev",
6
6
  "build": "npm run build:package && npm run build:docs",