Skip to content

Commit d7d2b16

Browse files
crisbetotinayuangao
authored andcommitted
fix(dialog): delay focus until animation is done (#3774)
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.
1 parent cd0b853 commit d7d2b16

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/lib/dialog/dialog-container.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
9191
throw new MdDialogContentAlreadyAttachedError();
9292
}
9393

94-
let attachResult = this._portalHost.attachComponentPortal(portal);
95-
this._trapFocus();
96-
return attachResult;
94+
return this._portalHost.attachComponentPortal(portal);
9795
}
9896

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

108-
let attachedResult = this._portalHost.attachTemplatePortal(portal);
109-
this._trapFocus();
110-
return attachedResult;
106+
return this._portalHost.attachTemplatePortal(portal);
111107
}
112108

113109
/**
@@ -142,6 +138,10 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
142138
* @docs-private
143139
*/
144140
_onAnimationDone(event: AnimationEvent) {
141+
if (event.toState === 'enter') {
142+
this._trapFocus();
143+
}
144+
145145
this._onAnimationStateChange.emit(event.toState as MdDialogContainerAnimationState);
146146
}
147147

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

165-
this._focusTrap.destroy();
165+
if (this._focusTrap) {
166+
this._focusTrap.destroy();
167+
}
166168
}
167169
}

0 commit comments

Comments
 (0)