@internetarchive/modal-manager 2.0.4 → 2.0.5-webdev-8155.1
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/.github/workflows/ci.yml +40 -30
- package/.github/workflows/gh-pages-main.yml +42 -42
- package/.github/workflows/pr-preview.yml +40 -40
- package/.prettierrc +4 -0
- package/.vscode/extensions.json +10 -0
- package/.vscode/tasks.json +12 -0
- package/LICENSE +661 -661
- package/README.md +139 -139
- package/custom-elements.json +170 -170
- package/demo/app-root.ts +366 -0
- package/dist/demo/app-root.d.ts +22 -0
- package/dist/demo/app-root.js +338 -0
- package/dist/demo/app-root.js.map +1 -0
- package/dist/index.d.ts +7 -7
- package/dist/index.js +5 -5
- package/dist/src/assets/arrow-left-icon.d.ts +2 -2
- package/dist/src/assets/arrow-left-icon.js +2 -2
- package/dist/src/assets/ia-logo-icon.d.ts +2 -2
- package/dist/src/assets/ia-logo-icon.js +2 -2
- package/dist/src/modal-config.d.ts +104 -104
- package/dist/src/modal-config.js +23 -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.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-mode.d.ts +10 -10
- package/dist/src/modal-manager-mode.js +11 -11
- package/dist/src/modal-manager.d.ts +137 -137
- package/dist/src/modal-manager.js +243 -249
- package/dist/src/modal-manager.js.map +1 -1
- package/dist/src/modal-template.d.ts +41 -41
- package/dist/src/modal-template.js +119 -118
- 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 +28 -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 +130 -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 +168 -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 +68 -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 +306 -309
- 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 +158 -167
- 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/vitest.config.ci.d.ts +2 -0
- package/dist/vitest.config.ci.js +24 -0
- package/dist/vitest.config.ci.js.map +1 -0
- 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/eslint.config.mjs +53 -0
- package/index.html +33 -300
- package/package.json +73 -85
- package/renovate.json +7 -7
- package/src/modal-config.ts +14 -14
- package/src/modal-manager-host-bridge.ts +2 -2
- package/src/modal-manager.ts +305 -305
- package/src/modal-template.ts +4 -2
- package/src/shoelace/LICENSE.md +6 -6
- package/src/shoelace/active-elements.ts +3 -2
- package/src/shoelace/modal.ts +5 -5
- package/src/shoelace/tabbable.ts +4 -3
- package/test/modal-config.test.ts +4 -4
- package/test/modal-manager.test.ts +402 -393
- package/test/modal-template.test.ts +42 -35
- package/tsconfig.json +25 -21
- package/vitest.config.ci.ts +27 -0
- package/.eslintrc.js +0 -14
- package/karma.conf.js +0 -24
|
@@ -1,132 +1,131 @@
|
|
|
1
|
-
/*
|
|
2
|
-
import { activeElements, getDeepestActiveElement } from './active-elements.js';
|
|
3
|
-
import { getTabbableElements } from './tabbable.js';
|
|
4
|
-
let activeModals = [];
|
|
5
|
-
export default class Modal {
|
|
6
|
-
constructor(element) {
|
|
7
|
-
this.isExternalActivated = false;
|
|
8
|
-
this.tabDirection = 'forward';
|
|
9
|
-
this.currentFocus = null;
|
|
10
|
-
this.previousFocus = null;
|
|
11
|
-
this.handleFocusIn = () => {
|
|
12
|
-
if (!this.isActive())
|
|
13
|
-
return;
|
|
14
|
-
this.checkFocus();
|
|
15
|
-
};
|
|
16
|
-
this.handleKeyDown = (event) => {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
//
|
|
23
|
-
//
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
this.previousFocus
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
document.addEventListener('
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
this.
|
|
95
|
-
document.removeEventListener('
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
}
|
|
1
|
+
/* c8 ignore start */
|
|
2
|
+
import { activeElements, getDeepestActiveElement } from './active-elements.js';
|
|
3
|
+
import { getTabbableElements } from './tabbable.js';
|
|
4
|
+
let activeModals = [];
|
|
5
|
+
export default class Modal {
|
|
6
|
+
constructor(element) {
|
|
7
|
+
this.isExternalActivated = false;
|
|
8
|
+
this.tabDirection = 'forward';
|
|
9
|
+
this.currentFocus = null;
|
|
10
|
+
this.previousFocus = null;
|
|
11
|
+
this.handleFocusIn = () => {
|
|
12
|
+
if (!this.isActive())
|
|
13
|
+
return;
|
|
14
|
+
this.checkFocus();
|
|
15
|
+
};
|
|
16
|
+
this.handleKeyDown = (event) => {
|
|
17
|
+
if (event.key !== 'Tab' || this.isExternalActivated)
|
|
18
|
+
return;
|
|
19
|
+
if (!this.isActive())
|
|
20
|
+
return;
|
|
21
|
+
// Because sometimes focus can actually be taken over from outside sources,
|
|
22
|
+
// we don't want to rely on `this.currentFocus`. Instead we check the actual `activeElement` and
|
|
23
|
+
// recurse through shadowRoots.
|
|
24
|
+
const currentActiveElement = getDeepestActiveElement();
|
|
25
|
+
this.previousFocus = currentActiveElement;
|
|
26
|
+
if (this.previousFocus &&
|
|
27
|
+
this.possiblyHasTabbableChildren(this.previousFocus)) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
if (event.shiftKey) {
|
|
31
|
+
this.tabDirection = 'backward';
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
this.tabDirection = 'forward';
|
|
35
|
+
}
|
|
36
|
+
const tabbableElements = getTabbableElements(this.element);
|
|
37
|
+
let currentFocusIndex = tabbableElements.findIndex((el) => el === currentActiveElement);
|
|
38
|
+
this.previousFocus = this.currentFocus;
|
|
39
|
+
const addition = this.tabDirection === 'forward' ? 1 : -1;
|
|
40
|
+
while (true) {
|
|
41
|
+
if (currentFocusIndex + addition >= tabbableElements.length) {
|
|
42
|
+
currentFocusIndex = 0;
|
|
43
|
+
}
|
|
44
|
+
else if (currentFocusIndex + addition < 0) {
|
|
45
|
+
currentFocusIndex = tabbableElements.length - 1;
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
currentFocusIndex += addition;
|
|
49
|
+
}
|
|
50
|
+
this.previousFocus = this.currentFocus;
|
|
51
|
+
const nextFocus =
|
|
52
|
+
/** @type {HTMLElement} */ tabbableElements[currentFocusIndex];
|
|
53
|
+
// This is a special case. We need to make sure we're not calling .focus() if we're already focused on an element
|
|
54
|
+
// that possibly has "controls"
|
|
55
|
+
if (this.tabDirection === 'backward') {
|
|
56
|
+
if (this.previousFocus &&
|
|
57
|
+
this.possiblyHasTabbableChildren(this.previousFocus)) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (nextFocus && this.possiblyHasTabbableChildren(nextFocus)) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
event.preventDefault();
|
|
65
|
+
this.currentFocus = nextFocus;
|
|
66
|
+
this.currentFocus?.focus({ preventScroll: false });
|
|
67
|
+
// 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.
|
|
68
|
+
const allActiveElements = [...activeElements()];
|
|
69
|
+
if (allActiveElements.includes(this.currentFocus) ||
|
|
70
|
+
!allActiveElements.includes(this.previousFocus)) {
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
setTimeout(() => this.checkFocus());
|
|
75
|
+
};
|
|
76
|
+
this.handleKeyUp = () => {
|
|
77
|
+
this.tabDirection = 'forward';
|
|
78
|
+
};
|
|
79
|
+
this.element = element;
|
|
80
|
+
this.elementsWithTabbableControls = ['iframe'];
|
|
81
|
+
}
|
|
82
|
+
/** Activates focus trapping. */
|
|
83
|
+
activate() {
|
|
84
|
+
activeModals.push(this.element);
|
|
85
|
+
document.addEventListener('focusin', this.handleFocusIn);
|
|
86
|
+
document.addEventListener('keydown', this.handleKeyDown);
|
|
87
|
+
document.addEventListener('keyup', this.handleKeyUp);
|
|
88
|
+
}
|
|
89
|
+
/** Deactivates focus trapping. */
|
|
90
|
+
deactivate() {
|
|
91
|
+
activeModals = activeModals.filter((modal) => modal !== this.element);
|
|
92
|
+
this.currentFocus = null;
|
|
93
|
+
document.removeEventListener('focusin', this.handleFocusIn);
|
|
94
|
+
document.removeEventListener('keydown', this.handleKeyDown);
|
|
95
|
+
document.removeEventListener('keyup', this.handleKeyUp);
|
|
96
|
+
}
|
|
97
|
+
/** Determines if this modal element is currently active or not. */
|
|
98
|
+
isActive() {
|
|
99
|
+
// The "active" modal is always the most recent one shown
|
|
100
|
+
return activeModals[activeModals.length - 1] === this.element;
|
|
101
|
+
}
|
|
102
|
+
/** Activates external modal behavior and temporarily disables focus trapping. */
|
|
103
|
+
activateExternal() {
|
|
104
|
+
this.isExternalActivated = true;
|
|
105
|
+
}
|
|
106
|
+
/** Deactivates external modal behavior and re-enables focus trapping. */
|
|
107
|
+
deactivateExternal() {
|
|
108
|
+
this.isExternalActivated = false;
|
|
109
|
+
}
|
|
110
|
+
checkFocus() {
|
|
111
|
+
if (this.isActive() && !this.isExternalActivated) {
|
|
112
|
+
const tabbableElements = getTabbableElements(this.element);
|
|
113
|
+
if (!this.element.matches(':focus-within')) {
|
|
114
|
+
const start = tabbableElements[0];
|
|
115
|
+
const end = tabbableElements[tabbableElements.length - 1];
|
|
116
|
+
const target = this.tabDirection === 'forward' ? start : end;
|
|
117
|
+
if (typeof target?.focus === 'function') {
|
|
118
|
+
this.currentFocus = target;
|
|
119
|
+
target.focus({ preventScroll: false });
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
possiblyHasTabbableChildren(element) {
|
|
125
|
+
return (this.elementsWithTabbableControls.includes(element.tagName.toLowerCase()) || element.hasAttribute('controls')
|
|
126
|
+
// 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?
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/* c8 ignore end */
|
|
132
131
|
//# sourceMappingURL=modal.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"modal.js","sourceRoot":"","sources":["../../../src/shoelace/modal.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"modal.js","sourceRoot":"","sources":["../../../src/shoelace/modal.ts"],"names":[],"mappings":"AAAA,qBAAqB;AACrB,OAAO,EAAE,cAAc,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAC;AAC/E,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AAEpD,IAAI,YAAY,GAAkB,EAAE,CAAC;AAErC,MAAM,CAAC,OAAO,OAAO,KAAK;IAQxB,YAAY,OAAoB;QANhC,wBAAmB,GAAY,KAAK,CAAC;QACrC,iBAAY,GAA2B,SAAS,CAAC;QACjD,iBAAY,GAAuB,IAAI,CAAC;QACxC,kBAAa,GAAuB,IAAI,CAAC;QA0DjC,kBAAa,GAAG,GAAG,EAAE;YAC3B,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE;gBAAE,OAAO;YAC7B,IAAI,CAAC,UAAU,EAAE,CAAC;QACpB,CAAC,CAAC;QAWM,kBAAa,GAAG,CAAC,KAAoB,EAAE,EAAE;YAC/C,IAAI,KAAK,CAAC,GAAG,KAAK,KAAK,IAAI,IAAI,CAAC,mBAAmB;gBAAE,OAAO;YAC5D,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE;gBAAE,OAAO;YAE7B,2EAA2E;YAC3E,gGAAgG;YAChG,+BAA+B;YAC/B,MAAM,oBAAoB,GAAG,uBAAuB,EAAE,CAAC;YACvD,IAAI,CAAC,aAAa,GAAG,oBAA0C,CAAC;YAEhE,IACE,IAAI,CAAC,aAAa;gBAClB,IAAI,CAAC,2BAA2B,CAAC,IAAI,CAAC,aAAa,CAAC,EACpD,CAAC;gBACD,OAAO;YACT,CAAC;YAED,IAAI,KAAK,CAAC,QAAQ,EAAE,CAAC;gBACnB,IAAI,CAAC,YAAY,GAAG,UAAU,CAAC;YACjC,CAAC;iB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AAC;QACH,CAAC;IACH,CAAC;IAOO,2BAA2B,CAAC,OAAoB;QACtD,OAAO,CACL,IAAI,CAAC,4BAA4B,CAAC,QAAQ,CACxC,OAAO,CAAC,OAAO,CAAC,WAAW,EAAE,CAC9B,IAAI,OAAO,CAAC,YAAY,CAAC,UAAU,CAAC;QACrC,+IAA+I;SAChJ,CAAC;IACJ,CAAC;CAmFF;AACD,mBAAmB","sourcesContent":["/* c8 ignore start */\nimport { activeElements, getDeepestActiveElement } from './active-elements.js';\nimport { getTabbableElements } from './tabbable.js';\n\nlet activeModals: HTMLElement[] = [];\n\nexport default class Modal {\n element: HTMLElement;\n isExternalActivated: boolean = false;\n tabDirection: 'forward' | 'backward' = 'forward';\n currentFocus: HTMLElement | null = null;\n previousFocus: HTMLElement | null = null;\n elementsWithTabbableControls: string[];\n\n constructor(element: HTMLElement) {\n this.element = element;\n\n this.elementsWithTabbableControls = ['iframe'];\n }\n\n /** Activates focus trapping. */\n activate() {\n activeModals.push(this.element);\n document.addEventListener('focusin', this.handleFocusIn);\n document.addEventListener('keydown', this.handleKeyDown);\n document.addEventListener('keyup', this.handleKeyUp);\n }\n\n /** Deactivates focus trapping. */\n deactivate() {\n activeModals = activeModals.filter((modal) => modal !== this.element);\n this.currentFocus = null;\n document.removeEventListener('focusin', this.handleFocusIn);\n document.removeEventListener('keydown', this.handleKeyDown);\n document.removeEventListener('keyup', this.handleKeyUp);\n }\n\n /** Determines if this modal element is currently active or not. */\n isActive() {\n // The \"active\" modal is always the most recent one shown\n return activeModals[activeModals.length - 1] === this.element;\n }\n\n /** Activates external modal behavior and temporarily disables focus trapping. */\n activateExternal() {\n this.isExternalActivated = true;\n }\n\n /** Deactivates external modal behavior and re-enables focus trapping. */\n deactivateExternal() {\n this.isExternalActivated = false;\n }\n\n private checkFocus() {\n if (this.isActive() && !this.isExternalActivated) {\n const tabbableElements = getTabbableElements(this.element);\n if (!this.element.matches(':focus-within')) {\n const start = tabbableElements[0];\n const end = tabbableElements[tabbableElements.length - 1];\n const target = this.tabDirection === 'forward' ? start : end;\n\n if (typeof target?.focus === 'function') {\n this.currentFocus = target;\n target.focus({ preventScroll: false });\n }\n }\n }\n }\n\n private handleFocusIn = () => {\n if (!this.isActive()) return;\n this.checkFocus();\n };\n\n private possiblyHasTabbableChildren(element: HTMLElement) {\n return (\n this.elementsWithTabbableControls.includes(\n element.tagName.toLowerCase(),\n ) || element.hasAttribute('controls')\n // 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?\n );\n }\n\n private handleKeyDown = (event: KeyboardEvent) => {\n if (event.key !== 'Tab' || this.isExternalActivated) return;\n if (!this.isActive()) return;\n\n // Because sometimes focus can actually be taken over from outside sources,\n // we don't want to rely on `this.currentFocus`. Instead we check the actual `activeElement` and\n // recurse through shadowRoots.\n const currentActiveElement = getDeepestActiveElement();\n this.previousFocus = currentActiveElement as HTMLElement | null;\n\n if (\n this.previousFocus &&\n this.possiblyHasTabbableChildren(this.previousFocus)\n ) {\n return;\n }\n\n if (event.shiftKey) {\n this.tabDirection = 'backward';\n } else {\n this.tabDirection = 'forward';\n }\n\n const tabbableElements = getTabbableElements(this.element);\n\n let currentFocusIndex = tabbableElements.findIndex(\n (el) => el === currentActiveElement,\n );\n\n this.previousFocus = this.currentFocus;\n\n const addition = this.tabDirection === 'forward' ? 1 : -1;\n\n while (true) {\n if (currentFocusIndex + addition >= tabbableElements.length) {\n currentFocusIndex = 0;\n } else if (currentFocusIndex + addition < 0) {\n currentFocusIndex = tabbableElements.length - 1;\n } else {\n currentFocusIndex += addition;\n }\n\n this.previousFocus = this.currentFocus;\n const nextFocus =\n /** @type {HTMLElement} */ tabbableElements[currentFocusIndex];\n\n // This is a special case. We need to make sure we're not calling .focus() if we're already focused on an element\n // that possibly has \"controls\"\n if (this.tabDirection === 'backward') {\n if (\n this.previousFocus &&\n this.possiblyHasTabbableChildren(this.previousFocus)\n ) {\n return;\n }\n }\n\n if (nextFocus && this.possiblyHasTabbableChildren(nextFocus)) {\n return;\n }\n\n event.preventDefault();\n this.currentFocus = nextFocus;\n this.currentFocus?.focus({ preventScroll: false });\n\n // 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.\n const allActiveElements = [...activeElements()];\n if (\n allActiveElements.includes(this.currentFocus) ||\n !allActiveElements.includes(this.previousFocus!)\n ) {\n break;\n }\n }\n\n setTimeout(() => this.checkFocus());\n };\n\n private handleKeyUp = () => {\n this.tabDirection = 'forward';\n };\n}\n/* c8 ignore end */\n"]}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Returns the first and last bounding elements that are tabbable. This is more performant than checking every single
|
|
3
|
-
* element because it short-circuits after finding the first and last ones.
|
|
4
|
-
*/
|
|
5
|
-
export declare function getTabbableBoundary(root: HTMLElement | ShadowRoot): {
|
|
6
|
-
start: HTMLElement;
|
|
7
|
-
end: HTMLElement;
|
|
8
|
-
};
|
|
9
|
-
export declare function getTabbableElements(root: HTMLElement | ShadowRoot): HTMLElement[];
|
|
1
|
+
/**
|
|
2
|
+
* Returns the first and last bounding elements that are tabbable. This is more performant than checking every single
|
|
3
|
+
* element because it short-circuits after finding the first and last ones.
|
|
4
|
+
*/
|
|
5
|
+
export declare function getTabbableBoundary(root: HTMLElement | ShadowRoot): {
|
|
6
|
+
start: HTMLElement;
|
|
7
|
+
end: HTMLElement;
|
|
8
|
+
};
|
|
9
|
+
export declare function getTabbableElements(root: HTMLElement | ShadowRoot): HTMLElement[];
|