@y14e/roving-tabindex 3.0.10 → 3.0.11
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/dist/index.bundle.cjs +257 -0
- package/dist/index.bundle.js +255 -0
- package/dist/index.cjs +13 -329
- package/dist/index.d.cts +1 -5
- package/dist/index.d.ts +1 -5
- package/dist/index.js +6 -316
- package/package.json +5 -3
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var attributesUtils = require('@y14e/attributes-utils');
|
|
4
|
+
var powerFocusable = require('power-focusable');
|
|
5
|
+
|
|
6
|
+
// src/index.ts
|
|
7
|
+
function createRovingTabIndex(container, options = {}) {
|
|
8
|
+
if (!(container instanceof Element)) {
|
|
9
|
+
console.warn("Invalid container element");
|
|
10
|
+
return () => {
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
const roving = new RovingTabIndex(container, options);
|
|
14
|
+
return () => roving.destroy();
|
|
15
|
+
}
|
|
16
|
+
var RovingTabIndex = class _RovingTabIndex {
|
|
17
|
+
static #initialized = /* @__PURE__ */ new Set();
|
|
18
|
+
#container;
|
|
19
|
+
#settings;
|
|
20
|
+
#focusables = /* @__PURE__ */ new Set();
|
|
21
|
+
#focusablesByFirstChar = /* @__PURE__ */ new Map();
|
|
22
|
+
#selectorFilter;
|
|
23
|
+
#controller = null;
|
|
24
|
+
#isDestroyed = false;
|
|
25
|
+
constructor(container, options = {}) {
|
|
26
|
+
this.#container = container;
|
|
27
|
+
let {
|
|
28
|
+
direction,
|
|
29
|
+
navigationOnly = false,
|
|
30
|
+
noMemory = false,
|
|
31
|
+
noStart = false,
|
|
32
|
+
selector,
|
|
33
|
+
typeahead = false,
|
|
34
|
+
wrap = false
|
|
35
|
+
} = options;
|
|
36
|
+
if (typeof direction !== "undefined" && !["horizontal", "vertical"].includes(direction)) {
|
|
37
|
+
console.warn("Invalid direction option. Fallback: both (undefined).");
|
|
38
|
+
direction = void 0;
|
|
39
|
+
}
|
|
40
|
+
if (typeof navigationOnly !== "boolean") {
|
|
41
|
+
console.warn("Invalid navigationOnly option. Fallback: false.");
|
|
42
|
+
navigationOnly = false;
|
|
43
|
+
}
|
|
44
|
+
if (typeof noMemory !== "boolean") {
|
|
45
|
+
console.warn("Invalid noMemory option. Fallback: false.");
|
|
46
|
+
noMemory = false;
|
|
47
|
+
}
|
|
48
|
+
if (typeof noStart !== "boolean") {
|
|
49
|
+
console.warn("Invalid noStart option. Fallback: false.");
|
|
50
|
+
noStart = false;
|
|
51
|
+
}
|
|
52
|
+
if (typeof selector !== "undefined" && (typeof selector !== "string" || !selector.trim())) {
|
|
53
|
+
console.warn(
|
|
54
|
+
"Invalid selector. Fallback: all focusable elements (undefined)."
|
|
55
|
+
);
|
|
56
|
+
selector = void 0;
|
|
57
|
+
}
|
|
58
|
+
if (typeof typeahead !== "boolean") {
|
|
59
|
+
console.warn("Invalid typeahead option. Fallback: false.");
|
|
60
|
+
typeahead = false;
|
|
61
|
+
}
|
|
62
|
+
if (typeof wrap !== "boolean") {
|
|
63
|
+
console.warn("Invalid wrap option. Fallback: false.");
|
|
64
|
+
wrap = false;
|
|
65
|
+
}
|
|
66
|
+
this.#settings = {
|
|
67
|
+
navigationOnly,
|
|
68
|
+
noMemory,
|
|
69
|
+
noStart,
|
|
70
|
+
typeahead,
|
|
71
|
+
wrap
|
|
72
|
+
};
|
|
73
|
+
direction && Object.assign(this.#settings, { direction });
|
|
74
|
+
selector && Object.assign(this.#settings, { selector });
|
|
75
|
+
this.#selectorFilter = this.#createSelectorFilter();
|
|
76
|
+
this.#initialize();
|
|
77
|
+
}
|
|
78
|
+
destroy() {
|
|
79
|
+
if (this.#isDestroyed) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
this.#isDestroyed = true;
|
|
83
|
+
this.#controller?.abort();
|
|
84
|
+
this.#controller = null;
|
|
85
|
+
attributesUtils.restoreAttributes([...this.#focusables]);
|
|
86
|
+
this.#focusables.clear();
|
|
87
|
+
this.#focusablesByFirstChar.clear();
|
|
88
|
+
}
|
|
89
|
+
#initialize() {
|
|
90
|
+
this.#update(document.activeElement);
|
|
91
|
+
if (!(this.#container instanceof HTMLElement)) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
this.#controller = new AbortController();
|
|
95
|
+
const { signal } = this.#controller;
|
|
96
|
+
this.#container.addEventListener("focusin", this.#onFocusIn, {
|
|
97
|
+
capture: true,
|
|
98
|
+
signal
|
|
99
|
+
});
|
|
100
|
+
this.#container.addEventListener("keydown", this.#onKeyDown, {
|
|
101
|
+
capture: true,
|
|
102
|
+
signal
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
#onFocusIn = (event) => {
|
|
106
|
+
const { target } = event;
|
|
107
|
+
if (!(target instanceof Element)) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const isFocusable = this.#focusables.has(target);
|
|
111
|
+
this.#settings.noMemory && !isFocusable ? this.#update(null) : isFocusable && this.#update(target);
|
|
112
|
+
};
|
|
113
|
+
#onKeyDown = (event) => {
|
|
114
|
+
const { key, altKey, ctrlKey, metaKey, shiftKey } = event;
|
|
115
|
+
if (altKey || ctrlKey || metaKey || shiftKey) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
const { direction, typeahead, wrap } = this.#settings;
|
|
119
|
+
const isBoth = !direction;
|
|
120
|
+
const isHorizontal = direction === "horizontal";
|
|
121
|
+
if (![
|
|
122
|
+
"End",
|
|
123
|
+
"Home",
|
|
124
|
+
...isBoth ? ["ArrowLeft", "ArrowUp"] : [`Arrow${isHorizontal ? "Left" : "Up"}`],
|
|
125
|
+
...isBoth ? ["ArrowRight", "ArrowDown"] : [`Arrow${isHorizontal ? "Right" : "Down"}`]
|
|
126
|
+
].includes(key)) {
|
|
127
|
+
if (!typeahead || !/^\S$/i.test(key) || !this.#focusablesByFirstChar.has(key.toUpperCase())) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
const active = powerFocusable.getActiveElement();
|
|
132
|
+
if (!(active instanceof HTMLElement)) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
const current = this.#getFocusables();
|
|
136
|
+
if (!current.includes(active)) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
event.preventDefault();
|
|
140
|
+
const currentIndex = current.indexOf(active);
|
|
141
|
+
let newIndex;
|
|
142
|
+
let target = current;
|
|
143
|
+
switch (key) {
|
|
144
|
+
case "End":
|
|
145
|
+
newIndex = -1;
|
|
146
|
+
break;
|
|
147
|
+
case "Home":
|
|
148
|
+
newIndex = 0;
|
|
149
|
+
break;
|
|
150
|
+
case "ArrowLeft":
|
|
151
|
+
case "ArrowUp": {
|
|
152
|
+
const rawIndex = currentIndex - 1;
|
|
153
|
+
newIndex = wrap ? rawIndex : Math.max(rawIndex, 0);
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
case "ArrowRight":
|
|
157
|
+
case "ArrowDown": {
|
|
158
|
+
const rawIndex = currentIndex + 1;
|
|
159
|
+
newIndex = wrap ? rawIndex % current.length : Math.min(rawIndex, current.length - 1);
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
default: {
|
|
163
|
+
target = this.#focusablesByFirstChar.get(key.toUpperCase()) ?? [];
|
|
164
|
+
const foundIndex = target.findIndex(
|
|
165
|
+
(focusable2) => current.indexOf(focusable2) > currentIndex
|
|
166
|
+
);
|
|
167
|
+
newIndex = foundIndex >= 0 ? foundIndex : 0;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
const focusable = target.at(newIndex);
|
|
171
|
+
focusable && powerFocusable.focusElement(focusable);
|
|
172
|
+
};
|
|
173
|
+
#update(active) {
|
|
174
|
+
const current = new Set(this.#getFocusables());
|
|
175
|
+
for (const focusable of this.#focusables) {
|
|
176
|
+
if (!current.has(focusable)) {
|
|
177
|
+
focusable.isConnected && attributesUtils.restoreAttributes([focusable]);
|
|
178
|
+
this.#focusables.delete(focusable);
|
|
179
|
+
this.#focusablesByFirstChar.forEach((focusables) => {
|
|
180
|
+
const index = focusables.indexOf(focusable);
|
|
181
|
+
index >= 0 && focusables.splice(index, 1);
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
const { navigationOnly, noStart, typeahead } = this.#settings;
|
|
186
|
+
for (const focusable of current) {
|
|
187
|
+
if (this.#focusables.has(focusable)) {
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
if (_RovingTabIndex.#initialized.has(focusable)) {
|
|
191
|
+
throw new TypeError("Already initialized");
|
|
192
|
+
}
|
|
193
|
+
this.#focusables.add(focusable);
|
|
194
|
+
_RovingTabIndex.#initialized.add(focusable);
|
|
195
|
+
if (!navigationOnly) {
|
|
196
|
+
attributesUtils.saveAttributes([focusable], ["tabindex"]);
|
|
197
|
+
focusable.setAttribute("tabindex", "-1");
|
|
198
|
+
}
|
|
199
|
+
if (!typeahead) {
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
const char = focusable.textContent?.trim()?.at(0)?.toUpperCase();
|
|
203
|
+
const value = focusable.ariaKeyShortcuts?.trim();
|
|
204
|
+
const keys = new Set(
|
|
205
|
+
value ? value.split(/\s+/).filter((key) => /^\S$/i.test(key)).map((key) => key.toUpperCase()) : []
|
|
206
|
+
);
|
|
207
|
+
if (char) {
|
|
208
|
+
keys.add(char);
|
|
209
|
+
attributesUtils.saveAttributes([focusable], ["aria-keyshortcuts"]);
|
|
210
|
+
attributesUtils.addTokenToAttribute(focusable, "aria-keyshortcuts", char, {
|
|
211
|
+
caseInsensitive: true
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
keys.forEach((key) => {
|
|
215
|
+
const focusables = this.#focusablesByFirstChar.get(key) ?? [];
|
|
216
|
+
focusables.push(focusable);
|
|
217
|
+
this.#focusablesByFirstChar.set(key, focusables);
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
if (!navigationOnly) {
|
|
221
|
+
if (active && this.#focusables.has(active)) {
|
|
222
|
+
this.#focusables.forEach((focusable) => {
|
|
223
|
+
focusable.setAttribute("tabindex", focusable === active ? "0" : "-1");
|
|
224
|
+
});
|
|
225
|
+
} else {
|
|
226
|
+
[...this.#focusables].forEach((focusable, i) => {
|
|
227
|
+
focusable.setAttribute("tabindex", i || noStart ? "-1" : "0");
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
#createSelectorFilter() {
|
|
233
|
+
const { selector } = this.#settings;
|
|
234
|
+
return (element) => !selector || [...this.#container.querySelectorAll(selector)].includes(element);
|
|
235
|
+
}
|
|
236
|
+
#getFocusables() {
|
|
237
|
+
return powerFocusable.getFocusables(this.#container, {
|
|
238
|
+
composed: true,
|
|
239
|
+
filter: this.#selectorFilter,
|
|
240
|
+
skipNegativeTabIndexCheck: !this.#settings.navigationOnly,
|
|
241
|
+
skipVisibilityCheck: true
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
/**
|
|
246
|
+
* Roving Tabindex
|
|
247
|
+
* Lightweight roving tabindex utility with fully focus management.
|
|
248
|
+
* Designed for accessible menus, tabs, toolbars, and composite widgets.
|
|
249
|
+
*
|
|
250
|
+
* @version 3.0.11
|
|
251
|
+
* @author Yusuke Kamiyamane
|
|
252
|
+
* @license MIT
|
|
253
|
+
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
254
|
+
* @see {@link https://github.com/y14e/roving-tabindex}
|
|
255
|
+
*/
|
|
256
|
+
|
|
257
|
+
exports.createRovingTabIndex = createRovingTabIndex;
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import { restoreAttributes, saveAttributes, addTokenToAttribute } from '@y14e/attributes-utils';
|
|
2
|
+
import { getActiveElement, focusElement, getFocusables } from 'power-focusable';
|
|
3
|
+
|
|
4
|
+
// src/index.ts
|
|
5
|
+
function createRovingTabIndex(container, options = {}) {
|
|
6
|
+
if (!(container instanceof Element)) {
|
|
7
|
+
console.warn("Invalid container element");
|
|
8
|
+
return () => {
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
const roving = new RovingTabIndex(container, options);
|
|
12
|
+
return () => roving.destroy();
|
|
13
|
+
}
|
|
14
|
+
var RovingTabIndex = class _RovingTabIndex {
|
|
15
|
+
static #initialized = /* @__PURE__ */ new Set();
|
|
16
|
+
#container;
|
|
17
|
+
#settings;
|
|
18
|
+
#focusables = /* @__PURE__ */ new Set();
|
|
19
|
+
#focusablesByFirstChar = /* @__PURE__ */ new Map();
|
|
20
|
+
#selectorFilter;
|
|
21
|
+
#controller = null;
|
|
22
|
+
#isDestroyed = false;
|
|
23
|
+
constructor(container, options = {}) {
|
|
24
|
+
this.#container = container;
|
|
25
|
+
let {
|
|
26
|
+
direction,
|
|
27
|
+
navigationOnly = false,
|
|
28
|
+
noMemory = false,
|
|
29
|
+
noStart = false,
|
|
30
|
+
selector,
|
|
31
|
+
typeahead = false,
|
|
32
|
+
wrap = false
|
|
33
|
+
} = options;
|
|
34
|
+
if (typeof direction !== "undefined" && !["horizontal", "vertical"].includes(direction)) {
|
|
35
|
+
console.warn("Invalid direction option. Fallback: both (undefined).");
|
|
36
|
+
direction = void 0;
|
|
37
|
+
}
|
|
38
|
+
if (typeof navigationOnly !== "boolean") {
|
|
39
|
+
console.warn("Invalid navigationOnly option. Fallback: false.");
|
|
40
|
+
navigationOnly = false;
|
|
41
|
+
}
|
|
42
|
+
if (typeof noMemory !== "boolean") {
|
|
43
|
+
console.warn("Invalid noMemory option. Fallback: false.");
|
|
44
|
+
noMemory = false;
|
|
45
|
+
}
|
|
46
|
+
if (typeof noStart !== "boolean") {
|
|
47
|
+
console.warn("Invalid noStart option. Fallback: false.");
|
|
48
|
+
noStart = false;
|
|
49
|
+
}
|
|
50
|
+
if (typeof selector !== "undefined" && (typeof selector !== "string" || !selector.trim())) {
|
|
51
|
+
console.warn(
|
|
52
|
+
"Invalid selector. Fallback: all focusable elements (undefined)."
|
|
53
|
+
);
|
|
54
|
+
selector = void 0;
|
|
55
|
+
}
|
|
56
|
+
if (typeof typeahead !== "boolean") {
|
|
57
|
+
console.warn("Invalid typeahead option. Fallback: false.");
|
|
58
|
+
typeahead = false;
|
|
59
|
+
}
|
|
60
|
+
if (typeof wrap !== "boolean") {
|
|
61
|
+
console.warn("Invalid wrap option. Fallback: false.");
|
|
62
|
+
wrap = false;
|
|
63
|
+
}
|
|
64
|
+
this.#settings = {
|
|
65
|
+
navigationOnly,
|
|
66
|
+
noMemory,
|
|
67
|
+
noStart,
|
|
68
|
+
typeahead,
|
|
69
|
+
wrap
|
|
70
|
+
};
|
|
71
|
+
direction && Object.assign(this.#settings, { direction });
|
|
72
|
+
selector && Object.assign(this.#settings, { selector });
|
|
73
|
+
this.#selectorFilter = this.#createSelectorFilter();
|
|
74
|
+
this.#initialize();
|
|
75
|
+
}
|
|
76
|
+
destroy() {
|
|
77
|
+
if (this.#isDestroyed) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
this.#isDestroyed = true;
|
|
81
|
+
this.#controller?.abort();
|
|
82
|
+
this.#controller = null;
|
|
83
|
+
restoreAttributes([...this.#focusables]);
|
|
84
|
+
this.#focusables.clear();
|
|
85
|
+
this.#focusablesByFirstChar.clear();
|
|
86
|
+
}
|
|
87
|
+
#initialize() {
|
|
88
|
+
this.#update(document.activeElement);
|
|
89
|
+
if (!(this.#container instanceof HTMLElement)) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
this.#controller = new AbortController();
|
|
93
|
+
const { signal } = this.#controller;
|
|
94
|
+
this.#container.addEventListener("focusin", this.#onFocusIn, {
|
|
95
|
+
capture: true,
|
|
96
|
+
signal
|
|
97
|
+
});
|
|
98
|
+
this.#container.addEventListener("keydown", this.#onKeyDown, {
|
|
99
|
+
capture: true,
|
|
100
|
+
signal
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
#onFocusIn = (event) => {
|
|
104
|
+
const { target } = event;
|
|
105
|
+
if (!(target instanceof Element)) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
const isFocusable = this.#focusables.has(target);
|
|
109
|
+
this.#settings.noMemory && !isFocusable ? this.#update(null) : isFocusable && this.#update(target);
|
|
110
|
+
};
|
|
111
|
+
#onKeyDown = (event) => {
|
|
112
|
+
const { key, altKey, ctrlKey, metaKey, shiftKey } = event;
|
|
113
|
+
if (altKey || ctrlKey || metaKey || shiftKey) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
const { direction, typeahead, wrap } = this.#settings;
|
|
117
|
+
const isBoth = !direction;
|
|
118
|
+
const isHorizontal = direction === "horizontal";
|
|
119
|
+
if (![
|
|
120
|
+
"End",
|
|
121
|
+
"Home",
|
|
122
|
+
...isBoth ? ["ArrowLeft", "ArrowUp"] : [`Arrow${isHorizontal ? "Left" : "Up"}`],
|
|
123
|
+
...isBoth ? ["ArrowRight", "ArrowDown"] : [`Arrow${isHorizontal ? "Right" : "Down"}`]
|
|
124
|
+
].includes(key)) {
|
|
125
|
+
if (!typeahead || !/^\S$/i.test(key) || !this.#focusablesByFirstChar.has(key.toUpperCase())) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
const active = getActiveElement();
|
|
130
|
+
if (!(active instanceof HTMLElement)) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
const current = this.#getFocusables();
|
|
134
|
+
if (!current.includes(active)) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
event.preventDefault();
|
|
138
|
+
const currentIndex = current.indexOf(active);
|
|
139
|
+
let newIndex;
|
|
140
|
+
let target = current;
|
|
141
|
+
switch (key) {
|
|
142
|
+
case "End":
|
|
143
|
+
newIndex = -1;
|
|
144
|
+
break;
|
|
145
|
+
case "Home":
|
|
146
|
+
newIndex = 0;
|
|
147
|
+
break;
|
|
148
|
+
case "ArrowLeft":
|
|
149
|
+
case "ArrowUp": {
|
|
150
|
+
const rawIndex = currentIndex - 1;
|
|
151
|
+
newIndex = wrap ? rawIndex : Math.max(rawIndex, 0);
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
case "ArrowRight":
|
|
155
|
+
case "ArrowDown": {
|
|
156
|
+
const rawIndex = currentIndex + 1;
|
|
157
|
+
newIndex = wrap ? rawIndex % current.length : Math.min(rawIndex, current.length - 1);
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
default: {
|
|
161
|
+
target = this.#focusablesByFirstChar.get(key.toUpperCase()) ?? [];
|
|
162
|
+
const foundIndex = target.findIndex(
|
|
163
|
+
(focusable2) => current.indexOf(focusable2) > currentIndex
|
|
164
|
+
);
|
|
165
|
+
newIndex = foundIndex >= 0 ? foundIndex : 0;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
const focusable = target.at(newIndex);
|
|
169
|
+
focusable && focusElement(focusable);
|
|
170
|
+
};
|
|
171
|
+
#update(active) {
|
|
172
|
+
const current = new Set(this.#getFocusables());
|
|
173
|
+
for (const focusable of this.#focusables) {
|
|
174
|
+
if (!current.has(focusable)) {
|
|
175
|
+
focusable.isConnected && restoreAttributes([focusable]);
|
|
176
|
+
this.#focusables.delete(focusable);
|
|
177
|
+
this.#focusablesByFirstChar.forEach((focusables) => {
|
|
178
|
+
const index = focusables.indexOf(focusable);
|
|
179
|
+
index >= 0 && focusables.splice(index, 1);
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
const { navigationOnly, noStart, typeahead } = this.#settings;
|
|
184
|
+
for (const focusable of current) {
|
|
185
|
+
if (this.#focusables.has(focusable)) {
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
if (_RovingTabIndex.#initialized.has(focusable)) {
|
|
189
|
+
throw new TypeError("Already initialized");
|
|
190
|
+
}
|
|
191
|
+
this.#focusables.add(focusable);
|
|
192
|
+
_RovingTabIndex.#initialized.add(focusable);
|
|
193
|
+
if (!navigationOnly) {
|
|
194
|
+
saveAttributes([focusable], ["tabindex"]);
|
|
195
|
+
focusable.setAttribute("tabindex", "-1");
|
|
196
|
+
}
|
|
197
|
+
if (!typeahead) {
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
const char = focusable.textContent?.trim()?.at(0)?.toUpperCase();
|
|
201
|
+
const value = focusable.ariaKeyShortcuts?.trim();
|
|
202
|
+
const keys = new Set(
|
|
203
|
+
value ? value.split(/\s+/).filter((key) => /^\S$/i.test(key)).map((key) => key.toUpperCase()) : []
|
|
204
|
+
);
|
|
205
|
+
if (char) {
|
|
206
|
+
keys.add(char);
|
|
207
|
+
saveAttributes([focusable], ["aria-keyshortcuts"]);
|
|
208
|
+
addTokenToAttribute(focusable, "aria-keyshortcuts", char, {
|
|
209
|
+
caseInsensitive: true
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
keys.forEach((key) => {
|
|
213
|
+
const focusables = this.#focusablesByFirstChar.get(key) ?? [];
|
|
214
|
+
focusables.push(focusable);
|
|
215
|
+
this.#focusablesByFirstChar.set(key, focusables);
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
if (!navigationOnly) {
|
|
219
|
+
if (active && this.#focusables.has(active)) {
|
|
220
|
+
this.#focusables.forEach((focusable) => {
|
|
221
|
+
focusable.setAttribute("tabindex", focusable === active ? "0" : "-1");
|
|
222
|
+
});
|
|
223
|
+
} else {
|
|
224
|
+
[...this.#focusables].forEach((focusable, i) => {
|
|
225
|
+
focusable.setAttribute("tabindex", i || noStart ? "-1" : "0");
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
#createSelectorFilter() {
|
|
231
|
+
const { selector } = this.#settings;
|
|
232
|
+
return (element) => !selector || [...this.#container.querySelectorAll(selector)].includes(element);
|
|
233
|
+
}
|
|
234
|
+
#getFocusables() {
|
|
235
|
+
return getFocusables(this.#container, {
|
|
236
|
+
composed: true,
|
|
237
|
+
filter: this.#selectorFilter,
|
|
238
|
+
skipNegativeTabIndexCheck: !this.#settings.navigationOnly,
|
|
239
|
+
skipVisibilityCheck: true
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
/**
|
|
244
|
+
* Roving Tabindex
|
|
245
|
+
* Lightweight roving tabindex utility with fully focus management.
|
|
246
|
+
* Designed for accessible menus, tabs, toolbars, and composite widgets.
|
|
247
|
+
*
|
|
248
|
+
* @version 3.0.11
|
|
249
|
+
* @author Yusuke Kamiyamane
|
|
250
|
+
* @license MIT
|
|
251
|
+
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
252
|
+
* @see {@link https://github.com/y14e/roving-tabindex}
|
|
253
|
+
*/
|
|
254
|
+
|
|
255
|
+
export { createRovingTabIndex };
|
package/dist/index.cjs
CHANGED
|
@@ -1,291 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
var
|
|
5
|
-
var DEFAULT_SERIALIZER = (tokens) => tokens.join(" ");
|
|
6
|
-
function addTokenToAttribute(element, attribute, token, options = {}) {
|
|
7
|
-
const {
|
|
8
|
-
caseInsensitive = false,
|
|
9
|
-
parse = DEFAULT_PARSER,
|
|
10
|
-
serialize = DEFAULT_SERIALIZER
|
|
11
|
-
} = options;
|
|
12
|
-
const value = element.getAttribute(attribute)?.trim();
|
|
13
|
-
const tokens = value ? parse(value).filter(Boolean) : [];
|
|
14
|
-
if (caseInsensitive) {
|
|
15
|
-
const lower = token.toLowerCase();
|
|
16
|
-
if (tokens.every((token2) => token2.toLowerCase() !== lower)) {
|
|
17
|
-
tokens.push(token);
|
|
18
|
-
element.setAttribute(attribute, serialize(tokens));
|
|
19
|
-
}
|
|
20
|
-
} else {
|
|
21
|
-
const set = new Set(tokens);
|
|
22
|
-
set.add(token);
|
|
23
|
-
element.setAttribute(attribute, serialize([...set]));
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
var snapshots = /* @__PURE__ */ new WeakMap();
|
|
27
|
-
function restoreAttributes(elements) {
|
|
28
|
-
for (const element of elements) {
|
|
29
|
-
const snapshot = snapshots.get(element);
|
|
30
|
-
if (!snapshot) {
|
|
31
|
-
continue;
|
|
32
|
-
}
|
|
33
|
-
for (const [attribute, value] of snapshot.entries()) {
|
|
34
|
-
value === null ? element.removeAttribute(attribute) : element.setAttribute(attribute, value);
|
|
35
|
-
}
|
|
36
|
-
snapshots.delete(element);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
function saveAttributes(elements, attributes) {
|
|
40
|
-
elements.forEach((element) => {
|
|
41
|
-
let snapshot = snapshots.get(element);
|
|
42
|
-
if (!snapshot) {
|
|
43
|
-
snapshot = /* @__PURE__ */ new Map();
|
|
44
|
-
snapshots.set(element, snapshot);
|
|
45
|
-
}
|
|
46
|
-
attributes.forEach((attribute) => {
|
|
47
|
-
snapshot.set(attribute, element.getAttribute(attribute));
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// node_modules/power-focusable/dist/index.js
|
|
53
|
-
var FOCUSABLE_SELECTOR = `:is(a[href], area[href], button, embed, iframe, input:not([type="hidden" i]), object, select, details > summary:first-of-type, textarea, [contenteditable]:not([contenteditable="false" i]), [controls], [tabindex]):not(:disabled, [hidden], [inert], [tabindex="-1"])`;
|
|
54
|
-
function getFocusables(container = document.body, options = {}) {
|
|
55
|
-
if (!(container instanceof Element)) {
|
|
56
|
-
console.warn("Invalid container element. Fallback: <body> element.");
|
|
57
|
-
container = document.body;
|
|
58
|
-
}
|
|
59
|
-
let {
|
|
60
|
-
composed = false,
|
|
61
|
-
filter,
|
|
62
|
-
include,
|
|
63
|
-
skipNegativeTabIndexCheck = false,
|
|
64
|
-
skipVisibilityCheck = false
|
|
65
|
-
} = options;
|
|
66
|
-
if (typeof composed !== "boolean") {
|
|
67
|
-
console.warn("Invalid composed option. Fallback: false.");
|
|
68
|
-
composed = false;
|
|
69
|
-
}
|
|
70
|
-
if (typeof filter !== "undefined" && typeof filter !== "function") {
|
|
71
|
-
console.warn(
|
|
72
|
-
"Invalid filter function. Fallback: no filter function (undefined)."
|
|
73
|
-
);
|
|
74
|
-
filter = void 0;
|
|
75
|
-
}
|
|
76
|
-
if (typeof include !== "undefined" && typeof include !== "function") {
|
|
77
|
-
console.warn(
|
|
78
|
-
"Invalid include function. Fallback: no include function (undefined)."
|
|
79
|
-
);
|
|
80
|
-
include = void 0;
|
|
81
|
-
}
|
|
82
|
-
if (typeof skipNegativeTabIndexCheck !== "boolean") {
|
|
83
|
-
console.warn("Invalid skipNegativeTabIndexCheck option. Fallback: false.");
|
|
84
|
-
skipNegativeTabIndexCheck = false;
|
|
85
|
-
}
|
|
86
|
-
if (typeof skipVisibilityCheck !== "boolean") {
|
|
87
|
-
console.warn("Invalid skipVisibilityCheck option. Fallback: false.");
|
|
88
|
-
skipVisibilityCheck = false;
|
|
89
|
-
}
|
|
90
|
-
const elements = [];
|
|
91
|
-
if (composed || include) {
|
|
92
|
-
let traverse2 = function(node) {
|
|
93
|
-
if (!(node instanceof Element)) {
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
if (isFocusable(node, { skipNegativeTabIndexCheck, skipVisibilityCheck }) || include?.(node)) {
|
|
97
|
-
elements[elements.length] = node;
|
|
98
|
-
}
|
|
99
|
-
const children = getComposedChildren(node);
|
|
100
|
-
for (let i = 0, l = children.length; i < l; i++) {
|
|
101
|
-
const child = children[i];
|
|
102
|
-
child && traverse2(child);
|
|
103
|
-
}
|
|
104
|
-
};
|
|
105
|
-
traverse2(container);
|
|
106
|
-
} else {
|
|
107
|
-
const candidates = container.querySelectorAll(FOCUSABLE_SELECTOR);
|
|
108
|
-
for (let i = 0, l = candidates.length; i < l; i++) {
|
|
109
|
-
const candidate = candidates[i];
|
|
110
|
-
if (candidate && isFocusable(candidate, {
|
|
111
|
-
skipNegativeTabIndexCheck,
|
|
112
|
-
skipVisibilityCheck
|
|
113
|
-
})) {
|
|
114
|
-
elements[elements.length] = candidate;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
const unfiltered = normalizeRadioGroup(sortByTabIndex(elements));
|
|
119
|
-
return filter ? unfiltered.filter(filter) : unfiltered;
|
|
120
|
-
}
|
|
121
|
-
function isFocusable(element, options = {}) {
|
|
122
|
-
if (!(element instanceof Element)) {
|
|
123
|
-
console.warn("Invalid element");
|
|
124
|
-
return false;
|
|
125
|
-
}
|
|
126
|
-
let { skipNegativeTabIndexCheck = false, skipVisibilityCheck = false } = options;
|
|
127
|
-
if (typeof skipNegativeTabIndexCheck !== "boolean") {
|
|
128
|
-
console.warn("Invalid skipNegativeTabIndexCheck option. Fallback: false.");
|
|
129
|
-
skipNegativeTabIndexCheck = false;
|
|
130
|
-
}
|
|
131
|
-
if (typeof skipVisibilityCheck !== "boolean") {
|
|
132
|
-
console.warn("Invalid skipVisibilityCheck option. Fallback: false.");
|
|
133
|
-
skipVisibilityCheck = false;
|
|
134
|
-
}
|
|
135
|
-
if (element.hasAttribute("hidden") || isInert(element)) {
|
|
136
|
-
return false;
|
|
137
|
-
}
|
|
138
|
-
if (!skipNegativeTabIndexCheck && getTabIndex(element) < 0) {
|
|
139
|
-
return false;
|
|
140
|
-
}
|
|
141
|
-
if (!element.matches(
|
|
142
|
-
skipNegativeTabIndexCheck ? FOCUSABLE_SELECTOR.replace(/(,\s*)?\[tabindex="-1"\]/g, "") : FOCUSABLE_SELECTOR
|
|
143
|
-
)) {
|
|
144
|
-
return false;
|
|
145
|
-
}
|
|
146
|
-
if (isDisabledDeep(element)) {
|
|
147
|
-
return false;
|
|
148
|
-
}
|
|
149
|
-
if (!skipVisibilityCheck && !element.checkVisibility({
|
|
150
|
-
contentVisibilityAuto: true,
|
|
151
|
-
opacityProperty: true,
|
|
152
|
-
visibilityProperty: true
|
|
153
|
-
})) {
|
|
154
|
-
return false;
|
|
155
|
-
}
|
|
156
|
-
return true;
|
|
157
|
-
}
|
|
158
|
-
function isDisabledDeep(element) {
|
|
159
|
-
let current = element;
|
|
160
|
-
while (current) {
|
|
161
|
-
if (current instanceof ShadowRoot) {
|
|
162
|
-
if (current.mode !== "open") {
|
|
163
|
-
return false;
|
|
164
|
-
}
|
|
165
|
-
current = current.host;
|
|
166
|
-
continue;
|
|
167
|
-
}
|
|
168
|
-
if (!(current instanceof Element)) {
|
|
169
|
-
current = current.parentNode;
|
|
170
|
-
continue;
|
|
171
|
-
}
|
|
172
|
-
if (current === element && isFormControl(current) && isDisabled(current)) {
|
|
173
|
-
return true;
|
|
174
|
-
}
|
|
175
|
-
if (isInert(current)) {
|
|
176
|
-
return true;
|
|
177
|
-
}
|
|
178
|
-
if (isFormControl(element) && current.tagName === "FIELDSET" && isDisabled(current)) {
|
|
179
|
-
if (!current.querySelector(":scope > legend:first-of-type")?.contains(element)) {
|
|
180
|
-
return true;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
current = current.parentNode;
|
|
184
|
-
}
|
|
185
|
-
return false;
|
|
186
|
-
}
|
|
187
|
-
function normalizeRadioGroup(elements) {
|
|
188
|
-
let map = null;
|
|
189
|
-
for (let i = 0, l = elements.length; i < l; i++) {
|
|
190
|
-
const element = elements[i];
|
|
191
|
-
if (!(element instanceof HTMLInputElement)) {
|
|
192
|
-
continue;
|
|
193
|
-
}
|
|
194
|
-
if (!isUngroupedRadio(element)) {
|
|
195
|
-
continue;
|
|
196
|
-
}
|
|
197
|
-
if (!map) {
|
|
198
|
-
map = /* @__PURE__ */ new Map();
|
|
199
|
-
}
|
|
200
|
-
const key = `${element.form?.id ?? "no-form"}::${element.name}`;
|
|
201
|
-
const group = map.get(key) ?? map.set(key, []).get(key);
|
|
202
|
-
if (group) {
|
|
203
|
-
group[group.length] = element;
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
if (!map) {
|
|
207
|
-
return elements;
|
|
208
|
-
}
|
|
209
|
-
const placeholder = /* @__PURE__ */ new Set();
|
|
210
|
-
for (const group of map.values()) {
|
|
211
|
-
placeholder.add(group.find((radio) => radio.checked) ?? group[0]);
|
|
212
|
-
}
|
|
213
|
-
return elements.filter(
|
|
214
|
-
(element) => isUngroupedRadio(element) ? placeholder.has(element) : true
|
|
215
|
-
);
|
|
216
|
-
}
|
|
217
|
-
function sortByTabIndex(elements) {
|
|
218
|
-
const ordered = [];
|
|
219
|
-
const natural = [];
|
|
220
|
-
for (let i = 0, l = elements.length; i < l; i++) {
|
|
221
|
-
const element = elements[i];
|
|
222
|
-
if (element) {
|
|
223
|
-
const target = getTabIndex(element) > 0 ? ordered : natural;
|
|
224
|
-
target[target.length] = element;
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
ordered.sort((a, b) => getTabIndex(a) - getTabIndex(b));
|
|
228
|
-
let count = 0;
|
|
229
|
-
const sorted = new Array(ordered.length + natural.length);
|
|
230
|
-
for (let i = 0, l = ordered.length; i < l; i++) {
|
|
231
|
-
sorted[count++] = ordered[i];
|
|
232
|
-
}
|
|
233
|
-
for (let i = 0, l = natural.length; i < l; i++) {
|
|
234
|
-
sorted[count++] = natural[i];
|
|
235
|
-
}
|
|
236
|
-
return sorted;
|
|
237
|
-
}
|
|
238
|
-
function getComposedChildren(node) {
|
|
239
|
-
if (node instanceof ShadowRoot) {
|
|
240
|
-
return getChildren(node);
|
|
241
|
-
}
|
|
242
|
-
if (!(node instanceof Element)) {
|
|
243
|
-
return [];
|
|
244
|
-
}
|
|
245
|
-
if (node instanceof HTMLSlotElement) {
|
|
246
|
-
const assigned = node.assignedElements({ flatten: true });
|
|
247
|
-
if (assigned.length) {
|
|
248
|
-
return assigned;
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
if (node instanceof HTMLElement && node.shadowRoot?.mode === "open") {
|
|
252
|
-
return getChildren(node.shadowRoot);
|
|
253
|
-
}
|
|
254
|
-
return getChildren(node);
|
|
255
|
-
}
|
|
256
|
-
function focusElement(element) {
|
|
257
|
-
"focus" in element && typeof element.focus === "function" && element.focus();
|
|
258
|
-
}
|
|
259
|
-
function getActiveElement() {
|
|
260
|
-
let current = document.activeElement;
|
|
261
|
-
while (current?.shadowRoot?.activeElement) {
|
|
262
|
-
current = current.shadowRoot.activeElement;
|
|
263
|
-
}
|
|
264
|
-
return current;
|
|
265
|
-
}
|
|
266
|
-
function getChildren(node) {
|
|
267
|
-
const elements = [];
|
|
268
|
-
for (let child = node.firstElementChild; child; child = child.nextElementSibling) {
|
|
269
|
-
elements[elements.length] = child;
|
|
270
|
-
}
|
|
271
|
-
return elements;
|
|
272
|
-
}
|
|
273
|
-
function getTabIndex(element) {
|
|
274
|
-
return "tabIndex" in element ? Number(element.tabIndex) : 0;
|
|
275
|
-
}
|
|
276
|
-
function isDisabled(element) {
|
|
277
|
-
return "disabled" in element && !!element.disabled;
|
|
278
|
-
}
|
|
279
|
-
function isFormControl(element) {
|
|
280
|
-
const name = element.tagName;
|
|
281
|
-
return name === "BUTTON" || name === "INPUT" || name === "SELECT" || name === "TEXTAREA";
|
|
282
|
-
}
|
|
283
|
-
function isInert(element) {
|
|
284
|
-
return "inert" in element && !!element.inert;
|
|
285
|
-
}
|
|
286
|
-
function isUngroupedRadio(element) {
|
|
287
|
-
return element instanceof HTMLInputElement && element.type === "radio" && !!element.name;
|
|
288
|
-
}
|
|
3
|
+
var attributesUtils = require('@y14e/attributes-utils');
|
|
4
|
+
var powerFocusable = require('power-focusable');
|
|
289
5
|
|
|
290
6
|
// src/index.ts
|
|
291
7
|
function createRovingTabIndex(container, options = {}) {
|
|
@@ -366,7 +82,7 @@ var RovingTabIndex = class _RovingTabIndex {
|
|
|
366
82
|
this.#isDestroyed = true;
|
|
367
83
|
this.#controller?.abort();
|
|
368
84
|
this.#controller = null;
|
|
369
|
-
restoreAttributes([...this.#focusables]);
|
|
85
|
+
attributesUtils.restoreAttributes([...this.#focusables]);
|
|
370
86
|
this.#focusables.clear();
|
|
371
87
|
this.#focusablesByFirstChar.clear();
|
|
372
88
|
}
|
|
@@ -391,8 +107,8 @@ var RovingTabIndex = class _RovingTabIndex {
|
|
|
391
107
|
if (!(target instanceof Element)) {
|
|
392
108
|
return;
|
|
393
109
|
}
|
|
394
|
-
const
|
|
395
|
-
this.#settings.noMemory && !
|
|
110
|
+
const isFocusable = this.#focusables.has(target);
|
|
111
|
+
this.#settings.noMemory && !isFocusable ? this.#update(null) : isFocusable && this.#update(target);
|
|
396
112
|
};
|
|
397
113
|
#onKeyDown = (event) => {
|
|
398
114
|
const { key, altKey, ctrlKey, metaKey, shiftKey } = event;
|
|
@@ -412,7 +128,7 @@ var RovingTabIndex = class _RovingTabIndex {
|
|
|
412
128
|
return;
|
|
413
129
|
}
|
|
414
130
|
}
|
|
415
|
-
const active = getActiveElement();
|
|
131
|
+
const active = powerFocusable.getActiveElement();
|
|
416
132
|
if (!(active instanceof HTMLElement)) {
|
|
417
133
|
return;
|
|
418
134
|
}
|
|
@@ -452,13 +168,13 @@ var RovingTabIndex = class _RovingTabIndex {
|
|
|
452
168
|
}
|
|
453
169
|
}
|
|
454
170
|
const focusable = target.at(newIndex);
|
|
455
|
-
focusable && focusElement(focusable);
|
|
171
|
+
focusable && powerFocusable.focusElement(focusable);
|
|
456
172
|
};
|
|
457
173
|
#update(active) {
|
|
458
174
|
const current = new Set(this.#getFocusables());
|
|
459
175
|
for (const focusable of this.#focusables) {
|
|
460
176
|
if (!current.has(focusable)) {
|
|
461
|
-
focusable.isConnected && restoreAttributes([focusable]);
|
|
177
|
+
focusable.isConnected && attributesUtils.restoreAttributes([focusable]);
|
|
462
178
|
this.#focusables.delete(focusable);
|
|
463
179
|
this.#focusablesByFirstChar.forEach((focusables) => {
|
|
464
180
|
const index = focusables.indexOf(focusable);
|
|
@@ -477,7 +193,7 @@ var RovingTabIndex = class _RovingTabIndex {
|
|
|
477
193
|
this.#focusables.add(focusable);
|
|
478
194
|
_RovingTabIndex.#initialized.add(focusable);
|
|
479
195
|
if (!navigationOnly) {
|
|
480
|
-
saveAttributes([focusable], ["tabindex"]);
|
|
196
|
+
attributesUtils.saveAttributes([focusable], ["tabindex"]);
|
|
481
197
|
focusable.setAttribute("tabindex", "-1");
|
|
482
198
|
}
|
|
483
199
|
if (!typeahead) {
|
|
@@ -490,8 +206,8 @@ var RovingTabIndex = class _RovingTabIndex {
|
|
|
490
206
|
);
|
|
491
207
|
if (char) {
|
|
492
208
|
keys.add(char);
|
|
493
|
-
saveAttributes([focusable], ["aria-keyshortcuts"]);
|
|
494
|
-
addTokenToAttribute(focusable, "aria-keyshortcuts", char, {
|
|
209
|
+
attributesUtils.saveAttributes([focusable], ["aria-keyshortcuts"]);
|
|
210
|
+
attributesUtils.addTokenToAttribute(focusable, "aria-keyshortcuts", char, {
|
|
495
211
|
caseInsensitive: true
|
|
496
212
|
});
|
|
497
213
|
}
|
|
@@ -518,7 +234,7 @@ var RovingTabIndex = class _RovingTabIndex {
|
|
|
518
234
|
return (element) => !selector || [...this.#container.querySelectorAll(selector)].includes(element);
|
|
519
235
|
}
|
|
520
236
|
#getFocusables() {
|
|
521
|
-
return getFocusables(this.#container, {
|
|
237
|
+
return powerFocusable.getFocusables(this.#container, {
|
|
522
238
|
composed: true,
|
|
523
239
|
filter: this.#selectorFilter,
|
|
524
240
|
skipNegativeTabIndexCheck: !this.#settings.navigationOnly,
|
|
@@ -531,43 +247,11 @@ var RovingTabIndex = class _RovingTabIndex {
|
|
|
531
247
|
* Lightweight roving tabindex utility with fully focus management.
|
|
532
248
|
* Designed for accessible menus, tabs, toolbars, and composite widgets.
|
|
533
249
|
*
|
|
534
|
-
* @version 3.0.
|
|
250
|
+
* @version 3.0.11
|
|
535
251
|
* @author Yusuke Kamiyamane
|
|
536
252
|
* @license MIT
|
|
537
253
|
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
538
254
|
* @see {@link https://github.com/y14e/roving-tabindex}
|
|
539
255
|
*/
|
|
540
|
-
/*! Bundled license information:
|
|
541
|
-
|
|
542
|
-
@y14e/attributes-utils/dist/index.js:
|
|
543
|
-
(**
|
|
544
|
-
* Attributes Utils
|
|
545
|
-
*
|
|
546
|
-
* @version 1.1.2
|
|
547
|
-
* @author Yusuke Kamiyamane
|
|
548
|
-
* @license MIT
|
|
549
|
-
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
550
|
-
* @see {@link https://github.com/y14e/attributes-utils}
|
|
551
|
-
*)
|
|
552
|
-
|
|
553
|
-
power-focusable/dist/index.js:
|
|
554
|
-
(**
|
|
555
|
-
* Power Focusable
|
|
556
|
-
* High-precision focus management utility with full composed tree support.
|
|
557
|
-
* Handles complex focus rules including tabindex ordering, radio groups, inert.
|
|
558
|
-
*
|
|
559
|
-
* @version 4.3.3
|
|
560
|
-
* @author Yusuke Kamiyamane
|
|
561
|
-
* @license MIT
|
|
562
|
-
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
563
|
-
* @see {@link https://github.com/y14e/power-focusable}
|
|
564
|
-
*)
|
|
565
|
-
*/
|
|
566
256
|
|
|
567
|
-
exports.addTokenToAttribute = addTokenToAttribute;
|
|
568
257
|
exports.createRovingTabIndex = createRovingTabIndex;
|
|
569
|
-
exports.focusElement = focusElement;
|
|
570
|
-
exports.getActiveElement = getActiveElement;
|
|
571
|
-
exports.getFocusables = getFocusables;
|
|
572
|
-
exports.restoreAttributes = restoreAttributes;
|
|
573
|
-
exports.saveAttributes = saveAttributes;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,18 +1,14 @@
|
|
|
1
|
-
export { addTokenToAttribute, restoreAttributes, saveAttributes } from '@y14e/attributes-utils';
|
|
2
|
-
export { focusElement, getActiveElement, getFocusables } from 'power-focusable';
|
|
3
|
-
|
|
4
1
|
/**
|
|
5
2
|
* Roving Tabindex
|
|
6
3
|
* Lightweight roving tabindex utility with fully focus management.
|
|
7
4
|
* Designed for accessible menus, tabs, toolbars, and composite widgets.
|
|
8
5
|
*
|
|
9
|
-
* @version 3.0.
|
|
6
|
+
* @version 3.0.11
|
|
10
7
|
* @author Yusuke Kamiyamane
|
|
11
8
|
* @license MIT
|
|
12
9
|
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
13
10
|
* @see {@link https://github.com/y14e/roving-tabindex}
|
|
14
11
|
*/
|
|
15
|
-
|
|
16
12
|
interface RovingTabIndexOptions {
|
|
17
13
|
readonly direction?: 'horizontal' | 'vertical';
|
|
18
14
|
readonly navigationOnly?: boolean;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,18 +1,14 @@
|
|
|
1
|
-
export { addTokenToAttribute, restoreAttributes, saveAttributes } from '@y14e/attributes-utils';
|
|
2
|
-
export { focusElement, getActiveElement, getFocusables } from 'power-focusable';
|
|
3
|
-
|
|
4
1
|
/**
|
|
5
2
|
* Roving Tabindex
|
|
6
3
|
* Lightweight roving tabindex utility with fully focus management.
|
|
7
4
|
* Designed for accessible menus, tabs, toolbars, and composite widgets.
|
|
8
5
|
*
|
|
9
|
-
* @version 3.0.
|
|
6
|
+
* @version 3.0.11
|
|
10
7
|
* @author Yusuke Kamiyamane
|
|
11
8
|
* @license MIT
|
|
12
9
|
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
13
10
|
* @see {@link https://github.com/y14e/roving-tabindex}
|
|
14
11
|
*/
|
|
15
|
-
|
|
16
12
|
interface RovingTabIndexOptions {
|
|
17
13
|
readonly direction?: 'horizontal' | 'vertical';
|
|
18
14
|
readonly navigationOnly?: boolean;
|
package/dist/index.js
CHANGED
|
@@ -1,289 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var DEFAULT_SERIALIZER = (tokens) => tokens.join(" ");
|
|
4
|
-
function addTokenToAttribute(element, attribute, token, options = {}) {
|
|
5
|
-
const {
|
|
6
|
-
caseInsensitive = false,
|
|
7
|
-
parse = DEFAULT_PARSER,
|
|
8
|
-
serialize = DEFAULT_SERIALIZER
|
|
9
|
-
} = options;
|
|
10
|
-
const value = element.getAttribute(attribute)?.trim();
|
|
11
|
-
const tokens = value ? parse(value).filter(Boolean) : [];
|
|
12
|
-
if (caseInsensitive) {
|
|
13
|
-
const lower = token.toLowerCase();
|
|
14
|
-
if (tokens.every((token2) => token2.toLowerCase() !== lower)) {
|
|
15
|
-
tokens.push(token);
|
|
16
|
-
element.setAttribute(attribute, serialize(tokens));
|
|
17
|
-
}
|
|
18
|
-
} else {
|
|
19
|
-
const set = new Set(tokens);
|
|
20
|
-
set.add(token);
|
|
21
|
-
element.setAttribute(attribute, serialize([...set]));
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
var snapshots = /* @__PURE__ */ new WeakMap();
|
|
25
|
-
function restoreAttributes(elements) {
|
|
26
|
-
for (const element of elements) {
|
|
27
|
-
const snapshot = snapshots.get(element);
|
|
28
|
-
if (!snapshot) {
|
|
29
|
-
continue;
|
|
30
|
-
}
|
|
31
|
-
for (const [attribute, value] of snapshot.entries()) {
|
|
32
|
-
value === null ? element.removeAttribute(attribute) : element.setAttribute(attribute, value);
|
|
33
|
-
}
|
|
34
|
-
snapshots.delete(element);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
function saveAttributes(elements, attributes) {
|
|
38
|
-
elements.forEach((element) => {
|
|
39
|
-
let snapshot = snapshots.get(element);
|
|
40
|
-
if (!snapshot) {
|
|
41
|
-
snapshot = /* @__PURE__ */ new Map();
|
|
42
|
-
snapshots.set(element, snapshot);
|
|
43
|
-
}
|
|
44
|
-
attributes.forEach((attribute) => {
|
|
45
|
-
snapshot.set(attribute, element.getAttribute(attribute));
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// node_modules/power-focusable/dist/index.js
|
|
51
|
-
var FOCUSABLE_SELECTOR = `:is(a[href], area[href], button, embed, iframe, input:not([type="hidden" i]), object, select, details > summary:first-of-type, textarea, [contenteditable]:not([contenteditable="false" i]), [controls], [tabindex]):not(:disabled, [hidden], [inert], [tabindex="-1"])`;
|
|
52
|
-
function getFocusables(container = document.body, options = {}) {
|
|
53
|
-
if (!(container instanceof Element)) {
|
|
54
|
-
console.warn("Invalid container element. Fallback: <body> element.");
|
|
55
|
-
container = document.body;
|
|
56
|
-
}
|
|
57
|
-
let {
|
|
58
|
-
composed = false,
|
|
59
|
-
filter,
|
|
60
|
-
include,
|
|
61
|
-
skipNegativeTabIndexCheck = false,
|
|
62
|
-
skipVisibilityCheck = false
|
|
63
|
-
} = options;
|
|
64
|
-
if (typeof composed !== "boolean") {
|
|
65
|
-
console.warn("Invalid composed option. Fallback: false.");
|
|
66
|
-
composed = false;
|
|
67
|
-
}
|
|
68
|
-
if (typeof filter !== "undefined" && typeof filter !== "function") {
|
|
69
|
-
console.warn(
|
|
70
|
-
"Invalid filter function. Fallback: no filter function (undefined)."
|
|
71
|
-
);
|
|
72
|
-
filter = void 0;
|
|
73
|
-
}
|
|
74
|
-
if (typeof include !== "undefined" && typeof include !== "function") {
|
|
75
|
-
console.warn(
|
|
76
|
-
"Invalid include function. Fallback: no include function (undefined)."
|
|
77
|
-
);
|
|
78
|
-
include = void 0;
|
|
79
|
-
}
|
|
80
|
-
if (typeof skipNegativeTabIndexCheck !== "boolean") {
|
|
81
|
-
console.warn("Invalid skipNegativeTabIndexCheck option. Fallback: false.");
|
|
82
|
-
skipNegativeTabIndexCheck = false;
|
|
83
|
-
}
|
|
84
|
-
if (typeof skipVisibilityCheck !== "boolean") {
|
|
85
|
-
console.warn("Invalid skipVisibilityCheck option. Fallback: false.");
|
|
86
|
-
skipVisibilityCheck = false;
|
|
87
|
-
}
|
|
88
|
-
const elements = [];
|
|
89
|
-
if (composed || include) {
|
|
90
|
-
let traverse2 = function(node) {
|
|
91
|
-
if (!(node instanceof Element)) {
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
|
-
if (isFocusable(node, { skipNegativeTabIndexCheck, skipVisibilityCheck }) || include?.(node)) {
|
|
95
|
-
elements[elements.length] = node;
|
|
96
|
-
}
|
|
97
|
-
const children = getComposedChildren(node);
|
|
98
|
-
for (let i = 0, l = children.length; i < l; i++) {
|
|
99
|
-
const child = children[i];
|
|
100
|
-
child && traverse2(child);
|
|
101
|
-
}
|
|
102
|
-
};
|
|
103
|
-
traverse2(container);
|
|
104
|
-
} else {
|
|
105
|
-
const candidates = container.querySelectorAll(FOCUSABLE_SELECTOR);
|
|
106
|
-
for (let i = 0, l = candidates.length; i < l; i++) {
|
|
107
|
-
const candidate = candidates[i];
|
|
108
|
-
if (candidate && isFocusable(candidate, {
|
|
109
|
-
skipNegativeTabIndexCheck,
|
|
110
|
-
skipVisibilityCheck
|
|
111
|
-
})) {
|
|
112
|
-
elements[elements.length] = candidate;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
const unfiltered = normalizeRadioGroup(sortByTabIndex(elements));
|
|
117
|
-
return filter ? unfiltered.filter(filter) : unfiltered;
|
|
118
|
-
}
|
|
119
|
-
function isFocusable(element, options = {}) {
|
|
120
|
-
if (!(element instanceof Element)) {
|
|
121
|
-
console.warn("Invalid element");
|
|
122
|
-
return false;
|
|
123
|
-
}
|
|
124
|
-
let { skipNegativeTabIndexCheck = false, skipVisibilityCheck = false } = options;
|
|
125
|
-
if (typeof skipNegativeTabIndexCheck !== "boolean") {
|
|
126
|
-
console.warn("Invalid skipNegativeTabIndexCheck option. Fallback: false.");
|
|
127
|
-
skipNegativeTabIndexCheck = false;
|
|
128
|
-
}
|
|
129
|
-
if (typeof skipVisibilityCheck !== "boolean") {
|
|
130
|
-
console.warn("Invalid skipVisibilityCheck option. Fallback: false.");
|
|
131
|
-
skipVisibilityCheck = false;
|
|
132
|
-
}
|
|
133
|
-
if (element.hasAttribute("hidden") || isInert(element)) {
|
|
134
|
-
return false;
|
|
135
|
-
}
|
|
136
|
-
if (!skipNegativeTabIndexCheck && getTabIndex(element) < 0) {
|
|
137
|
-
return false;
|
|
138
|
-
}
|
|
139
|
-
if (!element.matches(
|
|
140
|
-
skipNegativeTabIndexCheck ? FOCUSABLE_SELECTOR.replace(/(,\s*)?\[tabindex="-1"\]/g, "") : FOCUSABLE_SELECTOR
|
|
141
|
-
)) {
|
|
142
|
-
return false;
|
|
143
|
-
}
|
|
144
|
-
if (isDisabledDeep(element)) {
|
|
145
|
-
return false;
|
|
146
|
-
}
|
|
147
|
-
if (!skipVisibilityCheck && !element.checkVisibility({
|
|
148
|
-
contentVisibilityAuto: true,
|
|
149
|
-
opacityProperty: true,
|
|
150
|
-
visibilityProperty: true
|
|
151
|
-
})) {
|
|
152
|
-
return false;
|
|
153
|
-
}
|
|
154
|
-
return true;
|
|
155
|
-
}
|
|
156
|
-
function isDisabledDeep(element) {
|
|
157
|
-
let current = element;
|
|
158
|
-
while (current) {
|
|
159
|
-
if (current instanceof ShadowRoot) {
|
|
160
|
-
if (current.mode !== "open") {
|
|
161
|
-
return false;
|
|
162
|
-
}
|
|
163
|
-
current = current.host;
|
|
164
|
-
continue;
|
|
165
|
-
}
|
|
166
|
-
if (!(current instanceof Element)) {
|
|
167
|
-
current = current.parentNode;
|
|
168
|
-
continue;
|
|
169
|
-
}
|
|
170
|
-
if (current === element && isFormControl(current) && isDisabled(current)) {
|
|
171
|
-
return true;
|
|
172
|
-
}
|
|
173
|
-
if (isInert(current)) {
|
|
174
|
-
return true;
|
|
175
|
-
}
|
|
176
|
-
if (isFormControl(element) && current.tagName === "FIELDSET" && isDisabled(current)) {
|
|
177
|
-
if (!current.querySelector(":scope > legend:first-of-type")?.contains(element)) {
|
|
178
|
-
return true;
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
current = current.parentNode;
|
|
182
|
-
}
|
|
183
|
-
return false;
|
|
184
|
-
}
|
|
185
|
-
function normalizeRadioGroup(elements) {
|
|
186
|
-
let map = null;
|
|
187
|
-
for (let i = 0, l = elements.length; i < l; i++) {
|
|
188
|
-
const element = elements[i];
|
|
189
|
-
if (!(element instanceof HTMLInputElement)) {
|
|
190
|
-
continue;
|
|
191
|
-
}
|
|
192
|
-
if (!isUngroupedRadio(element)) {
|
|
193
|
-
continue;
|
|
194
|
-
}
|
|
195
|
-
if (!map) {
|
|
196
|
-
map = /* @__PURE__ */ new Map();
|
|
197
|
-
}
|
|
198
|
-
const key = `${element.form?.id ?? "no-form"}::${element.name}`;
|
|
199
|
-
const group = map.get(key) ?? map.set(key, []).get(key);
|
|
200
|
-
if (group) {
|
|
201
|
-
group[group.length] = element;
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
if (!map) {
|
|
205
|
-
return elements;
|
|
206
|
-
}
|
|
207
|
-
const placeholder = /* @__PURE__ */ new Set();
|
|
208
|
-
for (const group of map.values()) {
|
|
209
|
-
placeholder.add(group.find((radio) => radio.checked) ?? group[0]);
|
|
210
|
-
}
|
|
211
|
-
return elements.filter(
|
|
212
|
-
(element) => isUngroupedRadio(element) ? placeholder.has(element) : true
|
|
213
|
-
);
|
|
214
|
-
}
|
|
215
|
-
function sortByTabIndex(elements) {
|
|
216
|
-
const ordered = [];
|
|
217
|
-
const natural = [];
|
|
218
|
-
for (let i = 0, l = elements.length; i < l; i++) {
|
|
219
|
-
const element = elements[i];
|
|
220
|
-
if (element) {
|
|
221
|
-
const target = getTabIndex(element) > 0 ? ordered : natural;
|
|
222
|
-
target[target.length] = element;
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
ordered.sort((a, b) => getTabIndex(a) - getTabIndex(b));
|
|
226
|
-
let count = 0;
|
|
227
|
-
const sorted = new Array(ordered.length + natural.length);
|
|
228
|
-
for (let i = 0, l = ordered.length; i < l; i++) {
|
|
229
|
-
sorted[count++] = ordered[i];
|
|
230
|
-
}
|
|
231
|
-
for (let i = 0, l = natural.length; i < l; i++) {
|
|
232
|
-
sorted[count++] = natural[i];
|
|
233
|
-
}
|
|
234
|
-
return sorted;
|
|
235
|
-
}
|
|
236
|
-
function getComposedChildren(node) {
|
|
237
|
-
if (node instanceof ShadowRoot) {
|
|
238
|
-
return getChildren(node);
|
|
239
|
-
}
|
|
240
|
-
if (!(node instanceof Element)) {
|
|
241
|
-
return [];
|
|
242
|
-
}
|
|
243
|
-
if (node instanceof HTMLSlotElement) {
|
|
244
|
-
const assigned = node.assignedElements({ flatten: true });
|
|
245
|
-
if (assigned.length) {
|
|
246
|
-
return assigned;
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
if (node instanceof HTMLElement && node.shadowRoot?.mode === "open") {
|
|
250
|
-
return getChildren(node.shadowRoot);
|
|
251
|
-
}
|
|
252
|
-
return getChildren(node);
|
|
253
|
-
}
|
|
254
|
-
function focusElement(element) {
|
|
255
|
-
"focus" in element && typeof element.focus === "function" && element.focus();
|
|
256
|
-
}
|
|
257
|
-
function getActiveElement() {
|
|
258
|
-
let current = document.activeElement;
|
|
259
|
-
while (current?.shadowRoot?.activeElement) {
|
|
260
|
-
current = current.shadowRoot.activeElement;
|
|
261
|
-
}
|
|
262
|
-
return current;
|
|
263
|
-
}
|
|
264
|
-
function getChildren(node) {
|
|
265
|
-
const elements = [];
|
|
266
|
-
for (let child = node.firstElementChild; child; child = child.nextElementSibling) {
|
|
267
|
-
elements[elements.length] = child;
|
|
268
|
-
}
|
|
269
|
-
return elements;
|
|
270
|
-
}
|
|
271
|
-
function getTabIndex(element) {
|
|
272
|
-
return "tabIndex" in element ? Number(element.tabIndex) : 0;
|
|
273
|
-
}
|
|
274
|
-
function isDisabled(element) {
|
|
275
|
-
return "disabled" in element && !!element.disabled;
|
|
276
|
-
}
|
|
277
|
-
function isFormControl(element) {
|
|
278
|
-
const name = element.tagName;
|
|
279
|
-
return name === "BUTTON" || name === "INPUT" || name === "SELECT" || name === "TEXTAREA";
|
|
280
|
-
}
|
|
281
|
-
function isInert(element) {
|
|
282
|
-
return "inert" in element && !!element.inert;
|
|
283
|
-
}
|
|
284
|
-
function isUngroupedRadio(element) {
|
|
285
|
-
return element instanceof HTMLInputElement && element.type === "radio" && !!element.name;
|
|
286
|
-
}
|
|
1
|
+
import { restoreAttributes, saveAttributes, addTokenToAttribute } from '@y14e/attributes-utils';
|
|
2
|
+
import { getActiveElement, focusElement, getFocusables } from 'power-focusable';
|
|
287
3
|
|
|
288
4
|
// src/index.ts
|
|
289
5
|
function createRovingTabIndex(container, options = {}) {
|
|
@@ -389,8 +105,8 @@ var RovingTabIndex = class _RovingTabIndex {
|
|
|
389
105
|
if (!(target instanceof Element)) {
|
|
390
106
|
return;
|
|
391
107
|
}
|
|
392
|
-
const
|
|
393
|
-
this.#settings.noMemory && !
|
|
108
|
+
const isFocusable = this.#focusables.has(target);
|
|
109
|
+
this.#settings.noMemory && !isFocusable ? this.#update(null) : isFocusable && this.#update(target);
|
|
394
110
|
};
|
|
395
111
|
#onKeyDown = (event) => {
|
|
396
112
|
const { key, altKey, ctrlKey, metaKey, shiftKey } = event;
|
|
@@ -529,37 +245,11 @@ var RovingTabIndex = class _RovingTabIndex {
|
|
|
529
245
|
* Lightweight roving tabindex utility with fully focus management.
|
|
530
246
|
* Designed for accessible menus, tabs, toolbars, and composite widgets.
|
|
531
247
|
*
|
|
532
|
-
* @version 3.0.
|
|
248
|
+
* @version 3.0.11
|
|
533
249
|
* @author Yusuke Kamiyamane
|
|
534
250
|
* @license MIT
|
|
535
251
|
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
536
252
|
* @see {@link https://github.com/y14e/roving-tabindex}
|
|
537
253
|
*/
|
|
538
|
-
/*! Bundled license information:
|
|
539
|
-
|
|
540
|
-
@y14e/attributes-utils/dist/index.js:
|
|
541
|
-
(**
|
|
542
|
-
* Attributes Utils
|
|
543
|
-
*
|
|
544
|
-
* @version 1.1.2
|
|
545
|
-
* @author Yusuke Kamiyamane
|
|
546
|
-
* @license MIT
|
|
547
|
-
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
548
|
-
* @see {@link https://github.com/y14e/attributes-utils}
|
|
549
|
-
*)
|
|
550
|
-
|
|
551
|
-
power-focusable/dist/index.js:
|
|
552
|
-
(**
|
|
553
|
-
* Power Focusable
|
|
554
|
-
* High-precision focus management utility with full composed tree support.
|
|
555
|
-
* Handles complex focus rules including tabindex ordering, radio groups, inert.
|
|
556
|
-
*
|
|
557
|
-
* @version 4.3.3
|
|
558
|
-
* @author Yusuke Kamiyamane
|
|
559
|
-
* @license MIT
|
|
560
|
-
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
561
|
-
* @see {@link https://github.com/y14e/power-focusable}
|
|
562
|
-
*)
|
|
563
|
-
*/
|
|
564
254
|
|
|
565
|
-
export {
|
|
255
|
+
export { createRovingTabIndex };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@y14e/roving-tabindex",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.11",
|
|
4
4
|
"description": "Lightweight roving tabindex utility with fully focus management",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -48,13 +48,15 @@
|
|
|
48
48
|
},
|
|
49
49
|
"homepage": "https://github.com/y14e/roving-tabindex#readme",
|
|
50
50
|
"devDependencies": {
|
|
51
|
-
"@y14e/attributes-utils": "^1.1.2",
|
|
52
51
|
"bun-types": "latest",
|
|
53
|
-
"power-focusable": "^4.3.4",
|
|
54
52
|
"tsup": "^8.0.0",
|
|
55
53
|
"typescript": "^5.6.0"
|
|
56
54
|
},
|
|
57
55
|
"engines": {
|
|
58
56
|
"node": ">=18"
|
|
57
|
+
},
|
|
58
|
+
"dependencies": {
|
|
59
|
+
"@y14e/attributes-utils": "^1.1.2",
|
|
60
|
+
"power-focusable": "^4.3.4"
|
|
59
61
|
}
|
|
60
62
|
}
|