react icon indicating copy to clipboard operation
react copied to clipboard

Support Passive Event Listeners

Open sebmarkbage opened this issue 10 years ago • 62 comments

https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md

It would be good to have everything be passive by default and only opt-in to active when needed. E.g. you could listen to text input events but only preventDefault or used controlled behavior when you have active listeners.

Similarly, we could unify this with React Native's threading model. E.g. one thing we could do there is synchronously block the UI thread when there are active listeners such as handling keystrokes.

cc @vjeux @ide

sebmarkbage avatar Apr 07 '16 18:04 sebmarkbage

This landed in Chrome 51. Is there any updated plan to support this in React? :O

jhgg avatar May 29 '16 09:05 jhgg

How is this possible if React has only one event listener on document, and then delegates to others? @sebmarkbage

aleksandar-b avatar Jul 29 '16 07:07 aleksandar-b

What's the current status of issue with Passive Events ?

followdarko avatar Aug 17 '16 09:08 followdarko

I just hit a warning in chrome about handling the wheel event, which could be optimized if it were registered as a passive event handler. So having this in React would be neat!

radubrehar avatar Aug 18 '16 08:08 radubrehar

You'll also want to handle arbitrary options, such as once which has already landed in Firefox nightly: https://twitter.com/mozhacks/status/758763803991474176. Full list: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener

nolanlawson avatar Sep 29 '16 20:09 nolanlawson

FWIW, Facebook listens to active wheel events to block outer scrolling when sidebars or chat windows are scrolled. We can't implement the UI without it. We still want to support this as an option but the problem space is still incomplete so there might evolve alternative solutions to this problem that doesn't involve passive event listeners. So it is still an active design space.

sebmarkbage avatar Sep 29 '16 20:09 sebmarkbage

It's important to keep both active listeners and add support passive ones. On desktop applications you don't see any difference, but on mobile apps passive scroll listeners give a great speed boost.

Little suggestion:

<SomeElement
  onScroll={this.onScrollThatCallsPreventDefault}
  onScrollPassive={this.onScrollThatJustListens}
  ...this.props
/>

romulof avatar Sep 29 '16 20:09 romulof

@romulof yeah, this is how you register events on the capture phase as well

<SomeElement
  onClick={this.onClick}
  onClickCapture={this.onClickCapture}
  onScrollPassive={this.onScrollPassive}
/>

so I imagine this would be the proper API to support passive events as well.

Side note: a tricky question is - how would you register passive events for the capture phase? I suppose this is not possible, by the nature of passive events. Since they are even not allowed to call event.preventDefault(), so probably this is a non-issue.

radubrehar avatar Sep 30 '16 07:09 radubrehar

@radubrehar, onScrollCapturePassive looks like the whole bible in camel-case.

romulof avatar Sep 30 '16 07:09 romulof

:) It's not the case, since there are no passive events on the capture phase.

radubrehar avatar Sep 30 '16 08:09 radubrehar

Sure it doesn't make sense, but I would't count on it. There's also other types event binding, such as once.

Another suggestion:

<SomeElement
  onScroll={this.onScrollThatCallsPreventDefault}
/>
<SomePassiveElement
  onScroll={{
    passive: true,
    capture: true,
    handler: this.onScrollThatJustListens,
  }}
/>

This way React would have to detect whether the event handler is a function (normal binding), or and object containing binding options and the handler function.

romulof avatar Sep 30 '16 08:09 romulof

I think the object approach with options makes more sense than onFooPassive, since there are other options that might be needed. If combined with @sebmarkbage's suggestion that events should be passive by default, this probably wouldn't be too cumbersome.

Another approach that comes to mind would be to attach properties to the event handler to allow them to opt out of passive mode (or toggle other options). Something like this:

class Foo extends React.Component {
  constructor() {
    this.handleScroll = this.handleScroll.bind(this);
    this.handleScroll.passive = false;
  }

  handleScroll() {
    ...
  }

  render() {
    return <div onScroll={this.handleScroll} />;
  }
}

In theory, this would work pretty nicely with decorators, once they land.

