Skip to content

Commit

Permalink
Revert "Revert "fix(tabs): duplicate animation events on some browsers (
Browse files Browse the repository at this point in the history
angular#13674)" (angular#14015)"

This reverts commit 11c96d6.

Turns out the original change wasn't actually the problem. It was angular#13888
instead.
  • Loading branch information
jelbourn committed Nov 7, 2018
1 parent 11c96d6 commit f7d3023
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/lib/tabs/tab-body.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
params: {animationDuration: animationDuration}
}"
(@translateTab.start)="_onTranslateTabStarted($event)"
(@translateTab.done)="_onTranslateTabComplete($event)">
(@translateTab.done)="_translateTabComplete.next($event)">
<ng-template matTabBodyHost></ng-template>
</div>
38 changes: 23 additions & 15 deletions src/lib/tabs/tab-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ import {
import {AnimationEvent} from '@angular/animations';
import {TemplatePortal, CdkPortalOutlet, PortalHostDirective} from '@angular/cdk/portal';
import {Directionality, Direction} from '@angular/cdk/bidi';
import {Subscription} from 'rxjs';
import {Subscription, Subject} from 'rxjs';
import {matTabsAnimations} from './tabs-animations';
import {startWith} from 'rxjs/operators';
import {startWith, distinctUntilChanged} from 'rxjs/operators';

/**
* These position states are used internally as animation states for the tab body. Setting the
Expand Down Expand Up @@ -125,6 +125,9 @@ export class MatTabBody implements OnInit, OnDestroy {
/** Tab body position state. Used by the animation trigger for the current state. */
_position: MatTabBodyPositionState;

/** Emits when an animation on the tab is complete. */
_translateTabComplete = new Subject<AnimationEvent>();

/** Event emitted when the tab begins to animate towards the center as the active tab. */
@Output() readonly _onCentering: EventEmitter<number> = new EventEmitter<number>();

Expand Down Expand Up @@ -171,6 +174,21 @@ export class MatTabBody implements OnInit, OnDestroy {
changeDetectorRef.markForCheck();
});
}

// Ensure that we get unique animation events, because the `.done` callback can get
// invoked twice in some browsers. See https://github.com/angular/angular/issues/24084.
this._translateTabComplete.pipe(distinctUntilChanged((x, y) => {
return x.fromState === y.fromState && x.toState === y.toState;
})).subscribe(event => {
// If the transition to the center is complete, emit an event.
if (this._isCenterPosition(event.toState) && this._isCenterPosition(this._position)) {
this._onCentered.emit();
}

if (this._isCenterPosition(event.fromState) && !this._isCenterPosition(this._position)) {
this._afterLeavingCenter.emit();
}
});
}

/**
Expand All @@ -185,27 +203,17 @@ export class MatTabBody implements OnInit, OnDestroy {

ngOnDestroy() {
this._dirChangeSubscription.unsubscribe();
this._translateTabComplete.complete();
}

_onTranslateTabStarted(e: AnimationEvent): void {
const isCentering = this._isCenterPosition(e.toState);
_onTranslateTabStarted(event: AnimationEvent): void {
const isCentering = this._isCenterPosition(event.toState);
this._beforeCentering.emit(isCentering);
if (isCentering) {
this._onCentering.emit(this._elementRef.nativeElement.clientHeight);
}
}

_onTranslateTabComplete(e: AnimationEvent): void {
// If the transition to the center is complete, emit an event.
if (this._isCenterPosition(e.toState) && this._isCenterPosition(this._position)) {
this._onCentered.emit();
}

if (this._isCenterPosition(e.fromState) && !this._isCenterPosition(this._position)) {
this._afterLeavingCenter.emit();
}
}

/** The text direction of the containing app. */
_getLayoutDirection(): Direction {
return this._dir && this._dir.value === 'rtl' ? 'rtl' : 'ltr';
Expand Down
2 changes: 1 addition & 1 deletion src/lib/tabs/tab-group.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ describe('MatTabGroup', () => {
fixture.detectChanges();
tick();

expect(fixture.componentInstance.animationDone).toHaveBeenCalled();
expect(fixture.componentInstance.animationDone).toHaveBeenCalledTimes(1);
}));

it('should add the proper `aria-setsize` and `aria-posinset`', () => {
Expand Down
9 changes: 5 additions & 4 deletions src/lib/tabs/tab-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,15 +328,16 @@ export class MatTabGroup extends _MatTabGroupMixinBase implements AfterContentIn

/** Removes the height of the tab body wrapper. */
_removeTabBodyWrapperHeight(): void {
this._tabBodyWrapperHeight = this._tabBodyWrapper.nativeElement.clientHeight;
this._tabBodyWrapper.nativeElement.style.height = '';
const wrapper = this._tabBodyWrapper.nativeElement;
this._tabBodyWrapperHeight = wrapper.clientHeight;
wrapper.style.height = '';
this.animationDone.emit();
}

/** Handle click events, setting new selected index if appropriate. */
_handleClick(tab: MatTab, tabHeader: MatTabHeader, idx: number) {
_handleClick(tab: MatTab, tabHeader: MatTabHeader, index: number) {
if (!tab.disabled) {
this.selectedIndex = tabHeader.focusIndex = idx;
this.selectedIndex = tabHeader.focusIndex = index;
}
}

Expand Down

0 comments on commit f7d3023

Please sign in to comment.