@y14e/roving-tabindex 3.0.9 → 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/README.md +4 -4
- package/dist/index.bundle.cjs +257 -0
- package/dist/index.bundle.js +255 -0
- package/dist/index.cjs +19 -337
- package/dist/index.d.cts +1 -5
- package/dist/index.d.ts +1 -5
- package/dist/index.js +12 -324
- package/package.json +5 -3
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 = {}) {
|
|
@@ -295,8 +11,8 @@ function createRovingTabIndex(container, options = {}) {
|
|
|
295
11
|
const roving = new RovingTabIndex(container, options);
|
|
296
12
|
return () => roving.destroy();
|
|
297
13
|
}
|
|
298
|
-
var
|
|
299
|
-
|
|
14
|
+
var RovingTabIndex = class _RovingTabIndex {
|
|
15
|
+
static #initialized = /* @__PURE__ */ new Set();
|
|
300
16
|
#container;
|
|
301
17
|
#settings;
|
|
302
18
|
#focusables = /* @__PURE__ */ new Set();
|
|
@@ -367,7 +83,6 @@ var RovingTabIndex = class {
|
|
|
367
83
|
restoreAttributes([...this.#focusables]);
|
|
368
84
|
this.#focusables.clear();
|
|
369
85
|
this.#focusablesByFirstChar.clear();
|
|
370
|
-
this.#container.removeAttribute("data-roving-tabindex-initialized");
|
|
371
86
|
}
|
|
372
87
|
#initialize() {
|
|
373
88
|
this.#update(document.activeElement);
|
|
@@ -384,15 +99,14 @@ var RovingTabIndex = class {
|
|
|
384
99
|
capture: true,
|
|
385
100
|
signal
|
|
386
101
|
});
|
|
387
|
-
this.#container.setAttribute("data-roving-tabindex-initialized", "");
|
|
388
102
|
}
|
|
389
103
|
#onFocusIn = (event) => {
|
|
390
104
|
const { target } = event;
|
|
391
105
|
if (!(target instanceof Element)) {
|
|
392
106
|
return;
|
|
393
107
|
}
|
|
394
|
-
const
|
|
395
|
-
this.#settings.noMemory && !
|
|
108
|
+
const isFocusable = this.#focusables.has(target);
|
|
109
|
+
this.#settings.noMemory && !isFocusable ? this.#update(null) : isFocusable && this.#update(target);
|
|
396
110
|
};
|
|
397
111
|
#onKeyDown = (event) => {
|
|
398
112
|
const { key, altKey, ctrlKey, metaKey, shiftKey } = event;
|
|
@@ -468,14 +182,14 @@ var RovingTabIndex = class {
|
|
|
468
182
|
}
|
|
469
183
|
const { navigationOnly, noStart, typeahead } = this.#settings;
|
|
470
184
|
for (const focusable of current) {
|
|
471
|
-
if (initialized.has(focusable)) {
|
|
472
|
-
throw new TypeError("Already initialized");
|
|
473
|
-
}
|
|
474
185
|
if (this.#focusables.has(focusable)) {
|
|
475
186
|
continue;
|
|
476
187
|
}
|
|
188
|
+
if (_RovingTabIndex.#initialized.has(focusable)) {
|
|
189
|
+
throw new TypeError("Already initialized");
|
|
190
|
+
}
|
|
477
191
|
this.#focusables.add(focusable);
|
|
478
|
-
initialized.add(focusable);
|
|
192
|
+
_RovingTabIndex.#initialized.add(focusable);
|
|
479
193
|
if (!navigationOnly) {
|
|
480
194
|
saveAttributes([focusable], ["tabindex"]);
|
|
481
195
|
focusable.setAttribute("tabindex", "-1");
|
|
@@ -531,37 +245,11 @@ var RovingTabIndex = class {
|
|
|
531
245
|
* Lightweight roving tabindex utility with fully focus management.
|
|
532
246
|
* Designed for accessible menus, tabs, toolbars, and composite widgets.
|
|
533
247
|
*
|
|
534
|
-
* @version 3.0.
|
|
248
|
+
* @version 3.0.11
|
|
535
249
|
* @author Yusuke Kamiyamane
|
|
536
250
|
* @license MIT
|
|
537
251
|
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
538
252
|
* @see {@link https://github.com/y14e/roving-tabindex}
|
|
539
253
|
*/
|
|
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
254
|
|
|
567
|
-
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
|
}
|