question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hidden `onfocus` side-effect may interfere with other code

See original GitHub issue

We had this case come up in a project we were working on. We were using window.onfocus to track when our app was focused or not, but then we added react-dropzone to the application and noticed that this code was no longer working. Apparently (at least on Chrome), document.body.onfocus and window.onfocus are the same callback. So, the componentDidMount code was overriding our onfocus function.

This was most likely introduced my this change: https://github.com/okonet/react-dropzone/commit/c2b3b16e2f606ceec30b26be934ad5a1d532dc0f#diff-1fdf421c05c1140f6d71444ea2b27638R30

And I can see why this change was made. But wouldn’t the following work just as well?

  componentDidMount() {
    this.enterCounter = 0;
    window.addEventListener('focus', this.onFileDialogCancel, true);
  }

  componentWillUnmount() {
    window.removeEventListener('focus', this.onFileDialogCancel);
  }

The reason I ask instead of making a PR directly is that you might have some insight as to the problems this causes that I may not be aware of.

Thanks!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
aprilandjancommented, Jan 10, 2018

This is still a problem because overriding onfocus cause other problems (for example, node-webkit window focus event rely on document onfocus attribute so when using React-Dropzone my previous listener bind for nw is lost).

1reaction
imontielcommented, Nov 30, 2016

I think the problem is the focus event doesn’t actually work on document.body consistently. It is a special global event that should be attached to the window.

IE supports focus on almost every element, while FF and Chrome only support it for form elements and the window.

I’ll submit a PR when I get a chance to verify that it actually works.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Focus in Jetpack Compose - Medium
The fix for this, I first thought, was to try and run the focusManager.moveFocus call in a SideEffect - a piece of code...
Read more >
How to Use the Focus Subsystem - Oracle Help Center
There are several situations in which you need to make focus requests after all other changes that might affect the focus applies to:...
Read more >
Considerations for fireEvent | Testing Library
If an element is focused, a focus event is dispatched, the active element in the document changes, and the previously focused element is...
Read more >
onclick() and onblur() ordering issue - javascript - Stack Overflow
This triggers our container div to lose focus which closes the dropdown so the focus event can't bubble further and therefore can't trigger ......
Read more >
A Story of a React Re-Rendering Bug - Engineering Blog
The input field is a text input, and event handlers for focus and blur events. · When it gets focused, it will trigger...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found