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

[Header drawer] fix header locales closing dropdown too soon and ESC behaviour #2472

Merged
merged 6 commits into from
Apr 6, 2023

Conversation

ludoboludo
Copy link
Contributor

@ludoboludo ludoboludo commented Mar 27, 2023

PR Summary:

Issue with locale selector in the drawer menu and closing the menu drawer with the ESC key.

Why are these changes introduced?

Fixes #2475

What approach did you take?

When you click on the locale selector it opens the dropdown. If you click on the scrollbar, because it's not a focusable element (it's part of the ul), and because the #menu-drawer has a tabindex="-1' it's triggering the focusout event listener we have and closes the dropdown.

It works without an issue in the header and footer outside of the menu drawer 🤔

  • I've gone ahead and removed the tabindex on the #menu-drawer cause I couldn't see where it was necessary. That fixes our issue.
  • I'm also adding a fix so that when you close the menu drawer via ESC, it removes the overlay accordingly. It wasn't before.
  • And finally, I've added some logic around the ESC listener for the localization-form, so that when you press ESC it closes only the dropdown. And if the dropdown is closed it closes the menu drawer.

Other considerations

Decision log

# Decision Alternatives Rationale Downsides
1

Visual impact on existing themes

Shouldn't visually impact anything.

Testing steps/scenarios

  • Either shrink your window to have the hamburger menu icon or enable the drawer menu type on desktop.
  • Test the locale selectors and make sure you can scroll using the scrollbar.
  • Try with keyboard navigation only as well and using ESC to close the dropdown and menu drawer.
  • Test the filters on the collection page. Set them to be shown as a drawer and make sure it works as expected. It's reusing the same menu-drawer custom element.
  • Using ESC to close the menu drawer should remove it's overlay.
  • Then test in Safari/Firefox as well.

Demo links

Checklist

@@ -398,6 +398,8 @@ class MenuDrawer extends HTMLElement {
document.body.classList.remove(`overflow-hidden-${this.dataset.breakpoint}`);
removeTrapFocus(elementToFocus);
this.closeAnimation(this.mainDetailsToggle);

if (event instanceof KeyboardEvent) elementToFocus?.setAttribute('aria-expanded', false);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is to fix the issue where when using the ESC key to close the menu drawer, it doesn't remove the overlay.
The aria-expanded attribute is being toggle based on a click event on line 17-19. But there is nothing taking care of it for the ESC event.

Copy link
Contributor

Choose a reason for hiding this comment

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

Naturally I went to see why the more generic handlers and onKeyUpEscape weren't also managing the esc event, and it appears to be because of the nested details/summary elements specifically involved in the header drawer using if (summary.closest('header-drawer')) return; I don't like that this needs to be an exception for a specific element, but since header drawer is already handling other managing other stuff on open/close, it's probably ok.

Though I notice the filters drawer always closes the whole drawer on esc, even from a nested details. I'd somewhat expect if we solved the header issue more generically, it would carry over to this (and maybe future use cases) https://screenshot.click/05-40-k2cj6-im39u.mp4

That said, it's probably more important that we fix the header issues so I won't block this fix.

Copy link
Contributor Author

@ludoboludo ludoboludo Apr 5, 2023

Choose a reason for hiding this comment

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

Ok so I found the source of the problem. I pushed a fix for it. I don't think it's ideal in a way as it might not be very future proof and we could maybe have a better logic. But for now it does the trick. Let me know what you think 👍

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I'm imagining targeting those elements without using section-specific classes by checking if they don't have details parents or something, so it's completely generic, but I also don't know how feasible that is in practice, so what you have covers us for now.

Comment on lines 25 to 27
if(this.elements.button.getAttribute('aria-expanded') == 'false') return;
this.hidePanel();
event.stopPropagation();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here I added some logic so that when pressing the ESC key, it doesn't trigger both the local dropdown being closed and the menu drawer.
Now it should close the dropdown if it's open. Then if you press again it will close the menu drawer.

<div id="menu-drawer" class="gradient menu-drawer motion-reduce" tabindex="-1">
<div id="menu-drawer" class="gradient menu-drawer motion-reduce">
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here I couldn't see where this could be used. I tried to look at where we apply focus in our JS but it didn't seem like we are actually focusing on that element at any moment 🤔
I might be missing something so let me know if you catch anything.

Basically here, when clicking on the scrollbar from the locale dropdown, it would register the click as if it was on this menu-drawer element and trigger the focusout event which closes the dropdown. Removing that tab index fixes it.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's an interesting one, I tried to remove tabindex="-1" from children elements of this element as well and didn't notice any difference 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

hmm I don't like removing stuff I can't vouch for, but I also couldn't find the purpose for it being on this element. If anything I would expect the tabindex to be on the role="list".

@ludoboludo ludoboludo changed the title fix header locales when trying to close dropdown [Header drawer] fix header locales closing dropdown too soon and ESC behaviour Mar 30, 2023
@ludoboludo ludoboludo marked this pull request as ready for review March 30, 2023 15:21
@kmeleta kmeleta self-requested a review March 30, 2023 18:27
@eugenekasimov eugenekasimov self-requested a review March 31, 2023 20:35
@melissaperreault melissaperreault self-requested a review April 3, 2023 20:56
Copy link
Contributor

@eugenekasimov eugenekasimov left a comment

Choose a reason for hiding this comment

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

Works as expected. I couldn't find anything.

Copy link
Contributor

@kmeleta kmeleta left a comment

Choose a reason for hiding this comment

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

Fixes all work great. Left a couple comments and I also opened an issue to fix focus trapping in the header drawer #2496 which I noticed while testing

<div id="menu-drawer" class="gradient menu-drawer motion-reduce" tabindex="-1">
<div id="menu-drawer" class="gradient menu-drawer motion-reduce">
Copy link
Contributor

Choose a reason for hiding this comment

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

hmm I don't like removing stuff I can't vouch for, but I also couldn't find the purpose for it being on this element. If anything I would expect the tabindex to be on the role="list".

@@ -398,6 +398,8 @@ class MenuDrawer extends HTMLElement {
document.body.classList.remove(`overflow-hidden-${this.dataset.breakpoint}`);
removeTrapFocus(elementToFocus);
this.closeAnimation(this.mainDetailsToggle);

if (event instanceof KeyboardEvent) elementToFocus?.setAttribute('aria-expanded', false);
Copy link
Contributor

Choose a reason for hiding this comment

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

Naturally I went to see why the more generic handlers and onKeyUpEscape weren't also managing the esc event, and it appears to be because of the nested details/summary elements specifically involved in the header drawer using if (summary.closest('header-drawer')) return; I don't like that this needs to be an exception for a specific element, but since header drawer is already handling other managing other stuff on open/close, it's probably ok.

Though I notice the filters drawer always closes the whole drawer on esc, even from a nested details. I'd somewhat expect if we solved the header issue more generically, it would carry over to this (and maybe future use cases) https://screenshot.click/05-40-k2cj6-im39u.mp4

That said, it's probably more important that we fix the header issues so I won't block this fix.

assets/localization-form.js Outdated Show resolved Hide resolved
assets/global.js Outdated Show resolved Hide resolved
@ludoboludo ludoboludo merged commit 93078bf into main Apr 6, 2023
@ludoboludo ludoboludo deleted the fix-drawer-locales-closing-issues branch April 6, 2023 16:36
phapsidesGT pushed a commit to Gravytrain-UK/gt-shopify-dawn-theme that referenced this pull request Sep 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Locale selectors] In drawer issues when clicking on the scroll bar or using ESC key
3 participants