@spectrum-web-components/reactive-controllers 0.2.4 → 0.2.5-devmode.79
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +23 -5
- package/src/FocusGroup.dev.js +217 -0
- package/src/FocusGroup.dev.js.map +7 -0
- package/src/FocusGroup.js +215 -226
- package/src/FocusGroup.js.map +7 -1
- package/src/MatchMedia.dev.js +26 -0
- package/src/MatchMedia.dev.js.map +7 -0
- package/src/MatchMedia.js +24 -24
- package/src/MatchMedia.js.map +7 -1
- package/src/RovingTabindex.dev.js +65 -0
- package/src/RovingTabindex.dev.js.map +7 -0
- package/src/RovingTabindex.js +61 -74
- package/src/RovingTabindex.js.map +7 -1
- package/src/index.dev.js +3 -0
- package/src/index.dev.js.map +7 -0
- package/src/index.js +3 -14
- package/src/index.js.map +7 -1
- package/test/match-media.test.js +17 -29
- package/test/match-media.test.js.map +7 -1
- package/test/roving-tabindex-integration.test.js +76 -88
- package/test/roving-tabindex-integration.test.js.map +7 -1
- package/test/roving-tabindex.test.js +15 -26
- package/test/roving-tabindex.test.js.map +7 -1
package/src/FocusGroup.js
CHANGED
|
@@ -1,228 +1,217 @@
|
|
|
1
1
|
export class FocusGroupController {
|
|
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
|
-
this.
|
|
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
|
-
removeEventListeners() {
|
|
217
|
-
this.host.removeEventListener('focusin', this.handleFocusin);
|
|
218
|
-
this.host.removeEventListener('focusout', this.handleFocusout);
|
|
219
|
-
this.host.removeEventListener('keydown', this.handleKeydown);
|
|
220
|
-
}
|
|
221
|
-
hostConnected() {
|
|
222
|
-
this.addEventListeners();
|
|
223
|
-
}
|
|
224
|
-
hostDisconnected() {
|
|
225
|
-
this.removeEventListeners();
|
|
226
|
-
}
|
|
2
|
+
constructor(host, {
|
|
3
|
+
direction,
|
|
4
|
+
elementEnterAction,
|
|
5
|
+
elements,
|
|
6
|
+
focusInIndex,
|
|
7
|
+
isFocusableElement,
|
|
8
|
+
listenerScope
|
|
9
|
+
} = { elements: () => [] }) {
|
|
10
|
+
this._currentIndex = -1;
|
|
11
|
+
this._direction = () => "both";
|
|
12
|
+
this.directionLength = 5;
|
|
13
|
+
this.elementEnterAction = (_el) => {
|
|
14
|
+
return;
|
|
15
|
+
};
|
|
16
|
+
this._focused = false;
|
|
17
|
+
this._focusInIndex = (_elements) => 0;
|
|
18
|
+
this.isFocusableElement = (_el) => true;
|
|
19
|
+
this._listenerScope = () => this.host;
|
|
20
|
+
this.offset = 0;
|
|
21
|
+
this.handleFocusin = (event) => {
|
|
22
|
+
if (!this.isEventWithinListenerScope(event))
|
|
23
|
+
return;
|
|
24
|
+
if (this.isRelatedTargetAnElement(event)) {
|
|
25
|
+
this.hostContainsFocus();
|
|
26
|
+
}
|
|
27
|
+
const path = event.composedPath();
|
|
28
|
+
let targetIndex = -1;
|
|
29
|
+
path.find((el) => {
|
|
30
|
+
targetIndex = this.elements.indexOf(el);
|
|
31
|
+
return targetIndex !== -1;
|
|
32
|
+
});
|
|
33
|
+
this.currentIndex = targetIndex > -1 ? targetIndex : this.currentIndex;
|
|
34
|
+
};
|
|
35
|
+
this.handleFocusout = (event) => {
|
|
36
|
+
if (this.isRelatedTargetAnElement(event)) {
|
|
37
|
+
this.hostNoLongerContainsFocus();
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
this.handleKeydown = (event) => {
|
|
41
|
+
if (!this.acceptsEventCode(event.code) || event.defaultPrevented) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
let diff = 0;
|
|
45
|
+
switch (event.code) {
|
|
46
|
+
case "ArrowRight":
|
|
47
|
+
diff += 1;
|
|
48
|
+
break;
|
|
49
|
+
case "ArrowDown":
|
|
50
|
+
diff += this.direction === "grid" ? this.directionLength : 1;
|
|
51
|
+
break;
|
|
52
|
+
case "ArrowLeft":
|
|
53
|
+
diff -= 1;
|
|
54
|
+
break;
|
|
55
|
+
case "ArrowUp":
|
|
56
|
+
diff -= this.direction === "grid" ? this.directionLength : 1;
|
|
57
|
+
break;
|
|
58
|
+
case "End":
|
|
59
|
+
this.currentIndex = 0;
|
|
60
|
+
diff -= 1;
|
|
61
|
+
break;
|
|
62
|
+
case "Home":
|
|
63
|
+
this.currentIndex = this.elements.length - 1;
|
|
64
|
+
diff += 1;
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
event.preventDefault();
|
|
68
|
+
if (this.direction === "grid" && this.currentIndex + diff < 0) {
|
|
69
|
+
this.currentIndex = 0;
|
|
70
|
+
} else if (this.direction === "grid" && this.currentIndex + diff > this.elements.length - 1) {
|
|
71
|
+
this.currentIndex = this.elements.length - 1;
|
|
72
|
+
} else {
|
|
73
|
+
this.setCurrentIndexCircularly(diff);
|
|
74
|
+
}
|
|
75
|
+
this.elementEnterAction(this.elements[this.currentIndex]);
|
|
76
|
+
this.focus();
|
|
77
|
+
};
|
|
78
|
+
this.host = host;
|
|
79
|
+
this.host.addController(this);
|
|
80
|
+
this._elements = elements;
|
|
81
|
+
this.isFocusableElement = isFocusableElement || this.isFocusableElement;
|
|
82
|
+
if (typeof direction === "string") {
|
|
83
|
+
this._direction = () => direction;
|
|
84
|
+
} else if (typeof direction === "function") {
|
|
85
|
+
this._direction = direction;
|
|
86
|
+
}
|
|
87
|
+
this.elementEnterAction = elementEnterAction || this.elementEnterAction;
|
|
88
|
+
if (typeof focusInIndex === "number") {
|
|
89
|
+
this._focusInIndex = () => focusInIndex;
|
|
90
|
+
} else if (typeof focusInIndex === "function") {
|
|
91
|
+
this._focusInIndex = focusInIndex;
|
|
92
|
+
}
|
|
93
|
+
if (typeof listenerScope === "object") {
|
|
94
|
+
this._listenerScope = () => listenerScope;
|
|
95
|
+
} else if (typeof listenerScope === "function") {
|
|
96
|
+
this._listenerScope = listenerScope;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
get currentIndex() {
|
|
100
|
+
if (this._currentIndex === -1) {
|
|
101
|
+
this._currentIndex = this.focusInIndex;
|
|
102
|
+
}
|
|
103
|
+
return this._currentIndex - this.offset;
|
|
104
|
+
}
|
|
105
|
+
set currentIndex(currentIndex) {
|
|
106
|
+
this._currentIndex = currentIndex + this.offset;
|
|
107
|
+
}
|
|
108
|
+
get direction() {
|
|
109
|
+
return this._direction();
|
|
110
|
+
}
|
|
111
|
+
get elements() {
|
|
112
|
+
if (!this.cachedElements) {
|
|
113
|
+
this.cachedElements = this._elements();
|
|
114
|
+
}
|
|
115
|
+
return this.cachedElements;
|
|
116
|
+
}
|
|
117
|
+
set focused(focused) {
|
|
118
|
+
if (focused === this.focused)
|
|
119
|
+
return;
|
|
120
|
+
this._focused = focused;
|
|
121
|
+
}
|
|
122
|
+
get focused() {
|
|
123
|
+
return this._focused;
|
|
124
|
+
}
|
|
125
|
+
get focusInElement() {
|
|
126
|
+
return this.elements[this.focusInIndex];
|
|
127
|
+
}
|
|
128
|
+
get focusInIndex() {
|
|
129
|
+
return this._focusInIndex(this.elements);
|
|
130
|
+
}
|
|
131
|
+
isEventWithinListenerScope(event) {
|
|
132
|
+
if (this._listenerScope() === this.host)
|
|
133
|
+
return true;
|
|
134
|
+
return event.composedPath().includes(this._listenerScope());
|
|
135
|
+
}
|
|
136
|
+
update({ elements } = { elements: () => [] }) {
|
|
137
|
+
this.unmanage();
|
|
138
|
+
this._elements = elements;
|
|
139
|
+
this.clearElementCache();
|
|
140
|
+
this.manage();
|
|
141
|
+
}
|
|
142
|
+
focus(options) {
|
|
143
|
+
let focusElement = this.elements[this.currentIndex];
|
|
144
|
+
if (!focusElement || !this.isFocusableElement(focusElement)) {
|
|
145
|
+
this.setCurrentIndexCircularly(1);
|
|
146
|
+
focusElement = this.elements[this.currentIndex];
|
|
147
|
+
}
|
|
148
|
+
if (focusElement && this.isFocusableElement(focusElement)) {
|
|
149
|
+
focusElement.focus(options);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
clearElementCache(offset = 0) {
|
|
153
|
+
delete this.cachedElements;
|
|
154
|
+
this.offset = offset;
|
|
155
|
+
}
|
|
156
|
+
setCurrentIndexCircularly(diff) {
|
|
157
|
+
const { length } = this.elements;
|
|
158
|
+
let steps = length;
|
|
159
|
+
let nextIndex = (length + this.currentIndex + diff) % length;
|
|
160
|
+
while (steps && this.elements[nextIndex] && !this.isFocusableElement(this.elements[nextIndex])) {
|
|
161
|
+
nextIndex = (length + nextIndex + diff) % length;
|
|
162
|
+
steps -= 1;
|
|
163
|
+
}
|
|
164
|
+
this.currentIndex = nextIndex;
|
|
165
|
+
}
|
|
166
|
+
hostContainsFocus() {
|
|
167
|
+
this.host.addEventListener("focusout", this.handleFocusout);
|
|
168
|
+
this.host.addEventListener("keydown", this.handleKeydown);
|
|
169
|
+
this.focused = true;
|
|
170
|
+
}
|
|
171
|
+
hostNoLongerContainsFocus() {
|
|
172
|
+
this.host.addEventListener("focusin", this.handleFocusin);
|
|
173
|
+
this.host.removeEventListener("focusout", this.handleFocusout);
|
|
174
|
+
this.host.removeEventListener("keydown", this.handleKeydown);
|
|
175
|
+
this.currentIndex = this.focusInIndex;
|
|
176
|
+
this.focused = false;
|
|
177
|
+
}
|
|
178
|
+
isRelatedTargetAnElement(event) {
|
|
179
|
+
const relatedTarget = event.relatedTarget;
|
|
180
|
+
return !this.elements.includes(relatedTarget);
|
|
181
|
+
}
|
|
182
|
+
acceptsEventCode(code) {
|
|
183
|
+
if (code === "End" || code === "Home") {
|
|
184
|
+
return true;
|
|
185
|
+
}
|
|
186
|
+
switch (this.direction) {
|
|
187
|
+
case "horizontal":
|
|
188
|
+
return code === "ArrowLeft" || code === "ArrowRight";
|
|
189
|
+
case "vertical":
|
|
190
|
+
return code === "ArrowUp" || code === "ArrowDown";
|
|
191
|
+
case "both":
|
|
192
|
+
case "grid":
|
|
193
|
+
return code.startsWith("Arrow");
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
manage() {
|
|
197
|
+
this.addEventListeners();
|
|
198
|
+
}
|
|
199
|
+
unmanage() {
|
|
200
|
+
this.removeEventListeners();
|
|
201
|
+
}
|
|
202
|
+
addEventListeners() {
|
|
203
|
+
this.host.addEventListener("focusin", this.handleFocusin);
|
|
204
|
+
}
|
|
205
|
+
removeEventListeners() {
|
|
206
|
+
this.host.removeEventListener("focusin", this.handleFocusin);
|
|
207
|
+
this.host.removeEventListener("focusout", this.handleFocusout);
|
|
208
|
+
this.host.removeEventListener("keydown", this.handleKeydown);
|
|
209
|
+
}
|
|
210
|
+
hostConnected() {
|
|
211
|
+
this.addEventListeners();
|
|
212
|
+
}
|
|
213
|
+
hostDisconnected() {
|
|
214
|
+
this.removeEventListeners();
|
|
215
|
+
}
|
|
227
216
|
}
|
|
228
|
-
//# sourceMappingURL=FocusGroup.js.map
|
|
217
|
+
//# sourceMappingURL=FocusGroup.js.map
|
package/src/FocusGroup.js.map
CHANGED
|
@@ -1 +1,7 @@
|
|
|
1
|
-
{"version":3,"file":"FocusGroup.js","sourceRoot":"","sources":["FocusGroup.ts"],"names":[],"mappings":"AAuBA,MAAM,OAAO,oBAAoB;IA6E7B,YACI,IAAqB,EACrB,EACI,SAAS,EACT,kBAAkB,EAClB,QAAQ,EACR,YAAY,EACZ,kBAAkB,EAClB,aAAa,MACQ,EAAE,QAAQ,EAAE,GAAG,EAAE,CAAC,EAAE,EAAE;QAtE3C,kBAAa,GAAG,CAAC,CAAC,CAAC;QAM3B,eAAU,GAAG,GAAmB,EAAE,CAAC,MAAM,CAAC;QAEnC,oBAAe,GAAG,CAAC,CAAC;QAE3B,uBAAkB,GAAG,CAAC,GAAM,EAAQ,EAAE;YAClC,OAAO;QACX,CAAC,CAAC;QAoBM,aAAQ,GAAG,KAAK,CAAC;QAUzB,6DAA6D;QAC7D,kBAAa,GAAG,CAAC,SAAc,EAAU,EAAE,CAAC,CAAC,CAAC;QAI9C,6DAA6D;QAC7D,uBAAkB,GAAG,CAAC,GAAM,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AI,CAAC,gBAAgB,CAAC,SAAS,EAAE,IAAI,CAAC,aAAa,CAAC,CAAC;IAC9D,CAAC;IAED,oBAAoB;QAChB,IAAI,CAAC,IAAI,CAAC,mBAAmB,CAAC,SAAS,EAAE,IAAI,CAAC,aAAa,CAAC,CAAC;QAC7D,IAAI,CAAC,IAAI,CAAC,mBAAmB,CAAC,UAAU,EAAE,IAAI,CAAC,cAAc,CAAC,CAAC;QAC/D,IAAI,CAAC,IAAI,CAAC,mBAAmB,CAAC,SAAS,EAAE,IAAI,CAAC,aAAa,CAAC,CAAC;IACjE,CAAC;IAED,aAAa;QACT,IAAI,CAAC,iBAAiB,EAAE,CAAC;IAC7B,CAAC;IAED,gBAAgB;QACZ,IAAI,CAAC,oBAAoB,EAAE,CAAC;IAChC,CAAC;CACJ","sourcesContent":["/*\nCopyright 2020 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\nimport type { ReactiveController, ReactiveElement } from 'lit';\n\ntype DirectionTypes = 'horizontal' | 'vertical' | 'both' | 'grid';\nexport type FocusGroupConfig<T> = {\n focusInIndex?: (_elements: T[]) => number;\n direction?: DirectionTypes | (() => DirectionTypes);\n elementEnterAction?: (el: T) => void;\n elements: () => T[];\n isFocusableElement?: (el: T) => boolean;\n listenerScope?: HTMLElement | (() => HTMLElement);\n};\n\nexport class FocusGroupController<T extends HTMLElement>\n implements ReactiveController\n{\n protected cachedElements?: T[];\n\n get currentIndex(): number {\n if (this._currentIndex === -1) {\n this._currentIndex = this.focusInIndex;\n }\n return this._currentIndex - this.offset;\n }\n\n set currentIndex(currentIndex) {\n this._currentIndex = currentIndex + this.offset;\n }\n\n private _currentIndex = -1;\n\n get direction(): DirectionTypes {\n return this._direction();\n }\n\n _direction = (): DirectionTypes => 'both';\n\n public directionLength = 5;\n\n elementEnterAction = (_el: T): void => {\n return;\n };\n\n get elements(): T[] {\n if (!this.cachedElements) {\n this.cachedElements = this._elements();\n }\n return this.cachedElements;\n }\n\n private _elements!: () => T[];\n\n protected set focused(focused: boolean) {\n if (focused === this.focused) return;\n this._focused = focused;\n }\n\n protected get focused(): boolean {\n return this._focused;\n }\n\n private _focused = false;\n\n get focusInElement(): T {\n return this.elements[this.focusInIndex];\n }\n\n get focusInIndex(): number {\n return this._focusInIndex(this.elements);\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n _focusInIndex = (_elements: T[]): number => 0;\n\n host: ReactiveElement;\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n isFocusableElement = (_el: T): boolean => true;\n\n isEventWithinListenerScope(event: Event): boolean {\n if (this._listenerScope() === this.host) return true;\n return event.composedPath().includes(this._listenerScope());\n }\n\n _listenerScope = (): HTMLElement => this.host;\n\n // When elements are virtualized, the delta between the first element\n // and the first rendered element.\n offset = 0;\n\n constructor(\n host: ReactiveElement,\n {\n direction,\n elementEnterAction,\n elements,\n focusInIndex,\n isFocusableElement,\n listenerScope,\n }: FocusGroupConfig<T> = { elements: () => [] }\n ) {\n this.host = host;\n this.host.addController(this);\n this._elements = elements;\n this.isFocusableElement = isFocusableElement || this.isFocusableElement;\n // @TODO: abstract a method to simplify the conditional wrapping of the values as functions.\n if (typeof direction === 'string') {\n this._direction = () => direction;\n } else if (typeof direction === 'function') {\n this._direction = direction;\n }\n this.elementEnterAction = elementEnterAction || this.elementEnterAction;\n if (typeof focusInIndex === 'number') {\n this._focusInIndex = () => focusInIndex;\n } else if (typeof focusInIndex === 'function') {\n this._focusInIndex = focusInIndex;\n }\n if (typeof listenerScope === 'object') {\n this._listenerScope = () => listenerScope;\n } else if (typeof listenerScope === 'function') {\n this._listenerScope = listenerScope;\n }\n }\n\n update({ elements }: FocusGroupConfig<T> = { elements: () => [] }): void {\n this.unmanage();\n this._elements = elements;\n this.clearElementCache();\n this.manage();\n }\n\n focus(options?: FocusOptions): void {\n let focusElement = this.elements[this.currentIndex];\n if (!focusElement || !this.isFocusableElement(focusElement)) {\n this.setCurrentIndexCircularly(1);\n focusElement = this.elements[this.currentIndex];\n }\n if (focusElement && this.isFocusableElement(focusElement)) {\n focusElement.focus(options);\n }\n }\n\n clearElementCache(offset = 0): void {\n delete this.cachedElements;\n this.offset = offset;\n }\n\n setCurrentIndexCircularly(diff: number): void {\n const { length } = this.elements;\n let steps = length;\n // start at a possibly not 0 index\n let nextIndex = (length + this.currentIndex + diff) % length;\n while (\n // don't cycle the elements more than once\n steps &&\n this.elements[nextIndex] &&\n !this.isFocusableElement(this.elements[nextIndex])\n ) {\n nextIndex = (length + nextIndex + diff) % length;\n steps -= 1;\n }\n this.currentIndex = nextIndex;\n }\n\n hostContainsFocus(): void {\n this.host.addEventListener('focusout', this.handleFocusout);\n this.host.addEventListener('keydown', this.handleKeydown);\n this.focused = true;\n }\n\n hostNoLongerContainsFocus(): void {\n this.host.addEventListener('focusin', this.handleFocusin);\n this.host.removeEventListener('focusout', this.handleFocusout);\n this.host.removeEventListener('keydown', this.handleKeydown);\n this.currentIndex = this.focusInIndex;\n this.focused = false;\n }\n\n isRelatedTargetAnElement(event: FocusEvent): boolean {\n const relatedTarget = event.relatedTarget as null | Element;\n return !this.elements.includes(relatedTarget as T);\n }\n\n handleFocusin = (event: FocusEvent): void => {\n if (!this.isEventWithinListenerScope(event)) return;\n if (this.isRelatedTargetAnElement(event)) {\n this.hostContainsFocus();\n }\n const path = event.composedPath() as T[];\n let targetIndex = -1;\n path.find((el) => {\n targetIndex = this.elements.indexOf(el);\n return targetIndex !== -1;\n });\n this.currentIndex = targetIndex > -1 ? targetIndex : this.currentIndex;\n };\n\n handleFocusout = (event: FocusEvent): void => {\n if (this.isRelatedTargetAnElement(event)) {\n this.hostNoLongerContainsFocus();\n }\n };\n\n acceptsEventCode(code: string): boolean {\n if (code === 'End' || code === 'Home') {\n return true;\n }\n switch (this.direction) {\n case 'horizontal':\n return code === 'ArrowLeft' || code === 'ArrowRight';\n case 'vertical':\n return code === 'ArrowUp' || code === 'ArrowDown';\n case 'both':\n case 'grid':\n return code.startsWith('Arrow');\n }\n }\n\n handleKeydown = (event: KeyboardEvent): void => {\n if (!this.acceptsEventCode(event.code) || event.defaultPrevented) {\n return;\n }\n let diff = 0;\n switch (event.code) {\n case 'ArrowRight':\n diff += 1;\n break;\n case 'ArrowDown':\n diff += this.direction === 'grid' ? this.directionLength : 1;\n break;\n case 'ArrowLeft':\n diff -= 1;\n break;\n case 'ArrowUp':\n diff -= this.direction === 'grid' ? this.directionLength : 1;\n break;\n case 'End':\n this.currentIndex = 0;\n diff -= 1;\n break;\n case 'Home':\n this.currentIndex = this.elements.length - 1;\n diff += 1;\n break;\n }\n event.preventDefault();\n if (this.direction === 'grid' && this.currentIndex + diff < 0) {\n this.currentIndex = 0;\n } else if (\n this.direction === 'grid' &&\n this.currentIndex + diff > this.elements.length - 1\n ) {\n this.currentIndex = this.elements.length - 1;\n } else {\n this.setCurrentIndexCircularly(diff);\n }\n // To allow the `focusInIndex` to be calculated with the \"after\" state of the keyboard interaction\n // do `elementEnterAction` _before_ focusing the next element.\n this.elementEnterAction(this.elements[this.currentIndex]);\n this.focus();\n };\n\n manage(): void {\n this.addEventListeners();\n }\n\n unmanage(): void {\n this.removeEventListeners();\n }\n\n addEventListeners(): void {\n this.host.addEventListener('focusin', this.handleFocusin);\n }\n\n removeEventListeners(): void {\n this.host.removeEventListener('focusin', this.handleFocusin);\n this.host.removeEventListener('focusout', this.handleFocusout);\n this.host.removeEventListener('keydown', this.handleKeydown);\n }\n\n hostConnected(): void {\n this.addEventListeners();\n }\n\n hostDisconnected(): void {\n this.removeEventListeners();\n }\n}\n"]}
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["FocusGroup.ts"],
|
|
4
|
+
"sourcesContent": ["/*\nCopyright 2020 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\nimport type { ReactiveController, ReactiveElement } from 'lit';\n\ntype DirectionTypes = 'horizontal' | 'vertical' | 'both' | 'grid';\nexport type FocusGroupConfig<T> = {\n focusInIndex?: (_elements: T[]) => number;\n direction?: DirectionTypes | (() => DirectionTypes);\n elementEnterAction?: (el: T) => void;\n elements: () => T[];\n isFocusableElement?: (el: T) => boolean;\n listenerScope?: HTMLElement | (() => HTMLElement);\n};\n\nexport class FocusGroupController<T extends HTMLElement>\n implements ReactiveController\n{\n protected cachedElements?: T[];\n\n get currentIndex(): number {\n if (this._currentIndex === -1) {\n this._currentIndex = this.focusInIndex;\n }\n return this._currentIndex - this.offset;\n }\n\n set currentIndex(currentIndex) {\n this._currentIndex = currentIndex + this.offset;\n }\n\n private _currentIndex = -1;\n\n get direction(): DirectionTypes {\n return this._direction();\n }\n\n _direction = (): DirectionTypes => 'both';\n\n public directionLength = 5;\n\n elementEnterAction = (_el: T): void => {\n return;\n };\n\n get elements(): T[] {\n if (!this.cachedElements) {\n this.cachedElements = this._elements();\n }\n return this.cachedElements;\n }\n\n private _elements!: () => T[];\n\n protected set focused(focused: boolean) {\n if (focused === this.focused) return;\n this._focused = focused;\n }\n\n protected get focused(): boolean {\n return this._focused;\n }\n\n private _focused = false;\n\n get focusInElement(): T {\n return this.elements[this.focusInIndex];\n }\n\n get focusInIndex(): number {\n return this._focusInIndex(this.elements);\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n _focusInIndex = (_elements: T[]): number => 0;\n\n host: ReactiveElement;\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n isFocusableElement = (_el: T): boolean => true;\n\n isEventWithinListenerScope(event: Event): boolean {\n if (this._listenerScope() === this.host) return true;\n return event.composedPath().includes(this._listenerScope());\n }\n\n _listenerScope = (): HTMLElement => this.host;\n\n // When elements are virtualized, the delta between the first element\n // and the first rendered element.\n offset = 0;\n\n constructor(\n host: ReactiveElement,\n {\n direction,\n elementEnterAction,\n elements,\n focusInIndex,\n isFocusableElement,\n listenerScope,\n }: FocusGroupConfig<T> = { elements: () => [] }\n ) {\n this.host = host;\n this.host.addController(this);\n this._elements = elements;\n this.isFocusableElement = isFocusableElement || this.isFocusableElement;\n // @TODO: abstract a method to simplify the conditional wrapping of the values as functions.\n if (typeof direction === 'string') {\n this._direction = () => direction;\n } else if (typeof direction === 'function') {\n this._direction = direction;\n }\n this.elementEnterAction = elementEnterAction || this.elementEnterAction;\n if (typeof focusInIndex === 'number') {\n this._focusInIndex = () => focusInIndex;\n } else if (typeof focusInIndex === 'function') {\n this._focusInIndex = focusInIndex;\n }\n if (typeof listenerScope === 'object') {\n this._listenerScope = () => listenerScope;\n } else if (typeof listenerScope === 'function') {\n this._listenerScope = listenerScope;\n }\n }\n\n update({ elements }: FocusGroupConfig<T> = { elements: () => [] }): void {\n this.unmanage();\n this._elements = elements;\n this.clearElementCache();\n this.manage();\n }\n\n focus(options?: FocusOptions): void {\n let focusElement = this.elements[this.currentIndex];\n if (!focusElement || !this.isFocusableElement(focusElement)) {\n this.setCurrentIndexCircularly(1);\n focusElement = this.elements[this.currentIndex];\n }\n if (focusElement && this.isFocusableElement(focusElement)) {\n focusElement.focus(options);\n }\n }\n\n clearElementCache(offset = 0): void {\n delete this.cachedElements;\n this.offset = offset;\n }\n\n setCurrentIndexCircularly(diff: number): void {\n const { length } = this.elements;\n let steps = length;\n // start at a possibly not 0 index\n let nextIndex = (length + this.currentIndex + diff) % length;\n while (\n // don't cycle the elements more than once\n steps &&\n this.elements[nextIndex] &&\n !this.isFocusableElement(this.elements[nextIndex])\n ) {\n nextIndex = (length + nextIndex + diff) % length;\n steps -= 1;\n }\n this.currentIndex = nextIndex;\n }\n\n hostContainsFocus(): void {\n this.host.addEventListener('focusout', this.handleFocusout);\n this.host.addEventListener('keydown', this.handleKeydown);\n this.focused = true;\n }\n\n hostNoLongerContainsFocus(): void {\n this.host.addEventListener('focusin', this.handleFocusin);\n this.host.removeEventListener('focusout', this.handleFocusout);\n this.host.removeEventListener('keydown', this.handleKeydown);\n this.currentIndex = this.focusInIndex;\n this.focused = false;\n }\n\n isRelatedTargetAnElement(event: FocusEvent): boolean {\n const relatedTarget = event.relatedTarget as null | Element;\n return !this.elements.includes(relatedTarget as T);\n }\n\n handleFocusin = (event: FocusEvent): void => {\n if (!this.isEventWithinListenerScope(event)) return;\n if (this.isRelatedTargetAnElement(event)) {\n this.hostContainsFocus();\n }\n const path = event.composedPath() as T[];\n let targetIndex = -1;\n path.find((el) => {\n targetIndex = this.elements.indexOf(el);\n return targetIndex !== -1;\n });\n this.currentIndex = targetIndex > -1 ? targetIndex : this.currentIndex;\n };\n\n handleFocusout = (event: FocusEvent): void => {\n if (this.isRelatedTargetAnElement(event)) {\n this.hostNoLongerContainsFocus();\n }\n };\n\n acceptsEventCode(code: string): boolean {\n if (code === 'End' || code === 'Home') {\n return true;\n }\n switch (this.direction) {\n case 'horizontal':\n return code === 'ArrowLeft' || code === 'ArrowRight';\n case 'vertical':\n return code === 'ArrowUp' || code === 'ArrowDown';\n case 'both':\n case 'grid':\n return code.startsWith('Arrow');\n }\n }\n\n handleKeydown = (event: KeyboardEvent): void => {\n if (!this.acceptsEventCode(event.code) || event.defaultPrevented) {\n return;\n }\n let diff = 0;\n switch (event.code) {\n case 'ArrowRight':\n diff += 1;\n break;\n case 'ArrowDown':\n diff += this.direction === 'grid' ? this.directionLength : 1;\n break;\n case 'ArrowLeft':\n diff -= 1;\n break;\n case 'ArrowUp':\n diff -= this.direction === 'grid' ? this.directionLength : 1;\n break;\n case 'End':\n this.currentIndex = 0;\n diff -= 1;\n break;\n case 'Home':\n this.currentIndex = this.elements.length - 1;\n diff += 1;\n break;\n }\n event.preventDefault();\n if (this.direction === 'grid' && this.currentIndex + diff < 0) {\n this.currentIndex = 0;\n } else if (\n this.direction === 'grid' &&\n this.currentIndex + diff > this.elements.length - 1\n ) {\n this.currentIndex = this.elements.length - 1;\n } else {\n this.setCurrentIndexCircularly(diff);\n }\n // To allow the `focusInIndex` to be calculated with the \"after\" state of the keyboard interaction\n // do `elementEnterAction` _before_ focusing the next element.\n this.elementEnterAction(this.elements[this.currentIndex]);\n this.focus();\n };\n\n manage(): void {\n this.addEventListeners();\n }\n\n unmanage(): void {\n this.removeEventListeners();\n }\n\n addEventListeners(): void {\n this.host.addEventListener('focusin', this.handleFocusin);\n }\n\n removeEventListeners(): void {\n this.host.removeEventListener('focusin', this.handleFocusin);\n this.host.removeEventListener('focusout', this.handleFocusout);\n this.host.removeEventListener('keydown', this.handleKeydown);\n }\n\n hostConnected(): void {\n this.addEventListeners();\n }\n\n hostDisconnected(): void {\n this.removeEventListeners();\n }\n}\n"],
|
|
5
|
+
"mappings": "AAuBO,aAAM,qBAEb;AAAA,EA2EI,YACI,MACA;AAAA,IACI;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,MACqB,EAAE,UAAU,MAAM,CAAC,EAAE,GAChD;AAvEM,yBAAgB;AAMxB,sBAAa,MAAsB;AAE5B,2BAAkB;AAEzB,8BAAqB,CAAC,QAAiB;AACnC;AAAA,IACJ;AAoBQ,oBAAW;AAWnB,yBAAgB,CAAC,cAA2B;AAK5C,8BAAqB,CAAC,QAAoB;AAO1C,0BAAiB,MAAmB,KAAK;AAIzC,kBAAS;AA+FT,yBAAgB,CAAC,UAA4B;AACzC,UAAI,CAAC,KAAK,2BAA2B,KAAK;AAAG;AAC7C,UAAI,KAAK,yBAAyB,KAAK,GAAG;AACtC,aAAK,kB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,KAAK,SAAS,SAAS,aAAkB;AAAA,EACrD;AAAA,EAsBA,iBAAiB,MAAuB;AACpC,QAAI,SAAS,SAAS,SAAS,QAAQ;AACnC,aAAO;AAAA,IACX;AACA,YAAQ,KAAK;AAAA,WACJ;AACD,eAAO,SAAS,eAAe,SAAS;AAAA,WACvC;AACD,eAAO,SAAS,aAAa,SAAS;AAAA,WACrC;AAAA,WACA;AACD,eAAO,KAAK,WAAW,OAAO;AAAA;AAAA,EAE1C;AAAA,EA8CA,SAAe;AACX,SAAK,kBAAkB;AAAA,EAC3B;AAAA,EAEA,WAAiB;AACb,SAAK,qBAAqB;AAAA,EAC9B;AAAA,EAEA,oBAA0B;AACtB,SAAK,KAAK,iBAAiB,WAAW,KAAK,aAAa;AAAA,EAC5D;AAAA,EAEA,uBAA6B;AACzB,SAAK,KAAK,oBAAoB,WAAW,KAAK,aAAa;AAC3D,SAAK,KAAK,oBAAoB,YAAY,KAAK,cAAc;AAC7D,SAAK,KAAK,oBAAoB,WAAW,KAAK,aAAa;AAAA,EAC/D;AAAA,EAEA,gBAAsB;AAClB,SAAK,kBAAkB;AAAA,EAC3B;AAAA,EAEA,mBAAyB;AACrB,SAAK,qBAAqB;AAAA,EAC9B;AACJ;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export const DARK_MODE = "(prefers-color-scheme: dark)";
|
|
2
|
+
export const IS_MOBILE = "(max-width: 700px) and (hover: none) and (pointer: coarse), (max-height: 700px) and (hover: none) and (pointer: coarse)";
|
|
3
|
+
export class MatchMediaController {
|
|
4
|
+
constructor(host, query) {
|
|
5
|
+
this.key = Symbol("match-media-key");
|
|
6
|
+
this.matches = false;
|
|
7
|
+
this.host = host;
|
|
8
|
+
this.media = window.matchMedia(query);
|
|
9
|
+
this.matches = this.media.matches;
|
|
10
|
+
this.onChange = this.onChange.bind(this);
|
|
11
|
+
host.addController(this);
|
|
12
|
+
}
|
|
13
|
+
hostConnected() {
|
|
14
|
+
this.media.addEventListener("change", this.onChange);
|
|
15
|
+
}
|
|
16
|
+
hostDisconnected() {
|
|
17
|
+
this.media.removeEventListener("change", this.onChange);
|
|
18
|
+
}
|
|
19
|
+
onChange(event) {
|
|
20
|
+
if (this.matches === event.matches)
|
|
21
|
+
return;
|
|
22
|
+
this.matches = event.matches;
|
|
23
|
+
this.host.requestUpdate(this.key, !this.matches);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=MatchMedia.dev.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["MatchMedia.ts"],
|
|
4
|
+
"sourcesContent": ["/*\nCopyright 2020 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\nimport type { ReactiveController, ReactiveElement } from 'lit';\n\nexport const DARK_MODE = '(prefers-color-scheme: dark)';\nexport const IS_MOBILE =\n '(max-width: 700px) and (hover: none) and (pointer: coarse), (max-height: 700px) and (hover: none) and (pointer: coarse)';\n\nexport class MatchMediaController implements ReactiveController {\n key = Symbol('match-media-key');\n\n matches = false;\n\n protected host: ReactiveElement;\n\n protected media: MediaQueryList;\n\n constructor(host: ReactiveElement, query: string) {\n this.host = host;\n this.media = window.matchMedia(query);\n this.matches = this.media.matches;\n this.onChange = this.onChange.bind(this);\n host.addController(this);\n }\n\n public hostConnected(): void {\n this.media.addEventListener('change', this.onChange);\n }\n\n public hostDisconnected(): void {\n this.media.removeEventListener('change', this.onChange);\n }\n\n protected onChange(event: MediaQueryListEvent): void {\n if (this.matches === event.matches) return;\n this.matches = event.matches;\n this.host.requestUpdate(this.key, !this.matches);\n }\n}\n"],
|
|
5
|
+
"mappings": "AAaO,aAAM,YAAY;AAClB,aAAM,YACT;AAEG,aAAM,qBAAmD;AAAA,EAS5D,YAAY,MAAuB,OAAe;AARlD,eAAM,OAAO,iBAAiB;AAE9B,mBAAU;AAON,SAAK,OAAO;AACZ,SAAK,QAAQ,OAAO,WAAW,KAAK;AACpC,SAAK,UAAU,KAAK,MAAM;AAC1B,SAAK,WAAW,KAAK,SAAS,KAAK,IAAI;AACvC,SAAK,cAAc,IAAI;AAAA,EAC3B;AAAA,EAEO,gBAAsB;AACzB,SAAK,MAAM,iBAAiB,UAAU,KAAK,QAAQ;AAAA,EACvD;AAAA,EAEO,mBAAyB;AAC5B,SAAK,MAAM,oBAAoB,UAAU,KAAK,QAAQ;AAAA,EAC1D;AAAA,EAEU,SAAS,OAAkC;AACjD,QAAI,KAAK,YAAY,MAAM;AAAS;AACpC,SAAK,UAAU,MAAM;AACrB,SAAK,KAAK,cAAc,KAAK,KAAK,CAAC,KAAK,OAAO;AAAA,EACnD;AACJ;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/src/MatchMedia.js
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
export const DARK_MODE =
|
|
2
|
-
export const IS_MOBILE =
|
|
1
|
+
export const DARK_MODE = "(prefers-color-scheme: dark)";
|
|
2
|
+
export const IS_MOBILE = "(max-width: 700px) and (hover: none) and (pointer: coarse), (max-height: 700px) and (hover: none) and (pointer: coarse)";
|
|
3
3
|
export class MatchMediaController {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
4
|
+
constructor(host, query) {
|
|
5
|
+
this.key = Symbol("match-media-key");
|
|
6
|
+
this.matches = false;
|
|
7
|
+
this.host = host;
|
|
8
|
+
this.media = window.matchMedia(query);
|
|
9
|
+
this.matches = this.media.matches;
|
|
10
|
+
this.onChange = this.onChange.bind(this);
|
|
11
|
+
host.addController(this);
|
|
12
|
+
}
|
|
13
|
+
hostConnected() {
|
|
14
|
+
this.media.addEventListener("change", this.onChange);
|
|
15
|
+
}
|
|
16
|
+
hostDisconnected() {
|
|
17
|
+
this.media.removeEventListener("change", this.onChange);
|
|
18
|
+
}
|
|
19
|
+
onChange(event) {
|
|
20
|
+
if (this.matches === event.matches)
|
|
21
|
+
return;
|
|
22
|
+
this.matches = event.matches;
|
|
23
|
+
this.host.requestUpdate(this.key, !this.matches);
|
|
24
|
+
}
|
|
25
25
|
}
|
|
26
|
-
//# sourceMappingURL=MatchMedia.js.map
|
|
26
|
+
//# sourceMappingURL=MatchMedia.js.map
|