Skip to content

Commit

Permalink
Menu: Close other modals when when the "open" button is clicked (#97)
Browse files Browse the repository at this point in the history
* Attempt at closing other modals when when the "open" button is clicked

This does not work because `MicroModal.close` overwrites the internal modal ID, so it looses track of what the current open modal is.

* Switch to faking a click directly on the close buttons.
  • Loading branch information
ryelle authored Jan 18, 2022
1 parent 4a2989e commit 74244af
Showing 1 changed file with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,24 @@
);
};

/* eslint-disable @wordpress/no-global-event-listener */
window.addEventListener( 'load', function () {
new navMenu( '.global-header .global-header__navigation' );

const openButtons = document.querySelectorAll( '[data-micromodal-trigger]' );
openButtons.forEach( function ( button ) {
// When any open menu button is clicked, find any existing close buttons and click them.
button.addEventListener( 'click', function ( event ) {
const thisModal = event.target.getAttribute( 'data-micromodal-trigger' );
const closeButtons = Array.from(
document.querySelectorAll( 'button[data-micromodal-close]' )
).filter(
// Filter to find visible close buttons that are not for this modal.
( _button ) => _button.offsetWidth > 0 && null === _button.closest( `#${ thisModal }` )
);

closeButtons.forEach( ( _button ) => _button.click() );
} );
} );
} );
} )();

0 comments on commit 74244af

Please sign in to comment.