@@ -321,6 +321,8 @@ export class MatDrawer implements AfterViewInit, AfterContentChecked, OnDestroy
321
321
*/
322
322
readonly _modeChanged = new Subject < void > ( ) ;
323
323
324
+ private _injector = inject ( Injector ) ;
325
+
324
326
constructor (
325
327
private _elementRef : ElementRef < HTMLElement > ,
326
328
private _focusTrapFactory : FocusTrapFactory ,
@@ -331,12 +333,11 @@ export class MatDrawer implements AfterViewInit, AfterContentChecked, OnDestroy
331
333
@Optional ( ) @Inject ( DOCUMENT ) private _doc : any ,
332
334
@Optional ( ) @Inject ( MAT_DRAWER_CONTAINER ) public _container ?: MatDrawerContainer ,
333
335
) {
334
- this . openedChange . subscribe ( ( opened : boolean ) => {
336
+ this . openedChange . pipe ( takeUntil ( this . _destroyed ) ) . subscribe ( ( opened : boolean ) => {
335
337
if ( opened ) {
336
338
if ( this . _doc ) {
337
339
this . _elementFocusedBeforeDrawerWasOpened = this . _doc . activeElement as HTMLElement ;
338
340
}
339
-
340
341
this . _takeFocus ( ) ;
341
342
} else if ( this . _isFocusWithinDrawer ( ) ) {
342
343
this . _restoreFocus ( this . _openedVia || 'program' ) ;
@@ -441,11 +442,15 @@ export class MatDrawer implements AfterViewInit, AfterContentChecked, OnDestroy
441
442
return ;
442
443
case true :
443
444
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
+ ) ;
449
454
break ;
450
455
case 'first-heading' :
451
456
this . _focusByCssSelector ( 'h1, h2, h3, h4, h5, h6, [role="heading"]' ) ;
0 commit comments