Skip to content

Commit

Permalink
DON-1065: Auto-close other sub-menus when opening one via click / touch
Browse files Browse the repository at this point in the history
  • Loading branch information
bdsl committed Dec 18, 2024
1 parent 645f87f commit 90afe04
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/components/biggive-main-menu/biggive-main-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,29 @@ export class BiggiveMainMenu {
throw new Error('Missing subMenuElements');
}

/**
* Closes all the sub-menus except the one exception.
*/
const closeAllSubMenus = ({ except }: { except: HTMLAnchorElement }) => {
subMenuElements.forEach(subMenuElement => {
const link = subMenuElement.parentElement?.querySelector('a');

if (link === except) {
return;
}

link?.classList.remove('transform-90');
subMenuElement.classList.remove('display-sub-menu');
});
};

subMenuElements.forEach(subMenuElement => {
// the subMenuLink is a sibling element to the actual sub-menu
const subMenuLink = subMenuElement.parentElement?.querySelector('a');

subMenuLink!.onclick = () => {
const subMenuArrow = subMenuLink!.querySelector('.sub-menu-arrow');
closeAllSubMenus({ except: subMenuLink! });
subMenuArrow!.classList.toggle('transform-90');
subMenuElement.classList.toggle('display-sub-menu');
};
Expand Down

0 comments on commit 90afe04

Please sign in to comment.