-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
focus-group.js
169 lines (149 loc) · 4.47 KB
/
focus-group.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
let ROLES = {
button: ['toolbar'],
checkbox: ['toolbar'],
menuitem: ['menu', 'menubar'],
option: ['listbox'],
tab: ['tablist']
}
function focus(current, next) {
next.tabIndex = 0
next.focus()
current.tabIndex = -1
}
function findGroupNodeByEventTarget(target) {
let itemRole = target.role || target.type || target.tagName
if (!itemRole) return null
let groupRoles = ROLES[itemRole.toLowerCase()]
if (!groupRoles) return null
for (let role of groupRoles) {
let node = target.closest(`[role=${role}]`)
if (node) return node
}
}
function getItems(target, group) {
if (group.role === 'toolbar') return getToolbarItems(group)
return group.querySelectorAll(`[role=${target.role}]`)
}
function getToolbarItems(group) {
let items = [...group.querySelectorAll('*')]
return items.filter(item => {
return (
item.role === 'button' ||
item.type === 'button' ||
item.role === 'checkbox' ||
item.type === 'checkbox'
)
})
}
function isHorizontalOrientation(group) {
let ariaOrientation = group.getAttribute('aria-orientation')
if (ariaOrientation === 'vertical') return false
if (ariaOrientation === 'horizontal') return true
let role = group.role
return role === 'menubar' || role === 'tablist' || role === 'toolbar'
}
export function focusGroupKeyUX(options) {
return window => {
let inGroup = false
let typingDelayMs = options?.searchDelayMs || 300
let lastTyped = 0
let searchPrefix = ''
function keyDown(event) {
let group = findGroupNodeByEventTarget(event.target)
if (!group) {
stop()
return
}
let items = getItems(event.target, group)
let index = Array.from(items).indexOf(event.target)
let nextKey = 'ArrowDown'
let prevKey = 'ArrowUp'
if (isHorizontalOrientation(group)) {
if (window.document.dir === 'rtl') {
nextKey = 'ArrowLeft'
prevKey = 'ArrowRight'
} else {
nextKey = 'ArrowRight'
prevKey = 'ArrowLeft'
}
}
if (event.key === nextKey) {
event.preventDefault()
focus(event.target, items[index + 1] || items[0])
} else if (event.key === prevKey) {
event.preventDefault()
focus(event.target, items[index - 1] || items[items.length - 1])
} else if (event.key === 'Home') {
event.preventDefault()
focus(event.target, items[0])
} else if (event.key === 'End') {
event.preventDefault()
focus(event.target, items[items.length - 1])
} else if (event.key.length === 1 && group.role !== 'tablist') {
if (event.timeStamp - lastTyped <= typingDelayMs) {
searchPrefix += event.key.toLowerCase()
} else {
searchPrefix = event.key.toLowerCase()
}
lastTyped = event.timeStamp
let found = Array.from(items).find(item => {
return item.textContent
?.trim()
?.toLowerCase()
?.startsWith(searchPrefix)
})
if (found) {
event.preventDefault()
focus(event.target, found)
}
}
}
function stop() {
inGroup = false
window.removeEventListener('keydown', keyDown)
}
function focusIn(event) {
let group = findGroupNodeByEventTarget(event.target)
if (group) {
if (!inGroup) {
inGroup = true
window.addEventListener('keydown', keyDown)
}
let items = getItems(event.target, group)
for (let item of items) {
if (item !== event.target) {
item.setAttribute('tabindex', -1)
}
}
} else if (inGroup) {
stop()
}
}
function focusOut(event) {
if (!event.relatedTarget || event.relatedTarget === window.document) {
stop()
}
}
function click(event) {
let group = findGroupNodeByEventTarget(event.target)
if (group) {
let items = getItems(event.target, group)
for (let item of items) {
if (item !== event.target) {
item.setAttribute('tabindex', -1)
}
}
event.target.setAttribute('tabindex', 0)
}
}
window.addEventListener('click', click)
window.addEventListener('focusin', focusIn)
window.addEventListener('focusout', focusOut)
return () => {
stop()
window.removeEventListener('click', click)
window.removeEventListener('focusin', focusIn)
window.removeEventListener('focusout', focusOut)
}
}
}