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

Bail out of dispatching a listener in useEventListenerOutside when the document doesn't have focus #620

Merged
merged 4 commits into from
Apr 15, 2020

Conversation

tom-sherman
Copy link
Member

Fixes #619

Does this PR introduce a breaking change?

No

@codesandbox-ci
Copy link

codesandbox-ci bot commented Apr 15, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 273a0cc:

Sandbox Source
ecstatic-sunset-i6why Configuration
reakit-firefox-hideonclickoutside-bug Issue #619

@ariakit-bot
Copy link

ariakit-bot commented Apr 15, 2020

Deploy preview for reakit ready!

Built with commit 273a0cc

https://deploy-preview-620--reakit.netlify.com

@tom-sherman tom-sherman marked this pull request as draft April 15, 2020 14:18
@tom-sherman
Copy link
Member Author

It appears that the reason this issue appears on Firefox and not Chrome is that on Firefox the focus event fires with document as the target (making the contains(container, target) evaluate to true), whereas in Chrome the focus is passed directly to the element that you have clicked on.

For resizing, focus events aren't fired in Chrome but they are in Firefox; again, with the target set to the document.

@tom-sherman
Copy link
Member Author

You can verify that it's fixed using the first Modal example in the deploy preview: https://deploy-preview-620--reakit.netlify.com/docs/dialog/#usage

@diegohaz
Copy link
Member

You can verify that it's fixed using the first Modal example in the deploy preview: https://deploy-preview-620--reakit.netlify.com/docs/dialog/#usage

It seems that all clicks outside were disabled now. Tests are failing because of this.

It appears that the reason this issue appears on Firefox and not Chrome is that on Firefox the focus event fires with document as the target (making the contains(container, target) evaluate to true), whereas in Chrome the focus is passed directly to the element that you have clicked on.

For resizing, focus events aren't fired in Chrome but they are in Firefox; again, with the target set to the document.

So I guess the check should be if (target === document) return;.

@codecov
Copy link

codecov bot commented Apr 15, 2020

Codecov Report

Merging #620 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #620   +/-   ##
=======================================
  Coverage   95.37%   95.37%           
=======================================
  Files         118      118           
  Lines        2269     2272    +3     
  Branches      674      675    +1     
=======================================
+ Hits         2164     2167    +3     
  Misses        105      105           
Impacted Files Coverage Δ
...reakit/src/Dialog/__utils/useHideOnClickOutside.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update db71535...273a0cc. Read the comment docs.

@tom-sherman tom-sherman marked this pull request as ready for review April 15, 2020 15:04
}
},
options.visible && options.hideOnClickOutside
);

const document = getDocument(dialogRef.current);
Copy link
Member

Choose a reason for hiding this comment

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

dialogRef.current may not be populated at the time the useHideOnClickOutside function is called, so this should be inside the event handler in this case (it could be outside the event handler on the other custom hook because it was still inside useEffect.

Copy link
Member Author

Choose a reason for hiding this comment

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

It sounds like getDocument shouldn't accept null as a parameter in that case, should the type be changed? Looking at it's call sites, it currently only accepts null from getActiveElement, and getActiveElement's call sites don't pass in null either.

Copy link
Member

Choose a reason for hiding this comment

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

getDocument will return the current window's document as a fallback. My understanding is that that's safer as most people will not need the specific element.ownerDocument (as far as I know, only those using components inside of an iframe will need it).

But we can discuss it better on #456

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah ok, vscode missed a load of callsites where null unions were being passed in anyway so the change isn't as trivial as I first thought.

Copy link
Member

@diegohaz diegohaz left a comment

Choose a reason for hiding this comment

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

It looks like it's working on Firefox now, and tests are passing! Thanks @tom-sherman! ❤️

if (mouseDownRef.current === event.target && options.hide) {
options.hide();
if (mouseDownRef.current === event.target) {
options.hide?.();
Copy link
Member

Choose a reason for hiding this comment

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

Nice 👍

@diegohaz diegohaz merged commit 7c3119d into master Apr 15, 2020
@diegohaz diegohaz deleted the bugfix/gh-619-firefox-hideonclickoutside branch April 15, 2020 15:40
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.

Dialog closes unexpectedly on Firefox when using hideOnClickOutside
3 participants