Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix for navigation anchor links to close modal
  • Loading branch information
coreyworrell committed Nov 16, 2022
commit 625cf1f12271b2b1d656fdbe3abecf503892dcb5
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 isLinkToAnchorOnSamePage( 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( '#' ) ||
! isLinkToAnchorOnSamePage( link ) ||
link.attributes?.target === '_blank'
) {
return;
Expand Down