mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-09-11 10:28:41 -04:00
Fixed bugs with sidenav
This commit is contained in:
@@ -1,42 +1,57 @@
|
|||||||
document.addEventListener('DOMContentLoaded', () => {
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
const sidenavItems = document.querySelectorAll('.usa-sidenav__item > .parent-link');
|
const sidenavItems = document.querySelectorAll('.usa-sidenav__item > .parent-link');
|
||||||
|
let lastPath = window.location.pathname;
|
||||||
|
let debounceTimeout = null;
|
||||||
|
|
||||||
sidenavItems.forEach((link) => {
|
sidenavItems.forEach((link) => {
|
||||||
const parentItem = link.parentElement;
|
const parentItem = link.parentElement;
|
||||||
const hasChildren = parentItem.querySelector('.usa-sidenav__sublist');
|
const sublist = parentItem.querySelector('.usa-sidenav__sublist');
|
||||||
const currentUrl = window.location.pathname;
|
const targetHref = link.getAttribute('href');
|
||||||
|
|
||||||
if (
|
// initialize the menu to open the correct submenu based on the current route
|
||||||
link.getAttribute('href') === currentUrl ||
|
if (window.location.pathname.startsWith(targetHref)) {
|
||||||
currentUrl.startsWith(link.getAttribute('href'))
|
|
||||||
) {
|
|
||||||
parentItem.classList.add('open');
|
parentItem.classList.add('open');
|
||||||
link.setAttribute('aria-expanded', 'true');
|
link.setAttribute('aria-expanded', 'true');
|
||||||
}
|
}
|
||||||
|
|
||||||
link.addEventListener('click', (event) => {
|
link.addEventListener('click', (event) => {
|
||||||
if (hasChildren) {
|
const currentPath = window.location.pathname;
|
||||||
|
|
||||||
|
// prevent default behavior only if navigating to the same route
|
||||||
|
if (currentPath === targetHref) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const isOpen = parentItem.classList.contains('open');
|
if (sublist && !parentItem.classList.contains('open')) {
|
||||||
|
// debounce the menu update to avoid flickering
|
||||||
document.querySelectorAll('.usa-sidenav__item.open').forEach((item) => {
|
clearTimeout(debounceTimeout);
|
||||||
if (item !== parentItem) {
|
debounceTimeout = setTimeout(() => {
|
||||||
item.classList.remove('open');
|
|
||||||
item.querySelector('.parent-link').setAttribute('aria-expanded', 'false');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!isOpen) {
|
|
||||||
parentItem.classList.add('open');
|
parentItem.classList.add('open');
|
||||||
link.setAttribute('aria-expanded', 'true');
|
link.setAttribute('aria-expanded', 'true');
|
||||||
} else {
|
}, 50);
|
||||||
parentItem.classList.remove('open');
|
|
||||||
link.setAttribute('aria-expanded', 'false');
|
|
||||||
}
|
|
||||||
|
|
||||||
window.location.href = link.getAttribute('href');
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// handle browser back/forward navigation
|
||||||
|
window.addEventListener('popstate', () => {
|
||||||
|
const currentPath = window.location.pathname;
|
||||||
|
|
||||||
|
// sync menu state
|
||||||
|
sidenavItems.forEach((link) => {
|
||||||
|
const parentItem = link.parentElement;
|
||||||
|
const targetHref = link.getAttribute('href');
|
||||||
|
|
||||||
|
if (currentPath.startsWith(targetHref)) {
|
||||||
|
parentItem.classList.add('open');
|
||||||
|
link.setAttribute('aria-expanded', 'true');
|
||||||
|
} else {
|
||||||
|
parentItem.classList.remove('open');
|
||||||
|
link.setAttribute('aria-expanded', 'false');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
lastPath = currentPath;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -915,17 +915,20 @@ li.linked-card:hover svg,
|
|||||||
}
|
}
|
||||||
|
|
||||||
.usa-sidenav__sublist {
|
.usa-sidenav__sublist {
|
||||||
display: none; /* Default: hidden */
|
display: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
.usa-sidenav__item.open .usa-sidenav__sublist {
|
.usa-sidenav__item.open .usa-sidenav__sublist {
|
||||||
display: block; /* Show sublist when the parent has the 'open' class */
|
display: block;
|
||||||
}
|
}
|
||||||
|
|
||||||
.usa-sidenav__sublist .bold {
|
.usa-sidenav__sublist .bold {
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.usa-sidenav__sublist li[role="menuitem"] {
|
||||||
|
border-top: 1px solid #dfe1e2;
|
||||||
|
}
|
||||||
|
|
||||||
.icon-list {
|
.icon-list {
|
||||||
display: flex;
|
display: flex;
|
||||||
|
|||||||
@@ -50,7 +50,7 @@
|
|||||||
{% block maincolumn_content %}
|
{% block maincolumn_content %}
|
||||||
<div class="grid-row">
|
<div class="grid-row">
|
||||||
{% if navigation_links %}
|
{% if navigation_links %}
|
||||||
<div class="tablet:grid-col-2 margin-bottom-4">
|
<div class="tablet:grid-col-3 margin-bottom-4">
|
||||||
<nav class="nav">
|
<nav class="nav">
|
||||||
<ul class="usa-sidenav">
|
<ul class="usa-sidenav">
|
||||||
{% for item in navigation_links %}
|
{% for item in navigation_links %}
|
||||||
@@ -80,9 +80,9 @@
|
|||||||
|
|
||||||
</nav>
|
</nav>
|
||||||
</div>
|
</div>
|
||||||
<div class="tablet:grid-col-10 tablet:padding-left-4 usa-prose site-prose">
|
<div class="tablet:grid-col-9 tablet:padding-left-4 usa-prose site-prose">
|
||||||
{% else %}
|
{% else %}
|
||||||
<div class="tablet:grid-col-10">
|
<div class="tablet:grid-col-9">
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% block content_column_content %}{% endblock %}
|
{% block content_column_content %}{% endblock %}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -39,7 +39,7 @@
|
|||||||
|
|
||||||
{% if FEATURE_ABOUT_PAGE_ENABLED %}
|
{% if FEATURE_ABOUT_PAGE_ENABLED %}
|
||||||
{% set navigation = navigation + [
|
{% set navigation = navigation + [
|
||||||
{"href": url_for('main.about_notify'), "text": "About Notify", "active": request.path == '/about'},
|
{"href": url_for('main.about_notify'), "text": "About Notify", "active": request.path.startswith('/about')},
|
||||||
{"href": url_for('main.join_notify'), "text": "Join Notify", "active": request.path == '/join-notify'},
|
{"href": url_for('main.join_notify'), "text": "Join Notify", "active": request.path == '/join-notify'},
|
||||||
{"href": url_for('main.contact'), "text": "Contact us", "active": request.path == '/contact'}
|
{"href": url_for('main.contact'), "text": "Contact us", "active": request.path == '/contact'}
|
||||||
] %}
|
] %}
|
||||||
|
|||||||
Reference in New Issue
Block a user