Skip to content

Commit

Permalink
Support sitenav having multiple items with submenus
Browse files Browse the repository at this point in the history
The script for Amethyst theme was imported here from the qunitjs.com
repo which only had one item  with a submenu (Guides), however
the api.qunitjs.com had a newer version [1] that supported multiple
items having submenus which is needed for "Guides" and "Documentation"
both to be collapsible.

[1] qunitjs/qunit@a66c0bc
  • Loading branch information
Krinkle committed Jul 26, 2020
1 parent a3c6156 commit ab6434b
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions _layouts/wrapper.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,24 @@
</footer>
<script type="module">
window.addEventListener('DOMContentLoaded', function setupSubNav() {
const itemWithSubNav = document.querySelector('.site-nav-item.has-sub-list');
const closeSubNav = () => itemWithSubNav.classList.remove('is-open');
itemWithSubNav.addEventListener('click', (event) => {
const siteNav = document.querySelector('.site-nav-list');
const closeSubNav = () => {
const prevSub = siteNav.querySelector('.is-open');
if (prevSub) {
prevSub.classList.remove('is-open');
}
}
siteNav.addEventListener('click', (event) => {
if (event.target.classList.contains('has-sub-list')) {
event.preventDefault();
}

if (itemWithSubNav.classList.contains('is-open')) {
closeSubNav();
document.removeEventListener('click', closeSubNav);
} else {
itemWithSubNav.classList.add('is-open');
document.addEventListener('click', closeSubNav);
event.stopPropagation();

if (event.target.parentNode.classList.contains('is-open')) {
closeSubNav();
} else {
closeSubNav();
event.target.parentNode.classList.add('is-open');
}
}
});
});
Expand Down

0 comments on commit ab6434b

Please sign in to comment.