Skip to content

Commit

Permalink
attach click event to links in nav bar so that it closes the nav bar …
Browse files Browse the repository at this point in the history
…& add ids to the navbar links
  • Loading branch information
Ellie-Brakoniecki committed Jul 14, 2023
1 parent 84be850 commit d642a07
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
19 changes: 17 additions & 2 deletions gov_uk_dashboards/assets/mobile-nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var mobileNav = null;
* @type {boolean}
*/
var isOpen = false;

if (typeof attachEventToDash !== 'undefined') {
attachEventToDash('mobile-menu-btn', 'click', function () {
if (mobileNav === null) {
Expand All @@ -18,6 +18,21 @@ if (typeof attachEventToDash !== 'undefined') {
mobileNav.style.display = isOpen ? 'none' : 'inline-block';
isOpen = !isOpen;
}, false)

// attach click events that close the menu to links in the nav menu
var numberOfLinks = 8; // ideally get dynamically but document.querySelectorAll and trying to match id before attaching event to Dash didn't work..
for (var i = 0; i < numberOfLinks; i++) {
var linkId = 'nav-bar-link-' + i + '-mobile';
attachEventToDash(linkId, 'click', function () {
if (mobileNav === null) {
mobileNav = document.getElementById('nav-section')
if (!mobileNav) {
return;
}
}
mobileNav.style.display = 'none';
isOpen = false;
}, false);
}

/**
Expand Down Expand Up @@ -46,4 +61,4 @@ window.addEventListener(
}
}
}, 150)
);
);}
6 changes: 4 additions & 2 deletions gov_uk_dashboards/components/plotly/side_navbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ def side_navbar(
)


def side_navbar_link(text, href):
def side_navbar_link(text, href, link_id):
"""A link to another dashboard"""
return html.Li(
dcc.Link(text, href=href, className="govuk-link govuk-link--no-visited-state"),
className="moj-side-navigation__item govuk-!-margin-bottom-1",
id=link_id,
)


def side_navbar_link_active(text, href):
def side_navbar_link_active(text, href, link_id):
"""
A link to another dashboard that appears highlighted, suggesting to the user that they are
already viewing the linked dashboard.
Expand All @@ -39,4 +40,5 @@ def side_navbar_link_active(text, href):
"moj-side-navigation__item moj-side-navigation__item--active "
"govuk-!-margin-bottom-1"
),
id=link_id,
)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
author="Department for Levelling Up, Housing and Communities",
description="Provides access to functionality common to creating a data dashboard.",
name="gov_uk_dashboards",
version="9.13.4",
version="9.14.0",
long_description=long_description,
long_description_content_type="text/markdown",
packages=find_packages(),
Expand Down

0 comments on commit d642a07

Please sign in to comment.