Declarative API for installing global DOM event handlers
#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.)
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?
+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?
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.
For mousemove and mouseup, I think @jordwalke was suggesting using ResponderEventPlugin…
I'll also add that a way to bind to onbeforeunload declaratively could be helpful.
Also will be cool to have context keyDown. Like context hokeys for keyboard driven apps.
@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.
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();
}
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.
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.
:+1: For React support window-level events such as keydown, keyup, etc. for keyboard shortcuts.
:+1:
:+1:
:+1:
:+1:
👍
:+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
// …
}
}
// …
})
:+1:
:+1:
:+1: