Skip to content

Instantly share code, notes, and snippets.

@neverything
Created July 15, 2024 09:18
Show Gist options
  • Save neverything/18847344f884390ec56284e39abd4005 to your computer and use it in GitHub Desktop.
Save neverything/18847344f884390ec56284e39abd4005 to your computer and use it in GitHub Desktop.
GenerateBlocks Accordion Block as Mega Menu in WordPress: https://silvanhagen.com/writing/wordpress-mega-menu-blocks
document.addEventListener('DOMContentLoaded', function () {
// Get all accordion items
const allItems = document.querySelectorAll('header .gb-accordion__item');
// Function to close all other accordion items
function closeOtherAccordionItems() {
allItems.forEach(item => {
item.classList.remove('gb-accordion__item-open');
let toggle = item.querySelector('.gb-accordion__toggle');
toggle.setAttribute('aria-expanded', 'false');
toggle.classList.remove('gb-block-is-current');
});
}
// Close on escape key press
document.addEventListener('keydown', function (event) {
if (event.key === "Escape") {
closeOtherAccordionItems();
}
});
// Close accordion if clicked outside gb-accordion
document.addEventListener('click', function (event) {
// Close accordion if clicked outside gb-accordion
if (!event.target.closest('.gb-accordion')) {
closeOtherAccordionItems();
}
});
// Close accordion if clicked on overlay
document.querySelectorAll('.navigation-overlay').forEach(overlay => {
overlay.addEventListener('click', event => {
if (event.target === overlay) {
closeOtherAccordionItems();
}
});
});
// Prevent the initial click on the active accordion item
document.querySelectorAll('.gb-accordion__item').forEach(item => {
const toggle = item.querySelector('.gb-accordion__toggle');
toggle.addEventListener('click', function (event) {
// Prevent the link click
if (item.classList.contains('gb-accordion__item-open')) {
event.preventDefault();
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment