@@ -2,6 +2,7 @@ import {FocusMonitor, FocusOrigin} from '@angular/cdk/a11y';
2
2
import { Directionality } from '@angular/cdk/bidi' ;
3
3
import { A , ESCAPE } from '@angular/cdk/keycodes' ;
4
4
import { Overlay , OverlayContainer , ScrollStrategy } from '@angular/cdk/overlay' ;
5
+ import { _supportsShadowDom } from '@angular/cdk/platform' ;
5
6
import { ScrollDispatcher } from '@angular/cdk/scrolling' ;
6
7
import {
7
8
createKeyboardEvent ,
@@ -23,7 +24,8 @@ import {
23
24
NgZone ,
24
25
TemplateRef ,
25
26
ViewChild ,
26
- ViewContainerRef
27
+ ViewContainerRef ,
28
+ ViewEncapsulation
27
29
} from '@angular/core' ;
28
30
import {
29
31
ComponentFixture ,
@@ -34,6 +36,7 @@ import {
34
36
TestBed ,
35
37
tick ,
36
38
} from '@angular/core/testing' ;
39
+ import { By } from '@angular/platform-browser' ;
37
40
import { BrowserAnimationsModule , NoopAnimationsModule } from '@angular/platform-browser/animations' ;
38
41
import { numbers } from '@material/dialog' ;
39
42
import { Subject } from 'rxjs' ;
@@ -1075,6 +1078,32 @@ describe('MDC-based MatDialog', () => {
1075
1078
document . body . removeChild ( button ) ;
1076
1079
} ) ) ;
1077
1080
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
+
1078
1107
it ( 'should re-focus the trigger via keyboard when closed via escape key' , fakeAsync ( ( ) => {
1079
1108
const button = document . createElement ( 'button' ) ;
1080
1109
let lastFocusOrigin : FocusOrigin = null ;
@@ -1870,6 +1899,12 @@ class DialogWithInjectedData {
1870
1899
class DialogWithoutFocusableElements {
1871
1900
}
1872
1901
1902
+ @Component ( {
1903
+ template : `<button>I'm a button</button>` ,
1904
+ encapsulation : ViewEncapsulation . ShadowDom
1905
+ } )
1906
+ class ShadowDomComponent { }
1907
+
1873
1908
// Create a real (non-test) NgModule as a workaround for
1874
1909
// https://github.com/angular/angular/issues/10760
1875
1910
const TEST_DIRECTIVES = [
@@ -1882,6 +1917,7 @@ const TEST_DIRECTIVES = [
1882
1917
DialogWithInjectedData ,
1883
1918
DialogWithoutFocusableElements ,
1884
1919
ComponentWithContentElementTemplateRef ,
1920
+ ShadowDomComponent ,
1885
1921
] ;
1886
1922
1887
1923
@NgModule ( {
0 commit comments