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

Global event handlers on document.body #13142

Closed
jdzijlstra opened this issue Jul 3, 2018 · 1 comment
Closed

Global event handlers on document.body #13142

jdzijlstra opened this issue Jul 3, 2018 · 1 comment

Comments

@jdzijlstra
Copy link

Related to ##7094

We need to implement a global keyboard handler.
We currently have an onkeydown handler on our main-component, which fills the whole page.
However, by default, the tag has the focus and therefore our handler of our main-component does not see these key-events.

We were thinking about adding an onkeydown handler on document, but in this case we would have to add an handler for native events. We would however like to use a handler for React's Synthetic events.

Searching the web shows that many people have this same issue, but I did not find real solutions.

As far as I see, there are currently some solutions:

  1. Add a handler to our main-component, and make sure this main-component gets the focus, instead of the tag. Problem here is that you need to make sure all the time, that the main-component gets focused.
  2. Add a hander to document, that handles native events. However, there seem to be problems, when the stopPropagation() or preventDefault() is called on the native events in relation to React.

Is there already a good solution for this, or is there anything planned?

@gaearon
Copy link
Collaborator

gaearon commented Aug 2, 2018

Having a document-level native handler sounds like a solution. I don't understand your point about stopPropagation — if you call it, it means you want it not to reach the top, right?

Regardless, this doesn't look like a bug report — more like a support question or a feature request. We try to keep support outside of the repo, check these resources instead: https://reactjs.org/community/support.html. For the feature request, you can follow #285. (Nothing planned in short term though.)

@gaearon gaearon closed this as completed Aug 2, 2018
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

No branches or pull requests

2 participants