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

react-dom: Ability to access window.event in development #11687

Closed
ConradIrwin opened this issue Nov 28, 2017 · 4 comments · Fixed by #11696
Closed

react-dom: Ability to access window.event in development #11687

ConradIrwin opened this issue Nov 28, 2017 · 4 comments · Fixed by #11696

Comments

@ConradIrwin
Copy link
Contributor

Imported from #10474 (comment) at request of @gaearon

Do you want to request a feature or report a bug? Bug

What is the current behavior?

In development mode React uses invokeGuardedCallback to be able to catch errors without interrupting the normal "break on uncaught exceptions" feature of devtools.

In the current implementation of invokeGuardedCallback an event is created and dispatched, which allows for error handling without a try{ } catch block.

Unfortunately it has the side effect of overriding window.event, and so you are unable to access window.event within guarded callbacks when running React in development mode. In production mode it works fine.

We would like to access window.event at Superhuman to be able to detect where DOM focus is from the user's point of view. Usually this is document.activeElement, but in a few cases (like during a blur event, or when clicking between two iframes) the focus will end up in a different place.

This works fine in production, and we currently work around this in development mode by overriding ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactDOMEventListener.handleTopLevel and maintiaining a reference to the event. In production mode we just use window.event.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via https://jsfiddle.net or similar (template for React 16: https://jsfiddle.net/Luktwrdm/, template for React 15: https://jsfiddle.net/hmbg7e9w/).

in development: https://jsfiddle.net/n782L2qg/, in production: https://jsfiddle.net/kqt2o7pr/. In both cases I would expect the alert to say window.event.type: click, but in development it says window.event.type: react-invokeguardedcallback

What is the expected behavior?

I would like a way to access window.event in development. I'm happy for this to be somewhat obscure, but ideally I would not have to use the secret internals to make this work.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React? Development 0.15 and 0.16 at least. probably before.

@gaearon
Copy link
Collaborator

gaearon commented Nov 28, 2017

Can we just store window.event and immediately set it back when we're inside our event?

@ConradIrwin
Copy link
Contributor Author

ConradIrwin commented Nov 28, 2017 via email

@gaearon
Copy link
Collaborator

gaearon commented Nov 28, 2017

Yeah if you'd like.

@ConradIrwin
Copy link
Contributor Author

It's here: #11696

gaearon pushed a commit that referenced this issue Aug 14, 2018
Before this change in development window.event was overridden
in invokeGuardedCallback.

After this change window.event is preserved in the browsers that
support it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants