Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tabs): duplicate animation events on some browsers #13674

Merged
merged 1 commit into from
Nov 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/tabs/tab-body.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="mat-tab-body-content" #content
[@translateTab]="_position"
(@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 @@ -166,6 +169,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 @@ -180,27 +198,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 @@ -310,15 +310,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