-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
jump.js
72 lines (66 loc) · 1.7 KB
/
jump.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
export function jumpKeyUX() {
return window => {
let jumps = []
function focus(next) {
let current = window.document.activeElement
if (current && current !== window.document.body) {
jumps.push(new WeakRef(current))
}
next.focus({ focusVisible: true })
}
function back() {
let ref = jumps.pop()
if (!ref) {
window.document.activeElement.blur()
return
}
let el = ref.deref()
if (el && el.isConnected) {
el.focus()
} else {
back()
}
}
let tries = 0
let finding
function jump(from) {
clearInterval(finding)
let ariaControls = from.getAttribute('aria-controls')
finding = setInterval(() => {
if (tries++ > 50) {
clearInterval(finding)
return
}
let area = window.document.getElementById(ariaControls)
if (area) {
let next = area.querySelector(
'a, button, select, textarea, ' +
'input:not([type=radio]), [type=radio]:checked, ' +
'[tabindex]:not([tabindex="-1"])'
)
if (next) {
clearInterval(finding)
area.dispatchEvent(
new window.CustomEvent('keyuxJump', { bubbles: true })
)
focus(next)
}
}
}, 50)
}
function keyDown(event) {
if (event.target.getAttribute('aria-controls')) {
if (event.key === 'Enter') {
jump(event.target)
}
}
if (event.key === 'Escape') {
back()
}
}
window.addEventListener('keydown', keyDown)
return () => {
window.removeEventListener('keydown', keyDown)
}
}
}