Make WordPress Core

Ticket #53976: 53976.patch

File 53976.patch, 1.2 KB (added by karthickmurugan, 13 months ago)

Patch file for scroll issue when mobile menu closes

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

     
    111111                        return;
    112112                }
    113113
    114                 if ( mobileButton ) {
    115                         mobileButton.onclick = function() {
    116                                 wrapper.classList.toggle( id + '-navigation-open' );
    117                                 wrapper.classList.toggle( 'lock-scrolling' );
    118                                 twentytwentyoneToggleAriaExpanded( mobileButton );
    119                                 mobileButton.focus();
     114                if (mobileButton) {
     115                        let scrollPosition = 0; // To store the current scroll position
     116               
     117                        mobileButton.onclick = function () {
     118                                const isMenuOpen = wrapper.classList.contains(id + '-navigation-open');
     119               
     120                                if (!isMenuOpen) {
     121                                        // Save the scroll position when opening the menu
     122                                        scrollPosition = window.scrollY;
     123                                }
     124               
     125                                wrapper.classList.toggle(id + '-navigation-open');
     126                                wrapper.classList.toggle('lock-scrolling');
     127                                twentytwentyoneToggleAriaExpanded(mobileButton);
     128               
     129                                if (isMenuOpen) {
     130                                        // Restore scroll position when closing the menu
     131                                        window.scrollTo(0, scrollPosition);
     132                                }
    120133                        };
    121134                }
    122135