@internetarchive/modal-manager 2.0.3 → 2.0.4-alpha-webdev7960.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/.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/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 +24 -24
- 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-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 -127
- package/dist/src/modal-manager.js +212 -197
- package/dist/src/modal-manager.js.map +1 -1
- package/dist/src/modal-template.d.ts +41 -41
- package/dist/src/modal-template.js +115 -115
- package/dist/src/shoelace/active-elements.d.ts +15 -15
- package/dist/src/shoelace/active-elements.js +27 -27
- package/dist/src/shoelace/modal.d.ts +24 -24
- package/dist/src/shoelace/modal.js +131 -131
- package/dist/src/shoelace/tabbable.d.ts +9 -9
- package/dist/src/shoelace/tabbable.js +169 -169
- package/dist/test/modal-config.test.d.ts +1 -1
- package/dist/test/modal-config.test.js +69 -69
- package/dist/test/modal-manager.test.d.ts +1 -1
- package/dist/test/modal-manager.test.js +275 -240
- 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 +156 -156
- package/dist/vite.config.d.ts +2 -2
- package/dist/vite.config.js +22 -22
- 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/karma.conf.js +24 -24
- package/package.json +85 -85
- package/renovate.json +7 -7
- package/src/modal-manager.ts +22 -0
- package/src/shoelace/LICENSE.md +6 -6
- package/test/modal-manager.test.ts +52 -6
- package/tsconfig.json +21 -21
|
@@ -1,132 +1,132 @@
|
|
|
1
|
-
/* istanbul ignore file */
|
|
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
|
-
var _a;
|
|
18
|
-
if (event.key !== 'Tab' || this.isExternalActivated)
|
|
19
|
-
return;
|
|
20
|
-
if (!this.isActive())
|
|
21
|
-
return;
|
|
22
|
-
// Because sometimes focus can actually be taken over from outside sources,
|
|
23
|
-
// we don't want to rely on `this.currentFocus`. Instead we check the actual `activeElement` and
|
|
24
|
-
// recurse through shadowRoots.
|
|
25
|
-
const currentActiveElement = getDeepestActiveElement();
|
|
26
|
-
this.previousFocus = currentActiveElement;
|
|
27
|
-
if (this.previousFocus &&
|
|
28
|
-
this.possiblyHasTabbableChildren(this.previousFocus)) {
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
if (event.shiftKey) {
|
|
32
|
-
this.tabDirection = 'backward';
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
35
|
-
this.tabDirection = 'forward';
|
|
36
|
-
}
|
|
37
|
-
const tabbableElements = getTabbableElements(this.element);
|
|
38
|
-
let currentFocusIndex = tabbableElements.findIndex(el => el === currentActiveElement);
|
|
39
|
-
this.previousFocus = this.currentFocus;
|
|
40
|
-
const addition = this.tabDirection === 'forward' ? 1 : -1;
|
|
41
|
-
// eslint-disable-next-line
|
|
42
|
-
while (true) {
|
|
43
|
-
if (currentFocusIndex + addition >= tabbableElements.length) {
|
|
44
|
-
currentFocusIndex = 0;
|
|
45
|
-
}
|
|
46
|
-
else if (currentFocusIndex + addition < 0) {
|
|
47
|
-
currentFocusIndex = tabbableElements.length - 1;
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
currentFocusIndex += addition;
|
|
51
|
-
}
|
|
52
|
-
this.previousFocus = this.currentFocus;
|
|
53
|
-
const nextFocus =
|
|
54
|
-
/** @type {HTMLElement} */ tabbableElements[currentFocusIndex];
|
|
55
|
-
// This is a special case. We need to make sure we're not calling .focus() if we're already focused on an element
|
|
56
|
-
// that possibly has "controls"
|
|
57
|
-
if (this.tabDirection === 'backward') {
|
|
58
|
-
if (this.previousFocus &&
|
|
59
|
-
this.possiblyHasTabbableChildren(this.previousFocus)) {
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
if (nextFocus && this.possiblyHasTabbableChildren(nextFocus)) {
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
event.preventDefault();
|
|
67
|
-
this.currentFocus = nextFocus;
|
|
68
|
-
(_a = this.currentFocus) === null || _a === void 0 ? void 0 : _a.focus({ preventScroll: false });
|
|
69
|
-
// 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.
|
|
70
|
-
const allActiveElements = [...activeElements()];
|
|
71
|
-
if (allActiveElements.includes(this.currentFocus) ||
|
|
72
|
-
!allActiveElements.includes(this.previousFocus)) {
|
|
73
|
-
break;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
setTimeout(() => this.checkFocus());
|
|
77
|
-
};
|
|
78
|
-
this.handleKeyUp = () => {
|
|
79
|
-
this.tabDirection = 'forward';
|
|
80
|
-
};
|
|
81
|
-
this.element = element;
|
|
82
|
-
this.elementsWithTabbableControls = ['iframe'];
|
|
83
|
-
}
|
|
84
|
-
/** Activates focus trapping. */
|
|
85
|
-
activate() {
|
|
86
|
-
activeModals.push(this.element);
|
|
87
|
-
document.addEventListener('focusin', this.handleFocusIn);
|
|
88
|
-
document.addEventListener('keydown', this.handleKeyDown);
|
|
89
|
-
document.addEventListener('keyup', this.handleKeyUp);
|
|
90
|
-
}
|
|
91
|
-
/** Deactivates focus trapping. */
|
|
92
|
-
deactivate() {
|
|
93
|
-
activeModals = activeModals.filter(modal => modal !== this.element);
|
|
94
|
-
this.currentFocus = null;
|
|
95
|
-
document.removeEventListener('focusin', this.handleFocusIn);
|
|
96
|
-
document.removeEventListener('keydown', this.handleKeyDown);
|
|
97
|
-
document.removeEventListener('keyup', this.handleKeyUp);
|
|
98
|
-
}
|
|
99
|
-
/** Determines if this modal element is currently active or not. */
|
|
100
|
-
isActive() {
|
|
101
|
-
// The "active" modal is always the most recent one shown
|
|
102
|
-
return activeModals[activeModals.length - 1] === this.element;
|
|
103
|
-
}
|
|
104
|
-
/** Activates external modal behavior and temporarily disables focus trapping. */
|
|
105
|
-
activateExternal() {
|
|
106
|
-
this.isExternalActivated = true;
|
|
107
|
-
}
|
|
108
|
-
/** Deactivates external modal behavior and re-enables focus trapping. */
|
|
109
|
-
deactivateExternal() {
|
|
110
|
-
this.isExternalActivated = false;
|
|
111
|
-
}
|
|
112
|
-
checkFocus() {
|
|
113
|
-
if (this.isActive() && !this.isExternalActivated) {
|
|
114
|
-
const tabbableElements = getTabbableElements(this.element);
|
|
115
|
-
if (!this.element.matches(':focus-within')) {
|
|
116
|
-
const start = tabbableElements[0];
|
|
117
|
-
const end = tabbableElements[tabbableElements.length - 1];
|
|
118
|
-
const target = this.tabDirection === 'forward' ? start : end;
|
|
119
|
-
if (typeof (target === null || target === void 0 ? void 0 : target.focus) === 'function') {
|
|
120
|
-
this.currentFocus = target;
|
|
121
|
-
target.focus({ preventScroll: false });
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
possiblyHasTabbableChildren(element) {
|
|
127
|
-
return (this.elementsWithTabbableControls.includes(element.tagName.toLowerCase()) || element.hasAttribute('controls')
|
|
128
|
-
// 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?
|
|
129
|
-
);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
1
|
+
/* istanbul ignore file */
|
|
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
|
+
var _a;
|
|
18
|
+
if (event.key !== 'Tab' || this.isExternalActivated)
|
|
19
|
+
return;
|
|
20
|
+
if (!this.isActive())
|
|
21
|
+
return;
|
|
22
|
+
// Because sometimes focus can actually be taken over from outside sources,
|
|
23
|
+
// we don't want to rely on `this.currentFocus`. Instead we check the actual `activeElement` and
|
|
24
|
+
// recurse through shadowRoots.
|
|
25
|
+
const currentActiveElement = getDeepestActiveElement();
|
|
26
|
+
this.previousFocus = currentActiveElement;
|
|
27
|
+
if (this.previousFocus &&
|
|
28
|
+
this.possiblyHasTabbableChildren(this.previousFocus)) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
if (event.shiftKey) {
|
|
32
|
+
this.tabDirection = 'backward';
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
this.tabDirection = 'forward';
|
|
36
|
+
}
|
|
37
|
+
const tabbableElements = getTabbableElements(this.element);
|
|
38
|
+
let currentFocusIndex = tabbableElements.findIndex(el => el === currentActiveElement);
|
|
39
|
+
this.previousFocus = this.currentFocus;
|
|
40
|
+
const addition = this.tabDirection === 'forward' ? 1 : -1;
|
|
41
|
+
// eslint-disable-next-line
|
|
42
|
+
while (true) {
|
|
43
|
+
if (currentFocusIndex + addition >= tabbableElements.length) {
|
|
44
|
+
currentFocusIndex = 0;
|
|
45
|
+
}
|
|
46
|
+
else if (currentFocusIndex + addition < 0) {
|
|
47
|
+
currentFocusIndex = tabbableElements.length - 1;
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
currentFocusIndex += addition;
|
|
51
|
+
}
|
|
52
|
+
this.previousFocus = this.currentFocus;
|
|
53
|
+
const nextFocus =
|
|
54
|
+
/** @type {HTMLElement} */ tabbableElements[currentFocusIndex];
|
|
55
|
+
// This is a special case. We need to make sure we're not calling .focus() if we're already focused on an element
|
|
56
|
+
// that possibly has "controls"
|
|
57
|
+
if (this.tabDirection === 'backward') {
|
|
58
|
+
if (this.previousFocus &&
|
|
59
|
+
this.possiblyHasTabbableChildren(this.previousFocus)) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (nextFocus && this.possiblyHasTabbableChildren(nextFocus)) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
event.preventDefault();
|
|
67
|
+
this.currentFocus = nextFocus;
|
|
68
|
+
(_a = this.currentFocus) === null || _a === void 0 ? void 0 : _a.focus({ preventScroll: false });
|
|
69
|
+
// 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.
|
|
70
|
+
const allActiveElements = [...activeElements()];
|
|
71
|
+
if (allActiveElements.includes(this.currentFocus) ||
|
|
72
|
+
!allActiveElements.includes(this.previousFocus)) {
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
setTimeout(() => this.checkFocus());
|
|
77
|
+
};
|
|
78
|
+
this.handleKeyUp = () => {
|
|
79
|
+
this.tabDirection = 'forward';
|
|
80
|
+
};
|
|
81
|
+
this.element = element;
|
|
82
|
+
this.elementsWithTabbableControls = ['iframe'];
|
|
83
|
+
}
|
|
84
|
+
/** Activates focus trapping. */
|
|
85
|
+
activate() {
|
|
86
|
+
activeModals.push(this.element);
|
|
87
|
+
document.addEventListener('focusin', this.handleFocusIn);
|
|
88
|
+
document.addEventListener('keydown', this.handleKeyDown);
|
|
89
|
+
document.addEventListener('keyup', this.handleKeyUp);
|
|
90
|
+
}
|
|
91
|
+
/** Deactivates focus trapping. */
|
|
92
|
+
deactivate() {
|
|
93
|
+
activeModals = activeModals.filter(modal => modal !== this.element);
|
|
94
|
+
this.currentFocus = null;
|
|
95
|
+
document.removeEventListener('focusin', this.handleFocusIn);
|
|
96
|
+
document.removeEventListener('keydown', this.handleKeyDown);
|
|
97
|
+
document.removeEventListener('keyup', this.handleKeyUp);
|
|
98
|
+
}
|
|
99
|
+
/** Determines if this modal element is currently active or not. */
|
|
100
|
+
isActive() {
|
|
101
|
+
// The "active" modal is always the most recent one shown
|
|
102
|
+
return activeModals[activeModals.length - 1] === this.element;
|
|
103
|
+
}
|
|
104
|
+
/** Activates external modal behavior and temporarily disables focus trapping. */
|
|
105
|
+
activateExternal() {
|
|
106
|
+
this.isExternalActivated = true;
|
|
107
|
+
}
|
|
108
|
+
/** Deactivates external modal behavior and re-enables focus trapping. */
|
|
109
|
+
deactivateExternal() {
|
|
110
|
+
this.isExternalActivated = false;
|
|
111
|
+
}
|
|
112
|
+
checkFocus() {
|
|
113
|
+
if (this.isActive() && !this.isExternalActivated) {
|
|
114
|
+
const tabbableElements = getTabbableElements(this.element);
|
|
115
|
+
if (!this.element.matches(':focus-within')) {
|
|
116
|
+
const start = tabbableElements[0];
|
|
117
|
+
const end = tabbableElements[tabbableElements.length - 1];
|
|
118
|
+
const target = this.tabDirection === 'forward' ? start : end;
|
|
119
|
+
if (typeof (target === null || target === void 0 ? void 0 : target.focus) === 'function') {
|
|
120
|
+
this.currentFocus = target;
|
|
121
|
+
target.focus({ preventScroll: false });
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
possiblyHasTabbableChildren(element) {
|
|
127
|
+
return (this.elementsWithTabbableControls.includes(element.tagName.toLowerCase()) || element.hasAttribute('controls')
|
|
128
|
+
// 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?
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
132
|
//# sourceMappingURL=modal.js.map
|
|
@@ -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[];
|
|
@@ -1,170 +1,170 @@
|
|
|
1
|
-
/* istanbul ignore file */
|
|
2
|
-
// Cached compute style calls. This is specifically for browsers that dont support `checkVisibility()`.
|
|
3
|
-
// computedStyle calls are "live" so they only need to be retrieved once for an element.
|
|
4
|
-
const computedStyleMap = new WeakMap();
|
|
5
|
-
function getCachedComputedStyle(el) {
|
|
6
|
-
let computedStyle = computedStyleMap.get(el);
|
|
7
|
-
if (!computedStyle) {
|
|
8
|
-
computedStyle = window.getComputedStyle(el, null);
|
|
9
|
-
computedStyleMap.set(el, computedStyle);
|
|
10
|
-
}
|
|
11
|
-
return computedStyle;
|
|
12
|
-
}
|
|
13
|
-
function isVisible(el) {
|
|
14
|
-
// This is the fastest check, but isn't supported in Safari.
|
|
15
|
-
if ('checkVisibility' in el && typeof el['checkVisibility'] === 'function') {
|
|
16
|
-
// Opacity is focusable, visibility is not.
|
|
17
|
-
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
|
18
|
-
return el['checkVisibility']({
|
|
19
|
-
checkOpacity: false,
|
|
20
|
-
checkVisibilityCSS: true,
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
// Fallback "polyfill" for "checkVisibility"
|
|
24
|
-
const computedStyle = getCachedComputedStyle(el);
|
|
25
|
-
return (computedStyle.visibility !== 'hidden' && computedStyle.display !== 'none');
|
|
26
|
-
}
|
|
27
|
-
// While this behavior isn't standard in Safari / Chrome yet, I think it's the most reasonable
|
|
28
|
-
// way of handling tabbable overflow areas. Browser sniffing seems gross, and it's the most
|
|
29
|
-
// accessible way of handling overflow areas. [Konnor]
|
|
30
|
-
function isOverflowingAndTabbable(el) {
|
|
31
|
-
const computedStyle = getCachedComputedStyle(el);
|
|
32
|
-
const { overflowY, overflowX } = computedStyle;
|
|
33
|
-
if (overflowY === 'scroll' || overflowX === 'scroll') {
|
|
34
|
-
return true;
|
|
35
|
-
}
|
|
36
|
-
if (overflowY !== 'auto' || overflowX !== 'auto') {
|
|
37
|
-
return false;
|
|
38
|
-
}
|
|
39
|
-
// Always overflow === "auto" by this point
|
|
40
|
-
const isOverflowingY = el.scrollHeight > el.clientHeight;
|
|
41
|
-
if (isOverflowingY && overflowY === 'auto') {
|
|
42
|
-
return true;
|
|
43
|
-
}
|
|
44
|
-
const isOverflowingX = el.scrollWidth > el.clientWidth;
|
|
45
|
-
if (isOverflowingX && overflowX === 'auto') {
|
|
46
|
-
return true;
|
|
47
|
-
}
|
|
48
|
-
return false;
|
|
49
|
-
}
|
|
50
|
-
/** Determines if the specified element is tabbable using heuristics inspired by https://github.com/focus-trap/tabbable */
|
|
51
|
-
function isTabbable(el) {
|
|
52
|
-
const tag = el.tagName.toLowerCase();
|
|
53
|
-
const tabindex = Number(el.getAttribute('tabindex'));
|
|
54
|
-
const hasTabindex = el.hasAttribute('tabindex');
|
|
55
|
-
// elements with a tabindex attribute that is either NaN or <= -1 are not tabbable
|
|
56
|
-
if (hasTabindex && (isNaN(tabindex) || tabindex <= -1)) {
|
|
57
|
-
return false;
|
|
58
|
-
}
|
|
59
|
-
// Elements with a disabled attribute are not tabbable
|
|
60
|
-
if (el.hasAttribute('disabled')) {
|
|
61
|
-
return false;
|
|
62
|
-
}
|
|
63
|
-
// If any parents have "inert", we aren't "tabbable"
|
|
64
|
-
if (el.closest('[inert]')) {
|
|
65
|
-
return false;
|
|
66
|
-
}
|
|
67
|
-
// Radios without a checked attribute are not tabbable
|
|
68
|
-
if (tag === 'input' &&
|
|
69
|
-
el.getAttribute('type') === 'radio' &&
|
|
70
|
-
!el.hasAttribute('checked')) {
|
|
71
|
-
return false;
|
|
72
|
-
}
|
|
73
|
-
if (!isVisible(el)) {
|
|
74
|
-
return false;
|
|
75
|
-
}
|
|
76
|
-
// Audio and video elements with the controls attribute are tabbable
|
|
77
|
-
if ((tag === 'audio' || tag === 'video') && el.hasAttribute('controls')) {
|
|
78
|
-
return true;
|
|
79
|
-
}
|
|
80
|
-
// Elements with a tabindex other than -1 are tabbable
|
|
81
|
-
if (el.hasAttribute('tabindex')) {
|
|
82
|
-
return true;
|
|
83
|
-
}
|
|
84
|
-
// Elements with a contenteditable attribute are tabbable
|
|
85
|
-
if (el.hasAttribute('contenteditable') &&
|
|
86
|
-
el.getAttribute('contenteditable') !== 'false') {
|
|
87
|
-
return true;
|
|
88
|
-
}
|
|
89
|
-
// At this point, the following elements are considered tabbable
|
|
90
|
-
const isNativelyTabbable = [
|
|
91
|
-
'button',
|
|
92
|
-
'input',
|
|
93
|
-
'select',
|
|
94
|
-
'textarea',
|
|
95
|
-
'a',
|
|
96
|
-
'audio',
|
|
97
|
-
'video',
|
|
98
|
-
'summary',
|
|
99
|
-
'iframe',
|
|
100
|
-
].includes(tag);
|
|
101
|
-
if (isNativelyTabbable) {
|
|
102
|
-
return true;
|
|
103
|
-
}
|
|
104
|
-
// We save the overflow checks for last, because they're the most expensive
|
|
105
|
-
return isOverflowingAndTabbable(el);
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Returns the first and last bounding elements that are tabbable. This is more performant than checking every single
|
|
109
|
-
* element because it short-circuits after finding the first and last ones.
|
|
110
|
-
*/
|
|
111
|
-
export function getTabbableBoundary(root) {
|
|
112
|
-
var _a, _b;
|
|
113
|
-
const tabbableElements = getTabbableElements(root);
|
|
114
|
-
// Find the first and last tabbable elements
|
|
115
|
-
const start = (_a = tabbableElements[0]) !== null && _a !== void 0 ? _a : null;
|
|
116
|
-
const end = (_b = tabbableElements[tabbableElements.length - 1]) !== null && _b !== void 0 ? _b : null;
|
|
117
|
-
return { start, end };
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* This looks funky. Basically a slot's children will always be picked up *if* they're within the `root` element.
|
|
121
|
-
* However, there is an edge case when, if the `root` is wrapped by another shadow DOM, it won't grab the children.
|
|
122
|
-
* This fixes that fun edge case.
|
|
123
|
-
*/
|
|
124
|
-
function getSlottedChildrenOutsideRootElement(slotElement, root) {
|
|
125
|
-
var _a;
|
|
126
|
-
return (((_a = slotElement.getRootNode({ composed: true })) === null || _a === void 0 ? void 0 : _a.host) !==
|
|
127
|
-
root);
|
|
128
|
-
}
|
|
129
|
-
export function getTabbableElements(root) {
|
|
130
|
-
const walkedEls = new WeakMap();
|
|
131
|
-
const tabbableElements = [];
|
|
132
|
-
function walk(el) {
|
|
133
|
-
if (el instanceof Element) {
|
|
134
|
-
// if the element has "inert" we can just no-op it.
|
|
135
|
-
if (el.hasAttribute('inert') || el.closest('[inert]')) {
|
|
136
|
-
return;
|
|
137
|
-
}
|
|
138
|
-
if (walkedEls.has(el)) {
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
walkedEls.set(el, true);
|
|
142
|
-
if (!tabbableElements.includes(el) && isTabbable(el)) {
|
|
143
|
-
tabbableElements.push(el);
|
|
144
|
-
}
|
|
145
|
-
if (el instanceof HTMLSlotElement &&
|
|
146
|
-
getSlottedChildrenOutsideRootElement(el, root)) {
|
|
147
|
-
el.assignedElements({ flatten: true }).forEach((assignedEl) => {
|
|
148
|
-
walk(assignedEl);
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
if (el.shadowRoot !== null && el.shadowRoot.mode === 'open') {
|
|
152
|
-
walk(el.shadowRoot);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
for (const e of Array.from(el.children)) {
|
|
156
|
-
walk(e);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
// Collect all elements including the root
|
|
160
|
-
walk(root);
|
|
161
|
-
// Is this worth having? Most sorts will always add increased overhead. And positive tabindexes shouldn't really be used.
|
|
162
|
-
// So is it worth being right? Or fast?
|
|
163
|
-
return tabbableElements.sort((a, b) => {
|
|
164
|
-
// Make sure we sort by tabindex.
|
|
165
|
-
const aTabindex = Number(a.getAttribute('tabindex')) || 0;
|
|
166
|
-
const bTabindex = Number(b.getAttribute('tabindex')) || 0;
|
|
167
|
-
return bTabindex - aTabindex;
|
|
168
|
-
});
|
|
169
|
-
}
|
|
1
|
+
/* istanbul ignore file */
|
|
2
|
+
// Cached compute style calls. This is specifically for browsers that dont support `checkVisibility()`.
|
|
3
|
+
// computedStyle calls are "live" so they only need to be retrieved once for an element.
|
|
4
|
+
const computedStyleMap = new WeakMap();
|
|
5
|
+
function getCachedComputedStyle(el) {
|
|
6
|
+
let computedStyle = computedStyleMap.get(el);
|
|
7
|
+
if (!computedStyle) {
|
|
8
|
+
computedStyle = window.getComputedStyle(el, null);
|
|
9
|
+
computedStyleMap.set(el, computedStyle);
|
|
10
|
+
}
|
|
11
|
+
return computedStyle;
|
|
12
|
+
}
|
|
13
|
+
function isVisible(el) {
|
|
14
|
+
// This is the fastest check, but isn't supported in Safari.
|
|
15
|
+
if ('checkVisibility' in el && typeof el['checkVisibility'] === 'function') {
|
|
16
|
+
// Opacity is focusable, visibility is not.
|
|
17
|
+
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
|
18
|
+
return el['checkVisibility']({
|
|
19
|
+
checkOpacity: false,
|
|
20
|
+
checkVisibilityCSS: true,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
// Fallback "polyfill" for "checkVisibility"
|
|
24
|
+
const computedStyle = getCachedComputedStyle(el);
|
|
25
|
+
return (computedStyle.visibility !== 'hidden' && computedStyle.display !== 'none');
|
|
26
|
+
}
|
|
27
|
+
// While this behavior isn't standard in Safari / Chrome yet, I think it's the most reasonable
|
|
28
|
+
// way of handling tabbable overflow areas. Browser sniffing seems gross, and it's the most
|
|
29
|
+
// accessible way of handling overflow areas. [Konnor]
|
|
30
|
+
function isOverflowingAndTabbable(el) {
|
|
31
|
+
const computedStyle = getCachedComputedStyle(el);
|
|
32
|
+
const { overflowY, overflowX } = computedStyle;
|
|
33
|
+
if (overflowY === 'scroll' || overflowX === 'scroll') {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
if (overflowY !== 'auto' || overflowX !== 'auto') {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
// Always overflow === "auto" by this point
|
|
40
|
+
const isOverflowingY = el.scrollHeight > el.clientHeight;
|
|
41
|
+
if (isOverflowingY && overflowY === 'auto') {
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
const isOverflowingX = el.scrollWidth > el.clientWidth;
|
|
45
|
+
if (isOverflowingX && overflowX === 'auto') {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
/** Determines if the specified element is tabbable using heuristics inspired by https://github.com/focus-trap/tabbable */
|
|
51
|
+
function isTabbable(el) {
|
|
52
|
+
const tag = el.tagName.toLowerCase();
|
|
53
|
+
const tabindex = Number(el.getAttribute('tabindex'));
|
|
54
|
+
const hasTabindex = el.hasAttribute('tabindex');
|
|
55
|
+
// elements with a tabindex attribute that is either NaN or <= -1 are not tabbable
|
|
56
|
+
if (hasTabindex && (isNaN(tabindex) || tabindex <= -1)) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
// Elements with a disabled attribute are not tabbable
|
|
60
|
+
if (el.hasAttribute('disabled')) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
// If any parents have "inert", we aren't "tabbable"
|
|
64
|
+
if (el.closest('[inert]')) {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
// Radios without a checked attribute are not tabbable
|
|
68
|
+
if (tag === 'input' &&
|
|
69
|
+
el.getAttribute('type') === 'radio' &&
|
|
70
|
+
!el.hasAttribute('checked')) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
if (!isVisible(el)) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
// Audio and video elements with the controls attribute are tabbable
|
|
77
|
+
if ((tag === 'audio' || tag === 'video') && el.hasAttribute('controls')) {
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
// Elements with a tabindex other than -1 are tabbable
|
|
81
|
+
if (el.hasAttribute('tabindex')) {
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
// Elements with a contenteditable attribute are tabbable
|
|
85
|
+
if (el.hasAttribute('contenteditable') &&
|
|
86
|
+
el.getAttribute('contenteditable') !== 'false') {
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
// At this point, the following elements are considered tabbable
|
|
90
|
+
const isNativelyTabbable = [
|
|
91
|
+
'button',
|
|
92
|
+
'input',
|
|
93
|
+
'select',
|
|
94
|
+
'textarea',
|
|
95
|
+
'a',
|
|
96
|
+
'audio',
|
|
97
|
+
'video',
|
|
98
|
+
'summary',
|
|
99
|
+
'iframe',
|
|
100
|
+
].includes(tag);
|
|
101
|
+
if (isNativelyTabbable) {
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
// We save the overflow checks for last, because they're the most expensive
|
|
105
|
+
return isOverflowingAndTabbable(el);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Returns the first and last bounding elements that are tabbable. This is more performant than checking every single
|
|
109
|
+
* element because it short-circuits after finding the first and last ones.
|
|
110
|
+
*/
|
|
111
|
+
export function getTabbableBoundary(root) {
|
|
112
|
+
var _a, _b;
|
|
113
|
+
const tabbableElements = getTabbableElements(root);
|
|
114
|
+
// Find the first and last tabbable elements
|
|
115
|
+
const start = (_a = tabbableElements[0]) !== null && _a !== void 0 ? _a : null;
|
|
116
|
+
const end = (_b = tabbableElements[tabbableElements.length - 1]) !== null && _b !== void 0 ? _b : null;
|
|
117
|
+
return { start, end };
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* This looks funky. Basically a slot's children will always be picked up *if* they're within the `root` element.
|
|
121
|
+
* However, there is an edge case when, if the `root` is wrapped by another shadow DOM, it won't grab the children.
|
|
122
|
+
* This fixes that fun edge case.
|
|
123
|
+
*/
|
|
124
|
+
function getSlottedChildrenOutsideRootElement(slotElement, root) {
|
|
125
|
+
var _a;
|
|
126
|
+
return (((_a = slotElement.getRootNode({ composed: true })) === null || _a === void 0 ? void 0 : _a.host) !==
|
|
127
|
+
root);
|
|
128
|
+
}
|
|
129
|
+
export function getTabbableElements(root) {
|
|
130
|
+
const walkedEls = new WeakMap();
|
|
131
|
+
const tabbableElements = [];
|
|
132
|
+
function walk(el) {
|
|
133
|
+
if (el instanceof Element) {
|
|
134
|
+
// if the element has "inert" we can just no-op it.
|
|
135
|
+
if (el.hasAttribute('inert') || el.closest('[inert]')) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (walkedEls.has(el)) {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
walkedEls.set(el, true);
|
|
142
|
+
if (!tabbableElements.includes(el) && isTabbable(el)) {
|
|
143
|
+
tabbableElements.push(el);
|
|
144
|
+
}
|
|
145
|
+
if (el instanceof HTMLSlotElement &&
|
|
146
|
+
getSlottedChildrenOutsideRootElement(el, root)) {
|
|
147
|
+
el.assignedElements({ flatten: true }).forEach((assignedEl) => {
|
|
148
|
+
walk(assignedEl);
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
if (el.shadowRoot !== null && el.shadowRoot.mode === 'open') {
|
|
152
|
+
walk(el.shadowRoot);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
for (const e of Array.from(el.children)) {
|
|
156
|
+
walk(e);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
// Collect all elements including the root
|
|
160
|
+
walk(root);
|
|
161
|
+
// Is this worth having? Most sorts will always add increased overhead. And positive tabindexes shouldn't really be used.
|
|
162
|
+
// So is it worth being right? Or fast?
|
|
163
|
+
return tabbableElements.sort((a, b) => {
|
|
164
|
+
// Make sure we sort by tabindex.
|
|
165
|
+
const aTabindex = Number(a.getAttribute('tabindex')) || 0;
|
|
166
|
+
const bTabindex = Number(b.getAttribute('tabindex')) || 0;
|
|
167
|
+
return bTabindex - aTabindex;
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
170
|
//# sourceMappingURL=tabbable.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export {};
|
|
1
|
+
export {};
|