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

Close overlay menu when clicking an anchor link #39625

Merged
merged 8 commits into from
May 3, 2022
Merged

Close overlay menu when clicking an anchor link #39625

merged 8 commits into from
May 3, 2022

Conversation

Trapsta
Copy link
Contributor

@Trapsta Trapsta commented Mar 22, 2022

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 the data-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

  1. Open a post or page.
  2. Create a navigation menu and add an anchor link.
  3. View the post/page on the frontend.
  4. Make your browser screen smaller, below 600px.
  5. Open the menu and click the anchor link.
  6. Observe that the overlay menu modal closes and page navigates to the anchor link's content. Try also testing by changing the navigation block overlay menu display setting to off , always or mobile.

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

@github-actions
Copy link

👋 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.

@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Mar 22, 2022
@Trapsta Trapsta changed the title Fix/gutenberg 39585 Close overlay menu when clicking an anchor link Mar 22, 2022
@getdave
Copy link
Contributor

getdave commented Mar 22, 2022

Thanks for the PR! 👏

I'm wondering could you complete the How section to describe why you feel this is the best approach and what alternatives you may have considered.

I was considering tackling this so it's great to see it's already being addressed. Thank you!

@JosVelasco
Copy link

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:
Off | Mobile | Always

@Trapsta
Copy link
Contributor Author

Trapsta commented Mar 23, 2022

Thanks @getdave , sure thing - I will add some context on that. Noted @JosVelasco , thanks for the heads up , I had probably overlooked that part

@getdave getdave self-requested a review March 23, 2022 11:11
@getdave
Copy link
Contributor

getdave commented Mar 23, 2022

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 getdave mentioned this pull request Mar 23, 2022
42 tasks
@Trapsta Trapsta marked this pull request as ready for review April 13, 2022 10:43
@Trapsta
Copy link
Contributor Author

Trapsta commented Apr 13, 2022

@getdave @JosVelasco I have updated the PR, please review when you can

@paaljoachim
Copy link
Contributor

paaljoachim commented Apr 14, 2022

I was asked by Jos to take a look at this PR.
I am following this method: https://make.wordpress.org/design/2021/03/03/testing-a-gutenberg-pull-request-pr/

Testing.
Local site
WP 6.0 Beta 1.
Twenty Twenty One
Chrome
OSX on my Macbook Pro.

Opened an existing page.
Added a Navigation block.
Added a few random links to pages and posts.
Added an anchor link.
Noticed that the Navigation block also used the dot save system. Saved the page and the Navigation Menus.

Previewed on the frontend.
Opened up Chrome dev tools and made the screen smaller than 600px.
Noticed the menu is now showing the hamburger/mobile menu.
Clicked the anchor link.

Overlay menu modal closes and the page navigates to the anchor link's content.

Backend Navigation block settings.
Overlay Menu: Off - Mobile - Always. All three worked nicely with this PR fix.

Thank you @Trapsta !

Ps.
We should probably test this in multiple browsers.....

@paaljoachim paaljoachim added [Type] Bug An existing feature does not function as intended Backport to WP 6.7 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta labels Apr 14, 2022
@paaljoachim
Copy link
Contributor

As this is a bug fix I went ahead and added the backport to beta label.

@paaljoachim paaljoachim self-requested a review April 14, 2022 14:37
Copy link
Contributor

@paaljoachim paaljoachim left a 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!

@JosVelasco
Copy link

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
Local site
WordPress 5.9.3 and then WordPress 6.0-beta1
Twenty Twenty Two
Latest Firefox, Chrome and Safari.
OSX on my Macbook Air.

https://www.youtube.com/watch?v=uyP1rrljjuE

Copy link
Contributor

@getdave getdave left a 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.

Comment on lines 87 to 61
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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Copy link
Contributor

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.

Copy link
Contributor

@scruffian scruffian Apr 27, 2022

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.

… 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
@Trapsta
Copy link
Contributor Author

Trapsta commented Apr 28, 2022

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.

Copy link
Contributor

@getdave getdave left a 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 🙇

packages/block-library/src/navigation/view-modal.js Outdated Show resolved Hide resolved
packages/block-library/src/navigation/view-modal.js Outdated Show resolved Hide resolved
Trapsta and others added 2 commits May 3, 2022 13:09
Copy link
Member

@aristath aristath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 👍

@getdave getdave merged commit cc93e66 into WordPress:trunk May 3, 2022
@getdave
Copy link
Contributor

getdave commented May 3, 2022

Thank you again @Trapsta for the contribution.

@github-actions
Copy link

github-actions bot commented May 3, 2022

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!

@github-actions github-actions bot added this to the Gutenberg 13.2 milestone May 3, 2022
@gziolo gziolo added Backport to WP Minor Release Pull request that needs to be backported to a WordPress minor release and removed Backport to WP 6.7 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta labels May 6, 2022
@georgeh georgeh added the [Feature] Navigation in Site View Navigation section in the Site Editor when in Site View, offering a way to manage Navigation Menus a label May 11, 2022
@adamziel
Copy link
Contributor

adamziel commented Jun 21, 2022

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.

@coreyworrell
Copy link
Contributor

coreyworrell commented Nov 7, 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)
            }
        })
    }
}

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Navigation in Site View Navigation section in the Site Editor when in Site View, offering a way to manage Navigation Menus a First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Type] Bug An existing feature does not function as intended
Projects
No open projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Close overlay menu when clicking an anchor link