forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use interactivity API for Navigation and File blocks only in Gutenberg (
WordPress#51694) * Add conditional to navigation block * Add conditional to file block * Skip `view-modal` if `should_load_script` is false * Fix typo * Apply conditional in `block_type_metadata` filter
- Loading branch information
1 parent
b3f44cd
commit c9021a5
Showing
7 changed files
with
266 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { hidePdfEmbedsOnUnsupportedBrowsers } from './utils'; | ||
|
||
document.addEventListener( | ||
'DOMContentLoaded', | ||
hidePdfEmbedsOnUnsupportedBrowsers | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import MicroModal from 'micromodal'; | ||
|
||
// Responsive navigation toggle. | ||
function navigationToggleModal( modal ) { | ||
const dialogContainer = modal.querySelector( | ||
`.wp-block-navigation__responsive-dialog` | ||
); | ||
|
||
const isHidden = 'true' === modal.getAttribute( 'aria-hidden' ); | ||
|
||
modal.classList.toggle( 'has-modal-open', ! isHidden ); | ||
dialogContainer.toggleAttribute( 'aria-modal', ! isHidden ); | ||
|
||
if ( isHidden ) { | ||
dialogContainer.removeAttribute( 'role' ); | ||
dialogContainer.removeAttribute( 'aria-modal' ); | ||
} else { | ||
dialogContainer.setAttribute( 'role', 'dialog' ); | ||
dialogContainer.setAttribute( 'aria-modal', 'true' ); | ||
} | ||
|
||
// Add a class to indicate the modal is open. | ||
const htmlElement = document.documentElement; | ||
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, | ||
onClose: navigationToggleModal, | ||
openClass: 'is-menu-open', | ||
} ); | ||
|
||
// Close modal automatically on clicking anchor links inside modal. | ||
const navigationLinks = document.querySelectorAll( | ||
'.wp-block-navigation-item__content' | ||
); | ||
|
||
navigationLinks.forEach( function ( link ) { | ||
// Ignore non-anchor links and anchor links which open on a new tab. | ||
if ( | ||
! isLinkToAnchorOnCurrentPage( link ) || | ||
link.attributes?.target === '_blank' | ||
) { | ||
return; | ||
} | ||
|
||
// Find the specific parent modal for this link | ||
// since .close() won't work without an ID if there are | ||
// multiple navigation menus in a post/page. | ||
const modal = link.closest( | ||
'.wp-block-navigation__responsive-container' | ||
); | ||
const modalId = modal?.getAttribute( 'id' ); | ||
|
||
link.addEventListener( 'click', () => { | ||
// check if modal exists and is open before trying to close it | ||
// otherwise Micromodal will toggle the `has-modal-open` class | ||
// on the html tag which prevents scrolling | ||
if ( modalId && modal.classList.contains( 'has-modal-open' ) ) { | ||
MicroModal.close( modalId ); | ||
} | ||
} ); | ||
} ); | ||
} ); |
Oops, something went wrong.