-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Close overlay menu when clicking an anchor link #39625
Conversation
👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @Trapsta! In case you missed it, we'd love to have you join us in our Slack community, where we hold regularly weekly meetings open to anyone to coordinate with each other. If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information. |
Thanks for the PR! 👏 I'm wondering could you complete the I was considering tackling this so it's great to see it's already being addressed. Thank you! |
Hello @Trapsta. Thanks for the PR. For testing purposes consider that you can also set the overlay menu to be visible always. Collapses the navigation options in a menu icon opening an overlay: |
Thanks @getdave , sure thing - I will add some context on that. Noted @JosVelasco , thanks for the heads up , I had probably overlooked that part |
Thanks. Let me know when it's ready for review. |
@getdave @JosVelasco I have updated the PR, please review when you can |
I was asked by Jos to take a look at this PR. Testing. Opened an existing page. Previewed on the frontend. Overlay menu modal closes and the page navigates to the anchor link's content. Backend Navigation block settings. Thank you @Trapsta ! Ps. |
As this is a bug fix I went ahead and added the backport to beta label. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My focus was on testing the functionality that it worked as it should using Chrome.
The fix works very well!
Also tested the PR on my end successfully. I even installed the popular plugin Page scroll to id to animate the scroll and it worked like a charm. Thanks @Trapsta. Tested on |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for this PR. I tested it and it worked as expected.
Screen.Capture.on.2022-04-27.at.15-19-52.mp4
Once issue I noticed was that for non-anchor links, the modal will close prior to Navigation. That might not always be desirable and we can guard against it by testing for an anchor before we add the handlers.
navigationLinks.forEach( function ( link ) { | ||
// we need to find the specific parent modal for this link | ||
// since .close() won't work without an ID in case we have | ||
// mutiple navigation menus in a post/page. | ||
const modal = link.closest( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
navigationLinks.forEach( function ( link ) { | |
// we need to find the specific parent modal for this link | |
// since .close() won't work without an ID in case we have | |
// mutiple navigation menus in a post/page. | |
const modal = link.closest( | |
navigationLinks.forEach( function ( link ) { | |
// Ignore non-anchor links. | |
if ( ! link.getAttribute( 'href' )?.startsWith( '#' ) ) { | |
return; | |
} | |
// we need to find the specific parent modal for this link | |
// since .close() won't work without an ID in case we have | |
// mutiple navigation menus in a post/page. | |
const modal = link.closest( |
We can ignore links that are no anchors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would also need to update the comment
// Close modal on clicking internal and external links inside modal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this makes sense. The other cases I thought about where:
- What if the link opens in new tab?
- What if the link is slow to load or is broken?
But I think in both of these cases we wouldn't want to close the overlay.
…o an anchor link Closes #gutenberg-39585
…lay menu nav links Closes #gutenberg-39585
… overlay menu is open before trying to close it Closes #gutenberg-39585
…enu overlay for anchor links Closes #gutenberg-39585
…links to seperate view-modal.js script Closes #gutenberg-39585
…closing modal on clicking link Closes #gutenberg-39585
Great feedback , I can see adding the filter @getdave suggested enhances the UX for non-anchor links quite a bit and I also added a filter for any anchor links that would open in a new tab to cover some the edge cases @scruffian pointed out. Also updated the comment to be more accurate. Thanks everyone for taking your time to test and review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to go. I've also pinged @scruffian and @draganescu for their opinions but I'd say this is a good improvement. Thank you again for your work here 🙇
update comment description Co-authored-by: Dave Smith <[email protected]>
fix comment typo Co-authored-by: Dave Smith <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! 👍
Thank you again @Trapsta for the contribution. |
Congratulations on your first merged pull request, @Trapsta! We'd like to credit you for your contribution in the post announcing the next WordPress release, but we can't find a WordPress.org profile associated with your GitHub account. When you have a moment, visit the following URL and click "link your GitHub account" under "GitHub Username" to link your accounts: https://profiles.wordpress.org/me/profile/edit/ And if you don't have a WordPress.org account, you can create one on this page: https://login.wordpress.org/register Kudos! |
I am removing the Backport label here. This PR depends on #36176 which wasn't backported in time for 6.0 and is now planned for 6.1. |
If this was the fix recently included in 6.1, it does not cover the common case of a link like 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. |
What?
This PR adds an event Listener to toggle the navigation block overlay menu on clicking links inside the overlay.
Fixes #39585
Why?
Navigating to anchor links in the navigation block in an overlay menu does not automatically close the overlay. This requires manually closing the modal to view the link's content.
How?
Since we are using Micromodal to implement the overlay menu, am using the
MicroModal.close()
method to toggle the modal on clicking the navigation link. My initial idea was to use thedata-micromodal-close
custom attribute which Micromodal uses as a trigger to close the modal but I couldn't get it to work reliably. The overlay would close but sometimes the browser would not navigate to the correct anchor.Testing Instructions
off
,always
ormobile
.Screenshots or screencast
Before
Screen.Recording.2022-03-20.at.12.23.56.mov
After
Screen.Recording.2022-03-21.at.12.40.57.mov
Fixes #39585