Skip to content

fix(experimental/dialog): emitting events twice on some browsers #13587

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
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
38 changes: 24 additions & 14 deletions src/cdk-experimental/dialog/dialog-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
ViewEncapsulation,
} from '@angular/core';
import {Subject} from 'rxjs';
import {distinctUntilChanged} from 'rxjs/operators';
import {DialogConfig} from './dialog-config';


Expand Down Expand Up @@ -61,7 +62,7 @@ export function throwDialogContentAlreadyAttachedError() {
host: {
'[@dialog]': '_state',
'(@dialog.start)': '_onAnimationStart($event)',
'(@dialog.done)': '_onAnimationDone($event)',
'(@dialog.done)': '_animationDone.next($event)',
},
})
export class CdkDialogContainer extends BasePortalOutlet implements OnDestroy {
Expand Down Expand Up @@ -103,6 +104,9 @@ export class CdkDialogContainer extends BasePortalOutlet implements OnDestroy {
/** A subject emitting after the dialog exits the view. */
_afterExit: Subject<void> = new Subject();

/** Stream of animation `done` events. */
_animationDone = new Subject<AnimationEvent>();

constructor(
private _elementRef: ElementRef<HTMLElement>,
private _focusTrapFactory: FocusTrapFactory,
Expand All @@ -111,11 +115,30 @@ export class CdkDialogContainer extends BasePortalOutlet implements OnDestroy {
/** The dialog configuration. */
public _config: DialogConfig) {
super();

// We use a Subject with a distinctUntilChanged, rather than a callback attached to .done,
// because some browsers fire the done event twice and we don't want to emit duplicate events.
// See: https://github.com/angular/angular/issues/24084
this._animationDone.pipe(distinctUntilChanged((x, y) => {
return x.fromState === y.fromState && x.toState === y.toState;
})).subscribe(event => {
// Emit lifecycle events based on animation `done` callback.
if (event.toState === 'enter') {
this._autoFocusFirstTabbableElement();
this._afterEnter.next();
}

if (event.fromState === 'enter' && (event.toState === 'void' || event.toState === 'exit')) {
this._returnFocusAfterDialog();
this._afterExit.next();
}
});
}

/** Destroy focus trap to place focus back to the element focused before the dialog opened. */
ngOnDestroy() {
this._focusTrap.destroy();
this._animationDone.complete();
}

/**
Expand Down Expand Up @@ -154,19 +177,6 @@ export class CdkDialogContainer extends BasePortalOutlet implements OnDestroy {
}
}

/** Emit lifecycle events based on animation `done` callback. */
_onAnimationDone(event: AnimationEvent) {
if (event.toState === 'enter') {
this._autoFocusFirstTabbableElement();
this._afterEnter.next();
}

if (event.fromState === 'enter' && (event.toState === 'void' || event.toState === 'exit')) {
this._returnFocusAfterDialog();
this._afterExit.next();
}
}

/** Starts the dialog exit animation. */
_startExiting(): void {
this._state = 'exit';
Expand Down