@@ -16,7 +16,8 @@ import {
1616 NgModule ,
1717 TemplateRef ,
1818 ViewChild ,
19- ViewContainerRef
19+ ViewContainerRef ,
20+ ViewEncapsulation
2021} from '@angular/core' ;
2122import { By } from '@angular/platform-browser' ;
2223import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
@@ -26,11 +27,12 @@ import {Directionality} from '@angular/cdk/bidi';
2627import { CdkDialogContainer } from './dialog-container' ;
2728import { OverlayContainer } from '@angular/cdk/overlay' ;
2829import { A , ESCAPE } from '@angular/cdk/keycodes' ;
30+ import { _supportsShadowDom } from '@angular/cdk/platform' ;
2931import {
3032 dispatchKeyboardEvent ,
3133 createKeyboardEvent ,
3234 dispatchEvent ,
33- } from '../.. /cdk/testing/private' ;
35+ } from '@angular /cdk/testing/private' ;
3436import { DIALOG_DATA , Dialog , DialogModule , DialogRef } from './index' ;
3537
3638describe ( 'Dialog' , ( ) => {
@@ -935,6 +937,32 @@ describe('Dialog', () => {
935937 document . body . removeChild ( button ) ;
936938 } ) ) ;
937939
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+
938966 it ( 'should not move focus if it was moved outside the dialog while animating' , fakeAsync ( ( ) => {
939967 // Create a element that has focus before the dialog is opened.
940968 const button = document . createElement ( 'button' ) ;
@@ -1220,6 +1248,12 @@ class DialogWithInjectedData {
12201248@Component ( { template : '<p>Pasta</p>' } )
12211249class DialogWithoutFocusableElements { }
12221250
1251+ @Component ( {
1252+ template : `<button>I'm a button</button>` ,
1253+ encapsulation : ViewEncapsulation . ShadowDom
1254+ } )
1255+ class ShadowDomComponent { }
1256+
12231257// Create a real (non-test) NgModule as a workaround for
12241258// https://github.com/angular/angular/issues/10760
12251259const TEST_DIRECTIVES = [
@@ -1230,7 +1264,8 @@ const TEST_DIRECTIVES = [
12301264 ComponentWithOnPushViewContainer ,
12311265 ContentElementDialog ,
12321266 DialogWithInjectedData ,
1233- DialogWithoutFocusableElements
1267+ DialogWithoutFocusableElements ,
1268+ ShadowDomComponent ,
12341269] ;
12351270
12361271@NgModule ( {
0 commit comments