Skip to content

Commit

Permalink
Fix for navigation anchor links to close modal (#45829)
Browse files Browse the repository at this point in the history
* Fix for navigation anchor links to close modal

* Rename function to be more accurate
  • Loading branch information
coreyworrell authored Nov 17, 2022
1 parent 476ce03 commit 7147465
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/block-library/src/navigation/view-modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ function navigationToggleModal( modal ) {
htmlElement.classList.toggle( 'has-modal-open' );
}

function isLinkToAnchorOnCurrentPage( node ) {
return (
node.hash &&
node.protocol === window.location.protocol &&
node.host === window.location.host &&
node.pathname === window.location.pathname &&
node.search === window.location.search
);
}

window.addEventListener( 'load', () => {
MicroModal.init( {
onShow: navigationToggleModal,
Expand All @@ -42,7 +52,7 @@ window.addEventListener( 'load', () => {
navigationLinks.forEach( function ( link ) {
// Ignore non-anchor links and anchor links which open on a new tab.
if (
! link.getAttribute( 'href' )?.startsWith( '#' ) ||
! isLinkToAnchorOnCurrentPage( link ) ||
link.attributes?.target === '_blank'
) {
return;
Expand Down

0 comments on commit 7147465

Please sign in to comment.