Make WordPress Core

Ticket #52006: 52006.patch

File 52006.patch, 1.3 KB (added by poena, 4 years ago)

Close menu and scroll to anchor if an anchor link is clicked.

  • src/wp-content/themes/twentytwentyone/assets/js/primary-navigation.js

     
    150150                        }
    151151                } );
    152152
     153                /**
     154                 * Close menu and scroll to anchor when an anchor link is clicked.
     155                 * Adapted from TwentyTwenty.
     156                 */
     157                document.addEventListener( 'click', function( event ) {
     158                        // If target onclick is <a> with # within the href attribute
     159                        if ( event.target.hash && event.target.hash.includes( '#' ) ) {
     160                                wrapper.classList.remove( id + '-navigation-open', 'lock-scrolling' );
     161                                twentytwentyoneToggleAriaExpanded( mobileButton );
     162                                // Wait 550 and scroll to the anchor.
     163                                setTimeout(function () {
     164                                        var anchor = document.getElementById(event.target.hash.slice(1));
     165                                        anchor.scrollIntoView();
     166                                }, 550);
     167                        }
     168                } );
     169
    153170                document.getElementById( 'site-navigation' ).querySelectorAll( '.menu-wrapper > .menu-item-has-children' ).forEach( function( li ) {
    154171                        li.addEventListener( 'mouseenter', function() {
    155172                                this.querySelector( '.sub-menu-toggle' ).setAttribute( 'aria-expanded', 'true' );