Skip to content

Conversation

@emplums
Copy link

@emplums emplums commented Mar 14, 2019

This PR refactors the Details element to use a document event listener to show/hide the Details element when a click is made, instead of an overlay element. The overlay element works fine if you've only got one Details element on the page, but if you're using several (in the case of the new docs header), clicking on another Details element doesn't open the new element, because the overlay is blocking the DOM element.

I wrote this to prioritize not breaking the current API of the Details element, which is why there's a conditional in the toggle function now. I felt this was a fair compromise to keep the API the same :)

@emplums emplums requested a review from shawnbot March 14, 2019 18:40
@emplums emplums changed the base branch from master to release-12.0.0 March 14, 2019 18:41
@emplums emplums mentioned this pull request Mar 14, 2019
5 tasks
function openMenu() {
if (!open) {
setOpen(true)
document.addEventListener('click', closeMenu)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm trying to think of why this might not need to be qualified with listener options, namely:

  • {capture: true} so that other elements' click handlers can't prevent this one?
  • {once: true} to ensure that it's only called once?

But maybe we leave it as-is for now and revisit when we've used it more? /cc @muan who knows way too much about this subject 😄

Copy link

Choose a reason for hiding this comment

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

I don't really understand the premise here 😬. From the original issue body it sounds like those menus just shouldn't use the optional overlay?

But looks like this will also close details when user clicks inside the menu which wasn't the case.

Copy link
Author

Choose a reason for hiding this comment

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

The reason why I didn't want to use the CSS overlay is that when there is a menu open, and I want to click on another menu to open the new menu, clicking only closes the first menu, and doesn't open the second one (the second menu's click never triggers because it's behind the overlay)

Copy link
Author

Choose a reason for hiding this comment

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

And yeah, we do want to close the menu when the user clicks inside of a menu because that will jump them down to a different part of the page and at that point the menu can/should be closed

Copy link
Author

Choose a reason for hiding this comment

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

Let me see if I can get deploys working so that y'all can play around with it

Copy link

@muan muan Mar 15, 2019

Choose a reason for hiding this comment

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

In dotcom there is a JS class that listens to <details>'s toggle open event, and close other <details open> with the same class name; it can be used with or without .details-overlay.

When used with .details-overlay, the only way to get into that state is with keyboard, which was the use case the class was initially made for.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm yeah, I think our current assumption is that any click inside of the menu closes it because it's a link or button. 🤔

Emily Plummer and others added 2 commits March 15, 2019 13:00
@emplums emplums merged commit 951110e into release-12.0.0 Mar 18, 2019
@emplums emplums deleted the details-fix branch March 18, 2019 17:27
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.

4 participants