Skip to content

fix(sidenav): opened and closed events emitting twice on IE and Edge #13649

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

Merged
merged 1 commit into from
Oct 19, 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
48 changes: 31 additions & 17 deletions src/lib/sidenav/drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ import {
ViewEncapsulation,
} from '@angular/core';
import {fromEvent, merge, Observable, Subject} from 'rxjs';
import {debounceTime, filter, map, startWith, take, takeUntil} from 'rxjs/operators';
import {
debounceTime,
filter,
map,
startWith,
take,
takeUntil,
distinctUntilChanged,
} from 'rxjs/operators';
import {matDrawerAnimations} from './drawer-animations';
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';

Expand Down Expand Up @@ -108,8 +116,8 @@ export class MatDrawerContent extends CdkScrollable implements AfterContentInit
host: {
'class': 'mat-drawer',
'[@transform]': '_animationState',
'(@transform.start)': '_onAnimationStart($event)',
'(@transform.done)': '_onAnimationEnd($event)',
'(@transform.start)': '_animationStarted.next($event)',
'(@transform.done)': '_animationEnd.next($event)',
// must prevent the browser from aligning text based on value
'[attr.align]': 'null',
'[class.mat-drawer-end]': 'position === "end"',
Expand Down Expand Up @@ -166,7 +174,10 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
private _openedVia: FocusOrigin | null;

/** Emits whenever the drawer has started animating. */
_animationStarted = new EventEmitter<AnimationEvent>();
_animationStarted = new Subject<AnimationEvent>();

/** Emits whenever the drawer is done animating. */
_animationEnd = new Subject<AnimationEvent>();

/** Current state of the sidenav animation. */
_animationState: 'open-instant' | 'open' | 'void' = 'void';
Expand Down Expand Up @@ -255,6 +266,19 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
event.stopPropagation();
}));
});

// We need a Subject with distinctUntilChanged, because the `done` event
// fires twice on some browsers. See https://github.com/angular/angular/issues/24084
this._animationEnd.pipe(distinctUntilChanged((x, y) => {
return x.fromState === y.fromState && x.toState === y.toState;
})).subscribe((event: AnimationEvent) => {
const {fromState, toState} = event;

if ((toState.indexOf('open') === 0 && fromState === 'void') ||
(toState === 'void' && fromState.indexOf('open') === 0)) {
this.openedChange.emit(this._opened);
}
});
}

/** Traps focus inside the drawer. */
Expand Down Expand Up @@ -314,6 +338,9 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
if (this._focusTrap) {
this._focusTrap.destroy();
}

this._animationStarted.complete();
this._animationEnd.complete();
}

/**
Expand Down Expand Up @@ -367,19 +394,6 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
});
}

_onAnimationStart(event: AnimationEvent) {
this._animationStarted.emit(event);
}

_onAnimationEnd(event: AnimationEvent) {
const {fromState, toState} = event;

if ((toState.indexOf('open') === 0 && fromState === 'void') ||
(toState === 'void' && fromState.indexOf('open') === 0)) {
this.openedChange.emit(this._opened);
}
}

get _width(): number {
return this._elementRef.nativeElement ? (this._elementRef.nativeElement.offsetWidth || 0) : 0;
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/sidenav/sidenav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ export class MatSidenavContent extends MatDrawerContent {
'class': 'mat-drawer mat-sidenav',
'tabIndex': '-1',
'[@transform]': '_animationState',
'(@transform.start)': '_onAnimationStart($event)',
'(@transform.done)': '_onAnimationEnd($event)',
'(@transform.start)': '_animationStarted.next($event)',
'(@transform.done)': '_animationEnd.next($event)',
// must prevent the browser from aligning text based on value
'[attr.align]': 'null',
'[class.mat-drawer-end]': 'position === "end"',
Expand Down