Skip to content

Commit b14d2d9

Browse files
devversionjosephperrott
authored andcommitted
perf(focus-monitor): mark event listeners as passive (#13532)
1 parent f3b7798 commit b14d2d9

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,21 +241,28 @@ export class FocusMonitor implements OnDestroy {
241241
this._windowFocusTimeoutId = setTimeout(() => this._windowFocused = false);
242242
};
243243

244+
// Event listener options that enable capturing and also mark the the listener as passive
245+
// if the browser supports it.
246+
const captureEventListenerOptions = supportsPassiveEventListeners() ?
247+
{passive: true, capture: true} : true;
248+
244249
// Note: we listen to events in the capture phase so we can detect them even if the user stops
245250
// propagation.
246251
this._ngZone.runOutsideAngular(() => {
247-
document.addEventListener('keydown', documentKeydownListener, true);
248-
document.addEventListener('mousedown', documentMousedownListener, true);
252+
document.addEventListener('keydown', documentKeydownListener, captureEventListenerOptions);
253+
document.addEventListener('mousedown', documentMousedownListener,
254+
captureEventListenerOptions);
249255
document.addEventListener('touchstart', documentTouchstartListener,
250-
supportsPassiveEventListeners() ? ({passive: true, capture: true} as any) : true);
256+
captureEventListenerOptions);
251257
window.addEventListener('focus', windowFocusListener);
252258
});
253259

254260
this._unregisterGlobalListeners = () => {
255-
document.removeEventListener('keydown', documentKeydownListener, true);
256-
document.removeEventListener('mousedown', documentMousedownListener, true);
261+
document.removeEventListener('keydown', documentKeydownListener, captureEventListenerOptions);
262+
document.removeEventListener('mousedown', documentMousedownListener,
263+
captureEventListenerOptions);
257264
document.removeEventListener('touchstart', documentTouchstartListener,
258-
supportsPassiveEventListeners() ? ({passive: true, capture: true} as any) : true);
265+
captureEventListenerOptions);
259266
window.removeEventListener('focus', windowFocusListener);
260267

261268
// Clear timeouts for all potentially pending timeouts to prevent the leaks.

0 commit comments

Comments
 (0)