Skip to content

Conversation

@sunesimonsen
Copy link
Contributor

@sunesimonsen sunesimonsen commented Sep 13, 2018

Fixes: #137

Description

Take three, we had a pretty good solution in our previous menu component to handle closing the menu when clicked outside of the containers. This PR mirrors the functionality even closer.

Detail

  • Attaches the click outside handler to all documents when a menu opens.
  • Detaches the click outside handler when the menu closes or the component is unmounted.
  • Using the event capturing phase to beat event.stopPropagation()

Checklist

  • 💂‍♂️ includes new unit and snapshot tests
  • 📝 tested in Chrome, Firefox, Safari, Edge, and IE11

@sunesimonsen
Copy link
Contributor Author

sunesimonsen commented Sep 13, 2018

I need some help testing this. I already checked that the existing styleguide functionality works in Chrome, Firefox and Safari.

@coveralls
Copy link

coveralls commented Sep 13, 2018

Coverage Status

Coverage decreased (-0.07%) to 95.969% when pulling 80931e0 on ssimonsen/fix-menu-auto-close into 48ff63b on master.

Copy link
Contributor

@luis-almeida luis-almeida left a comment

Choose a reason for hiding this comment

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

Looks solid! 👍

Added a question but related to the unit test.

it('removes click outside event listener', () => {
const removeEventListenerSpy = jest.fn();

document.removeEventListener = removeEventListenerSpy;
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we restore the original method not to temper with future tests?
Not sure what's the cleanest way do this with jest but with sinon it looks something like this:

const removeEventListenerSpy =  sinon.spy(document, 'removeEventListener');
// ... assert
removeEventListenerSpy.restore();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is a good point, I just copied it from the old test. I'll just fix that.

Copy link
Contributor

Choose a reason for hiding this comment

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

The update looks good 👍

attachClickOutsideHandler() {
this.getDocuments().forEach(doc => {
doc.addEventListener('mousedown', this.handleOutsideMouseDown);
doc.addEventListener('mousedown', this.handleOutsideMouseDown, true);
Copy link
Member

@jzempel jzempel Sep 13, 2018

Choose a reason for hiding this comment

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

It's always friendly to provide a quick inline comment reminder for boolean parameters: i.e. doc.addEventListener(..., true /* useCapture*/);

Copy link
Member

@jzempel jzempel left a comment

Choose a reason for hiding this comment

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

Thanks, Sune!

@austingreendev
Copy link
Contributor

Are you able to link this locally within Guide to ensure that it works? We can also publish a beta tag if you need to test that way. Thanks for helping with this one!

@ryanseddon
Copy link
Contributor

Coverage decreased (-0.07%)

Seriously I don't get coveralls to me you've added tests yet almost every pr it slightly declines

@sunesimonsen
Copy link
Contributor Author

@Austin94

Are you able to link this locally within Guide to ensure that it works? We can also publish a beta tag if you need to test that way. Thanks for helping with this one!

I tested is quite heavily in the styleguide with different kinds of iframes. But I haven't tested the normal functionality with IE and Edge. I'll try to see how hard it is to link into the guide-chrome, but my guess is that it is not easy.

@sunesimonsen
Copy link
Contributor Author

I tested the styleguide in IE 11 and Edge as well. I believe this should be ready to merge.

@austingreendev
Copy link
Contributor

Perfect, I'll take it through the release cycle.

I'll try to see how hard it is to link into the guide-chrome, but my guess is that it is not easy

With yarn it's gotten a lot easier. Just yarn build in the repo and then you can yarn add ~/Repos/react-components/packages/menus to install it without worrying about duplicated dependencies.

@sunesimonsen
Copy link
Contributor Author

I know how to link stuff. But it needs to get through 3 steps of bundling, so that was what I thought might be challenging 😜

@austingreendev austingreendev merged commit 6206a2c into master Sep 14, 2018
@austingreendev austingreendev deleted the ssimonsen/fix-menu-auto-close branch September 14, 2018 18:22
@sunesimonsen
Copy link
Contributor Author

Thanks 💯

@austingreendev
Copy link
Contributor

Just published to the registry

 - @zendeskgarden/react-menus: 3.6.2 => 3.6.3
 - @zendeskgarden/react-select: 2.4.3 => 2.4.4

Cheers 🥂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Menu doesn't close when an outside click hits an iframe

7 participants