react icon indicating copy to clipboard operation
react copied to clipboard

Declarative API for installing global DOM event handlers

Open sophiebits opened this issue 13 years ago • 63 comments

#284 reminded me that one thing I've sometimes wanted is to install a handler on window for keypress (for keyboard shortcuts) or scroll. Right now I can just do window.addEventListener in componentDidMount but since React is listening already, it would be nice if there were some way for me to intercept those events. (In addition, receiving normalized synthetic events is generally more useful.)

sophiebits avatar Aug 21 '13 21:08 sophiebits

Yes, I've wanted this for resize as well. We talked once about adding maybe a onWindowResize event that fires on every component, but how would it bubble?

petehunt avatar Aug 21 '13 21:08 petehunt

+1 on that, just encountered a case for that

We talked once about adding maybe a onWindowResize event that fires on every component, but how would it bubble?

If it fires on every component does it makes sense for it to bubble?

andreypopp avatar Aug 22 '13 11:08 andreypopp

I wanted this for mousemove and mouseup as well. :) We're thinking about a larger declarative event system that could do conflict resolution but we should probably get this in the mean time. Not sure about the API though.

sebmarkbage avatar Sep 06 '13 16:09 sebmarkbage

For mousemove and mouseup, I think @jordwalke was suggesting using ResponderEventPlugin…

sophiebits avatar Sep 06 '13 18:09 sophiebits

I'll also add that a way to bind to onbeforeunload declaratively could be helpful.

sophiebits avatar Sep 06 '13 19:09 sophiebits

Also will be cool to have context keyDown. Like context hokeys for keyboard driven apps.

Aetet avatar Feb 15 '14 20:02 Aetet

@Aetet Sadly though, all of them operate on the assumption of US/Western keyboard layouts, unless you're willing to avoid support for ctrl and alt. Also, you could easily make this as a Mixin yourself.

@spicyj @petehunt As for this specific PR, what about simply exposing it as React.addEventListener(node, event, callback) (could be useful for when leaving the confines of React, like innerHTML) or as a mixin ReactEventsMixin + listenToEvents: [{event: callback}] which could then take care of the cleaning up itself.

Depending on the use-cases, I guess you could even do ReactWindowResizeMixin + handleWindowResize:, although you might end up with a lot of mixins. Again, depending on the size of the problem, you could even just have a single mixin that attaches to the events that have defined handlers/methods, handleWindowResize, etc.

The mixins could even be implemented as an addon, although it kind of feels like a "native" implementation would be nice.

syranide avatar Feb 18 '14 15:02 syranide

Maybe it would be useful to consider a Flux Store-like solution here? Like some kind of ReactEvents store which wraps window-level events and emits synthetic events. Your components could subscribe and unsubscribe as they see fit.

onWindowResize: function(event) {
  // do whatever you want in response to the resize event
},
componentDidMount: function() {
  this.subscription = ReactEvents.subscribe(ReactEvents.constants.WINDOW_RESIZE, this.onWindowResize);
},

componentWillUnmount: function() {
  this.subscription.remove();
}

nick-thompson avatar Aug 19 '14 18:08 nick-thompson

A couple of additional data points on this - the demo in http://kentwilliam.com/articles/rich-drag-and-drop-in-react-js mentions having to drop out of react events to do document listeners for mouse movements.

I've got a small module I put together for handing hotkeys that reaches into a bunch of React internals in order to produce synthetic keyboard events for document key events: https://github.com/glenjamin/react-hotkey/blob/master/index.js - providing a neat top-level listener that can be subscribed to, but forcing components to manage their own subscriptions' lifecycle seems like a reasonable tradeoff to me.

glenjamin avatar Sep 03 '14 22:09 glenjamin

An API to hook events into the component events, like @glenjamin described, to produce synthetic events would be a nice thing to have.

this.events.fromEvent( ... )   // events on this components DOM representatoin
this.events.fromEventTarget( ... ) // events from other targets like "document"

I would recommend a look at the Bacon.js wrappers. Maybe a fromCallback binder would be great, too?

It needs to be useable declaratively ( like other events )

// inside the component:
render: function() {
    return (
      <div onMyEvent={handler} > test </div>
    );
}

// from outside of the component, too? ( i do not think so )
<MyComponent onMyCusomEvent={eventhandler} />

and when could it be registered?

// before the first rendering, because of the custom event attribute
componendWillMount: function(events) {
    events.fromEvent( ... ).as('onMyEvent')
}

i think that is basically what @nick-thompson and @syranide were saying.

ThomasDeutsch avatar Sep 05 '14 11:09 ThomasDeutsch

:+1: For React support window-level events such as keydown, keyup, etc. for keyboard shortcuts.

gasi avatar Sep 23 '14 18:09 gasi

:+1:

jsdir avatar Sep 23 '14 22:09 jsdir

:+1:

vimto avatar Sep 25 '14 21:09 vimto

:+1:

pxwise avatar Nov 14 '14 00:11 pxwise

:+1:

danyx23 avatar Nov 29 '14 22:11 danyx23

👍

byelims avatar Dec 02 '14 13:12 byelims

:+1:, I'd like something like this :

var Modal = React.createClass({
  componentDidMount() {
    React.addEventListener(document, "keyup", this.handleShortcuts)
  },
  componentWillUnmount() {
    React.removeEventListener(document, "keyup", this.handleShortcuts)
  },
  handleShortcuts(eventObject) {
    switch(eventObject.which) {
      case 27:
        this.props.hide()
        break
      // …
    }
  }
  // …
})

bloodyowl avatar Dec 02 '14 13:12 bloodyowl

:+1:

acdlite avatar Dec 02 '14 20:12 acdlite

:+1:

aldendaniels avatar Feb 04 '15 02:02 aldendaniels

:+1:

mathieumg avatar Feb 04 '15 02:02 mathieumg