@spectrum-web-components/reactive-controllers 0.2.1 → 0.2.4
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 +2 -2
- package/src/FocusGroup.d.ts +52 -0
- package/src/FocusGroup.js +228 -0
- package/src/FocusGroup.js.map +1 -0
- package/src/RovingTabindex.d.ts +5 -48
- package/src/RovingTabindex.js +21 -210
- package/src/RovingTabindex.js.map +1 -1
- package/test/match-media.test.js +32 -0
- package/test/match-media.test.js.map +1 -0
- package/test/roving-tabindex-integration.test.js +137 -0
- package/test/roving-tabindex-integration.test.js.map +1 -0
- package/test/roving-tabindex.test.js +27 -0
- package/test/roving-tabindex.test.js.map +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spectrum-web-components/reactive-controllers",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -47,5 +47,5 @@
|
|
|
47
47
|
},
|
|
48
48
|
"types": "./src/index.d.ts",
|
|
49
49
|
"customElements": "custom-elements.json",
|
|
50
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "275ee61b152ac3eed0cd1603d8eab2aec90876a0"
|
|
51
51
|
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { ReactiveController, ReactiveElement } from 'lit';
|
|
2
|
+
declare type DirectionTypes = 'horizontal' | 'vertical' | 'both' | 'grid';
|
|
3
|
+
export declare type FocusGroupConfig<T> = {
|
|
4
|
+
focusInIndex?: (_elements: T[]) => number;
|
|
5
|
+
direction?: DirectionTypes | (() => DirectionTypes);
|
|
6
|
+
elementEnterAction?: (el: T) => void;
|
|
7
|
+
elements: () => T[];
|
|
8
|
+
isFocusableElement?: (el: T) => boolean;
|
|
9
|
+
listenerScope?: HTMLElement | (() => HTMLElement);
|
|
10
|
+
};
|
|
11
|
+
export declare class FocusGroupController<T extends HTMLElement> implements ReactiveController {
|
|
12
|
+
protected cachedElements?: T[];
|
|
13
|
+
get currentIndex(): number;
|
|
14
|
+
set currentIndex(currentIndex: number);
|
|
15
|
+
private _currentIndex;
|
|
16
|
+
get direction(): DirectionTypes;
|
|
17
|
+
_direction: () => DirectionTypes;
|
|
18
|
+
directionLength: number;
|
|
19
|
+
elementEnterAction: (_el: T) => void;
|
|
20
|
+
get elements(): T[];
|
|
21
|
+
private _elements;
|
|
22
|
+
protected set focused(focused: boolean);
|
|
23
|
+
protected get focused(): boolean;
|
|
24
|
+
private _focused;
|
|
25
|
+
get focusInElement(): T;
|
|
26
|
+
get focusInIndex(): number;
|
|
27
|
+
_focusInIndex: (_elements: T[]) => number;
|
|
28
|
+
host: ReactiveElement;
|
|
29
|
+
isFocusableElement: (_el: T) => boolean;
|
|
30
|
+
isEventWithinListenerScope(event: Event): boolean;
|
|
31
|
+
_listenerScope: () => HTMLElement;
|
|
32
|
+
offset: number;
|
|
33
|
+
constructor(host: ReactiveElement, { direction, elementEnterAction, elements, focusInIndex, isFocusableElement, listenerScope, }?: FocusGroupConfig<T>);
|
|
34
|
+
update({ elements }?: FocusGroupConfig<T>): void;
|
|
35
|
+
focus(options?: FocusOptions): void;
|
|
36
|
+
clearElementCache(offset?: number): void;
|
|
37
|
+
setCurrentIndexCircularly(diff: number): void;
|
|
38
|
+
hostContainsFocus(): void;
|
|
39
|
+
hostNoLongerContainsFocus(): void;
|
|
40
|
+
isRelatedTargetAnElement(event: FocusEvent): boolean;
|
|
41
|
+
handleFocusin: (event: FocusEvent) => void;
|
|
42
|
+
handleFocusout: (event: FocusEvent) => void;
|
|
43
|
+
acceptsEventCode(code: string): boolean;
|
|
44
|
+
handleKeydown: (event: KeyboardEvent) => void;
|
|
45
|
+
manage(): void;
|
|
46
|
+
unmanage(): void;
|
|
47
|
+
addEventListeners(): void;
|
|
48
|
+
removeEventListeners(): void;
|
|
49
|
+
hostConnected(): void;
|
|
50
|
+
hostDisconnected(): void;
|
|
51
|
+
}
|
|
52
|
+
export {};
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
export class FocusGroupController {
|
|
2
|
+
constructor(host, { direction, elementEnterAction, elements, focusInIndex, isFocusableElement, listenerScope, } = { elements: () => [] }) {
|
|
3
|
+
this._currentIndex = -1;
|
|
4
|
+
this._direction = () => 'both';
|
|
5
|
+
this.directionLength = 5;
|
|
6
|
+
this.elementEnterAction = (_el) => {
|
|
7
|
+
return;
|
|
8
|
+
};
|
|
9
|
+
this._focused = false;
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
11
|
+
this._focusInIndex = (_elements) => 0;
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
13
|
+
this.isFocusableElement = (_el) => true;
|
|
14
|
+
this._listenerScope = () => this.host;
|
|
15
|
+
// When elements are virtualized, the delta between the first element
|
|
16
|
+
// and the first rendered element.
|
|
17
|
+
this.offset = 0;
|
|
18
|
+
this.handleFocusin = (event) => {
|
|
19
|
+
if (!this.isEventWithinListenerScope(event))
|
|
20
|
+
return;
|
|
21
|
+
if (this.isRelatedTargetAnElement(event)) {
|
|
22
|
+
this.hostContainsFocus();
|
|
23
|
+
}
|
|
24
|
+
const path = event.composedPath();
|
|
25
|
+
let targetIndex = -1;
|
|
26
|
+
path.find((el) => {
|
|
27
|
+
targetIndex = this.elements.indexOf(el);
|
|
28
|
+
return targetIndex !== -1;
|
|
29
|
+
});
|
|
30
|
+
this.currentIndex = targetIndex > -1 ? targetIndex : this.currentIndex;
|
|
31
|
+
};
|
|
32
|
+
this.handleFocusout = (event) => {
|
|
33
|
+
if (this.isRelatedTargetAnElement(event)) {
|
|
34
|
+
this.hostNoLongerContainsFocus();
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
this.handleKeydown = (event) => {
|
|
38
|
+
if (!this.acceptsEventCode(event.code) || event.defaultPrevented) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
let diff = 0;
|
|
42
|
+
switch (event.code) {
|
|
43
|
+
case 'ArrowRight':
|
|
44
|
+
diff += 1;
|
|
45
|
+
break;
|
|
46
|
+
case 'ArrowDown':
|
|
47
|
+
diff += this.direction === 'grid' ? this.directionLength : 1;
|
|
48
|
+
break;
|
|
49
|
+
case 'ArrowLeft':
|
|
50
|
+
diff -= 1;
|
|
51
|
+
break;
|
|
52
|
+
case 'ArrowUp':
|
|
53
|
+
diff -= this.direction === 'grid' ? this.directionLength : 1;
|
|
54
|
+
break;
|
|
55
|
+
case 'End':
|
|
56
|
+
this.currentIndex = 0;
|
|
57
|
+
diff -= 1;
|
|
58
|
+
break;
|
|
59
|
+
case 'Home':
|
|
60
|
+
this.currentIndex = this.elements.length - 1;
|
|
61
|
+
diff += 1;
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
event.preventDefault();
|
|
65
|
+
if (this.direction === 'grid' && this.currentIndex + diff < 0) {
|
|
66
|
+
this.currentIndex = 0;
|
|
67
|
+
}
|
|
68
|
+
else if (this.direction === 'grid' &&
|
|
69
|
+
this.currentIndex + diff > this.elements.length - 1) {
|
|
70
|
+
this.currentIndex = this.elements.length - 1;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
this.setCurrentIndexCircularly(diff);
|
|
74
|
+
}
|
|
75
|
+
// To allow the `focusInIndex` to be calculated with the "after" state of the keyboard interaction
|
|
76
|
+
// do `elementEnterAction` _before_ focusing the next element.
|
|
77
|
+
this.elementEnterAction(this.elements[this.currentIndex]);
|
|
78
|
+
this.focus();
|
|
79
|
+
};
|
|
80
|
+
this.host = host;
|
|
81
|
+
this.host.addController(this);
|
|
82
|
+
this._elements = elements;
|
|
83
|
+
this.isFocusableElement = isFocusableElement || this.isFocusableElement;
|
|
84
|
+
// @TODO: abstract a method to simplify the conditional wrapping of the values as functions.
|
|
85
|
+
if (typeof direction === 'string') {
|
|
86
|
+
this._direction = () => direction;
|
|
87
|
+
}
|
|
88
|
+
else if (typeof direction === 'function') {
|
|
89
|
+
this._direction = direction;
|
|
90
|
+
}
|
|
91
|
+
this.elementEnterAction = elementEnterAction || this.elementEnterAction;
|
|
92
|
+
if (typeof focusInIndex === 'number') {
|
|
93
|
+
this._focusInIndex = () => focusInIndex;
|
|
94
|
+
}
|
|
95
|
+
else if (typeof focusInIndex === 'function') {
|
|
96
|
+
this._focusInIndex = focusInIndex;
|
|
97
|
+
}
|
|
98
|
+
if (typeof listenerScope === 'object') {
|
|
99
|
+
this._listenerScope = () => listenerScope;
|
|
100
|
+
}
|
|
101
|
+
else if (typeof listenerScope === 'function') {
|
|
102
|
+
this._listenerScope = listenerScope;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
get currentIndex() {
|
|
106
|
+
if (this._currentIndex === -1) {
|
|
107
|
+
this._currentIndex = this.focusInIndex;
|
|
108
|
+
}
|
|
109
|
+
return this._currentIndex - this.offset;
|
|
110
|
+
}
|
|
111
|
+
set currentIndex(currentIndex) {
|
|
112
|
+
this._currentIndex = currentIndex + this.offset;
|
|
113
|
+
}
|
|
114
|
+
get direction() {
|
|
115
|
+
return this._direction();
|
|
116
|
+
}
|
|
117
|
+
get elements() {
|
|
118
|
+
if (!this.cachedElements) {
|
|
119
|
+
this.cachedElements = this._elements();
|
|
120
|
+
}
|
|
121
|
+
return this.cachedElements;
|
|
122
|
+
}
|
|
123
|
+
set focused(focused) {
|
|
124
|
+
if (focused === this.focused)
|
|
125
|
+
return;
|
|
126
|
+
this._focused = focused;
|
|
127
|
+
}
|
|
128
|
+
get focused() {
|
|
129
|
+
return this._focused;
|
|
130
|
+
}
|
|
131
|
+
get focusInElement() {
|
|
132
|
+
return this.elements[this.focusInIndex];
|
|
133
|
+
}
|
|
134
|
+
get focusInIndex() {
|
|
135
|
+
return this._focusInIndex(this.elements);
|
|
136
|
+
}
|
|
137
|
+
isEventWithinListenerScope(event) {
|
|
138
|
+
if (this._listenerScope() === this.host)
|
|
139
|
+
return true;
|
|
140
|
+
return event.composedPath().includes(this._listenerScope());
|
|
141
|
+
}
|
|
142
|
+
update({ elements } = { elements: () => [] }) {
|
|
143
|
+
this.unmanage();
|
|
144
|
+
this._elements = elements;
|
|
145
|
+
this.clearElementCache();
|
|
146
|
+
this.manage();
|
|
147
|
+
}
|
|
148
|
+
focus(options) {
|
|
149
|
+
let focusElement = this.elements[this.currentIndex];
|
|
150
|
+
if (!focusElement || !this.isFocusableElement(focusElement)) {
|
|
151
|
+
this.setCurrentIndexCircularly(1);
|
|
152
|
+
focusElement = this.elements[this.currentIndex];
|
|
153
|
+
}
|
|
154
|
+
if (focusElement && this.isFocusableElement(focusElement)) {
|
|
155
|
+
focusElement.focus(options);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
clearElementCache(offset = 0) {
|
|
159
|
+
delete this.cachedElements;
|
|
160
|
+
this.offset = offset;
|
|
161
|
+
}
|
|
162
|
+
setCurrentIndexCircularly(diff) {
|
|
163
|
+
const { length } = this.elements;
|
|
164
|
+
let steps = length;
|
|
165
|
+
// start at a possibly not 0 index
|
|
166
|
+
let nextIndex = (length + this.currentIndex + diff) % length;
|
|
167
|
+
while (
|
|
168
|
+
// don't cycle the elements more than once
|
|
169
|
+
steps &&
|
|
170
|
+
this.elements[nextIndex] &&
|
|
171
|
+
!this.isFocusableElement(this.elements[nextIndex])) {
|
|
172
|
+
nextIndex = (length + nextIndex + diff) % length;
|
|
173
|
+
steps -= 1;
|
|
174
|
+
}
|
|
175
|
+
this.currentIndex = nextIndex;
|
|
176
|
+
}
|
|
177
|
+
hostContainsFocus() {
|
|
178
|
+
this.host.addEventListener('focusout', this.handleFocusout);
|
|
179
|
+
this.host.addEventListener('keydown', this.handleKeydown);
|
|
180
|
+
this.focused = true;
|
|
181
|
+
}
|
|
182
|
+
hostNoLongerContainsFocus() {
|
|
183
|
+
this.host.addEventListener('focusin', this.handleFocusin);
|
|
184
|
+
this.host.removeEventListener('focusout', this.handleFocusout);
|
|
185
|
+
this.host.removeEventListener('keydown', this.handleKeydown);
|
|
186
|
+
this.currentIndex = this.focusInIndex;
|
|
187
|
+
this.focused = false;
|
|
188
|
+
}
|
|
189
|
+
isRelatedTargetAnElement(event) {
|
|
190
|
+
const relatedTarget = event.relatedTarget;
|
|
191
|
+
return !this.elements.includes(relatedTarget);
|
|
192
|
+
}
|
|
193
|
+
acceptsEventCode(code) {
|
|
194
|
+
if (code === 'End' || code === 'Home') {
|
|
195
|
+
return true;
|
|
196
|
+
}
|
|
197
|
+
switch (this.direction) {
|
|
198
|
+
case 'horizontal':
|
|
199
|
+
return code === 'ArrowLeft' || code === 'ArrowRight';
|
|
200
|
+
case 'vertical':
|
|
201
|
+
return code === 'ArrowUp' || code === 'ArrowDown';
|
|
202
|
+
case 'both':
|
|
203
|
+
case 'grid':
|
|
204
|
+
return code.startsWith('Arrow');
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
manage() {
|
|
208
|
+
this.addEventListeners();
|
|
209
|
+
}
|
|
210
|
+
unmanage() {
|
|
211
|
+
this.removeEventListeners();
|
|
212
|
+
}
|
|
213
|
+
addEventListeners() {
|
|
214
|
+
this.host.addEventListener('focusin', this.handleFocusin);
|
|
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
|
+
}
|
|
227
|
+
}
|
|
228
|
+
//# sourceMappingURL=FocusGroup.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FocusGroup.js","sourceRoot":"","sources":["FocusGroup.ts"],"names":[],"mappings":"AAuBA,MAAM,OAAO,oBAAoB;IA6E7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AK,MAAM,EAAE;YACnC,OAAO,IAAI,CAAC;SACf;QACD,QAAQ,IAAI,CAAC,SAAS,EAAE;YACpB,KAAK,YAAY;gBACb,OAAO,IAAI,KAAK,WAAW,IAAI,IAAI,KAAK,YAAY,CAAC;YACzD,KAAK,UAAU;gBACX,OAAO,IAAI,KAAK,SAAS,IAAI,IAAI,KAAK,WAAW,CAAC;YACtD,KAAK,MAAM,CAAC;YACZ,KAAK,MAAM;gBACP,OAAO,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;SACvC;IACL,CAAC;IA8CD,MAAM;QACF,IAAI,CAAC,iBAAiB,EAAE,CAAC;IAC7B,CAAC;IAED,QAAQ;QACJ,IAAI,CAAC,oBAAoB,EAAE,CAAC;IAChC,CAAC;IAED,iBAAiB;QACb,IAAI,CAAC,IAAI,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"]}
|
package/src/RovingTabindex.d.ts
CHANGED
|
@@ -1,62 +1,19 @@
|
|
|
1
|
-
import
|
|
2
|
-
declare type
|
|
3
|
-
export declare type RovingTabindexConfig<T> = {
|
|
4
|
-
focusInIndex?: (_elements: T[]) => number;
|
|
5
|
-
direction?: DirectionTypes | (() => DirectionTypes);
|
|
6
|
-
elementEnterAction?: (el: T) => void;
|
|
7
|
-
elements: () => T[];
|
|
8
|
-
isFocusableElement?: (el: T) => boolean;
|
|
9
|
-
listenerScope?: HTMLElement | (() => HTMLElement);
|
|
10
|
-
};
|
|
1
|
+
import { FocusGroupConfig, FocusGroupController } from './FocusGroup.js';
|
|
2
|
+
export declare type RovingTabindexConfig<T> = FocusGroupConfig<T>;
|
|
11
3
|
interface UpdateTabIndexes {
|
|
12
4
|
tabIndex: number;
|
|
13
5
|
removeTabIndex?: boolean;
|
|
14
6
|
}
|
|
15
|
-
export declare class RovingTabindexController<T extends HTMLElement>
|
|
16
|
-
|
|
17
|
-
get
|
|
18
|
-
set currentIndex(currentIndex: number);
|
|
19
|
-
private _currentIndex;
|
|
20
|
-
get direction(): DirectionTypes;
|
|
21
|
-
_direction: () => DirectionTypes;
|
|
22
|
-
directionLength: number;
|
|
23
|
-
elementEnterAction: (_el: T) => void;
|
|
24
|
-
get elements(): T[];
|
|
25
|
-
private _elements;
|
|
26
|
-
firstUpdated: boolean;
|
|
27
|
-
private set focused(value);
|
|
28
|
-
private get focused();
|
|
29
|
-
private _focused;
|
|
30
|
-
get focusInElement(): T;
|
|
31
|
-
get focusInIndex(): number;
|
|
32
|
-
_focusInIndex: (_elements: T[]) => number;
|
|
33
|
-
host: ReactiveElement;
|
|
34
|
-
isFocusableElement: (_el: T) => boolean;
|
|
35
|
-
isEventWithinListenerScope(event: Event): boolean;
|
|
36
|
-
_listenerScope: () => HTMLElement;
|
|
37
|
-
offset: number;
|
|
7
|
+
export declare class RovingTabindexController<T extends HTMLElement> extends FocusGroupController<T> {
|
|
8
|
+
protected set focused(focused: boolean);
|
|
9
|
+
protected get focused(): boolean;
|
|
38
10
|
private managed;
|
|
39
|
-
constructor(host: ReactiveElement, { direction, elementEnterAction, elements, focusInIndex, isFocusableElement, listenerScope, }?: RovingTabindexConfig<T>);
|
|
40
|
-
update({ elements }?: RovingTabindexConfig<T>): void;
|
|
41
|
-
focus(options?: FocusOptions): void;
|
|
42
11
|
private manageIndexesAnimationFrame;
|
|
43
12
|
clearElementCache(offset?: number): void;
|
|
44
|
-
setCurrentIndexCircularly(diff: number): void;
|
|
45
|
-
hostContainsFocus(): void;
|
|
46
|
-
hostNoLongerContainsFocus(): void;
|
|
47
|
-
isRelatedTargetAnElement(event: FocusEvent): boolean;
|
|
48
|
-
handleFocusin: (event: FocusEvent) => void;
|
|
49
|
-
handleFocusout: (event: FocusEvent) => void;
|
|
50
|
-
acceptsEventCode(code: string): boolean;
|
|
51
|
-
handleKeydown: (event: KeyboardEvent) => void;
|
|
52
13
|
manageTabindexes(): void;
|
|
53
14
|
updateTabindexes(getTabIndex: (el: HTMLElement) => UpdateTabIndexes): void;
|
|
54
15
|
manage(): void;
|
|
55
16
|
unmanage(): void;
|
|
56
|
-
addEventListeners(): void;
|
|
57
|
-
removeEventListeners(): void;
|
|
58
17
|
hostUpdated(): void;
|
|
59
|
-
hostConnected(): void;
|
|
60
|
-
hostDisconnected(): void;
|
|
61
18
|
}
|
|
62
19
|
export {};
|
package/src/RovingTabindex.js
CHANGED
|
@@ -1,211 +1,37 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
this._listenerScope = () => this.host;
|
|
17
|
-
// When elements are virtualized, the delta between the first element
|
|
18
|
-
// and the first rendered element.
|
|
19
|
-
this.offset = 0;
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020 Adobe. All rights reserved.
|
|
3
|
+
This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
|
|
7
|
+
Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
import { FocusGroupController } from './FocusGroup.js';
|
|
13
|
+
export class RovingTabindexController extends FocusGroupController {
|
|
14
|
+
constructor() {
|
|
15
|
+
super(...arguments);
|
|
20
16
|
this.managed = true;
|
|
21
17
|
this.manageIndexesAnimationFrame = 0;
|
|
22
|
-
this.handleFocusin = (event) => {
|
|
23
|
-
if (!this.isEventWithinListenerScope(event))
|
|
24
|
-
return;
|
|
25
|
-
if (this.isRelatedTargetAnElement(event)) {
|
|
26
|
-
this.hostContainsFocus();
|
|
27
|
-
}
|
|
28
|
-
const path = event.composedPath();
|
|
29
|
-
let targetIndex = -1;
|
|
30
|
-
path.find((el) => {
|
|
31
|
-
targetIndex = this.elements.indexOf(el);
|
|
32
|
-
return targetIndex !== -1;
|
|
33
|
-
});
|
|
34
|
-
this.currentIndex = targetIndex > -1 ? targetIndex : this.currentIndex;
|
|
35
|
-
};
|
|
36
|
-
this.handleFocusout = (event) => {
|
|
37
|
-
if (this.isRelatedTargetAnElement(event)) {
|
|
38
|
-
this.hostNoLongerContainsFocus();
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
this.handleKeydown = (event) => {
|
|
42
|
-
if (!this.acceptsEventCode(event.code) || event.defaultPrevented) {
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
let diff = 0;
|
|
46
|
-
switch (event.code) {
|
|
47
|
-
case 'ArrowRight':
|
|
48
|
-
diff += 1;
|
|
49
|
-
break;
|
|
50
|
-
case 'ArrowDown':
|
|
51
|
-
diff += this.direction === 'grid' ? this.directionLength : 1;
|
|
52
|
-
break;
|
|
53
|
-
case 'ArrowLeft':
|
|
54
|
-
diff -= 1;
|
|
55
|
-
break;
|
|
56
|
-
case 'ArrowUp':
|
|
57
|
-
diff -= this.direction === 'grid' ? this.directionLength : 1;
|
|
58
|
-
break;
|
|
59
|
-
case 'End':
|
|
60
|
-
this.currentIndex = 0;
|
|
61
|
-
diff -= 1;
|
|
62
|
-
break;
|
|
63
|
-
case 'Home':
|
|
64
|
-
this.currentIndex = this.elements.length - 1;
|
|
65
|
-
diff += 1;
|
|
66
|
-
break;
|
|
67
|
-
}
|
|
68
|
-
event.preventDefault();
|
|
69
|
-
if (this.direction === 'grid' && this.currentIndex + diff < 0) {
|
|
70
|
-
this.currentIndex = 0;
|
|
71
|
-
}
|
|
72
|
-
else if (this.direction === 'grid' &&
|
|
73
|
-
this.currentIndex + diff > this.elements.length - 1) {
|
|
74
|
-
this.currentIndex = this.elements.length - 1;
|
|
75
|
-
}
|
|
76
|
-
else {
|
|
77
|
-
this.setCurrentIndexCircularly(diff);
|
|
78
|
-
}
|
|
79
|
-
// To allow the `focusInIndex` to be calculated with the "after" state of the keyboard interaction
|
|
80
|
-
// do `elementEnterAction` _before_ focusing the next element.
|
|
81
|
-
this.elementEnterAction(this.elements[this.currentIndex]);
|
|
82
|
-
this.focus();
|
|
83
|
-
};
|
|
84
|
-
this.host = host;
|
|
85
|
-
this.host.addController(this);
|
|
86
|
-
this._elements = elements;
|
|
87
|
-
this.isFocusableElement = isFocusableElement || this.isFocusableElement;
|
|
88
|
-
if (typeof direction === 'string') {
|
|
89
|
-
this._direction = () => direction;
|
|
90
|
-
}
|
|
91
|
-
else if (typeof direction === 'function') {
|
|
92
|
-
this._direction = direction;
|
|
93
|
-
}
|
|
94
|
-
this.elementEnterAction = elementEnterAction || this.elementEnterAction;
|
|
95
|
-
if (typeof focusInIndex === 'number') {
|
|
96
|
-
this._focusInIndex = () => focusInIndex;
|
|
97
|
-
}
|
|
98
|
-
else if (typeof focusInIndex === 'function') {
|
|
99
|
-
this._focusInIndex = focusInIndex;
|
|
100
|
-
}
|
|
101
|
-
if (typeof listenerScope === 'object') {
|
|
102
|
-
this._listenerScope = () => listenerScope;
|
|
103
|
-
}
|
|
104
|
-
else if (typeof listenerScope === 'function') {
|
|
105
|
-
this._listenerScope = listenerScope;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
get currentIndex() {
|
|
109
|
-
if (this._currentIndex === -1) {
|
|
110
|
-
this._currentIndex = this.focusInIndex;
|
|
111
|
-
}
|
|
112
|
-
return this._currentIndex - this.offset;
|
|
113
|
-
}
|
|
114
|
-
set currentIndex(currentIndex) {
|
|
115
|
-
this._currentIndex = currentIndex + this.offset;
|
|
116
|
-
}
|
|
117
|
-
get direction() {
|
|
118
|
-
return this._direction();
|
|
119
|
-
}
|
|
120
|
-
get elements() {
|
|
121
|
-
if (!this.cachedElements) {
|
|
122
|
-
this.cachedElements = this._elements();
|
|
123
|
-
}
|
|
124
|
-
return this.cachedElements;
|
|
125
18
|
}
|
|
126
19
|
set focused(focused) {
|
|
127
20
|
if (focused === this.focused)
|
|
128
21
|
return;
|
|
129
|
-
|
|
22
|
+
super.focused = focused;
|
|
130
23
|
this.manageTabindexes();
|
|
131
24
|
}
|
|
132
25
|
get focused() {
|
|
133
|
-
return
|
|
134
|
-
}
|
|
135
|
-
get focusInElement() {
|
|
136
|
-
return this.elements[this.focusInIndex];
|
|
137
|
-
}
|
|
138
|
-
get focusInIndex() {
|
|
139
|
-
return this._focusInIndex(this.elements);
|
|
140
|
-
}
|
|
141
|
-
isEventWithinListenerScope(event) {
|
|
142
|
-
if (this._listenerScope() === this.host)
|
|
143
|
-
return true;
|
|
144
|
-
return event.composedPath().includes(this._listenerScope());
|
|
145
|
-
}
|
|
146
|
-
update({ elements } = { elements: () => [] }) {
|
|
147
|
-
this.unmanage();
|
|
148
|
-
this._elements = elements;
|
|
149
|
-
this.clearElementCache();
|
|
150
|
-
this.manage();
|
|
151
|
-
}
|
|
152
|
-
focus(options) {
|
|
153
|
-
var _a;
|
|
154
|
-
(_a = this.elements[this.currentIndex]) === null || _a === void 0 ? void 0 : _a.focus(options);
|
|
26
|
+
return super.focused;
|
|
155
27
|
}
|
|
156
28
|
clearElementCache(offset = 0) {
|
|
157
|
-
delete this.cachedElements;
|
|
158
29
|
cancelAnimationFrame(this.manageIndexesAnimationFrame);
|
|
159
|
-
|
|
30
|
+
super.clearElementCache(offset);
|
|
160
31
|
if (!this.managed)
|
|
161
32
|
return;
|
|
162
33
|
this.manageIndexesAnimationFrame = requestAnimationFrame(() => this.manageTabindexes());
|
|
163
34
|
}
|
|
164
|
-
setCurrentIndexCircularly(diff) {
|
|
165
|
-
const { length } = this.elements;
|
|
166
|
-
let steps = length;
|
|
167
|
-
// start at a possibly not 0 index
|
|
168
|
-
let nextIndex = (length + this.currentIndex + diff) % length;
|
|
169
|
-
while (
|
|
170
|
-
// don't cycle the elements more than once
|
|
171
|
-
steps &&
|
|
172
|
-
this.elements[nextIndex] &&
|
|
173
|
-
!this.isFocusableElement(this.elements[nextIndex])) {
|
|
174
|
-
nextIndex = (length + nextIndex + diff) % length;
|
|
175
|
-
steps -= 1;
|
|
176
|
-
}
|
|
177
|
-
this.currentIndex = nextIndex;
|
|
178
|
-
}
|
|
179
|
-
hostContainsFocus() {
|
|
180
|
-
this.host.addEventListener('focusout', this.handleFocusout);
|
|
181
|
-
this.host.addEventListener('keydown', this.handleKeydown);
|
|
182
|
-
this.focused = true;
|
|
183
|
-
}
|
|
184
|
-
hostNoLongerContainsFocus() {
|
|
185
|
-
this.host.addEventListener('focusin', this.handleFocusin);
|
|
186
|
-
this.host.removeEventListener('focusout', this.handleFocusout);
|
|
187
|
-
this.host.removeEventListener('keydown', this.handleKeydown);
|
|
188
|
-
this.currentIndex = this.focusInIndex;
|
|
189
|
-
this.focused = false;
|
|
190
|
-
}
|
|
191
|
-
isRelatedTargetAnElement(event) {
|
|
192
|
-
const relatedTarget = event.relatedTarget;
|
|
193
|
-
return !this.elements.includes(relatedTarget);
|
|
194
|
-
}
|
|
195
|
-
acceptsEventCode(code) {
|
|
196
|
-
if (code === 'End' || code === 'Home') {
|
|
197
|
-
return true;
|
|
198
|
-
}
|
|
199
|
-
switch (this.direction) {
|
|
200
|
-
case 'horizontal':
|
|
201
|
-
return code === 'ArrowLeft' || code === 'ArrowRight';
|
|
202
|
-
case 'vertical':
|
|
203
|
-
return code === 'ArrowUp' || code === 'ArrowDown';
|
|
204
|
-
case 'both':
|
|
205
|
-
case 'grid':
|
|
206
|
-
return code.startsWith('Arrow');
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
35
|
manageTabindexes() {
|
|
210
36
|
if (this.focused) {
|
|
211
37
|
this.updateTabindexes(() => ({ tabIndex: -1 }));
|
|
@@ -236,32 +62,17 @@ export class RovingTabindexController {
|
|
|
236
62
|
manage() {
|
|
237
63
|
this.managed = true;
|
|
238
64
|
this.manageTabindexes();
|
|
239
|
-
|
|
65
|
+
super.manage();
|
|
240
66
|
}
|
|
241
67
|
unmanage() {
|
|
242
68
|
this.managed = false;
|
|
243
69
|
this.updateTabindexes(() => ({ tabIndex: 0 }));
|
|
244
|
-
|
|
245
|
-
}
|
|
246
|
-
addEventListeners() {
|
|
247
|
-
this.host.addEventListener('focusin', this.handleFocusin);
|
|
248
|
-
}
|
|
249
|
-
removeEventListeners() {
|
|
250
|
-
this.host.removeEventListener('focusin', this.handleFocusin);
|
|
251
|
-
this.host.removeEventListener('focusout', this.handleFocusout);
|
|
252
|
-
this.host.removeEventListener('keydown', this.handleKeydown);
|
|
70
|
+
super.unmanage();
|
|
253
71
|
}
|
|
254
72
|
hostUpdated() {
|
|
255
|
-
if (this.
|
|
73
|
+
if (!this.host.hasUpdated) {
|
|
256
74
|
this.manageTabindexes();
|
|
257
|
-
this.firstUpdated = false;
|
|
258
75
|
}
|
|
259
76
|
}
|
|
260
|
-
hostConnected() {
|
|
261
|
-
this.addEventListeners();
|
|
262
|
-
}
|
|
263
|
-
hostDisconnected() {
|
|
264
|
-
this.removeEventListeners();
|
|
265
|
-
}
|
|
266
77
|
}
|
|
267
78
|
//# sourceMappingURL=RovingTabindex.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RovingTabindex.js","sourceRoot":"","sources":["RovingTabindex.ts"],"names":[],"mappings":"AA2BA,MAAM,OAAO,wBAAwB;IAkFjC,YACI,IAAqB,EACrB,EACI,SAAS,EACT,kBAAkB,EAClB,QAAQ,EACR,YAAY,EACZ,kBAAkB,EAClB,aAAa,MACY,EAAE,QAAQ,EAAE,GAAG,EAAE,CAAC,EAAE,EAAE;QA3E/C,kBAAa,GAAG,CAAC,CAAC,CAAC;QAM3B,eAAU,GAAG,GAAmB,EAAE,CAAC,MAAM,CAAC;QAEnC,oBAAe,GAAG,CAAC,CAAC;QAE3B,6DAA6D;QAC7D,uBAAkB,GAAG,CAAC,GAAM,EAAQ,EAAE;YAClC,OAAO;QACX,CAAC,CAAC;QAWF,iBAAY,GAAG,IAAI,CAAC;QAWZ,aAAQ,GAAG,KAAK,CAAC;QAUzB,6DAA6D;QAC7D,kBA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C,CAAC;IAED,iBAAiB;QACb,IAAI,CAAC,IAAI,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,WAAW;QACP,IAAI,IAAI,CAAC,YAAY,EAAE;YACnB,IAAI,CAAC,gBAAgB,EAAE,CAAC;YACxB,IAAI,CAAC,YAAY,GAAG,KAAK,CAAC;SAC7B;IACL,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 RovingTabindexConfig<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};\ninterface UpdateTabIndexes {\n tabIndex: number;\n removeTabIndex?: boolean;\n}\n\nexport class RovingTabindexController<T extends HTMLElement>\n implements ReactiveController\n{\n private 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 // eslint-disable-next-line @typescript-eslint/no-unused-vars\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 firstUpdated = true;\n\n private set focused(focused: boolean) {\n if (focused === this.focused) return;\n this._focused = focused;\n this.manageTabindexes();\n }\n\n private get focused(): boolean {\n return this._focused;\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 private managed = true;\n\n constructor(\n host: ReactiveElement,\n {\n direction,\n elementEnterAction,\n elements,\n focusInIndex,\n isFocusableElement,\n listenerScope,\n }: RovingTabindexConfig<T> = { elements: () => [] }\n ) {\n this.host = host;\n this.host.addController(this);\n this._elements = elements;\n this.isFocusableElement = isFocusableElement || this.isFocusableElement;\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(\n { elements }: RovingTabindexConfig<T> = { elements: () => [] }\n ): void {\n this.unmanage();\n this._elements = elements;\n this.clearElementCache();\n this.manage();\n }\n\n focus(options?: FocusOptions): void {\n this.elements[this.currentIndex]?.focus(options);\n }\n\n private manageIndexesAnimationFrame = 0;\n\n clearElementCache(offset = 0): void {\n delete this.cachedElements;\n cancelAnimationFrame(this.manageIndexesAnimationFrame);\n this.offset = offset;\n if (!this.managed) return;\n\n this.manageIndexesAnimationFrame = requestAnimationFrame(() =>\n this.manageTabindexes()\n );\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 manageTabindexes(): void {\n if (this.focused) {\n this.updateTabindexes(() => ({ tabIndex: -1 }));\n } else {\n this.updateTabindexes((el: HTMLElement): UpdateTabIndexes => {\n return {\n removeTabIndex:\n el.contains(this.focusInElement) &&\n el !== this.focusInElement,\n tabIndex: el === this.focusInElement ? 0 : -1,\n };\n });\n }\n }\n\n updateTabindexes(getTabIndex: (el: HTMLElement) => UpdateTabIndexes): void {\n this.elements.forEach((el) => {\n const { tabIndex, removeTabIndex } = getTabIndex(el);\n if (!removeTabIndex) {\n el.tabIndex = tabIndex;\n return;\n }\n el.removeAttribute('tabindex');\n const updatable = el as unknown as {\n requestUpdate?: () => void;\n };\n if (updatable.requestUpdate) updatable.requestUpdate();\n });\n }\n\n manage(): void {\n this.managed = true;\n this.manageTabindexes();\n this.addEventListeners();\n }\n\n unmanage(): void {\n this.managed = false;\n this.updateTabindexes(() => ({ tabIndex: 0 }));\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 hostUpdated(): void {\n if (this.firstUpdated) {\n this.manageTabindexes();\n this.firstUpdated = false;\n }\n }\n\n hostConnected(): void {\n this.addEventListeners();\n }\n\n hostDisconnected(): void {\n this.removeEventListeners();\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"RovingTabindex.js","sourceRoot":"","sources":["RovingTabindex.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;EAUE;AACF,OAAO,EAAoB,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AAQzE,MAAM,OAAO,wBAEX,SAAQ,oBAAuB;IAFjC;;QAaY,YAAO,GAAG,IAAI,CAAC;QAEf,gCAA2B,GAAG,CAAC,CAAC;IA2D5C,CAAC;IAvEG,IAAuB,OAAO,CAAC,OAAgB;QAC3C,IAAI,OAAO,KAAK,IAAI,CAAC,OAAO;YAAE,OAAO;QACrC,KAAK,CAAC,OAAO,GAAG,OAAO,C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,UAAU,CAAC,CAAC;YAC/B,MAAM,SAAS,GAAG,EAEjB,CAAC;YACF,IAAI,SAAS,CAAC,aAAa;gBAAE,SAAS,CAAC,aAAa,EAAE,CAAC;QAC3D,CAAC,CAAC,CAAC;IACP,CAAC;IAEQ,MAAM;QACX,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QACpB,IAAI,CAAC,gBAAgB,EAAE,CAAC;QACxB,KAAK,CAAC,MAAM,EAAE,CAAC;IACnB,CAAC;IAEQ,QAAQ;QACb,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC;QACrB,IAAI,CAAC,gBAAgB,CAAC,GAAG,EAAE,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;QAC/C,KAAK,CAAC,QAAQ,EAAE,CAAC;IACrB,CAAC;IAED,WAAW;QACP,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE;YACvB,IAAI,CAAC,gBAAgB,EAAE,CAAC;SAC3B;IACL,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 { FocusGroupConfig, FocusGroupController } from './FocusGroup.js';\n\nexport type RovingTabindexConfig<T> = FocusGroupConfig<T>;\ninterface UpdateTabIndexes {\n tabIndex: number;\n removeTabIndex?: boolean;\n}\n\nexport class RovingTabindexController<\n T extends HTMLElement\n> extends FocusGroupController<T> {\n protected override set focused(focused: boolean) {\n if (focused === this.focused) return;\n super.focused = focused;\n this.manageTabindexes();\n }\n\n protected override get focused(): boolean {\n return super.focused;\n }\n\n private managed = true;\n\n private manageIndexesAnimationFrame = 0;\n\n override clearElementCache(offset = 0): void {\n cancelAnimationFrame(this.manageIndexesAnimationFrame);\n super.clearElementCache(offset);\n if (!this.managed) return;\n\n this.manageIndexesAnimationFrame = requestAnimationFrame(() =>\n this.manageTabindexes()\n );\n }\n\n manageTabindexes(): void {\n if (this.focused) {\n this.updateTabindexes(() => ({ tabIndex: -1 }));\n } else {\n this.updateTabindexes((el: HTMLElement): UpdateTabIndexes => {\n return {\n removeTabIndex:\n el.contains(this.focusInElement) &&\n el !== this.focusInElement,\n tabIndex: el === this.focusInElement ? 0 : -1,\n };\n });\n }\n }\n\n updateTabindexes(getTabIndex: (el: HTMLElement) => UpdateTabIndexes): void {\n this.elements.forEach((el) => {\n const { tabIndex, removeTabIndex } = getTabIndex(el);\n if (!removeTabIndex) {\n el.tabIndex = tabIndex;\n return;\n }\n el.removeAttribute('tabindex');\n const updatable = el as unknown as {\n requestUpdate?: () => void;\n };\n if (updatable.requestUpdate) updatable.requestUpdate();\n });\n }\n\n override manage(): void {\n this.managed = true;\n this.manageTabindexes();\n super.manage();\n }\n\n override unmanage(): void {\n this.managed = false;\n this.updateTabindexes(() => ({ tabIndex: 0 }));\n super.unmanage();\n }\n\n hostUpdated(): void {\n if (!this.host.hasUpdated) {\n this.manageTabindexes();\n }\n }\n}\n"]}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020 Adobe. All rights reserved.
|
|
3
|
+
This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
|
|
7
|
+
Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
import { html, LitElement } from 'lit';
|
|
13
|
+
import { expect, fixture, nextFrame } from '@open-wc/testing';
|
|
14
|
+
import { setViewport } from '@web/test-runner-commands';
|
|
15
|
+
import { MatchMediaController } from '../';
|
|
16
|
+
describe('Match Media', () => {
|
|
17
|
+
it('responds to media changes', async () => {
|
|
18
|
+
class TestEl extends LitElement {
|
|
19
|
+
}
|
|
20
|
+
customElements.define('test-match-media-el', TestEl);
|
|
21
|
+
const el = await fixture(html `
|
|
22
|
+
<test-match-media-el></test-match-media-el>
|
|
23
|
+
`);
|
|
24
|
+
const controller = new MatchMediaController(el, '(min-width: 500px)');
|
|
25
|
+
expect(controller.matches).to.be.true;
|
|
26
|
+
await setViewport({ width: 360, height: 640 });
|
|
27
|
+
// Allow viewport update to propagate.
|
|
28
|
+
await nextFrame();
|
|
29
|
+
expect(controller.matches).to.be.false;
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
//# sourceMappingURL=match-media.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"match-media.test.js","sourceRoot":"","sources":["match-media.test.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;EAUE;AAEF,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,KAAK,CAAC;AACvC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC9D,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AACxD,OAAO,EAAE,oBAAoB,EAAE,MAAM,KAAK,CAAC;AAE3C,QAAQ,CAAC,aAAa,EAAE,GAAG,EAAE;IACzB,EAAE,CAAC,2BAA2B,EAAE,KAAK,IAAI,EAAE;QACvC,MAAM,MAAO,SAAQ,UAAU;SAAG;QAClC,cAAc,CAAC,MAAM,CAAC,qBAAqB,EAAE,MAAM,CAAC,CAAC;QACrD,MAAM,EAAE,GAAG,MAAM,OAAO,CACpB,IAAI,CAAA;;aAEH,CACJ,CAAC;QACF,MAAM,UAAU,GAAG,IAAI,oBAAoB,CACvC,EAA6C,EAC7C,oBAAoB,CACvB,CAAC;QACF,MAAM,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC;QACtC,MAAM,WAAW,CAAC,EAAE,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC;QAC/C,sCAAsC;QACtC,MAAM,SAAS,EAAE,CAAC;QAClB,MAAM,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,CAAC;IAC3C,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC","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*/\n\nimport { html, LitElement } from 'lit';\nimport { expect, fixture, nextFrame } from '@open-wc/testing';\nimport { setViewport } from '@web/test-runner-commands';\nimport { MatchMediaController } from '../';\n\ndescribe('Match Media', () => {\n it('responds to media changes', async () => {\n class TestEl extends LitElement {}\n customElements.define('test-match-media-el', TestEl);\n const el = await fixture(\n html`\n <test-match-media-el></test-match-media-el>\n `\n );\n const controller = new MatchMediaController(\n el as LitElement & { shadowRoot: ShadowRoot },\n '(min-width: 500px)'\n );\n expect(controller.matches).to.be.true;\n await setViewport({ width: 360, height: 640 });\n // Allow viewport update to propagate.\n await nextFrame();\n expect(controller.matches).to.be.false;\n });\n});\n"]}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020 Adobe. All rights reserved.
|
|
3
|
+
This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
|
|
7
|
+
Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
import '@spectrum-web-components/action-button/sp-action-button.js';
|
|
13
|
+
import '@spectrum-web-components/action-group/sp-action-group.js';
|
|
14
|
+
import '@spectrum-web-components/tabs/sp-tab-panel.js';
|
|
15
|
+
import '@spectrum-web-components/tabs/sp-tab.js';
|
|
16
|
+
import '@spectrum-web-components/tabs/sp-tabs.js';
|
|
17
|
+
import { elementUpdated, expect, fixture, nextFrame } from '@open-wc/testing';
|
|
18
|
+
import { html } from '@spectrum-web-components/base';
|
|
19
|
+
import { sendKeys } from '@web/test-runner-commands';
|
|
20
|
+
import { sendMouse } from '../../../test/plugins/browser.js';
|
|
21
|
+
const createTabs = async () => {
|
|
22
|
+
const tabs = await fixture(html `
|
|
23
|
+
<sp-tabs selected="second">
|
|
24
|
+
<sp-tab label="Tab 1" value="first"></sp-tab>
|
|
25
|
+
<sp-tab label="Tab 2" value="second"></sp-tab>
|
|
26
|
+
<sp-tab label="Tab 3" value="third"></sp-tab>
|
|
27
|
+
<sp-tab-panel value="first">
|
|
28
|
+
<sp-action-group selects="single">
|
|
29
|
+
<sp-action-button selected value="1">
|
|
30
|
+
Single Button 1
|
|
31
|
+
</sp-action-button>
|
|
32
|
+
<sp-action-button value="2">
|
|
33
|
+
Single Button 2
|
|
34
|
+
</sp-action-button>
|
|
35
|
+
<sp-action-button value="3">
|
|
36
|
+
Single Button 3
|
|
37
|
+
</sp-action-button>
|
|
38
|
+
</sp-action-group>
|
|
39
|
+
</sp-tab-panel>
|
|
40
|
+
<sp-tab-panel value="second">
|
|
41
|
+
<sp-action-group selects="multiple">
|
|
42
|
+
<sp-action-button value="1">
|
|
43
|
+
Multiple Button 1
|
|
44
|
+
</sp-action-button>
|
|
45
|
+
<sp-action-button selected value="2">
|
|
46
|
+
Multiple Button 2
|
|
47
|
+
</sp-action-button>
|
|
48
|
+
<sp-action-button selected value="3">
|
|
49
|
+
Multiple Button 3
|
|
50
|
+
</sp-action-button>
|
|
51
|
+
</sp-action-group>
|
|
52
|
+
</sp-tab-panel>
|
|
53
|
+
<sp-tab-panel value="third">
|
|
54
|
+
<sp-action-group>
|
|
55
|
+
<sp-action-button value="1">
|
|
56
|
+
None Button 1
|
|
57
|
+
</sp-action-button>
|
|
58
|
+
<sp-action-button value="2">
|
|
59
|
+
None Button 2
|
|
60
|
+
</sp-action-button>
|
|
61
|
+
<sp-action-button selected value="3">
|
|
62
|
+
None Button 3
|
|
63
|
+
</sp-action-button>
|
|
64
|
+
</sp-action-group>
|
|
65
|
+
</sp-tab-panel>
|
|
66
|
+
</sp-tabs>
|
|
67
|
+
`);
|
|
68
|
+
await elementUpdated(tabs);
|
|
69
|
+
return tabs;
|
|
70
|
+
};
|
|
71
|
+
describe('Action Group inside of Tabs', () => {
|
|
72
|
+
it('accurately navigates the desired element', async () => {
|
|
73
|
+
const el = await createTabs();
|
|
74
|
+
const tab1 = el.querySelector('sp-tab[value="first"]');
|
|
75
|
+
const tab2 = el.querySelector('sp-tab[value="second"]');
|
|
76
|
+
const tab3 = el.querySelector('sp-tab[value="third"]');
|
|
77
|
+
const tabPanel1 = el.querySelector('sp-tab-panel[value="first"]');
|
|
78
|
+
const tabPanel2 = el.querySelector('sp-tab-panel[value="second"]');
|
|
79
|
+
const tabPanel3 = el.querySelector('sp-tab-panel[value="third"]');
|
|
80
|
+
const actionGroup1 = tabPanel1.querySelector('sp-action-group');
|
|
81
|
+
const actionGroup2 = tabPanel2.querySelector('sp-action-group');
|
|
82
|
+
const actionGroup3 = tabPanel3.querySelector('sp-action-group');
|
|
83
|
+
const actionButton1 = actionGroup1.querySelector('[selected]');
|
|
84
|
+
const actionButton2 = actionGroup2.querySelector('[selected]');
|
|
85
|
+
const actionButton3 = actionGroup3.querySelector('[selected]');
|
|
86
|
+
el.focus();
|
|
87
|
+
expect(el.contains(document.activeElement)).to.be.true;
|
|
88
|
+
expect(document.activeElement === tab2).to.be.true;
|
|
89
|
+
actionGroup2.focus();
|
|
90
|
+
expect(document.activeElement === actionButton2).to.be.true;
|
|
91
|
+
await nextFrame();
|
|
92
|
+
await sendKeys({
|
|
93
|
+
press: 'ArrowLeft',
|
|
94
|
+
});
|
|
95
|
+
expect(document.activeElement === tab1).to.be.false;
|
|
96
|
+
expect(actionGroup2.contains(document.activeElement)).to.be.true;
|
|
97
|
+
el.focus();
|
|
98
|
+
expect(document.activeElement === tab2).to.be.true;
|
|
99
|
+
await sendKeys({
|
|
100
|
+
press: 'ArrowRight',
|
|
101
|
+
});
|
|
102
|
+
expect(document.activeElement === tab3).to.be.true;
|
|
103
|
+
await sendKeys({
|
|
104
|
+
press: 'Enter',
|
|
105
|
+
});
|
|
106
|
+
expect(document.activeElement === tab3).to.be.true;
|
|
107
|
+
actionGroup3.focus();
|
|
108
|
+
expect(document.activeElement === actionButton3).to.be.true;
|
|
109
|
+
await sendKeys({
|
|
110
|
+
press: 'ArrowLeft',
|
|
111
|
+
});
|
|
112
|
+
expect(document.activeElement === tab2).to.be.false;
|
|
113
|
+
expect(actionGroup3.contains(document.activeElement)).to.be.true;
|
|
114
|
+
const boundingRect = tab1.getBoundingClientRect();
|
|
115
|
+
// tab1.click() doesn't current reach into the focus management here.
|
|
116
|
+
await sendMouse({
|
|
117
|
+
steps: [
|
|
118
|
+
{
|
|
119
|
+
type: 'click',
|
|
120
|
+
position: [
|
|
121
|
+
boundingRect.left + boundingRect.width / 2,
|
|
122
|
+
boundingRect.top + boundingRect.height / 2,
|
|
123
|
+
],
|
|
124
|
+
},
|
|
125
|
+
],
|
|
126
|
+
});
|
|
127
|
+
expect(document.activeElement === tab1).to.be.true;
|
|
128
|
+
actionGroup1.focus();
|
|
129
|
+
expect(document.activeElement === actionButton1).to.be.true;
|
|
130
|
+
await sendKeys({
|
|
131
|
+
press: 'ArrowRight',
|
|
132
|
+
});
|
|
133
|
+
expect(document.activeElement === tab2).to.be.false;
|
|
134
|
+
expect(actionGroup1.contains(document.activeElement)).to.be.true;
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
//# sourceMappingURL=roving-tabindex-integration.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"roving-tabindex-integration.test.js","sourceRoot":"","sources":["roving-tabindex-integration.test.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;EAUE;AACF,OAAO,4DAA4D,CAAC;AAEpE,OAAO,0DAA0D,CAAC;AAElE,OAAO,+CAA+C,CAAC;AACvD,OAAO,yCAAyC,CAAC;AACjD,OAAO,0CAA0C,CAAC;AAElD,OAAO,EAAE,cAAc,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC9E,OAAO,EAAE,IAAI,EAAE,MAAM,+BAA+B,CAAC;AACrD,OAAO,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AACrD,OAAO,EAAE,SAAS,EAAE,MAAM,kCAAkC,CAAC;AAE7D,MAAM,UAAU,GAAG,KAAK,IAAmB,EAAE;IACzC,MAAM,IAAI,GAAG,MAAM,OAAO,CACtB,IAAI,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;SA6CH,CACJ,CAAC;IACF,MAAM,cAAc,CAAC,IAAI,CAAC,CAAC;IAC3B,OAAO,IAAI,CAAC;AAChB,CAAC,CAAC;AAEF,QAAQ,CAAC,6BAA6B,EAAE,GAAG,EAAE;IACzC,EAAE,CAAC,0CAA0C,EAAE,KAAK,IAAI,EAAE;QACtD,MAAM,EAAE,GAAG,MAAM,UAAU,E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ClD,qEAAqE;QACrE,MAAM,SAAS,CAAC;YACZ,KAAK,EAAE;gBACH;oBACI,IAAI,EAAE,OAAO;oBACb,QAAQ,EAAE;wBACN,YAAY,CAAC,IAAI,GAAG,YAAY,CAAC,KAAK,GAAG,CAAC;wBAC1C,YAAY,CAAC,GAAG,GAAG,YAAY,CAAC,MAAM,GAAG,CAAC;qBAC7C;iBACJ;aACJ;SACJ,CAAC,CAAC;QACH,MAAM,CAAC,QAAQ,CAAC,aAAa,KAAK,IAAI,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC;QAEnD,YAAY,CAAC,KAAK,EAAE,CAAC;QACrB,MAAM,CAAC,QAAQ,CAAC,aAAa,KAAK,aAAa,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC;QAE5D,MAAM,QAAQ,CAAC;YACX,KAAK,EAAE,YAAY;SACtB,CAAC,CAAC;QAEH,MAAM,CAAC,QAAQ,CAAC,aAAa,KAAK,IAAI,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,CAAC;QACpD,MAAM,CAAC,YAAY,CAAC,QAAQ,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC;IACrE,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC","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 '@spectrum-web-components/action-button/sp-action-button.js';\nimport { ActionButton } from '@spectrum-web-components/action-button';\nimport '@spectrum-web-components/action-group/sp-action-group.js';\nimport { ActionGroup } from '@spectrum-web-components/action-group';\nimport '@spectrum-web-components/tabs/sp-tab-panel.js';\nimport '@spectrum-web-components/tabs/sp-tab.js';\nimport '@spectrum-web-components/tabs/sp-tabs.js';\nimport { Tab, TabPanel, Tabs } from '@spectrum-web-components/tabs';\nimport { elementUpdated, expect, fixture, nextFrame } from '@open-wc/testing';\nimport { html } from '@spectrum-web-components/base';\nimport { sendKeys } from '@web/test-runner-commands';\nimport { sendMouse } from '../../../test/plugins/browser.js';\n\nconst createTabs = async (): Promise<Tabs> => {\n const tabs = await fixture<Tabs>(\n html`\n <sp-tabs selected=\"second\">\n <sp-tab label=\"Tab 1\" value=\"first\"></sp-tab>\n <sp-tab label=\"Tab 2\" value=\"second\"></sp-tab>\n <sp-tab label=\"Tab 3\" value=\"third\"></sp-tab>\n <sp-tab-panel value=\"first\">\n <sp-action-group selects=\"single\">\n <sp-action-button selected value=\"1\">\n Single Button 1\n </sp-action-button>\n <sp-action-button value=\"2\">\n Single Button 2\n </sp-action-button>\n <sp-action-button value=\"3\">\n Single Button 3\n </sp-action-button>\n </sp-action-group>\n </sp-tab-panel>\n <sp-tab-panel value=\"second\">\n <sp-action-group selects=\"multiple\">\n <sp-action-button value=\"1\">\n Multiple Button 1\n </sp-action-button>\n <sp-action-button selected value=\"2\">\n Multiple Button 2\n </sp-action-button>\n <sp-action-button selected value=\"3\">\n Multiple Button 3\n </sp-action-button>\n </sp-action-group>\n </sp-tab-panel>\n <sp-tab-panel value=\"third\">\n <sp-action-group>\n <sp-action-button value=\"1\">\n None Button 1\n </sp-action-button>\n <sp-action-button value=\"2\">\n None Button 2\n </sp-action-button>\n <sp-action-button selected value=\"3\">\n None Button 3\n </sp-action-button>\n </sp-action-group>\n </sp-tab-panel>\n </sp-tabs>\n `\n );\n await elementUpdated(tabs);\n return tabs;\n};\n\ndescribe('Action Group inside of Tabs', () => {\n it('accurately navigates the desired element', async () => {\n const el = await createTabs();\n const tab1 = el.querySelector('sp-tab[value=\"first\"]') as Tab;\n const tab2 = el.querySelector('sp-tab[value=\"second\"]');\n const tab3 = el.querySelector('sp-tab[value=\"third\"]') as Tab;\n const tabPanel1 = el.querySelector(\n 'sp-tab-panel[value=\"first\"]'\n ) as TabPanel;\n const tabPanel2 = el.querySelector(\n 'sp-tab-panel[value=\"second\"]'\n ) as TabPanel;\n const tabPanel3 = el.querySelector(\n 'sp-tab-panel[value=\"third\"]'\n ) as TabPanel;\n const actionGroup1 = tabPanel1.querySelector(\n 'sp-action-group'\n ) as ActionGroup;\n const actionGroup2 = tabPanel2.querySelector(\n 'sp-action-group'\n ) as ActionGroup;\n const actionGroup3 = tabPanel3.querySelector(\n 'sp-action-group'\n ) as ActionGroup;\n const actionButton1 = actionGroup1.querySelector(\n '[selected]'\n ) as ActionButton;\n const actionButton2 = actionGroup2.querySelector(\n '[selected]'\n ) as ActionButton;\n const actionButton3 = actionGroup3.querySelector(\n '[selected]'\n ) as ActionButton;\n\n el.focus();\n expect(el.contains(document.activeElement)).to.be.true;\n expect(document.activeElement === tab2).to.be.true;\n\n actionGroup2.focus();\n expect(document.activeElement === actionButton2).to.be.true;\n\n await nextFrame();\n await sendKeys({\n press: 'ArrowLeft',\n });\n\n expect(document.activeElement === tab1).to.be.false;\n expect(actionGroup2.contains(document.activeElement)).to.be.true;\n\n el.focus();\n expect(document.activeElement === tab2).to.be.true;\n\n await sendKeys({\n press: 'ArrowRight',\n });\n\n expect(document.activeElement === tab3).to.be.true;\n\n await sendKeys({\n press: 'Enter',\n });\n\n expect(document.activeElement === tab3).to.be.true;\n\n actionGroup3.focus();\n expect(document.activeElement === actionButton3).to.be.true;\n\n await sendKeys({\n press: 'ArrowLeft',\n });\n\n expect(document.activeElement === tab2).to.be.false;\n expect(actionGroup3.contains(document.activeElement)).to.be.true;\n\n const boundingRect = tab1.getBoundingClientRect();\n // tab1.click() doesn't current reach into the focus management here.\n await sendMouse({\n steps: [\n {\n type: 'click',\n position: [\n boundingRect.left + boundingRect.width / 2,\n boundingRect.top + boundingRect.height / 2,\n ],\n },\n ],\n });\n expect(document.activeElement === tab1).to.be.true;\n\n actionGroup1.focus();\n expect(document.activeElement === actionButton1).to.be.true;\n\n await sendKeys({\n press: 'ArrowRight',\n });\n\n expect(document.activeElement === tab2).to.be.false;\n expect(actionGroup1.contains(document.activeElement)).to.be.true;\n });\n});\n"]}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020 Adobe. All rights reserved.
|
|
3
|
+
This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
|
|
7
|
+
Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
import { LitElement } from 'lit';
|
|
13
|
+
import { expect } from '@open-wc/testing';
|
|
14
|
+
import { RovingTabindexController } from '../src';
|
|
15
|
+
describe('RovingTabindex', () => {
|
|
16
|
+
it('constructs with defaults', async () => {
|
|
17
|
+
class TestEl extends LitElement {
|
|
18
|
+
}
|
|
19
|
+
customElements.define('test-roving-tabindex-el', TestEl);
|
|
20
|
+
const el = new TestEl();
|
|
21
|
+
const controller = new RovingTabindexController(el);
|
|
22
|
+
expect(controller.direction).to.equal('both');
|
|
23
|
+
expect(controller.focusInIndex).to.equal(0);
|
|
24
|
+
expect(controller.isFocusableElement(el)).to.be.true;
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
//# sourceMappingURL=roving-tabindex.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"roving-tabindex.test.js","sourceRoot":"","sources":["roving-tabindex.test.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;EAUE;AAEF,OAAO,EAAE,UAAU,EAAE,MAAM,KAAK,CAAC;AACjC,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,wBAAwB,EAAE,MAAM,QAAQ,CAAC;AAElD,QAAQ,CAAC,gBAAgB,EAAE,GAAG,EAAE;IAC5B,EAAE,CAAC,0BAA0B,EAAE,KAAK,IAAI,EAAE;QACtC,MAAM,MAAO,SAAQ,UAAU;SAAG;QAClC,cAAc,CAAC,MAAM,CAAC,yBAAyB,EAAE,MAAM,CAAC,CAAC;QACzD,MAAM,EAAE,GAAG,IAAI,MAAM,EAAE,CAAC;QACxB,MAAM,UAAU,GAAG,IAAI,wBAAwB,CAC3C,EAA6C,CAChD,CAAC;QACF,MAAM,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;QAC9C,MAAM,CAAC,UAAU,CAAC,YAAY,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QAC5C,MAAM,CAAC,UAAU,CAAC,kBAAkB,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC;IACzD,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC","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*/\n\nimport { LitElement } from 'lit';\nimport { expect } from '@open-wc/testing';\nimport { RovingTabindexController } from '../src';\n\ndescribe('RovingTabindex', () => {\n it('constructs with defaults', async () => {\n class TestEl extends LitElement {}\n customElements.define('test-roving-tabindex-el', TestEl);\n const el = new TestEl();\n const controller = new RovingTabindexController(\n el as LitElement & { shadowRoot: ShadowRoot }\n );\n expect(controller.direction).to.equal('both');\n expect(controller.focusInIndex).to.equal(0);\n expect(controller.isFocusableElement(el)).to.be.true;\n });\n});\n"]}
|