@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/dist/index.cjs CHANGED
@@ -1,291 +1,7 @@
1
1
  'use strict';
2
2
 
3
- // node_modules/@y14e/attributes-utils/dist/index.js
4
- var DEFAULT_PARSER = (value) => value.split(/\s+/);
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 = {}) {
@@ -297,8 +13,8 @@ function createRovingTabIndex(container, options = {}) {
297
13
  const roving = new RovingTabIndex(container, options);
298
14
  return () => roving.destroy();
299
15
  }
300
- var initialized = /* @__PURE__ */ new Set();
301
- var RovingTabIndex = class {
16
+ var RovingTabIndex = class _RovingTabIndex {
17
+ static #initialized = /* @__PURE__ */ new Set();
302
18
  #container;
303
19
  #settings;
304
20
  #focusables = /* @__PURE__ */ new Set();
@@ -366,10 +82,9 @@ var RovingTabIndex = class {
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
- this.#container.removeAttribute("data-roving-tabindex-initialized");
373
88
  }
374
89
  #initialize() {
375
90
  this.#update(document.activeElement);
@@ -386,15 +101,14 @@ var RovingTabIndex = class {
386
101
  capture: true,
387
102
  signal
388
103
  });
389
- this.#container.setAttribute("data-roving-tabindex-initialized", "");
390
104
  }
391
105
  #onFocusIn = (event) => {
392
106
  const { target } = event;
393
107
  if (!(target instanceof Element)) {
394
108
  return;
395
109
  }
396
- const isFocusable2 = this.#focusables.has(target);
397
- this.#settings.noMemory && !isFocusable2 ? this.#update(null) : isFocusable2 && this.#update(target);
110
+ const isFocusable = this.#focusables.has(target);
111
+ this.#settings.noMemory && !isFocusable ? this.#update(null) : isFocusable && this.#update(target);
398
112
  };
399
113
  #onKeyDown = (event) => {
400
114
  const { key, altKey, ctrlKey, metaKey, shiftKey } = event;
@@ -414,7 +128,7 @@ var RovingTabIndex = class {
414
128
  return;
415
129
  }
416
130
  }
417
- const active = getActiveElement();
131
+ const active = powerFocusable.getActiveElement();
418
132
  if (!(active instanceof HTMLElement)) {
419
133
  return;
420
134
  }
@@ -454,13 +168,13 @@ var RovingTabIndex = class {
454
168
  }
455
169
  }
456
170
  const focusable = target.at(newIndex);
457
- focusable && focusElement(focusable);
171
+ focusable && powerFocusable.focusElement(focusable);
458
172
  };
459
173
  #update(active) {
460
174
  const current = new Set(this.#getFocusables());
461
175
  for (const focusable of this.#focusables) {
462
176
  if (!current.has(focusable)) {
463
- focusable.isConnected && restoreAttributes([focusable]);
177
+ focusable.isConnected && attributesUtils.restoreAttributes([focusable]);
464
178
  this.#focusables.delete(focusable);
465
179
  this.#focusablesByFirstChar.forEach((focusables) => {
466
180
  const index = focusables.indexOf(focusable);
@@ -470,16 +184,16 @@ var RovingTabIndex = class {
470
184
  }
471
185
  const { navigationOnly, noStart, typeahead } = this.#settings;
472
186
  for (const focusable of current) {
473
- if (initialized.has(focusable)) {
474
- throw new TypeError("Already initialized");
475
- }
476
187
  if (this.#focusables.has(focusable)) {
477
188
  continue;
478
189
  }
190
+ if (_RovingTabIndex.#initialized.has(focusable)) {
191
+ throw new TypeError("Already initialized");
192
+ }
479
193
  this.#focusables.add(focusable);
480
- initialized.add(focusable);
194
+ _RovingTabIndex.#initialized.add(focusable);
481
195
  if (!navigationOnly) {
482
- saveAttributes([focusable], ["tabindex"]);
196
+ attributesUtils.saveAttributes([focusable], ["tabindex"]);
483
197
  focusable.setAttribute("tabindex", "-1");
484
198
  }
485
199
  if (!typeahead) {
@@ -492,8 +206,8 @@ var RovingTabIndex = class {
492
206
  );
493
207
  if (char) {
494
208
  keys.add(char);
495
- saveAttributes([focusable], ["aria-keyshortcuts"]);
496
- addTokenToAttribute(focusable, "aria-keyshortcuts", char, {
209
+ attributesUtils.saveAttributes([focusable], ["aria-keyshortcuts"]);
210
+ attributesUtils.addTokenToAttribute(focusable, "aria-keyshortcuts", char, {
497
211
  caseInsensitive: true
498
212
  });
499
213
  }
@@ -520,7 +234,7 @@ var RovingTabIndex = class {
520
234
  return (element) => !selector || [...this.#container.querySelectorAll(selector)].includes(element);
521
235
  }
522
236
  #getFocusables() {
523
- return getFocusables(this.#container, {
237
+ return powerFocusable.getFocusables(this.#container, {
524
238
  composed: true,
525
239
  filter: this.#selectorFilter,
526
240
  skipNegativeTabIndexCheck: !this.#settings.navigationOnly,
@@ -533,43 +247,11 @@ var RovingTabIndex = class {
533
247
  * Lightweight roving tabindex utility with fully focus management.
534
248
  * Designed for accessible menus, tabs, toolbars, and composite widgets.
535
249
  *
536
- * @version 3.0.9
250
+ * @version 3.0.11
537
251
  * @author Yusuke Kamiyamane
538
252
  * @license MIT
539
253
  * @copyright Copyright (c) Yusuke Kamiyamane
540
254
  * @see {@link https://github.com/y14e/roving-tabindex}
541
255
  */
542
- /*! Bundled license information:
543
-
544
- @y14e/attributes-utils/dist/index.js:
545
- (**
546
- * Attributes Utils
547
- *
548
- * @version 1.1.2
549
- * @author Yusuke Kamiyamane
550
- * @license MIT
551
- * @copyright Copyright (c) Yusuke Kamiyamane
552
- * @see {@link https://github.com/y14e/attributes-utils}
553
- *)
554
-
555
- power-focusable/dist/index.js:
556
- (**
557
- * Power Focusable
558
- * High-precision focus management utility with full composed tree support.
559
- * Handles complex focus rules including tabindex ordering, radio groups, inert.
560
- *
561
- * @version 4.3.3
562
- * @author Yusuke Kamiyamane
563
- * @license MIT
564
- * @copyright Copyright (c) Yusuke Kamiyamane
565
- * @see {@link https://github.com/y14e/power-focusable}
566
- *)
567
- */
568
256
 
569
- exports.addTokenToAttribute = addTokenToAttribute;
570
257
  exports.createRovingTabIndex = createRovingTabIndex;
571
- exports.focusElement = focusElement;
572
- exports.getActiveElement = getActiveElement;
573
- exports.getFocusables = getFocusables;
574
- exports.restoreAttributes = restoreAttributes;
575
- 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.9
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.9
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;