Skip to content

Commit 992bcfb

Browse files
crisbetojosephperrott
authored andcommitted
refactor(platform): add utility to normalize passive event listener options (#13533)
1 parent c38ebb6 commit 992bcfb

File tree

6 files changed

+30
-16
lines changed

6 files changed

+30
-16
lines changed

src/cdk/a11y/focus-monitor/focus-monitor.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Platform, supportsPassiveEventListeners} from '@angular/cdk/platform';
9+
import {Platform, normalizePassiveListenerOptions} from '@angular/cdk/platform';
1010
import {
1111
Directive,
1212
ElementRef,
@@ -243,8 +243,10 @@ export class FocusMonitor implements OnDestroy {
243243

244244
// Event listener options that enable capturing and also mark the the listener as passive
245245
// if the browser supports it.
246-
const captureEventListenerOptions = supportsPassiveEventListeners() ?
247-
{passive: true, capture: true} : true;
246+
const captureEventListenerOptions = normalizePassiveListenerOptions({
247+
passive: true,
248+
capture: true
249+
});
248250

249251
// Note: we listen to events in the capture phase so we can detect them even if the user stops
250252
// propagation.

src/cdk/drag-drop/drag-drop-registry.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
import {Injectable, NgZone, OnDestroy, Inject} from '@angular/core';
1010
import {DOCUMENT} from '@angular/common';
11-
import {supportsPassiveEventListeners} from '@angular/cdk/platform';
11+
import {normalizePassiveListenerOptions} from '@angular/cdk/platform';
1212
import {Subject} from 'rxjs';
1313
import {toggleNativeDragInteractions} from './drag-styling';
1414

1515
/** Event options that can be used to bind an active event. */
16-
const activeEventOptions = supportsPassiveEventListeners() ? {passive: false} : false;
16+
const activeEventOptions = normalizePassiveListenerOptions({passive: false});
1717

1818
/** Handler for a pointer event callback. */
1919
type PointerEventHandler = (event: TouchEvent | MouseEvent) => void;

src/cdk/platform/features/passive-listeners.ts

+11
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,14 @@ export function supportsPassiveEventListeners(): boolean {
2626

2727
return supportsPassiveEvents;
2828
}
29+
30+
/**
31+
* Normalizes an `AddEventListener` object to something that can be passed
32+
* to `addEventListener` on any browser, no matter whether it supports the
33+
* `options` parameter.
34+
* @param options Object to be normalized.
35+
*/
36+
export function normalizePassiveListenerOptions(options: AddEventListenerOptions):
37+
AddEventListenerOptions | boolean {
38+
return supportsPassiveEventListeners() ? options : !!options.capture;
39+
}

src/cdk/text-field/autofill.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {supportsPassiveEventListeners} from '@angular/cdk/platform';
9+
import {normalizePassiveListenerOptions} from '@angular/cdk/platform';
1010
import {Component, ElementRef, NgZone, ViewChild} from '@angular/core';
1111
import {ComponentFixture, inject, TestBed} from '@angular/core/testing';
1212
import {EMPTY} from 'rxjs';
1313
import {AutofillEvent, AutofillMonitor} from './autofill';
1414
import {TextFieldModule} from './text-field-module';
1515

1616

17-
const listenerOptions: any = supportsPassiveEventListeners() ? {passive: true} : false;
17+
const listenerOptions = normalizePassiveListenerOptions({passive: true});
1818

1919

2020
describe('AutofillMonitor', () => {

src/cdk/text-field/autofill.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Platform, supportsPassiveEventListeners} from '@angular/cdk/platform';
9+
import {Platform, normalizePassiveListenerOptions} from '@angular/cdk/platform';
1010
import {
1111
Directive,
1212
ElementRef,
@@ -37,7 +37,7 @@ type MonitoredElementInfo = {
3737

3838

3939
/** Options to pass to the animationstart listener. */
40-
const listenerOptions: any = supportsPassiveEventListeners() ? {passive: true} : false;
40+
const listenerOptions = normalizePassiveListenerOptions({passive: true});
4141

4242

4343
/**

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

+8-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import {ElementRef, NgZone} from '@angular/core';
9-
import {Platform, supportsPassiveEventListeners} from '@angular/cdk/platform';
9+
import {Platform, normalizePassiveListenerOptions} from '@angular/cdk/platform';
1010
import {isFakeMousedownFromScreenReader} from '@angular/cdk/a11y';
1111
import {RippleRef, RippleState} from './ripple-ref';
1212

@@ -57,6 +57,9 @@ export const defaultRippleAnimationConfig = {
5757
*/
5858
const ignoreMouseEventsTimeout = 800;
5959

60+
/** Options that apply to all the event listeners that are bound by the ripple renderer. */
61+
const passiveEventOptions = normalizePassiveListenerOptions({passive: true});
62+
6063
/**
6164
* Helper service that performs DOM manipulations. Not intended to be used outside this module.
6265
* The constructor takes a reference to the ripple directive's host element and a map of DOM
@@ -86,9 +89,6 @@ export class RippleRenderer {
8689
/** Time in milliseconds when the last touchstart event happened. */
8790
private _lastTouchStartEvent: number;
8891

89-
/** Options that apply to all the event listeners that are bound by the renderer. */
90-
private _eventOptions = supportsPassiveEventListeners() ? ({passive: true} as any) : false;
91-
9292
/**
9393
* Cached dimensions of the ripple container. Set when the first
9494
* ripple is shown and cleared once no more ripples are visible.
@@ -235,8 +235,9 @@ export class RippleRenderer {
235235
this._removeTriggerEvents();
236236

237237
this._ngZone.runOutsideAngular(() => {
238-
this._triggerEvents.forEach((fn, type) =>
239-
element.addEventListener(type, fn, this._eventOptions));
238+
this._triggerEvents.forEach((fn, type) => {
239+
element.addEventListener(type, fn, passiveEventOptions);
240+
});
240241
});
241242

242243
this._triggerElement = element;
@@ -305,7 +306,7 @@ export class RippleRenderer {
305306
_removeTriggerEvents() {
306307
if (this._triggerElement) {
307308
this._triggerEvents.forEach((fn, type) => {
308-
this._triggerElement!.removeEventListener(type, fn, this._eventOptions);
309+
this._triggerElement!.removeEventListener(type, fn, passiveEventOptions);
309310
});
310311
}
311312
}

0 commit comments

Comments
 (0)