@@ -268,10 +268,6 @@ export class MatTooltip implements OnDestroy, AfterViewInit {
268268 this . touchGestures = _defaultOptions . touchGestures ;
269269 }
270270 }
271-
272- _ngZone . runOutsideAngular ( ( ) => {
273- _elementRef . nativeElement . addEventListener ( 'keydown' , this . _handleKeydown ) ;
274- } ) ;
275271 }
276272
277273 ngAfterViewInit ( ) {
@@ -304,7 +300,6 @@ export class MatTooltip implements OnDestroy, AfterViewInit {
304300 }
305301
306302 // Clean up the event listeners set in the constructor
307- nativeElement . removeEventListener ( 'keydown' , this . _handleKeydown ) ;
308303 this . _passiveListeners . forEach ( ( listener , event ) => {
309304 nativeElement . removeEventListener ( event , listener , passiveListenerOptions ) ;
310305 } ) ;
@@ -353,18 +348,6 @@ export class MatTooltip implements OnDestroy, AfterViewInit {
353348 return ! ! this . _tooltipInstance && this . _tooltipInstance . isVisible ( ) ;
354349 }
355350
356- /**
357- * Handles the keydown events on the host element.
358- * Needs to be an arrow function so that we can use it in addEventListener.
359- */
360- private _handleKeydown = ( event : KeyboardEvent ) => {
361- if ( this . _isTooltipVisible ( ) && event . keyCode === ESCAPE && ! hasModifierKey ( event ) ) {
362- event . preventDefault ( ) ;
363- event . stopPropagation ( ) ;
364- this . _ngZone . run ( ( ) => this . hide ( 0 ) ) ;
365- }
366- }
367-
368351 /** Create the overlay config and position strategy */
369352 private _createOverlay ( ) : OverlayRef {
370353 if ( this . _overlayRef ) {
@@ -392,7 +375,7 @@ export class MatTooltip implements OnDestroy, AfterViewInit {
392375 }
393376 } ) ;
394377
395- this . _overlayRef = this . _overlay . create ( {
378+ const overlayRef = this . _overlayRef = this . _overlay . create ( {
396379 direction : this . _dir ,
397380 positionStrategy : strategy ,
398381 panelClass : TOOLTIP_PANEL_CLASS ,
@@ -401,11 +384,21 @@ export class MatTooltip implements OnDestroy, AfterViewInit {
401384
402385 this . _updatePosition ( ) ;
403386
404- this . _overlayRef . detachments ( )
387+ overlayRef . keydownEvents ( )
388+ . pipe ( takeUntil ( this . _destroyed ) )
389+ . subscribe ( event => {
390+ if ( this . _isTooltipVisible ( ) && event . keyCode === ESCAPE && ! hasModifierKey ( event ) ) {
391+ event . preventDefault ( ) ;
392+ event . stopPropagation ( ) ;
393+ this . _ngZone . run ( ( ) => this . hide ( 0 ) ) ;
394+ }
395+ } ) ;
396+
397+ overlayRef . detachments ( )
405398 . pipe ( takeUntil ( this . _destroyed ) )
406399 . subscribe ( ( ) => this . _detach ( ) ) ;
407400
408- return this . _overlayRef ;
401+ return overlayRef ;
409402 }
410403
411404 /** Detaches the currently-attached tooltip. */
0 commit comments