-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Conversation
@@ -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); |
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 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.
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.
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.
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.
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 👍
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.
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.
assets/localization-form.js
Outdated
if(this.elements.button.getAttribute('aria-expanded') == 'false') return; | ||
this.hidePanel(); | ||
event.stopPropagation(); |
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.
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"> |
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.
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.
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.
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 🤔
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.
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".
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.
Works as expected. I couldn't find anything.
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.
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"> |
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.
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); |
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.
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.
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 atabindex="-1'
it's triggering thefocusout
event listener we have and closes the dropdown.It works without an issue in the header and footer outside of the menu drawer 🤔
tabindex
on the#menu-drawer
cause I couldn't see where it was necessary. That fixes our issue.ESC
, it removes the overlay accordingly. It wasn't before.ESC
listener for thelocalization-form
, so that when you pressESC
it closes only the dropdown. And if the dropdown is closed it closes the menu drawer.Other considerations
Decision log
Visual impact on existing themes
Shouldn't visually impact anything.
Testing steps/scenarios
ESC
to close the dropdown and menu drawer.menu-drawer
custom element.ESC
to close the menu drawer should remove it's overlay.Demo links
Checklist