@@ -27,6 +27,7 @@ import {
27
27
booleanAttribute ,
28
28
inject ,
29
29
} from '@angular/core' ;
30
+ import { _getEventTarget } from '@angular/cdk/platform' ;
30
31
import { Subscription } from 'rxjs' ;
31
32
import { takeWhile } from 'rxjs/operators' ;
32
33
import { Overlay } from './overlay' ;
@@ -311,7 +312,12 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
311
312
} ) ;
312
313
313
314
this . _overlayRef . outsidePointerEvents ( ) . subscribe ( ( event : MouseEvent ) => {
314
- this . overlayOutsideClick . next ( event ) ;
315
+ const origin = this . _getOriginElement ( ) ;
316
+ const target = _getEventTarget ( event ) as Element | null ;
317
+
318
+ if ( ! origin || ( origin !== target && ! origin . contains ( target ) ) ) {
319
+ this . overlayOutsideClick . next ( event ) ;
320
+ }
315
321
} ) ;
316
322
}
317
323
@@ -367,7 +373,7 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
367
373
} ) ) ;
368
374
369
375
return positionStrategy
370
- . setOrigin ( this . _getFlexibleConnectedPositionStrategyOrigin ( ) )
376
+ . setOrigin ( this . _getOrigin ( ) )
371
377
. withPositions ( positions )
372
378
. withFlexibleDimensions ( this . flexibleDimensions )
373
379
. withPush ( this . push )
@@ -379,21 +385,35 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
379
385
380
386
/** Returns the position strategy of the overlay to be set on the overlay config */
381
387
private _createPositionStrategy ( ) : FlexibleConnectedPositionStrategy {
382
- const strategy = this . _overlay
383
- . position ( )
384
- . flexibleConnectedTo ( this . _getFlexibleConnectedPositionStrategyOrigin ( ) ) ;
388
+ const strategy = this . _overlay . position ( ) . flexibleConnectedTo ( this . _getOrigin ( ) ) ;
385
389
this . _updatePositionStrategy ( strategy ) ;
386
390
return strategy ;
387
391
}
388
392
389
- private _getFlexibleConnectedPositionStrategyOrigin ( ) : FlexibleConnectedPositionStrategyOrigin {
393
+ private _getOrigin ( ) : FlexibleConnectedPositionStrategyOrigin {
390
394
if ( this . origin instanceof CdkOverlayOrigin ) {
391
395
return this . origin . elementRef ;
392
396
} else {
393
397
return this . origin ;
394
398
}
395
399
}
396
400
401
+ private _getOriginElement ( ) : Element | null {
402
+ if ( this . origin instanceof CdkOverlayOrigin ) {
403
+ return this . origin . elementRef . nativeElement ;
404
+ }
405
+
406
+ if ( this . origin instanceof ElementRef ) {
407
+ return this . origin . nativeElement ;
408
+ }
409
+
410
+ if ( typeof Element !== 'undefined' && this . origin instanceof Element ) {
411
+ return this . origin ;
412
+ }
413
+
414
+ return null ;
415
+ }
416
+
397
417
/** Attaches the overlay and subscribes to backdrop clicks if backdrop exists */
398
418
private _attachOverlay ( ) {
399
419
if ( ! this . _overlayRef ) {
0 commit comments