Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Nav block anchor link closing #45608

Closed
getdave opened this issue Nov 8, 2022 · 3 comments · Fixed by #45829
Closed

Fix Nav block anchor link closing #45608

getdave opened this issue Nov 8, 2022 · 3 comments · Fixed by #45829
Assignees
Labels
[Block] Navigation Affects the Navigation Block [Type] Bug An existing feature does not function as intended

Comments

@getdave
Copy link
Contributor

getdave commented Nov 8, 2022

    > If this was the fix recently included in 6.1, it does not cover the common case of a link like `/#anchor` or `/some-page/#anchor`. It only works if the `href` exactly starts with `#`. So if you are on `/some-page/` and you click the `/some-page/#anchor` link, it will scroll down to the anchor, but the modal will remain open.

My current fix:

import MicroModal from 'micromodal'

// Fix navigation block
const links = document.querySelectorAll('.wp-block-navigation-item__content')

for (const link of links) {
    const modal = link.closest('.wp-block-navigation__responsive-container')
    const id = modal?.id || ''

    if (id && link.href.indexOf('#') !== -1 && link.pathname === location.pathname) {
        link.addEventListener('click', () => {
            if (modal.classList.contains('has-modal-open')) {
                MicroModal.close(id)
            }
        })
    }
}

@draganescu I looks like we'll need a bug fix PR for this.

Originally posted by @getdave in #39625 (comment)

@getdave getdave added [Type] Bug An existing feature does not function as intended [Block] Navigation Affects the Navigation Block labels Nov 8, 2022
@coreyworrell
Copy link
Contributor

Just a note for the simple fix mentioned above, it may be good to check that the link.hostname matches as well to prevent a link like https://other-website.com/blog/#anchor from being included while on https://my-website.com/blog/.

Then again, closing the modal when clicking an external link probably doesn't pose any issues either.. Honestly since submenus are triggered by buttons (afaik), the modal should probably close after clicking any link. Unless that is just undesirable to see the menu close before the browser navigates to the next page.

@getdave
Copy link
Contributor Author

getdave commented Nov 10, 2022

@coreyworrell Thanks very much for the feedback and observations. That's really helpful.

I wonder, would you consider submitting a PR to fix this feature? I'm not sure whether it would be your first PR on this repo but I would be more than happy to help you through the process if that's of interest.

What do you think? All contributors received props on the Gutenberg Plugin releases and ultimately in WP Core.

@coreyworrell
Copy link
Contributor

@getdave sure, just created a pull request #45829

@talldan talldan linked a pull request Nov 17, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Navigation Affects the Navigation Block [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants