-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathkeyboardFocusOutline.js
69 lines (63 loc) · 1.83 KB
/
keyboardFocusOutline.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
import Adapt from 'core/js/adapt';
/**
* Manages whether or not the focus outline should be entirely removed
* or removed until a key is pressed on a tabbable element.
* @class
*/
export default class KeyboardFocusOutline extends Backbone.Controller {
initialize({ a11y }) {
this.a11y = a11y;
this._onKeyDown = this._onKeyDown.bind(this);
this.$html = $('html');
this.showOnKeys = {
9: true, // tab
13: true, // enter
32: true, // space
37: true, // arrow left
38: true, // arrow up
39: true, // arrow right
40: true // arrow down
};
this.listenTo(Adapt, {
'accessibility:ready': this._attachEventListeners
});
}
_attachEventListeners() {
document.addEventListener('keydown', this._onKeyDown);
this._start();
}
/**
* Add styling classes if required.
*/
_start() {
const config = this.a11y.config;
if (config._options._isFocusOutlineDisabled) {
this.$html.addClass('a11y-disable-focusoutline');
return;
}
if (!config._isEnabled || !config._options._isFocusOutlineKeyboardOnlyEnabled) {
return;
}
this.$html.addClass('a11y-disable-focusoutline');
}
/**
* Handle key down events for on a tabbable element.
*
* @param {JQuery.Event} event
*/
_onKeyDown(event) {
const config = this.a11y.config;
if (config._options._isFocusOutlineDisabled) {
this.$html.addClass('a11y-disable-focusoutline');
return;
}
if (!config._isEnabled || !config._options._isFocusOutlineKeyboardOnlyEnabled || !this.showOnKeys[event.keyCode]) {
return;
}
const $element = $(event.target);
if (!$element.is(config._options._tabbableElements) || $element.is(config._options._focusOutlineKeyboardOnlyIgnore)) {
return;
}
this.$html.removeClass('a11y-disable-focusoutline');
}
}