Skip to content

Commit 3a94415

Browse files
authored
fix(material-experimental/mdc-dialog): implement delayFocusTrap option (#24727)
When I added the `delayFocusTrap` option in #24121, I assumed that it would be inherited into the MDC dialog. Since it looks like that's not actually the case, these changes implement it.
1 parent 766a01e commit 3a94415

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

src/material-experimental/mdc-dialog/dialog-container.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,7 @@ export class MatDialogContainer extends _MatDialogContainerBase implements OnDes
168168
*/
169169
private _finishDialogOpen = () => {
170170
this._clearAnimationClasses();
171-
this._trapFocus();
172-
this._animationStateChanged.emit({state: 'opened', totalTime: this._openAnimationDuration});
171+
this._openAnimationDone(this._openAnimationDuration);
173172
};
174173

175174
/**

src/material/dialog/dialog-container.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ export abstract class _MatDialogContainerBase extends BasePortalOutlet {
117117
if (this._document) {
118118
this._elementFocusedBeforeDialogWasOpened = _getFocusedElementPierceShadowDom();
119119
}
120+
121+
if (!this._config.delayFocusTrap) {
122+
this._trapFocus();
123+
}
120124
}
121125

122126
/**
@@ -293,6 +297,18 @@ export abstract class _MatDialogContainerBase extends BasePortalOutlet {
293297
const activeElement = _getFocusedElementPierceShadowDom();
294298
return element === activeElement || element.contains(activeElement);
295299
}
300+
301+
/**
302+
* Callback for when the open dialog animation has finished. Intended to
303+
* be called by sub-classes that use different animation implementations.
304+
*/
305+
protected _openAnimationDone(totalTime: number) {
306+
if (this._config.delayFocusTrap) {
307+
this._trapFocus();
308+
}
309+
310+
this._animationStateChanged.next({state: 'opened', totalTime});
311+
}
296312
}
297313

298314
/**
@@ -330,11 +346,7 @@ export class MatDialogContainer extends _MatDialogContainerBase {
330346
/** Callback, invoked whenever an animation on the host completes. */
331347
_onAnimationDone({toState, totalTime}: AnimationEvent) {
332348
if (toState === 'enter') {
333-
if (this._config.delayFocusTrap) {
334-
this._trapFocus();
335-
}
336-
337-
this._animationStateChanged.next({state: 'opened', totalTime});
349+
this._openAnimationDone(totalTime);
338350
} else if (toState === 'exit') {
339351
this._restoreFocus();
340352
this._animationStateChanged.next({state: 'closed', totalTime});
@@ -359,14 +371,6 @@ export class MatDialogContainer extends _MatDialogContainerBase {
359371
this._changeDetectorRef.markForCheck();
360372
}
361373

362-
override _initializeWithAttachedContent() {
363-
super._initializeWithAttachedContent();
364-
365-
if (!this._config.delayFocusTrap) {
366-
this._trapFocus();
367-
}
368-
}
369-
370374
_getAnimationState() {
371375
return {
372376
value: this._state,

tools/public_api_guard/material/dialog.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,6 @@ export class MatDialogContainer extends _MatDialogContainerBase {
197197
exitAnimationDuration: string;
198198
};
199199
};
200-
// (undocumented)
201-
_initializeWithAttachedContent(): void;
202200
_onAnimationDone({ toState, totalTime }: AnimationEvent_2): void;
203201
_onAnimationStart({ toState, totalTime }: AnimationEvent_2): void;
204202
_startExitAnimation(): void;
@@ -231,6 +229,7 @@ export abstract class _MatDialogContainerBase extends BasePortalOutlet {
231229
protected _focusTrapFactory: FocusTrapFactory;
232230
_id: string;
233231
_initializeWithAttachedContent(): void;
232+
protected _openAnimationDone(totalTime: number): void;
234233
_portalOutlet: CdkPortalOutlet;
235234
_recaptureFocus(): void;
236235
protected _restoreFocus(): void;

0 commit comments

Comments
 (0)