Skip to content

Commit

Permalink
fix(dialog): delay focus until animation is done (#3774)
Browse files Browse the repository at this point in the history
Delays focusing the first tabbable element in a dialog until animation is done. This prevents issues where other components (e.g. autocomplete) may have a wrong position on load, because they measured dialog while it was animating.

Note on lack of tests: This is kind of hard to test, because we don't have much control over how the animations runs.

Fixes #3722.
  • Loading branch information
crisbeto authored and tinayuangao committed Mar 29, 2017
1 parent cd0b853 commit d7d2b16
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/lib/dialog/dialog-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
throw new MdDialogContentAlreadyAttachedError();
}

let attachResult = this._portalHost.attachComponentPortal(portal);
this._trapFocus();
return attachResult;
return this._portalHost.attachComponentPortal(portal);
}

/**
Expand All @@ -105,9 +103,7 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
throw new MdDialogContentAlreadyAttachedError();
}

let attachedResult = this._portalHost.attachTemplatePortal(portal);
this._trapFocus();
return attachedResult;
return this._portalHost.attachTemplatePortal(portal);
}

/**
Expand Down Expand Up @@ -142,6 +138,10 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
* @docs-private
*/
_onAnimationDone(event: AnimationEvent) {
if (event.toState === 'enter') {
this._trapFocus();
}

this._onAnimationStateChange.emit(event.toState as MdDialogContainerAnimationState);
}

Expand All @@ -162,6 +162,8 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
this._onAnimationStateChange.complete();
});

this._focusTrap.destroy();
if (this._focusTrap) {
this._focusTrap.destroy();
}
}
}

0 comments on commit d7d2b16

Please sign in to comment.