11import {
22 ElementRef ,
3+ NgZone ,
34} from '@angular/core' ;
45
56/** TODO: internal */
@@ -44,7 +45,9 @@ export class RippleRenderer {
4445 private _triggerElement : HTMLElement ;
4546 _opacity : string ;
4647
47- constructor ( _elementRef : ElementRef , private _eventHandlers : Map < string , ( e : Event ) => void > ) {
48+ constructor ( _elementRef : ElementRef ,
49+ private _eventHandlers : Map < string , ( e : Event ) => void > ,
50+ private _ngZone : NgZone ) {
4851 this . _rippleElement = _elementRef . nativeElement ;
4952 // The background div is created in createBackgroundIfNeeded when the ripple becomes enabled.
5053 // This avoids creating unneeded divs when the ripple is always disabled.
@@ -143,6 +146,16 @@ export class RippleRenderer {
143146
144147 rippleDiv . addEventListener ( 'transitionend' ,
145148 ( event : TransitionEvent ) => transitionEndCallback ( ripple , event ) ) ;
149+ // Ensure that ripples are always removed, even when transitionend doesn't fire.
150+ // Run this outside the Angular zone because there's nothing that Angular cares about.
151+ // If it were to run inside the Angular zone, every test that used ripples would have to be
152+ // either async or fakeAsync.
153+ this . _ngZone . runOutsideAngular ( ( ) => {
154+ // The ripple lasts a time equal to the sum of fade-in, transform,
155+ // and fade-out (3 * fade-in time).
156+ let rippleDuration = fadeInSeconds * 3 * 1000 ;
157+ setTimeout ( ( ) => this . removeRippleFromDom ( ripple . rippleElement ) , rippleDuration ) ;
158+ } ) ;
146159 }
147160
148161 /** Fades out a foreground ripple after it has fully expanded and faded in. */
@@ -153,7 +166,9 @@ export class RippleRenderer {
153166
154167 /** Removes a foreground ripple from the DOM after it has faded out. */
155168 removeRippleFromDom ( ripple : Element ) {
156- ripple . parentElement . removeChild ( ripple ) ;
169+ if ( ripple && ripple . parentElement ) {
170+ ripple . parentElement . removeChild ( ripple ) ;
171+ }
157172 }
158173
159174 /** Fades in the ripple background. */
0 commit comments