Skip to content

Commit

Permalink
handle closing if you click outside the submenu
Browse files Browse the repository at this point in the history
  • Loading branch information
kadencewp committed Nov 27, 2024
1 parent 942444a commit c2ffdac
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion includes/assets/js/kb-navigation-block.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions src/assets/js/kb-navigation-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,24 @@
});
}
};
/**
* Close all open submenus when clicking outside.
* @return {void}
*/
const handleClickOutsideSubmenus = function () {
document.addEventListener('click', function (e) {
// Find all open submenus.
const openSubmenus = document.querySelectorAll('.menu-item--toggled-on');

// Close any open submenus if the click is outside them.
openSubmenus.forEach((submenu) => {
// Check if the click is outside the submenu and its parent menu item.
if (!submenu.contains(e.target)) {
toggleSubMenu(submenu, false);
}
});
});
};
/**
* Initiate the script to process each
* navigation tab with submenu support.
Expand Down Expand Up @@ -418,6 +436,7 @@
}, 200);
});
trackOrientation(navigationBlocks);
handleClickOutsideSubmenus();
};

// Initialize immediately for already loaded DOM
Expand Down

0 comments on commit c2ffdac

Please sign in to comment.