@internetarchive/modal-manager 2.0.3 → 2.0.4-alpha-webdev7960.0
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/.editorconfig +29 -29
- package/.eslintrc.js +14 -14
- package/.github/workflows/ci.yml +30 -30
- package/.github/workflows/gh-pages-main.yml +42 -42
- package/.github/workflows/pr-preview.yml +40 -40
- package/LICENSE +661 -661
- package/README.md +139 -139
- package/custom-elements.json +170 -170
- package/dist/index.d.ts +7 -7
- package/dist/index.js +5 -5
- package/dist/index.js.map +1 -1
- package/dist/src/assets/arrow-left-icon.d.ts +2 -2
- package/dist/src/assets/arrow-left-icon.js +14 -14
- package/dist/src/assets/arrow-left-icon.js.map +1 -1
- package/dist/src/assets/ia-logo-icon.d.ts +2 -2
- package/dist/src/assets/ia-logo-icon.js +29 -29
- package/dist/src/assets/ia-logo-icon.js.map +1 -1
- package/dist/src/modal-config.d.ts +104 -104
- package/dist/src/modal-config.js +24 -24
- package/dist/src/modal-config.js.map +1 -1
- package/dist/src/modal-manager-host-bridge-interface.d.ts +12 -12
- package/dist/src/modal-manager-host-bridge-interface.js +1 -1
- package/dist/src/modal-manager-host-bridge-interface.js.map +1 -1
- package/dist/src/modal-manager-host-bridge.d.ts +34 -34
- package/dist/src/modal-manager-host-bridge.js +62 -62
- package/dist/src/modal-manager-host-bridge.js.map +1 -1
- package/dist/src/modal-manager-interface.d.ts +27 -27
- package/dist/src/modal-manager-interface.js +1 -1
- package/dist/src/modal-manager-interface.js.map +1 -1
- package/dist/src/modal-manager-mode.d.ts +10 -10
- package/dist/src/modal-manager-mode.js +11 -11
- package/dist/src/modal-manager-mode.js.map +1 -1
- package/dist/src/modal-manager.d.ts +137 -127
- package/dist/src/modal-manager.js +247 -234
- package/dist/src/modal-manager.js.map +1 -1
- package/dist/src/modal-template.d.ts +41 -41
- package/dist/src/modal-template.js +335 -335
- package/dist/src/modal-template.js.map +1 -1
- package/dist/src/shoelace/active-elements.d.ts +15 -15
- package/dist/src/shoelace/active-elements.js +27 -27
- package/dist/src/shoelace/active-elements.js.map +1 -1
- package/dist/src/shoelace/modal.d.ts +24 -24
- package/dist/src/shoelace/modal.js +131 -131
- package/dist/src/shoelace/modal.js.map +1 -1
- package/dist/src/shoelace/tabbable.d.ts +9 -9
- package/dist/src/shoelace/tabbable.js +169 -169
- package/dist/src/shoelace/tabbable.js.map +1 -1
- package/dist/test/modal-config.test.d.ts +1 -1
- package/dist/test/modal-config.test.js +69 -69
- package/dist/test/modal-config.test.js.map +1 -1
- package/dist/test/modal-manager.test.d.ts +1 -1
- package/dist/test/modal-manager.test.js +274 -274
- package/dist/test/modal-manager.test.js.map +1 -1
- package/dist/test/modal-template.test.d.ts +1 -1
- package/dist/test/modal-template.test.js +178 -178
- package/dist/test/modal-template.test.js.map +1 -1
- package/dist/vite.config.d.ts +2 -2
- package/dist/vite.config.js +22 -22
- package/dist/vite.config.js.map +1 -1
- package/docs/assets/css/main.css +2678 -2678
- package/docs/classes/_src_modal_config_.modalconfig.html +429 -429
- package/docs/classes/_src_modal_manager_.modalmanager.html +7702 -7702
- package/docs/classes/_src_modal_manager_host_bridge_.modalmanagerhostbridge.html +409 -409
- package/docs/classes/_src_modal_template_.modaltemplate.html +7096 -7096
- package/docs/enums/_src_modal_manager_mode_.modalmanagermode.html +196 -196
- package/docs/globals.html +150 -150
- package/docs/index.html +252 -252
- package/docs/interfaces/_src_modal_manager_host_bridge_interface_.modalmanagerhostbridgeinterface.html +210 -210
- package/docs/interfaces/_src_modal_manager_interface_.modalmanagerinterface.html +7095 -7095
- package/docs/modules/_index_.html +208 -208
- package/docs/modules/_src_modal_config_.html +146 -146
- package/docs/modules/_src_modal_manager_.html +146 -146
- package/docs/modules/_src_modal_manager_host_bridge_.html +146 -146
- package/docs/modules/_src_modal_manager_host_bridge_interface_.html +146 -146
- package/docs/modules/_src_modal_manager_interface_.html +146 -146
- package/docs/modules/_src_modal_manager_mode_.html +146 -146
- package/docs/modules/_src_modal_template_.html +146 -146
- package/docs/modules/_test_modal_config_test_.html +106 -106
- package/docs/modules/_test_modal_manager_test_.html +106 -106
- package/docs/modules/_test_modal_template_test_.html +106 -106
- package/index.html +300 -300
- package/index.ts +7 -7
- package/karma.conf.js +24 -24
- package/package.json +85 -85
- package/renovate.json +7 -7
- package/src/assets/arrow-left-icon.ts +15 -15
- package/src/assets/ia-logo-icon.ts +30 -30
- package/src/modal-config.ts +133 -133
- package/src/modal-manager-host-bridge-interface.ts +13 -13
- package/src/modal-manager-host-bridge.ts +82 -82
- package/src/modal-manager-interface.ts +30 -30
- package/src/modal-manager-mode.ts +10 -10
- package/src/modal-manager.ts +303 -283
- package/src/modal-template.ts +343 -343
- package/src/shoelace/LICENSE.md +6 -6
- package/src/shoelace/active-elements.ts +33 -33
- package/src/shoelace/modal.ts +166 -166
- package/src/shoelace/tabbable.ts +223 -223
- package/test/modal-config.test.ts +77 -77
- package/test/modal-manager.test.ts +347 -347
- package/test/modal-template.test.ts +206 -206
- package/tsconfig.json +21 -21
- package/vite.config.ts +23 -23
package/src/shoelace/LICENSE.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
Copyright (c) 2020 A Beautiful Site, LLC
|
|
2
|
-
|
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
-
|
|
5
|
-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
-
|
|
1
|
+
Copyright (c) 2020 A Beautiful Site, LLC
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
7
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
/* istanbul ignore file */
|
|
2
|
-
/**
|
|
3
|
-
* Use a generator so we can iterate and possibly break early.
|
|
4
|
-
* @example
|
|
5
|
-
* // to operate like a regular array. This kinda nullifies generator benefits, but worth knowing if you need the whole array.
|
|
6
|
-
* const allActiveElements = [...activeElements()]
|
|
7
|
-
*
|
|
8
|
-
* // Early return
|
|
9
|
-
* for (const activeElement of activeElements()) {
|
|
10
|
-
* if (<cond>) {
|
|
11
|
-
* break; // Break the loop, dont need to iterate over the whole array or store an array in memory!
|
|
12
|
-
* }
|
|
13
|
-
* }
|
|
14
|
-
*/
|
|
15
|
-
export function* activeElements(
|
|
16
|
-
activeElement: Element | null = document.activeElement
|
|
17
|
-
): Generator<Element> {
|
|
18
|
-
if (activeElement === null || activeElement === undefined) return;
|
|
19
|
-
|
|
20
|
-
yield activeElement;
|
|
21
|
-
|
|
22
|
-
if (
|
|
23
|
-
'shadowRoot' in activeElement &&
|
|
24
|
-
activeElement.shadowRoot &&
|
|
25
|
-
activeElement.shadowRoot.mode !== 'closed'
|
|
26
|
-
) {
|
|
27
|
-
yield* activeElements(activeElement.shadowRoot.activeElement);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function getDeepestActiveElement() {
|
|
32
|
-
return [...activeElements()].pop();
|
|
33
|
-
}
|
|
1
|
+
/* istanbul ignore file */
|
|
2
|
+
/**
|
|
3
|
+
* Use a generator so we can iterate and possibly break early.
|
|
4
|
+
* @example
|
|
5
|
+
* // to operate like a regular array. This kinda nullifies generator benefits, but worth knowing if you need the whole array.
|
|
6
|
+
* const allActiveElements = [...activeElements()]
|
|
7
|
+
*
|
|
8
|
+
* // Early return
|
|
9
|
+
* for (const activeElement of activeElements()) {
|
|
10
|
+
* if (<cond>) {
|
|
11
|
+
* break; // Break the loop, dont need to iterate over the whole array or store an array in memory!
|
|
12
|
+
* }
|
|
13
|
+
* }
|
|
14
|
+
*/
|
|
15
|
+
export function* activeElements(
|
|
16
|
+
activeElement: Element | null = document.activeElement
|
|
17
|
+
): Generator<Element> {
|
|
18
|
+
if (activeElement === null || activeElement === undefined) return;
|
|
19
|
+
|
|
20
|
+
yield activeElement;
|
|
21
|
+
|
|
22
|
+
if (
|
|
23
|
+
'shadowRoot' in activeElement &&
|
|
24
|
+
activeElement.shadowRoot &&
|
|
25
|
+
activeElement.shadowRoot.mode !== 'closed'
|
|
26
|
+
) {
|
|
27
|
+
yield* activeElements(activeElement.shadowRoot.activeElement);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function getDeepestActiveElement() {
|
|
32
|
+
return [...activeElements()].pop();
|
|
33
|
+
}
|
package/src/shoelace/modal.ts
CHANGED
|
@@ -1,166 +1,166 @@
|
|
|
1
|
-
/* istanbul ignore file */
|
|
2
|
-
import { activeElements, getDeepestActiveElement } from './active-elements.js';
|
|
3
|
-
import { getTabbableElements } from './tabbable.js';
|
|
4
|
-
|
|
5
|
-
let activeModals: HTMLElement[] = [];
|
|
6
|
-
|
|
7
|
-
export default class Modal {
|
|
8
|
-
element: HTMLElement;
|
|
9
|
-
isExternalActivated: boolean = false;
|
|
10
|
-
tabDirection: 'forward' | 'backward' = 'forward';
|
|
11
|
-
currentFocus: HTMLElement | null = null;
|
|
12
|
-
previousFocus: HTMLElement | null = null;
|
|
13
|
-
elementsWithTabbableControls: string[];
|
|
14
|
-
|
|
15
|
-
constructor(element: HTMLElement) {
|
|
16
|
-
this.element = element;
|
|
17
|
-
|
|
18
|
-
this.elementsWithTabbableControls = ['iframe'];
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/** Activates focus trapping. */
|
|
22
|
-
activate() {
|
|
23
|
-
activeModals.push(this.element);
|
|
24
|
-
document.addEventListener('focusin', this.handleFocusIn);
|
|
25
|
-
document.addEventListener('keydown', this.handleKeyDown);
|
|
26
|
-
document.addEventListener('keyup', this.handleKeyUp);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/** Deactivates focus trapping. */
|
|
30
|
-
deactivate() {
|
|
31
|
-
activeModals = activeModals.filter(modal => modal !== this.element);
|
|
32
|
-
this.currentFocus = null;
|
|
33
|
-
document.removeEventListener('focusin', this.handleFocusIn);
|
|
34
|
-
document.removeEventListener('keydown', this.handleKeyDown);
|
|
35
|
-
document.removeEventListener('keyup', this.handleKeyUp);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/** Determines if this modal element is currently active or not. */
|
|
39
|
-
isActive() {
|
|
40
|
-
// The "active" modal is always the most recent one shown
|
|
41
|
-
return activeModals[activeModals.length - 1] === this.element;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/** Activates external modal behavior and temporarily disables focus trapping. */
|
|
45
|
-
activateExternal() {
|
|
46
|
-
this.isExternalActivated = true;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** Deactivates external modal behavior and re-enables focus trapping. */
|
|
50
|
-
deactivateExternal() {
|
|
51
|
-
this.isExternalActivated = false;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
private checkFocus() {
|
|
55
|
-
if (this.isActive() && !this.isExternalActivated) {
|
|
56
|
-
const tabbableElements = getTabbableElements(this.element);
|
|
57
|
-
if (!this.element.matches(':focus-within')) {
|
|
58
|
-
const start = tabbableElements[0];
|
|
59
|
-
const end = tabbableElements[tabbableElements.length - 1];
|
|
60
|
-
const target = this.tabDirection === 'forward' ? start : end;
|
|
61
|
-
|
|
62
|
-
if (typeof target?.focus === 'function') {
|
|
63
|
-
this.currentFocus = target;
|
|
64
|
-
target.focus({ preventScroll: false });
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
private handleFocusIn = () => {
|
|
71
|
-
if (!this.isActive()) return;
|
|
72
|
-
this.checkFocus();
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
private possiblyHasTabbableChildren(element: HTMLElement) {
|
|
76
|
-
return (
|
|
77
|
-
this.elementsWithTabbableControls.includes(
|
|
78
|
-
element.tagName.toLowerCase()
|
|
79
|
-
) || element.hasAttribute('controls')
|
|
80
|
-
// Should we add a data-attribute for people to set just in case they have an element where we don't know if it has possibly tabbable elements?
|
|
81
|
-
);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
private handleKeyDown = (event: KeyboardEvent) => {
|
|
85
|
-
if (event.key !== 'Tab' || this.isExternalActivated) return;
|
|
86
|
-
if (!this.isActive()) return;
|
|
87
|
-
|
|
88
|
-
// Because sometimes focus can actually be taken over from outside sources,
|
|
89
|
-
// we don't want to rely on `this.currentFocus`. Instead we check the actual `activeElement` and
|
|
90
|
-
// recurse through shadowRoots.
|
|
91
|
-
const currentActiveElement = getDeepestActiveElement();
|
|
92
|
-
this.previousFocus = currentActiveElement as HTMLElement | null;
|
|
93
|
-
|
|
94
|
-
if (
|
|
95
|
-
this.previousFocus &&
|
|
96
|
-
this.possiblyHasTabbableChildren(this.previousFocus)
|
|
97
|
-
) {
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
if (event.shiftKey) {
|
|
102
|
-
this.tabDirection = 'backward';
|
|
103
|
-
} else {
|
|
104
|
-
this.tabDirection = 'forward';
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
const tabbableElements = getTabbableElements(this.element);
|
|
108
|
-
|
|
109
|
-
let currentFocusIndex = tabbableElements.findIndex(
|
|
110
|
-
el => el === currentActiveElement
|
|
111
|
-
);
|
|
112
|
-
|
|
113
|
-
this.previousFocus = this.currentFocus;
|
|
114
|
-
|
|
115
|
-
const addition = this.tabDirection === 'forward' ? 1 : -1;
|
|
116
|
-
|
|
117
|
-
// eslint-disable-next-line
|
|
118
|
-
while (true) {
|
|
119
|
-
if (currentFocusIndex + addition >= tabbableElements.length) {
|
|
120
|
-
currentFocusIndex = 0;
|
|
121
|
-
} else if (currentFocusIndex + addition < 0) {
|
|
122
|
-
currentFocusIndex = tabbableElements.length - 1;
|
|
123
|
-
} else {
|
|
124
|
-
currentFocusIndex += addition;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
this.previousFocus = this.currentFocus;
|
|
128
|
-
const nextFocus =
|
|
129
|
-
/** @type {HTMLElement} */ tabbableElements[currentFocusIndex];
|
|
130
|
-
|
|
131
|
-
// This is a special case. We need to make sure we're not calling .focus() if we're already focused on an element
|
|
132
|
-
// that possibly has "controls"
|
|
133
|
-
if (this.tabDirection === 'backward') {
|
|
134
|
-
if (
|
|
135
|
-
this.previousFocus &&
|
|
136
|
-
this.possiblyHasTabbableChildren(this.previousFocus)
|
|
137
|
-
) {
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
if (nextFocus && this.possiblyHasTabbableChildren(nextFocus)) {
|
|
143
|
-
return;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
event.preventDefault();
|
|
147
|
-
this.currentFocus = nextFocus;
|
|
148
|
-
this.currentFocus?.focus({ preventScroll: false });
|
|
149
|
-
|
|
150
|
-
// Check to make sure focus actually changed. It may not always be the next focus, we just don't want it to be the previousFocus.
|
|
151
|
-
const allActiveElements = [...activeElements()];
|
|
152
|
-
if (
|
|
153
|
-
allActiveElements.includes(this.currentFocus) ||
|
|
154
|
-
!allActiveElements.includes(this.previousFocus!)
|
|
155
|
-
) {
|
|
156
|
-
break;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
setTimeout(() => this.checkFocus());
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
private handleKeyUp = () => {
|
|
164
|
-
this.tabDirection = 'forward';
|
|
165
|
-
};
|
|
166
|
-
}
|
|
1
|
+
/* istanbul ignore file */
|
|
2
|
+
import { activeElements, getDeepestActiveElement } from './active-elements.js';
|
|
3
|
+
import { getTabbableElements } from './tabbable.js';
|
|
4
|
+
|
|
5
|
+
let activeModals: HTMLElement[] = [];
|
|
6
|
+
|
|
7
|
+
export default class Modal {
|
|
8
|
+
element: HTMLElement;
|
|
9
|
+
isExternalActivated: boolean = false;
|
|
10
|
+
tabDirection: 'forward' | 'backward' = 'forward';
|
|
11
|
+
currentFocus: HTMLElement | null = null;
|
|
12
|
+
previousFocus: HTMLElement | null = null;
|
|
13
|
+
elementsWithTabbableControls: string[];
|
|
14
|
+
|
|
15
|
+
constructor(element: HTMLElement) {
|
|
16
|
+
this.element = element;
|
|
17
|
+
|
|
18
|
+
this.elementsWithTabbableControls = ['iframe'];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Activates focus trapping. */
|
|
22
|
+
activate() {
|
|
23
|
+
activeModals.push(this.element);
|
|
24
|
+
document.addEventListener('focusin', this.handleFocusIn);
|
|
25
|
+
document.addEventListener('keydown', this.handleKeyDown);
|
|
26
|
+
document.addEventListener('keyup', this.handleKeyUp);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Deactivates focus trapping. */
|
|
30
|
+
deactivate() {
|
|
31
|
+
activeModals = activeModals.filter(modal => modal !== this.element);
|
|
32
|
+
this.currentFocus = null;
|
|
33
|
+
document.removeEventListener('focusin', this.handleFocusIn);
|
|
34
|
+
document.removeEventListener('keydown', this.handleKeyDown);
|
|
35
|
+
document.removeEventListener('keyup', this.handleKeyUp);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Determines if this modal element is currently active or not. */
|
|
39
|
+
isActive() {
|
|
40
|
+
// The "active" modal is always the most recent one shown
|
|
41
|
+
return activeModals[activeModals.length - 1] === this.element;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Activates external modal behavior and temporarily disables focus trapping. */
|
|
45
|
+
activateExternal() {
|
|
46
|
+
this.isExternalActivated = true;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Deactivates external modal behavior and re-enables focus trapping. */
|
|
50
|
+
deactivateExternal() {
|
|
51
|
+
this.isExternalActivated = false;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private checkFocus() {
|
|
55
|
+
if (this.isActive() && !this.isExternalActivated) {
|
|
56
|
+
const tabbableElements = getTabbableElements(this.element);
|
|
57
|
+
if (!this.element.matches(':focus-within')) {
|
|
58
|
+
const start = tabbableElements[0];
|
|
59
|
+
const end = tabbableElements[tabbableElements.length - 1];
|
|
60
|
+
const target = this.tabDirection === 'forward' ? start : end;
|
|
61
|
+
|
|
62
|
+
if (typeof target?.focus === 'function') {
|
|
63
|
+
this.currentFocus = target;
|
|
64
|
+
target.focus({ preventScroll: false });
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
private handleFocusIn = () => {
|
|
71
|
+
if (!this.isActive()) return;
|
|
72
|
+
this.checkFocus();
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
private possiblyHasTabbableChildren(element: HTMLElement) {
|
|
76
|
+
return (
|
|
77
|
+
this.elementsWithTabbableControls.includes(
|
|
78
|
+
element.tagName.toLowerCase()
|
|
79
|
+
) || element.hasAttribute('controls')
|
|
80
|
+
// Should we add a data-attribute for people to set just in case they have an element where we don't know if it has possibly tabbable elements?
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
private handleKeyDown = (event: KeyboardEvent) => {
|
|
85
|
+
if (event.key !== 'Tab' || this.isExternalActivated) return;
|
|
86
|
+
if (!this.isActive()) return;
|
|
87
|
+
|
|
88
|
+
// Because sometimes focus can actually be taken over from outside sources,
|
|
89
|
+
// we don't want to rely on `this.currentFocus`. Instead we check the actual `activeElement` and
|
|
90
|
+
// recurse through shadowRoots.
|
|
91
|
+
const currentActiveElement = getDeepestActiveElement();
|
|
92
|
+
this.previousFocus = currentActiveElement as HTMLElement | null;
|
|
93
|
+
|
|
94
|
+
if (
|
|
95
|
+
this.previousFocus &&
|
|
96
|
+
this.possiblyHasTabbableChildren(this.previousFocus)
|
|
97
|
+
) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (event.shiftKey) {
|
|
102
|
+
this.tabDirection = 'backward';
|
|
103
|
+
} else {
|
|
104
|
+
this.tabDirection = 'forward';
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const tabbableElements = getTabbableElements(this.element);
|
|
108
|
+
|
|
109
|
+
let currentFocusIndex = tabbableElements.findIndex(
|
|
110
|
+
el => el === currentActiveElement
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
this.previousFocus = this.currentFocus;
|
|
114
|
+
|
|
115
|
+
const addition = this.tabDirection === 'forward' ? 1 : -1;
|
|
116
|
+
|
|
117
|
+
// eslint-disable-next-line
|
|
118
|
+
while (true) {
|
|
119
|
+
if (currentFocusIndex + addition >= tabbableElements.length) {
|
|
120
|
+
currentFocusIndex = 0;
|
|
121
|
+
} else if (currentFocusIndex + addition < 0) {
|
|
122
|
+
currentFocusIndex = tabbableElements.length - 1;
|
|
123
|
+
} else {
|
|
124
|
+
currentFocusIndex += addition;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
this.previousFocus = this.currentFocus;
|
|
128
|
+
const nextFocus =
|
|
129
|
+
/** @type {HTMLElement} */ tabbableElements[currentFocusIndex];
|
|
130
|
+
|
|
131
|
+
// This is a special case. We need to make sure we're not calling .focus() if we're already focused on an element
|
|
132
|
+
// that possibly has "controls"
|
|
133
|
+
if (this.tabDirection === 'backward') {
|
|
134
|
+
if (
|
|
135
|
+
this.previousFocus &&
|
|
136
|
+
this.possiblyHasTabbableChildren(this.previousFocus)
|
|
137
|
+
) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (nextFocus && this.possiblyHasTabbableChildren(nextFocus)) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
event.preventDefault();
|
|
147
|
+
this.currentFocus = nextFocus;
|
|
148
|
+
this.currentFocus?.focus({ preventScroll: false });
|
|
149
|
+
|
|
150
|
+
// Check to make sure focus actually changed. It may not always be the next focus, we just don't want it to be the previousFocus.
|
|
151
|
+
const allActiveElements = [...activeElements()];
|
|
152
|
+
if (
|
|
153
|
+
allActiveElements.includes(this.currentFocus) ||
|
|
154
|
+
!allActiveElements.includes(this.previousFocus!)
|
|
155
|
+
) {
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
setTimeout(() => this.checkFocus());
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
private handleKeyUp = () => {
|
|
164
|
+
this.tabDirection = 'forward';
|
|
165
|
+
};
|
|
166
|
+
}
|