-
Notifications
You must be signed in to change notification settings - Fork 124
/
focus-ring.js
244 lines (214 loc) · 6.79 KB
/
focus-ring.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/**
* https://github.com/WICG/focus-ring
*/
function init() {
var hadKeyboardEvent = true;
var elWithFocusRing;
var inputTypesWhitelist = {
text: true,
search: true,
url: true,
tel: true,
email: true,
password: true,
number: true,
date: true,
month: true,
week: true,
time: true,
datetime: true,
'datetime-local': true
};
/**
* Computes whether the given element should automatically trigger the
* `focus-ring` class being added, i.e. whether it should always match
* `:focus-ring` when focused.
* @param {Element} el
* @return {boolean}
*/
function focusTriggersKeyboardModality(el) {
var type = el.type;
var tagName = el.tagName;
if (tagName == 'INPUT' && inputTypesWhitelist[type] && !el.readonly) {
return true;
}
if (tagName == 'TEXTAREA' && !el.readonly) {
return true;
}
if (el.contentEditable == 'true') {
return true;
}
return false;
}
/**
* Add the `focus-ring` class to the given element if it was not added by
* the author.
* @param {Element} el
*/
function addFocusRingClass(el) {
if (el.classList.contains('focus-ring')) {
return;
}
el.classList.add('focus-ring');
el.setAttribute('data-focus-ring-added', '');
}
/**
* Remove the `focus-ring` class from the given element if it was not
* originally added by the author.
* @param {Element} el
*/
function removeFocusRingClass(el) {
if (!el.hasAttribute('data-focus-ring-added')) {
return;
}
el.classList.remove('focus-ring');
el.removeAttribute('data-focus-ring-added');
}
/**
* On `keydown`, set `hadKeyboardEvent`, add `focus-ring` class if the
* key was Tab/Shift-Tab or Arrow Keys.
* @param {Event} e
*/
function onKeyDown(e) {
var allowedKeys = [
9, // TAB
37, // LEFT
38, // UP
39, // RIGHT
40 // DOWN
];
// Ignore keypresses if the user is holding down a modifier key.
if (e.altKey || e.ctrlKey || e.metaKey) {
return;
}
// Ignore keypresses which aren't related to keyboard navigation.
if (allowedKeys.indexOf(e.keyCode) === -1) {
return;
}
hadKeyboardEvent = true;
}
/**
* On `focus`, add the `focus-ring` class to the target if:
* - the target received focus as a result of keyboard navigation, or
* - the event target is an element that will likely require interaction
* via the keyboard (e.g. a text box)
* @param {Event} e
*/
function onFocus(e) {
if (e.target == document) {
return;
}
if (hadKeyboardEvent || focusTriggersKeyboardModality(e.target)) {
addFocusRingClass(e.target);
hadKeyboardEvent = false;
}
}
/**
* On `blur`, remove the `focus-ring` class from the target.
* @param {Event} e
*/
function onBlur(e) {
if (e.target == document) {
return;
}
removeFocusRingClass(e.target);
}
/**
* When the window regains focus, restore the focus-ring class to the element
* to which it was previously applied.
*/
function onWindowFocus() {
window.removeEventListener('focus', onWindowFocus, true);
if (document.activeElement == elWithFocusRing) {
addFocusRingClass(elWithFocusRing);
}
elWithFocusRing = null;
}
/**
* When switching windows, keep track of the focused element if it has a
* focus-ring class.
* @param {Event} e
*/
function onWindowBlur(e) {
if (e.target !== window) {
return;
}
window.addEventListener('focus', onWindowFocus, true);
addInitialPointerMoveListeners();
if (document.activeElement.classList.contains('focus-ring')) {
// Keep a reference to the element to which the focus-ring class is
// applied so the focus-ring class can be restored to it if the window
// regains focus after being blurred.
elWithFocusRing = document.activeElement;
}
}
/**
* Add a group of listeners to detect usage of any pointing devices.
* These listeners will be added when the polyfill first loads, and anytime
* the window is blurred, so that they are active when the window regains focus.
*/
function addInitialPointerMoveListeners() {
document.addEventListener('mousemove', onInitialPointerMove);
document.addEventListener('mousedown', onInitialPointerMove);
document.addEventListener('mouseup', onInitialPointerMove);
document.addEventListener('pointermove', onInitialPointerMove);
document.addEventListener('pointerdown', onInitialPointerMove);
document.addEventListener('pointerup', onInitialPointerMove);
document.addEventListener('touchmove', onInitialPointerMove);
document.addEventListener('touchstart', onInitialPointerMove);
document.addEventListener('touchend', onInitialPointerMove);
}
/**
* When the polfyill first loads, assume the user is in keyboard modality.
* If any event is received from a pointing device (e.g mouse, pointer, touch), turn off
* keyboard modality.
* This accounts for situations where focus enters the page from the URL bar.
* @param {Event} e
*/
function onInitialPointerMove(e) {
// Work around a Safari quirk that fires a mousemove on <html> whenever the
// window blurs, even if you're tabbing out of the page. ¯\_(ツ)_/¯
if (e.target.nodeName.toLowerCase() === 'html') return;
hadKeyboardEvent = false;
document.removeEventListener('mousemove', onInitialPointerMove);
document.removeEventListener('mousedown', onInitialPointerMove);
document.removeEventListener('mouseup', onInitialPointerMove);
document.removeEventListener('pointermove', onInitialPointerMove);
document.removeEventListener('pointerdown', onInitialPointerMove);
document.removeEventListener('pointerup', onInitialPointerMove);
document.removeEventListener('touchmove', onInitialPointerMove);
document.removeEventListener('touchstart', onInitialPointerMove);
document.removeEventListener('touchend', onInitialPointerMove);
}
document.addEventListener('keydown', onKeyDown, true);
document.addEventListener('focus', onFocus, true);
document.addEventListener('blur', onBlur, true);
window.addEventListener('focus', onWindowFocus, true);
window.addEventListener('blur', onWindowBlur, true);
addInitialPointerMoveListeners();
document.body.classList.add('js-focus-ring');
}
/**
* Subscription when the DOM is ready
* @param {Function} callback
*/
function onDOMReady(callback) {
var loaded;
/**
* Callback wrapper for check loaded state
*/
function load() {
if (!loaded) {
loaded = true;
callback();
}
}
if (document.readyState === 'complete') {
callback();
} else {
loaded = false;
document.addEventListener('DOMContentLoaded', load, false);
window.addEventListener('load', load, false);
}
}
onDOMReady(init);