Skip to content

Commit 988223e

Browse files
committed
perf(ripple): use passive event listeners
Switches all of the ripple-related event listeners to be passive for improved performance and to prevent Chrome from logging warnings for almost every Material component.
1 parent d6fec35 commit 988223e

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/lib/core/ripple/ripple-renderer.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
98
import {ElementRef, NgZone} from '@angular/core';
10-
import {Platform} from '@angular/cdk/platform';
9+
import {Platform, supportsPassiveEventListeners} from '@angular/cdk/platform';
1110
import {RippleRef, RippleState} from './ripple-ref';
1211

1312

@@ -70,12 +69,12 @@ export class RippleRenderer {
7069
this._containerElement = elementRef.nativeElement;
7170

7271
// Specify events which need to be registered on the trigger.
73-
this._triggerEvents.set('mousedown', this.onMousedown.bind(this));
74-
this._triggerEvents.set('mouseup', this.onPointerUp.bind(this));
75-
this._triggerEvents.set('mouseleave', this.onPointerUp.bind(this));
72+
this._triggerEvents.set('mousedown', this.onMousedown);
73+
this._triggerEvents.set('mouseup', this.onPointerUp);
74+
this._triggerEvents.set('mouseleave', this.onPointerUp);
7675

77-
this._triggerEvents.set('touchstart', this.onTouchStart.bind(this));
78-
this._triggerEvents.set('touchend', this.onPointerUp.bind(this));
76+
this._triggerEvents.set('touchstart', this.onTouchStart);
77+
this._triggerEvents.set('touchend', this.onPointerUp);
7978

8079
// By default use the host element as trigger element.
8180
this.setTriggerElement(this._containerElement);
@@ -170,25 +169,27 @@ export class RippleRenderer {
170169

171170
/** Sets the trigger element and registers the mouse events. */
172171
setTriggerElement(element: HTMLElement | null) {
172+
const eventOptions = supportsPassiveEventListeners() ? ({passive: true} as any) : false;
173+
173174
// Remove all previously register event listeners from the trigger element.
174175
if (this._triggerElement) {
175176
this._triggerEvents.forEach((fn, type) => {
176-
this._triggerElement!.removeEventListener(type, fn);
177+
this._triggerElement!.removeEventListener(type, fn, eventOptions);
177178
});
178179
}
179180

180181
if (element) {
181182
// If the element is not null, register all event listeners on the trigger element.
182183
this._ngZone.runOutsideAngular(() => {
183-
this._triggerEvents.forEach((fn, type) => element.addEventListener(type, fn));
184+
this._triggerEvents.forEach((fn, type) => element.addEventListener(type, fn, eventOptions));
184185
});
185186
}
186187

187188
this._triggerElement = element;
188189
}
189190

190191
/** Function being called whenever the trigger is being pressed using mouse. */
191-
private onMousedown(event: MouseEvent) {
192+
private onMousedown = (event: MouseEvent) => {
192193
const isSyntheticEvent = this._lastTouchStartEvent &&
193194
Date.now() < this._lastTouchStartEvent + IGNORE_MOUSE_EVENTS_TIMEOUT;
194195

@@ -199,7 +200,7 @@ export class RippleRenderer {
199200
}
200201

201202
/** Function being called whenever the trigger is being pressed using touch. */
202-
private onTouchStart(event: TouchEvent) {
203+
private onTouchStart = (event: TouchEvent) => {
203204
if (!this.rippleDisabled) {
204205
// Some browsers fire mouse events after a `touchstart` event. Those synthetic mouse
205206
// events will launch a second ripple if we don't ignore mouse events for a specific
@@ -212,7 +213,7 @@ export class RippleRenderer {
212213
}
213214

214215
/** Function being called whenever the trigger is being released. */
215-
private onPointerUp() {
216+
private onPointerUp = () => {
216217
if (!this._isPointerDown) {
217218
return;
218219
}

0 commit comments

Comments
 (0)