lencioni avatar Oct 12 '16 00:10 lencioni

Thinking about this a little more, I think it would be better to add an event options property to the function, instead of individual options. That would allow React to only have to worry about one property instead of potentially many. So, to adjust my example above:

class Foo extends React.Component {
  constructor() {
    this.handleScroll = this.handleScroll.bind(this);
    this.handleScroll.options = { passive: false };
  }

  handleScroll() {
    ...
  }

  render() {
    return <div onScroll={this.handleScroll} />;
  }
}

Another thought that occurred to me is what might this look like if we modified JSX syntax in a way that allowed for these options to be passed in via the JSX. Here's a random example that I haven't put much thought into:

return <div onScroll={this.handleScroll, { passive: false }} />;

I've also been thinking about whether events should be passive by default or not, and I'm a bit on the fence. On one hand, this would certainly be nice for events like scroll handlers, but I worry that it would cause too much turbulence and unexpected behavior for many click handlers. We could make it so some events are passive by default and others are not, but that would probably just end up being confusing for folks, so probably not a good idea.

lencioni avatar Oct 17 '16 16:10 lencioni

This way is pretty similar to what I proposed earlier, without modifying JSX syntax.

return <div onScroll={{ handler: this.handleScroll, passive: true }} />;

And documentation would be straightforward:

div.propTypes = {
  ...
  onScroll: React.PropTypes.oneOf([
    React.PropTypes.func,
    React.PropTypes.shape({
      handler: React.PropTypes.func.isRequired,
      capture: React.PropTypes.bool,
      passive: React.PropTypes.bool,
      once: React.PropTypes.bool,
    }),
};

romulof avatar Oct 17 '16 20:10 romulof

Are react events passive by default? It seems to be that way for touch events, at least. I am not able to preventDefault unless I fall back to vanilla document-level event listeners.

joshjg avatar Oct 22 '16 17:10 joshjg

@joshjg React handlers are passed "synthetic events," which are sort of like native events, but different. By the way, someone with more knowledge should correct what I'm about to say because I haven't actually read the code that does this.

I'm not super familiar with the implementation details, but I know that preventDefault works at least as long as the handlers you're preventing are also React event handlers. That's been my experience, anyway.

With stopPropagation you're more likely to be out of luck (e.g. you have a document click listener which can't be bound with React, and you want to avoid bubbling up if you click inside a certain element). In that case you can use:

function stopPropagation (e) {
  e.stopPropagation();
  e.nativeEvent.stopImmediatePropagation();
}

[MDN]

This got slightly off the main topic, but the short answer is that React doesn't use passive events, they're just sometimes handled in a strange order.

benwiley4000 avatar Oct 27 '16 07:10 benwiley4000

@joshjg @benwiley4000 @gaearon Recently the chrome team has changed their approach to document-level touch events, making them passive by default. And since React attaches events at document-level, you get this new behaviour.

See https://www.chromestatus.com/features/5093566007214080

This has indirectly changed they way React behaves - I suppose React does not explicitly mention passive: false when attaching events - hence the change in behavior.

I just hit this as well - so you need to register touch events by hand, with addEventListener

radubrehar avatar Mar 20 '17 12:03 radubrehar

Note that the Chrome passive-by-default intervention only applies to touchstart and touchmove, not wheel. So a wheel event without an explicit {passive: true} will still force synchronous scrolling, for mousewheel and two-finger trackpad scrolling. (I wrote a blog post about some of the subtleties here.)

Also we (the Edge team) have no intent to implement the same intervention, so when we ship passive event listeners you'll still want to explicitly specify {passive: true}.

nolanlawson avatar Jun 29 '17 21:06 nolanlawson

FYI, I started going down the passive: false path to prevent body from scrolling on mobile when there is a scrolling div, but it's a little heavy to use preventDefault() to to block scrolling. I could add and remove the handler depending if the div is present, or fall back to a body.height = 100% approach. The body.height fix feels a little hacky, but then I wouldn't need passive: false at all.

mikeaustin avatar Aug 25 '17 22:08 mikeaustin