@@ -16,7 +16,8 @@ import {
16
16
NgModule ,
17
17
TemplateRef ,
18
18
ViewChild ,
19
- ViewContainerRef
19
+ ViewContainerRef ,
20
+ ViewEncapsulation
20
21
} from '@angular/core' ;
21
22
import { By } from '@angular/platform-browser' ;
22
23
import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
@@ -26,11 +27,12 @@ import {Directionality} from '@angular/cdk/bidi';
26
27
import { CdkDialogContainer } from './dialog-container' ;
27
28
import { OverlayContainer } from '@angular/cdk/overlay' ;
28
29
import { A , ESCAPE } from '@angular/cdk/keycodes' ;
30
+ import { _supportsShadowDom } from '@angular/cdk/platform' ;
29
31
import {
30
32
dispatchKeyboardEvent ,
31
33
createKeyboardEvent ,
32
34
dispatchEvent ,
33
- } from '../.. /cdk/testing/private' ;
35
+ } from '@angular /cdk/testing/private' ;
34
36
import { DIALOG_DATA , Dialog , DialogModule , DialogRef } from './index' ;
35
37
36
38
describe ( 'Dialog' , ( ) => {
@@ -935,6 +937,32 @@ describe('Dialog', () => {
935
937
document . body . removeChild ( button ) ;
936
938
} ) ) ;
937
939
940
+ it ( 'should re-focus trigger element inside the shadow DOM when dialog closes' , fakeAsync ( ( ) => {
941
+ if ( ! _supportsShadowDom ( ) ) {
942
+ return ;
943
+ }
944
+
945
+ viewContainerFixture . destroy ( ) ;
946
+ const fixture = TestBed . createComponent ( ShadowDomComponent ) ;
947
+ fixture . detectChanges ( ) ;
948
+ const button = fixture . debugElement . query ( By . css ( 'button' ) ) ! . nativeElement ;
949
+
950
+ button . focus ( ) ;
951
+
952
+ const dialogRef = dialog . openFromComponent ( PizzaMsg ) ;
953
+ flushMicrotasks ( ) ;
954
+ fixture . detectChanges ( ) ;
955
+ flushMicrotasks ( ) ;
956
+
957
+ const spy = spyOn ( button , 'focus' ) . and . callThrough ( ) ;
958
+ dialogRef . close ( ) ;
959
+ flushMicrotasks ( ) ;
960
+ fixture . detectChanges ( ) ;
961
+ tick ( 500 ) ;
962
+
963
+ expect ( spy ) . toHaveBeenCalled ( ) ;
964
+ } ) ) ;
965
+
938
966
it ( 'should not move focus if it was moved outside the dialog while animating' , fakeAsync ( ( ) => {
939
967
// Create a element that has focus before the dialog is opened.
940
968
const button = document . createElement ( 'button' ) ;
@@ -1220,6 +1248,12 @@ class DialogWithInjectedData {
1220
1248
@Component ( { template : '<p>Pasta</p>' } )
1221
1249
class DialogWithoutFocusableElements { }
1222
1250
1251
+ @Component ( {
1252
+ template : `<button>I'm a button</button>` ,
1253
+ encapsulation : ViewEncapsulation . ShadowDom
1254
+ } )
1255
+ class ShadowDomComponent { }
1256
+
1223
1257
// Create a real (non-test) NgModule as a workaround for
1224
1258
// https://github.com/angular/angular/issues/10760
1225
1259
const TEST_DIRECTIVES = [
@@ -1230,7 +1264,8 @@ const TEST_DIRECTIVES = [
1230
1264
ComponentWithOnPushViewContainer ,
1231
1265
ContentElementDialog ,
1232
1266
DialogWithInjectedData ,
1233
- DialogWithoutFocusableElements
1267
+ DialogWithoutFocusableElements ,
1268
+ ShadowDomComponent ,
1234
1269
] ;
1235
1270
1236
1271
@NgModule ( {
0 commit comments