This repository has been archived by the owner on Jul 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdelegate-focus-mixin.js
156 lines (135 loc) · 3.92 KB
/
delegate-focus-mixin.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
const $$tabindex = Symbol('tabindex');
const $$oldTabindex = Symbol('oldTabindex');
const $$newTabindex = Symbol('newTabindex');
export const DelegateFocusMixin = superClass =>
class extends superClass {
static get properties() {
return {
tabIndex: {
converter: {
fromAttribute: Number,
toAttribute: value => (value == null ? null : value.toString())
},
noAccessor: true,
reflect: true
},
/**
* If true, the user cannot interact with this element.
*/
disabled: {
type: Boolean,
reflect: true
}
};
}
constructor() {
super();
if (!this.hasAttribute('tabindex')) {
this.tabIndex = 0;
}
}
get tabIndex() {
return this[$$tabindex];
}
set tabIndex(value) {
const oldValue = this[$$tabindex];
this[$$tabindex] = value;
this.requestUpdate('tabIndex', oldValue);
}
firstUpdated() {
this.addEventListener('focusin', e => {
if (e.composedPath()[0] === this) {
this._focus();
}
});
this.addEventListener('keydown', e => {
if (!e.defaultPrevented && e.shiftKey && e.keyCode === 9) {
// Flag is checked in _focus event handler.
this._isShiftTabbing = true;
HTMLElement.prototype.focus.apply(this);
// Event handling in IE is asynchronous and the flag is removed asynchronously as well
setTimeout(() => {
this._isShiftTabbing = false;
}, 0);
}
});
}
update(props) {
if (props.has('disabled')) {
this._disabledChanged(this.disabled, props.get('disabled'));
}
if (props.has('tabIndex')) {
// save value of tabindex, as it can be overridden to
// undefined in case if the element is disabled
this[$$newTabindex] = this.tabIndex;
this._tabIndexChanged(this.tabIndex);
}
super.update(props);
}
updated(props) {
super.updated(props);
if (props.has('disabled')) {
this.focusElement.disabled = this.disabled;
if (this.disabled) {
this.blur();
}
}
if (props.has('tabIndex') && this[$$newTabindex] !== undefined) {
this.focusElement.tabIndex = this[$$newTabindex];
this[$$newTabindex] = undefined;
}
}
/**
* Any element extending this mixin is required to implement this getter.
* It returns the actual focusable element in the component.
*/
get focusElement() {
window.console.warn(`Please implement the 'focusElement' property in <${this.localName}>`);
return this;
}
_focus() {
if (this._isShiftTabbing) {
return;
}
this.focusElement.focus();
}
/**
* Moving the focus from the host element causes firing of the blur event what leads to problems in IE.
* @protected
*/
focus() {
if (this.disabled) {
return;
}
this.focusElement.focus();
}
/**
* Native bluring in the host element does nothing because it does not have the focus.
* In chrome it works, but not in FF.
* @protected
*/
blur() {
this.focusElement.blur();
}
_disabledChanged(disabled, oldDisabled) {
if (disabled) {
this[$$oldTabindex] = this.tabIndex;
this.tabIndex = -1;
this.setAttribute('aria-disabled', 'true');
} else if (oldDisabled) {
if (this[$$oldTabindex] !== undefined) {
this.tabIndex = this[$$oldTabindex];
}
this.removeAttribute('aria-disabled');
}
}
_tabIndexChanged(tabindex) {
if (this.disabled && tabindex) {
// If tabindex attribute was changed while checkbox was disabled
if (this.tabIndex !== -1) {
this[$$oldTabindex] = this.tabIndex;
}
this.tabIndex = null;
}
}
};