diff --git a/src/cdk/a11y/focus-monitor/focus-monitor.ts b/src/cdk/a11y/focus-monitor/focus-monitor.ts index 2dedfebed41..612ec9d144e 100644 --- a/src/cdk/a11y/focus-monitor/focus-monitor.ts +++ b/src/cdk/a11y/focus-monitor/focus-monitor.ts @@ -241,21 +241,28 @@ export class FocusMonitor implements OnDestroy { this._windowFocusTimeoutId = setTimeout(() => this._windowFocused = false); }; + // Event listener options that enable capturing and also mark the the listener as passive + // if the browser supports it. + const captureEventListenerOptions = supportsPassiveEventListeners() ? + {passive: true, capture: true} : true; + // Note: we listen to events in the capture phase so we can detect them even if the user stops // propagation. this._ngZone.runOutsideAngular(() => { - document.addEventListener('keydown', documentKeydownListener, true); - document.addEventListener('mousedown', documentMousedownListener, true); + document.addEventListener('keydown', documentKeydownListener, captureEventListenerOptions); + document.addEventListener('mousedown', documentMousedownListener, + captureEventListenerOptions); document.addEventListener('touchstart', documentTouchstartListener, - supportsPassiveEventListeners() ? ({passive: true, capture: true} as any) : true); + captureEventListenerOptions); window.addEventListener('focus', windowFocusListener); }); this._unregisterGlobalListeners = () => { - document.removeEventListener('keydown', documentKeydownListener, true); - document.removeEventListener('mousedown', documentMousedownListener, true); + document.removeEventListener('keydown', documentKeydownListener, captureEventListenerOptions); + document.removeEventListener('mousedown', documentMousedownListener, + captureEventListenerOptions); document.removeEventListener('touchstart', documentTouchstartListener, - supportsPassiveEventListeners() ? ({passive: true, capture: true} as any) : true); + captureEventListenerOptions); window.removeEventListener('focus', windowFocusListener); // Clear timeouts for all potentially pending timeouts to prevent the leaks.