-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated code to allow sidenav expand on click
- Loading branch information
1 parent
7534de0
commit 168d93a
Showing
5 changed files
with
57 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
document.addEventListener('DOMContentLoaded', () => { | ||
const sidenavItems = document.querySelectorAll('.usa-sidenav__item > .parent-link'); | ||
|
||
sidenavItems.forEach((link) => { | ||
const parentItem = link.parentElement; | ||
const hasChildren = parentItem.querySelector('.usa-sidenav__sublist'); | ||
const currentUrl = window.location.pathname; | ||
|
||
if ( | ||
link.getAttribute('href') === currentUrl || | ||
currentUrl.startsWith(link.getAttribute('href')) | ||
) { | ||
parentItem.classList.add('open'); | ||
link.setAttribute('aria-expanded', 'true'); | ||
} | ||
|
||
link.addEventListener('click', (event) => { | ||
if (hasChildren) { | ||
event.preventDefault(); | ||
|
||
const isOpen = parentItem.classList.contains('open'); | ||
|
||
document.querySelectorAll('.usa-sidenav__item.open').forEach((item) => { | ||
if (item !== parentItem) { | ||
item.classList.remove('open'); | ||
item.querySelector('.parent-link').setAttribute('aria-expanded', 'false'); | ||
} | ||
}); | ||
|
||
if (!isOpen) { | ||
parentItem.classList.add('open'); | ||
link.setAttribute('aria-expanded', 'true'); | ||
} else { | ||
parentItem.classList.remove('open'); | ||
link.setAttribute('aria-expanded', 'false'); | ||
} | ||
|
||
window.location.href = link.getAttribute('href'); | ||
} | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters