Skip to content

Navigation block overlay does not close when using anchor # custom links #71322

@jhmonroe

Description

@jhmonroe

Description

Generally when you click an item in the mobile version of the nav, the page load causes it to appear the menu has closed.

In the case of someone using a jump link / id # as the target/destination of a custom link to jump down the page, the mobile menu does not close.

A simple/common use case is having the contact link in the nav jump to the footer where all the contact info appears.

I use this on my own site: https://monroeand.co/

Seems like whatever JS controls the mobile nav needs an instruction to close the menu if something is clicked and there's no page reload...?

This code (provided by Claude) worked for me when tweaked with the correct classes and placed in functions.php, but this should be in core with the mobile menu code.

/*
 * Close mobile menu when clicking anchors
 */

function add_mobile_menu_close_script() {
    ?>
    <script type="text/javascript">
    (function($) {
        $(document).ready(function() {
            $('a[href*="#"]').on('click', function(e) {
                // Close mobile menu - adjust selectors for your theme
                $('body').removeClass('has-modal-open');
                $('.wp-block-navigation__responsive-container-close').click();
            });
        });
    })(jQuery);
    </script>
    <?php
}
add_action('wp_footer', 'add_mobile_menu_close_script');

Step-by-step reproduction instructions

  • put a custom link in a nav that jumps down to an ID on the page
  • open in narrow browser or mobile
  • click the link
  • menu will not close since there's no page load

Screenshots, screen recording, code snippet

No response

Environment info

No response

Please confirm that you have searched existing issues in the repo.

Please confirm that you have tested with all plugins deactivated except Gutenberg.

  • Yes

Please confirm which theme type you used for testing.

  • Block
  • Classic
  • Hybrid (e.g. classic with theme.json)
  • Not sure

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions