perfect-scrollbar icon indicating copy to clipboard operation
perfect-scrollbar copied to clipboard

Add {passive: true} to addEventListener

Open nicraMarcin opened this issue 4 years ago • 12 comments

Using perfect-scrollbar in chrome produces a lot of Violation issues

zone-evergreen.js:1773 [Violation] Added non-passive event listener to a scroll-blocking event. Consider marking event handler as 'passive' to make the page more responsive. See https://www.chromestatus.com/feature/5745543795965952

adding {passive: true} should resolve this issue

bind(eventName, handler) {
    if (typeof this.handlers[eventName] === 'undefined') {
      this.handlers[eventName] = [];
    }
    this.handlers[eventName].push(handler);
    this.element.addEventListener(eventName, handler, {passive: true});
  }

  unbind(eventName, target) {
    this.handlers[eventName] = this.handlers[eventName].filter(handler => {
      if (target && handler !== target) {
        return true;
      }
      this.element.removeEventListener(eventName, handler, {passive: true});
      return false;
    });
  }

nicraMarcin avatar Oct 01 '20 07:10 nicraMarcin

Agree with this solution, +1

redrbrt avatar Oct 07 '20 19:10 redrbrt

Also agree.

NonieBenks avatar Oct 15 '20 17:10 NonieBenks

I have the same issue in Chrome latest, breaking my mobile version (scroll doesn't work at all), but I see it's being worked on. Thanks a lot!

matiyin avatar Nov 05 '20 18:11 matiyin

@matiyin I also noticed the performance penalty on mobile. But for mobile, there should be no need for polyfil like this.. instead I use a conditional component when detect it is touchable device.

https://gist.github.com/f716316b81d50c84cc12fd35f0278342

pwang2 avatar Nov 09 '20 03:11 pwang2

This has already been PR-ed: https://github.com/mdbootstrap/perfect-scrollbar/pull/945

Apart from your proposed fix, it also uses feature detection, so it continues to work as intended on older browser versions, without support for the new syntax. Details here.

andrei-gheorghiu avatar Nov 22 '20 16:11 andrei-gheorghiu

Also of note is that https://github.com/mdbootstrap/perfect-scrollbar/issues/873 had been open for a while asking for an option to set the listeners' passive value.

Splaktar avatar Dec 13 '20 04:12 Splaktar

According to https://caniuse.com/mdn-api_eventtarget_addeventlistener_options support for the options/boolean is pretty widespread - implementing the options feature detection doesn't seem too important?

hmoffatt avatar Apr 20 '21 02:04 hmoffatt

You can add via CSS

touch-action: none;

to your scrolling container. That helped me to get rid of the error

sinasita avatar Sep 14 '21 12:09 sinasita