@@ -185,6 +185,24 @@ describe('FocusTrap', () => {
185185 expect ( ( ) => focusTrapInstance . focusFirstTabbableElement ( ) ) . not . toThrow ( ) ;
186186 expect ( ( ) => focusTrapInstance . focusLastTabbableElement ( ) ) . not . toThrow ( ) ;
187187 } ) ;
188+
189+ it ( 'should find tabbable elements in shadow DOM' , ( ) => {
190+ if ( ! _supportsShadowDom ( ) ) {
191+ return ;
192+ }
193+
194+ const fixture = TestBed . createComponent ( FocusTrapWithShadowDom ) ;
195+ fixture . detectChanges ( ) ;
196+ const focusTrapInstance = fixture . componentInstance . focusTrapDirective . focusTrap ;
197+
198+ // The shadow button should be found as the first tabbable element
199+ expect ( focusTrapInstance . focusFirstTabbableElement ( ) ) . toBe ( true ) ;
200+ expect ( getActiveElement ( ) . textContent ?. trim ( ) ) . toBe ( 'Shadow Button' ) ;
201+
202+ // The shadow button should also be found as the last tabbable element
203+ expect ( focusTrapInstance . focusLastTabbableElement ( ) ) . toBe ( true ) ;
204+ expect ( getActiveElement ( ) . textContent ?. trim ( ) ) . toBe ( 'Shadow Button' ) ;
205+ } ) ;
188206 } ) ;
189207
190208 describe ( 'with autoCapture' , ( ) => {
@@ -448,3 +466,25 @@ class FocusTrapInsidePortal {
448466 @ViewChild ( 'template' ) template : TemplateRef < any > ;
449467 @ViewChild ( CdkPortalOutlet ) portalOutlet : CdkPortalOutlet ;
450468}
469+
470+ @Component ( {
471+ template : `
472+ <div cdkTrapFocus>
473+ <div #shadowHost></div>
474+ </div>
475+ ` ,
476+ imports : [ A11yModule ] ,
477+ } )
478+ class FocusTrapWithShadowDom {
479+ @ViewChild ( CdkTrapFocus ) focusTrapDirective : CdkTrapFocus ;
480+ @ViewChild ( 'shadowHost' , { static : true } ) shadowHost : any ;
481+
482+ ngAfterViewInit ( ) {
483+ if ( _supportsShadowDom ( ) ) {
484+ const shadowRoot = this . shadowHost . nativeElement . attachShadow ( { mode : 'open' } ) ;
485+ const shadowButton = document . createElement ( 'button' ) ;
486+ shadowButton . textContent = 'Shadow Button' ;
487+ shadowRoot . appendChild ( shadowButton ) ;
488+ }
489+ }
490+ }
0 commit comments