@@ -92,7 +92,9 @@ export class OverlayRef implements PortalHost {
92
92
93
93
// Add class to fade-in the backdrop after one frame.
94
94
requestAnimationFrame ( ( ) => {
95
- this . _backdropElement . classList . add ( 'md-overlay-backdrop-showing' ) ;
95
+ if ( this . _backdropElement ) {
96
+ this . _backdropElement . classList . add ( 'md-overlay-backdrop-showing' ) ;
97
+ }
96
98
} ) ;
97
99
}
98
100
@@ -101,18 +103,28 @@ export class OverlayRef implements PortalHost {
101
103
let backdropToDetach = this . _backdropElement ;
102
104
103
105
if ( backdropToDetach ) {
104
- backdropToDetach . classList . remove ( 'md-overlay-backdrop-showing' ) ;
105
- backdropToDetach . classList . remove ( this . _state . backdropClass ) ;
106
- backdropToDetach . addEventListener ( 'transitionend' , ( ) => {
107
- backdropToDetach . parentNode . removeChild ( backdropToDetach ) ;
106
+ let onTransitionEnd = ( ) => {
107
+ // It may not be attached to anything in certain cases (e.g. unit tests).
108
+ if ( backdropToDetach && backdropToDetach . parentNode ) {
109
+ backdropToDetach . parentNode . removeChild ( backdropToDetach ) ;
110
+ }
108
111
109
112
// It is possible that a new portal has been attached to this overlay since we started
110
113
// removing the backdrop. If that is the case, only clear the backdrop reference if it
111
114
// is still the same instance that we started to remove.
112
115
if ( this . _backdropElement == backdropToDetach ) {
113
116
this . _backdropElement = null ;
114
117
}
115
- } ) ;
118
+ } ;
119
+
120
+ backdropToDetach . classList . remove ( 'md-overlay-backdrop-showing' ) ;
121
+ backdropToDetach . classList . remove ( this . _state . backdropClass ) ;
122
+ backdropToDetach . addEventListener ( 'transitionend' , onTransitionEnd ) ;
123
+
124
+ // If the backdrop doesn't have a transition, the `transitionend` event won't fire.
125
+ // In this case we make it unclickable and we try to remove it after a delay.
126
+ backdropToDetach . style . pointerEvents = 'none' ;
127
+ setTimeout ( onTransitionEnd , 500 ) ;
116
128
}
117
129
}
118
130
}
0 commit comments