@@ -56,18 +56,20 @@ export class CustomModalComponent implements OnInit {
5656 @Output ( ) onClose = new EventEmitter < void > ( ) ;
5757
5858 readonly visible$ = new BehaviorSubject ( false ) ;
59+ private canClose = true ;
5960
6061 @ViewChild ( 'content' , { static : false } )
6162 private readonly content ! : ElementRef ;
6263
63- private clickListener : ( ) => void = this . close . bind ( this ) ;
64+ private clickListener : ( ) => void = this . maybeClose . bind ( this ) ;
6465
6566 constructor ( private readonly viewRef : ViewContainerRef ) { }
6667
6768 ngOnInit ( ) {
6869 this . visible$ . subscribe ( ( visible ) => {
6970 // Wait until after the next browser frame.
7071 window . requestAnimationFrame ( ( ) => {
72+ this . canClose = true ;
7173 if ( visible ) {
7274 this . ensureContentIsWithinWindow ( ) ;
7375 this . onOpen . emit ( ) ;
@@ -87,6 +89,7 @@ export class CustomModalComponent implements OnInit {
8789
8890 this . content . nativeElement . style . left = position . x + 'px' ;
8991 this . content . nativeElement . style . top = position . y + 'px' ;
92+ this . canClose = false ;
9093 this . visible$ . next ( true ) ;
9194 document . addEventListener ( 'click' , this . clickListener ) ;
9295 }
@@ -115,6 +118,13 @@ export class CustomModalComponent implements OnInit {
115118 }
116119
117120 @HostListener ( 'document:keydown.escape' , [ '$event' ] )
121+ private maybeClose ( ) {
122+ if ( ! this . canClose ) {
123+ return ;
124+ }
125+ this . close ( ) ;
126+ }
127+
118128 public close ( ) {
119129 document . removeEventListener ( 'click' , this . clickListener ) ;
120130 this . visible$ . next ( false ) ;
0 commit comments