From 70a62a45f70f4d3097226fd405d3376ae9f620e1 Mon Sep 17 00:00:00 2001 From: Patrick Cartlidge Date: Wed, 18 Dec 2024 12:04:38 +0000 Subject: [PATCH] add overview links, active style in subnav --- lib/navigation.js | 2 +- .../components/mobile-navigation.mjs | 27 ++++++++++----- .../components/_mobile-navigation.scss | 12 +++++++ views/partials/_navigation.njk | 33 ++++++++++--------- 4 files changed, 48 insertions(+), 26 deletions(-) diff --git a/lib/navigation.js b/lib/navigation.js index b476b42eab..334ea2ffe5 100644 --- a/lib/navigation.js +++ b/lib/navigation.js @@ -47,7 +47,7 @@ module.exports = (config) => (files, metalsmith, done) => { // Add subitem to navigation item.items.push({ - href: `${dirname(itemPath)}`, + href: `/${dirname(itemPath)}`, text: frontmatter.title, order: frontmatter.order, theme: frontmatter.theme, diff --git a/src/javascripts/components/mobile-navigation.mjs b/src/javascripts/components/mobile-navigation.mjs index 5552524399..2b72ad4f4e 100644 --- a/src/javascripts/components/mobile-navigation.mjs +++ b/src/javascripts/components/mobile-navigation.mjs @@ -46,14 +46,24 @@ class MobileNavigation extends Component { link.insertAdjacentElement('afterend', button) }) + const currentLink = Array.from( + this.$root.querySelectorAll( + `.app-navigation__subnav-item a[href="${window.location.pathname.slice(0, -1)}"]` + ) + ).pop() + + if (currentLink) { + currentLink.classList.add('app-mobile-navigation__link--active') + } + // A global const for storing a matchMedia instance which we'll use to detect when a screen size change happens // Set the matchMedia to the govuk-frontend tablet breakpoint - const x = getComputedStyle(document.documentElement).getPropertyValue( - '--govuk-frontend-breakpoint-tablet' - ) + const breakPoint = getComputedStyle( + document.documentElement + ).getPropertyValue('--govuk-frontend-breakpoint-tablet') - this.mql = window.matchMedia(`(min-width: ${x})`) + this.mql = window.matchMedia(`(min-width: ${breakPoint})`) // MediaQueryList.addEventListener isn't supported by Safari < 14 so we need // to be able to fall back to the deprecated MediaQueryList.addListener @@ -76,11 +86,10 @@ class MobileNavigation extends Component { 'click', (e) => { if (e.target.tagName === 'BUTTON') { - if (e.target.getAttribute('aria-expanded') === 'true') { - e.target.setAttribute('aria-expanded', 'false') - } else { - e.target.setAttribute('aria-expanded', 'true') - } + e.target.setAttribute( + 'aria-expanded', + !(e.target.getAttribute('aria-expanded') === 'true') + ) } }, { bubbles: true } diff --git a/src/stylesheets/components/_mobile-navigation.scss b/src/stylesheets/components/_mobile-navigation.scss index 9d31c122ae..10eedbac32 100644 --- a/src/stylesheets/components/_mobile-navigation.scss +++ b/src/stylesheets/components/_mobile-navigation.scss @@ -1,3 +1,15 @@ +.app-mobile-navigation .govuk-service-navigation__item--active { + @include govuk-media-query($until: tablet) { + border-color: transparent; + } +} + +.app-mobile-navigation__link--active { + @include govuk-media-query($until: tablet) { + border-left: 5px solid rgb(26.1, 100.8, 165.6); + } +} + .app-mobile-navigation__toggle-button { @include govuk-media-query($from: tablet) { display: none; diff --git a/views/partials/_navigation.njk b/views/partials/_navigation.njk index 77c0b0c9df..f27356516c 100644 --- a/views/partials/_navigation.njk +++ b/views/partials/_navigation.njk @@ -4,29 +4,30 @@ {% set navigationItems = [] %} {% for item in navigation %} - {% set navItem = {} %} + {% set subNavItems = [{ + href: item.href, + text: item.text + " overview", + active: permalink and permalink.startsWith(item.href.slice(1)), + current: permalink === item.href + }] %} {% if item.items %} - {% set subNavItemHtml = mobileNavigation({ items: item.items }) %} - {% set navItem = { - href: item.href, - html: item.text + '', - active: permalink and permalink.startsWith(item.url), - current: permalink === item.url - } %} - {% else %} - {% set navItem = { - href: item.href, - html: item.text, - active: permalink and permalink.startsWith(item.url), - current: permalink === item.url - } %} + {% set subNavItems = subNavItems.concat(item.items) %} {% endif %} + {% set subNavItemHtml = mobileNavigation({ items: subNavItems.concat(item.items) }) %} + + {% set navItem = { + href: item.href, + html: item.text + '', + active: permalink and permalink.startsWith(item.href.slice(1)), + current: permalink === item.href + } %} + {% set navigationItems = navigationItems.concat(navItem) %} {% endfor %} -
+
{{ govukServiceNavigation({ navigation: navigationItems }) }}