Twenty Twenty-One: Check for navigation element before using it.

This prevents a javascript error in case the primary navigation has been removed, for example in a child theme.

Props sushmak.
Fixes #52773.



git-svn-id: https://develop.svn.wordpress.org/trunk@51072 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Kelly Choyce-Dwan 2021-06-04 17:06:15 +00:00
parent e7ac82157e
commit a08bbbadd8

View File

@ -96,7 +96,13 @@ function twentytwentyoneExpandSubMenu( el ) { // jshint ignore:line
*/
var navMenu = function( id ) {
var wrapper = document.body, // this is the element to which a CSS class is added when a mobile nav menu is open
mobileButton = document.getElementById( id + '-mobile-menu' );
mobileButton = document.getElementById( id + '-mobile-menu' ),
navMenuEl = document.getElementById( 'site-navigation' );
// If there's no nav menu, none of this is necessary.
if ( ! navMenuEl ) {
return;
}
if ( mobileButton ) {
mobileButton.onclick = function() {
@ -167,7 +173,7 @@ function twentytwentyoneExpandSubMenu( el ) { // jshint ignore:line
}
} );
document.getElementById( 'site-navigation' ).querySelectorAll( '.menu-wrapper > .menu-item-has-children' ).forEach( function( li ) {
navMenuEl.querySelectorAll( '.menu-wrapper > .menu-item-has-children' ).forEach( function( li ) {
li.addEventListener( 'mouseenter', function() {
this.querySelector( '.sub-menu-toggle' ).setAttribute( 'aria-expanded', 'true' );
twentytwentyoneSubmenuPosition( li );