Skip to content

Commit b450c54

Browse files
committed
refactor(material/sidenav): Remove use of focusInitialElementWhenReady
1 parent d5aa679 commit b450c54

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/material/sidenav/drawer.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ export class MatDrawer implements AfterViewInit, AfterContentChecked, OnDestroy
321321
*/
322322
readonly _modeChanged = new Subject<void>();
323323

324+
private _injector = inject(Injector);
325+
324326
constructor(
325327
private _elementRef: ElementRef<HTMLElement>,
326328
private _focusTrapFactory: FocusTrapFactory,
@@ -336,7 +338,6 @@ export class MatDrawer implements AfterViewInit, AfterContentChecked, OnDestroy
336338
if (this._doc) {
337339
this._elementFocusedBeforeDrawerWasOpened = this._doc.activeElement as HTMLElement;
338340
}
339-
340341
this._takeFocus();
341342
} else if (this._isFocusWithinDrawer()) {
342343
this._restoreFocus(this._openedVia || 'program');
@@ -441,11 +442,15 @@ export class MatDrawer implements AfterViewInit, AfterContentChecked, OnDestroy
441442
return;
442443
case true:
443444
case 'first-tabbable':
444-
this._focusTrap.focusInitialElementWhenReady().then(hasMovedFocus => {
445-
if (!hasMovedFocus && typeof this._elementRef.nativeElement.focus === 'function') {
446-
element.focus();
447-
}
448-
});
445+
afterNextRender(
446+
() => {
447+
const hasMovedFocus = this._focusTrap!.focusInitialElement();
448+
if (!hasMovedFocus && typeof element.focus === 'function') {
449+
element.focus();
450+
}
451+
},
452+
{injector: this._injector},
453+
);
449454
break;
450455
case 'first-heading':
451456
this._focusByCssSelector('h1, h2, h3, h4, h5, h6, [role="heading"]');

0 commit comments

Comments
 (0)