1
1
import findTabbable from "./tabbable" ;
2
2
3
+ function getActiveElement ( el = document ) {
4
+ return el . activeElement . shadowRoot
5
+ ? getActiveElement ( el . activeElement . shadowRoot )
6
+ : el . activeElement ;
7
+ }
8
+
3
9
export default function scopeTab ( node , event ) {
4
10
const tabbable = findTabbable ( node ) ;
5
11
@@ -14,19 +20,20 @@ export default function scopeTab(node, event) {
14
20
const shiftKey = event . shiftKey ;
15
21
const head = tabbable [ 0 ] ;
16
22
const tail = tabbable [ tabbable . length - 1 ] ;
23
+ const activeElement = getActiveElement ( ) ;
17
24
18
25
// proceed with default browser behavior on tab.
19
26
// Focus on last element on shift + tab.
20
- if ( node === document . activeElement ) {
27
+ if ( node === activeElement ) {
21
28
if ( ! shiftKey ) return ;
22
29
target = tail ;
23
30
}
24
31
25
- if ( tail === document . activeElement && ! shiftKey ) {
32
+ if ( tail === activeElement && ! shiftKey ) {
26
33
target = head ;
27
34
}
28
35
29
- if ( head === document . activeElement && shiftKey ) {
36
+ if ( head === activeElement && shiftKey ) {
30
37
target = tail ;
31
38
}
32
39
@@ -57,7 +64,7 @@ export default function scopeTab(node, event) {
57
64
// the focus
58
65
if ( ! isSafariDesktop ) return ;
59
66
60
- var x = tabbable . indexOf ( document . activeElement ) ;
67
+ var x = tabbable . indexOf ( activeElement ) ;
61
68
62
69
if ( x > - 1 ) {
63
70
x += shiftKey ? - 1 : 1 ;
0 commit comments