@@ -2,6 +2,7 @@ import {FocusMonitor, FocusOrigin} from '@angular/cdk/a11y';
22import { Directionality } from '@angular/cdk/bidi' ;
33import { A , ESCAPE } from '@angular/cdk/keycodes' ;
44import { Overlay , OverlayContainer , ScrollStrategy } from '@angular/cdk/overlay' ;
5+ import { _supportsShadowDom } from '@angular/cdk/platform' ;
56import { ScrollDispatcher } from '@angular/cdk/scrolling' ;
67import {
78 createKeyboardEvent ,
@@ -23,7 +24,8 @@ import {
2324 NgZone ,
2425 TemplateRef ,
2526 ViewChild ,
26- ViewContainerRef
27+ ViewContainerRef ,
28+ ViewEncapsulation
2729} from '@angular/core' ;
2830import {
2931 ComponentFixture ,
@@ -34,6 +36,7 @@ import {
3436 TestBed ,
3537 tick ,
3638} from '@angular/core/testing' ;
39+ import { By } from '@angular/platform-browser' ;
3740import { BrowserAnimationsModule , NoopAnimationsModule } from '@angular/platform-browser/animations' ;
3841import { numbers } from '@material/dialog' ;
3942import { Subject } from 'rxjs' ;
@@ -1075,6 +1078,32 @@ describe('MDC-based MatDialog', () => {
10751078 document . body . removeChild ( button ) ;
10761079 } ) ) ;
10771080
1081+ it ( 'should re-focus trigger element inside the shadow DOM when dialog closes' , fakeAsync ( ( ) => {
1082+ if ( ! _supportsShadowDom ( ) ) {
1083+ return ;
1084+ }
1085+
1086+ viewContainerFixture . destroy ( ) ;
1087+ const fixture = TestBed . createComponent ( ShadowDomComponent ) ;
1088+ fixture . detectChanges ( ) ;
1089+ const button = fixture . debugElement . query ( By . css ( 'button' ) ) ! . nativeElement ;
1090+
1091+ button . focus ( ) ;
1092+
1093+ const dialogRef = dialog . open ( PizzaMsg ) ;
1094+ flushMicrotasks ( ) ;
1095+ fixture . detectChanges ( ) ;
1096+ flushMicrotasks ( ) ;
1097+
1098+ const spy = spyOn ( button , 'focus' ) . and . callThrough ( ) ;
1099+ dialogRef . close ( ) ;
1100+ flushMicrotasks ( ) ;
1101+ fixture . detectChanges ( ) ;
1102+ tick ( 500 ) ;
1103+
1104+ expect ( spy ) . toHaveBeenCalled ( ) ;
1105+ } ) ) ;
1106+
10781107 it ( 'should re-focus the trigger via keyboard when closed via escape key' , fakeAsync ( ( ) => {
10791108 const button = document . createElement ( 'button' ) ;
10801109 let lastFocusOrigin : FocusOrigin = null ;
@@ -1870,6 +1899,12 @@ class DialogWithInjectedData {
18701899class DialogWithoutFocusableElements {
18711900}
18721901
1902+ @Component ( {
1903+ template : `<button>I'm a button</button>` ,
1904+ encapsulation : ViewEncapsulation . ShadowDom
1905+ } )
1906+ class ShadowDomComponent { }
1907+
18731908// Create a real (non-test) NgModule as a workaround for
18741909// https://github.com/angular/angular/issues/10760
18751910const TEST_DIRECTIVES = [
@@ -1882,6 +1917,7 @@ const TEST_DIRECTIVES = [
18821917 DialogWithInjectedData ,
18831918 DialogWithoutFocusableElements ,
18841919 ComponentWithContentElementTemplateRef ,
1920+ ShadowDomComponent ,
18851921] ;
18861922
18871923@NgModule ( {
0 commit comments