forked from ai/keyux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hidden.js
41 lines (38 loc) · 1.14 KB
/
hidden.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
export function hiddenKeyUX() {
return window => {
let allowed
let wasHidden
function jump(e) {
if (e.target.getAttribute('aria-hidden') === 'true') {
allowed = e.target
allowed.setAttribute('aria-hidden', 'false')
wasHidden = allowed.hidden
if (wasHidden) allowed.hidden = false
let first = e.target.querySelector(
'a, button, select, textarea, ' +
'input:not([type=radio]), [type=radio]:checked, ' +
'[tabindex]:not([tabindex="-1"])'
)
if (first) first.tabIndex = 0
}
}
function focusOut(e) {
if (
allowed &&
allowed.contains(e.target) &&
(!e.relatedTarget || !allowed.contains(e.relatedTarget))
) {
e.target.tabIndex = -1
allowed.setAttribute('aria-hidden', 'true')
if (wasHidden) allowed.hidden = true
allowed = null
}
}
window.addEventListener('keyuxJump', jump)
window.addEventListener('focusout', focusOut)
return () => {
window.removeEventListener('keyuxJump', jump)
window.removeEventListener('focusout', focusOut)
}
}
}