@@ -28,9 +28,9 @@ import {
28
28
import { AnimationEvent } from '@angular/animations' ;
29
29
import { TemplatePortal , CdkPortalOutlet , PortalHostDirective } from '@angular/cdk/portal' ;
30
30
import { Directionality , Direction } from '@angular/cdk/bidi' ;
31
- import { Subscription } from 'rxjs' ;
31
+ import { Subscription , Subject } from 'rxjs' ;
32
32
import { matTabsAnimations } from './tabs-animations' ;
33
- import { startWith } from 'rxjs/operators' ;
33
+ import { startWith , distinctUntilChanged } from 'rxjs/operators' ;
34
34
35
35
/**
36
36
* These position states are used internally as animation states for the tab body. Setting the
@@ -125,6 +125,9 @@ export class MatTabBody implements OnInit, OnDestroy {
125
125
/** Tab body position state. Used by the animation trigger for the current state. */
126
126
_position : MatTabBodyPositionState ;
127
127
128
+ /** Emits when an animation on the tab is complete. */
129
+ _translateTabComplete = new Subject < AnimationEvent > ( ) ;
130
+
128
131
/** Event emitted when the tab begins to animate towards the center as the active tab. */
129
132
@Output ( ) readonly _onCentering : EventEmitter < number > = new EventEmitter < number > ( ) ;
130
133
@@ -171,6 +174,21 @@ export class MatTabBody implements OnInit, OnDestroy {
171
174
changeDetectorRef . markForCheck ( ) ;
172
175
} ) ;
173
176
}
177
+
178
+ // Ensure that we get unique animation events, because the `.done` callback can get
179
+ // invoked twice in some browsers. See https://github.com/angular/angular/issues/24084.
180
+ this . _translateTabComplete . pipe ( distinctUntilChanged ( ( x , y ) => {
181
+ return x . fromState === y . fromState && x . toState === y . toState ;
182
+ } ) ) . subscribe ( event => {
183
+ // If the transition to the center is complete, emit an event.
184
+ if ( this . _isCenterPosition ( event . toState ) && this . _isCenterPosition ( this . _position ) ) {
185
+ this . _onCentered . emit ( ) ;
186
+ }
187
+
188
+ if ( this . _isCenterPosition ( event . fromState ) && ! this . _isCenterPosition ( this . _position ) ) {
189
+ this . _afterLeavingCenter . emit ( ) ;
190
+ }
191
+ } ) ;
174
192
}
175
193
176
194
/**
@@ -185,27 +203,17 @@ export class MatTabBody implements OnInit, OnDestroy {
185
203
186
204
ngOnDestroy ( ) {
187
205
this . _dirChangeSubscription . unsubscribe ( ) ;
206
+ this . _translateTabComplete . complete ( ) ;
188
207
}
189
208
190
- _onTranslateTabStarted ( e : AnimationEvent ) : void {
191
- const isCentering = this . _isCenterPosition ( e . toState ) ;
209
+ _onTranslateTabStarted ( event : AnimationEvent ) : void {
210
+ const isCentering = this . _isCenterPosition ( event . toState ) ;
192
211
this . _beforeCentering . emit ( isCentering ) ;
193
212
if ( isCentering ) {
194
213
this . _onCentering . emit ( this . _elementRef . nativeElement . clientHeight ) ;
195
214
}
196
215
}
197
216
198
- _onTranslateTabComplete ( e : AnimationEvent ) : void {
199
- // If the transition to the center is complete, emit an event.
200
- if ( this . _isCenterPosition ( e . toState ) && this . _isCenterPosition ( this . _position ) ) {
201
- this . _onCentered . emit ( ) ;
202
- }
203
-
204
- if ( this . _isCenterPosition ( e . fromState ) && ! this . _isCenterPosition ( this . _position ) ) {
205
- this . _afterLeavingCenter . emit ( ) ;
206
- }
207
- }
208
-
209
217
/** The text direction of the containing app. */
210
218
_getLayoutDirection ( ) : Direction {
211
219
return this . _dir && this . _dir . value === 'rtl' ? 'rtl' : 'ltr' ;
0 commit comments