11import findTabbable from "./tabbable" ;
22
3+ function getActiveElement ( el = document ) {
4+ return el . activeElement . shadowRoot
5+ ? getActiveElement ( el . activeElement . shadowRoot )
6+ : el . activeElement ;
7+ }
8+
39export default function scopeTab ( node , event ) {
410 const tabbable = findTabbable ( node ) ;
511
@@ -14,19 +20,20 @@ export default function scopeTab(node, event) {
1420 const shiftKey = event . shiftKey ;
1521 const head = tabbable [ 0 ] ;
1622 const tail = tabbable [ tabbable . length - 1 ] ;
23+ const activeElement = getActiveElement ( ) ;
1724
1825 // proceed with default browser behavior on tab.
1926 // Focus on last element on shift + tab.
20- if ( node === document . activeElement ) {
27+ if ( node === activeElement ) {
2128 if ( ! shiftKey ) return ;
2229 target = tail ;
2330 }
2431
25- if ( tail === document . activeElement && ! shiftKey ) {
32+ if ( tail === activeElement && ! shiftKey ) {
2633 target = head ;
2734 }
2835
29- if ( head === document . activeElement && shiftKey ) {
36+ if ( head === activeElement && shiftKey ) {
3037 target = tail ;
3138 }
3239
@@ -57,7 +64,7 @@ export default function scopeTab(node, event) {
5764 // the focus
5865 if ( ! isSafariDesktop ) return ;
5966
60- var x = tabbable . indexOf ( document . activeElement ) ;
67+ var x = tabbable . indexOf ( activeElement ) ;
6168
6269 if ( x > - 1 ) {
6370 x += shiftKey ? - 1 : 1 ;
0 commit comments