@y14e/disclosure 1.3.15 → 1.3.17

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.js CHANGED
@@ -1,522 +1,5 @@
1
- // node_modules/@y14e/roving-tabindex/dist/index.js
2
- var DEFAULT_PARSER = (value) => value.split(/\s+/);
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
- 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"])`;
50
- function getFocusables(container = document.body, options = {}) {
51
- if (!(container instanceof Element)) {
52
- console.warn("Invalid container element. Fallback: <body> element.");
53
- container = document.body;
54
- }
55
- let {
56
- composed = false,
57
- filter,
58
- include,
59
- skipNegativeTabIndexCheck = false,
60
- skipVisibilityCheck = false
61
- } = options;
62
- if (typeof composed !== "boolean") {
63
- console.warn("Invalid composed option. Fallback: false.");
64
- composed = false;
65
- }
66
- if (typeof filter !== "undefined" && typeof filter !== "function") {
67
- console.warn(
68
- "Invalid filter function. Fallback: no filter function (undefined)."
69
- );
70
- filter = void 0;
71
- }
72
- if (typeof include !== "undefined" && typeof include !== "function") {
73
- console.warn(
74
- "Invalid include function. Fallback: no include function (undefined)."
75
- );
76
- include = void 0;
77
- }
78
- if (typeof skipNegativeTabIndexCheck !== "boolean") {
79
- console.warn("Invalid skipNegativeTabIndexCheck option. Fallback: false.");
80
- skipNegativeTabIndexCheck = false;
81
- }
82
- if (typeof skipVisibilityCheck !== "boolean") {
83
- console.warn("Invalid skipVisibilityCheck option. Fallback: false.");
84
- skipVisibilityCheck = false;
85
- }
86
- const elements = [];
87
- if (composed || include) {
88
- let traverse2 = function(node) {
89
- if (!(node instanceof Element)) {
90
- return;
91
- }
92
- if (isFocusable(node, { skipNegativeTabIndexCheck, skipVisibilityCheck }) || include?.(node)) {
93
- elements[elements.length] = node;
94
- }
95
- const children = getComposedChildren(node);
96
- for (let i = 0, l = children.length; i < l; i++) {
97
- const child = children[i];
98
- child && traverse2(child);
99
- }
100
- };
101
- traverse2(container);
102
- } else {
103
- const candidates = container.querySelectorAll(FOCUSABLE_SELECTOR);
104
- for (let i = 0, l = candidates.length; i < l; i++) {
105
- const candidate = candidates[i];
106
- if (candidate && isFocusable(candidate, {
107
- skipNegativeTabIndexCheck,
108
- skipVisibilityCheck
109
- })) {
110
- elements[elements.length] = candidate;
111
- }
112
- }
113
- }
114
- const unfiltered = normalizeRadioGroup(sortByTabIndex(elements));
115
- return filter ? unfiltered.filter(filter) : unfiltered;
116
- }
117
- function isFocusable(element, options = {}) {
118
- if (!(element instanceof Element)) {
119
- console.warn("Invalid element");
120
- return false;
121
- }
122
- let { skipNegativeTabIndexCheck = false, skipVisibilityCheck = false } = options;
123
- if (typeof skipNegativeTabIndexCheck !== "boolean") {
124
- console.warn("Invalid skipNegativeTabIndexCheck option. Fallback: false.");
125
- skipNegativeTabIndexCheck = false;
126
- }
127
- if (typeof skipVisibilityCheck !== "boolean") {
128
- console.warn("Invalid skipVisibilityCheck option. Fallback: false.");
129
- skipVisibilityCheck = false;
130
- }
131
- if (element.hasAttribute("hidden") || isInert(element)) {
132
- return false;
133
- }
134
- if (!skipNegativeTabIndexCheck && getTabIndex(element) < 0) {
135
- return false;
136
- }
137
- if (!element.matches(
138
- skipNegativeTabIndexCheck ? FOCUSABLE_SELECTOR.replace(/(,\s*)?\[tabindex="-1"\]/g, "") : FOCUSABLE_SELECTOR
139
- )) {
140
- return false;
141
- }
142
- if (isDisabledDeep(element)) {
143
- return false;
144
- }
145
- if (!skipVisibilityCheck && !element.checkVisibility({
146
- contentVisibilityAuto: true,
147
- opacityProperty: true,
148
- visibilityProperty: true
149
- })) {
150
- return false;
151
- }
152
- return true;
153
- }
154
- function isDisabledDeep(element) {
155
- let current = element;
156
- while (current) {
157
- if (current instanceof ShadowRoot) {
158
- if (current.mode !== "open") {
159
- return false;
160
- }
161
- current = current.host;
162
- continue;
163
- }
164
- if (!(current instanceof Element)) {
165
- current = current.parentNode;
166
- continue;
167
- }
168
- if (current === element && isFormControl(current) && isDisabled(current)) {
169
- return true;
170
- }
171
- if (isInert(current)) {
172
- return true;
173
- }
174
- if (isFormControl(element) && current.tagName === "FIELDSET" && isDisabled(current)) {
175
- if (!current.querySelector(":scope > legend:first-of-type")?.contains(element)) {
176
- return true;
177
- }
178
- }
179
- current = current.parentNode;
180
- }
181
- return false;
182
- }
183
- function normalizeRadioGroup(elements) {
184
- let map = null;
185
- for (let i = 0, l = elements.length; i < l; i++) {
186
- const element = elements[i];
187
- if (!(element instanceof HTMLInputElement)) {
188
- continue;
189
- }
190
- if (!isUngroupedRadio(element)) {
191
- continue;
192
- }
193
- if (!map) {
194
- map = /* @__PURE__ */ new Map();
195
- }
196
- const key = `${element.form?.id ?? "no-form"}::${element.name}`;
197
- const group = map.get(key) ?? map.set(key, []).get(key);
198
- if (group) {
199
- group[group.length] = element;
200
- }
201
- }
202
- if (!map) {
203
- return elements;
204
- }
205
- const placeholder = /* @__PURE__ */ new Set();
206
- for (const group of map.values()) {
207
- placeholder.add(group.find((radio) => radio.checked) ?? group[0]);
208
- }
209
- return elements.filter(
210
- (element) => isUngroupedRadio(element) ? placeholder.has(element) : true
211
- );
212
- }
213
- function sortByTabIndex(elements) {
214
- const ordered = [];
215
- const natural = [];
216
- for (let i = 0, l = elements.length; i < l; i++) {
217
- const element = elements[i];
218
- if (element) {
219
- const target = getTabIndex(element) > 0 ? ordered : natural;
220
- target[target.length] = element;
221
- }
222
- }
223
- ordered.sort((a, b) => getTabIndex(a) - getTabIndex(b));
224
- let count = 0;
225
- const sorted = new Array(ordered.length + natural.length);
226
- for (let i = 0, l = ordered.length; i < l; i++) {
227
- sorted[count++] = ordered[i];
228
- }
229
- for (let i = 0, l = natural.length; i < l; i++) {
230
- sorted[count++] = natural[i];
231
- }
232
- return sorted;
233
- }
234
- function getComposedChildren(node) {
235
- if (node instanceof ShadowRoot) {
236
- return getChildren(node);
237
- }
238
- if (!(node instanceof Element)) {
239
- return [];
240
- }
241
- if (node instanceof HTMLSlotElement) {
242
- const assigned = node.assignedElements({ flatten: true });
243
- if (assigned.length) {
244
- return assigned;
245
- }
246
- }
247
- if (node instanceof HTMLElement && node.shadowRoot?.mode === "open") {
248
- return getChildren(node.shadowRoot);
249
- }
250
- return getChildren(node);
251
- }
252
- function getChildren(node) {
253
- const elements = [];
254
- for (let child = node.firstElementChild; child; child = child.nextElementSibling) {
255
- elements[elements.length] = child;
256
- }
257
- return elements;
258
- }
259
- function getTabIndex(element) {
260
- return "tabIndex" in element ? Number(element.tabIndex) : 0;
261
- }
262
- function isDisabled(element) {
263
- return "disabled" in element && !!element.disabled;
264
- }
265
- function isFormControl(element) {
266
- const name = element.tagName;
267
- return name === "BUTTON" || name === "INPUT" || name === "SELECT" || name === "TEXTAREA";
268
- }
269
- function isInert(element) {
270
- return "inert" in element && !!element.inert;
271
- }
272
- function isUngroupedRadio(element) {
273
- return element instanceof HTMLInputElement && element.type === "radio" && !!element.name;
274
- }
275
- function createRovingTabIndex(container, options = {}) {
276
- if (!(container instanceof Element)) {
277
- console.warn("Invalid container element");
278
- return () => {
279
- };
280
- }
281
- const roving = new RovingTabIndex(container, options);
282
- return () => roving.destroy();
283
- }
284
- var RovingTabIndex = class {
285
- #container;
286
- #settings;
287
- #focusables = /* @__PURE__ */ new Set();
288
- #focusablesByFirstChar = /* @__PURE__ */ new Map();
289
- #selectorFilter;
290
- #controller = null;
291
- #isDestroyed = false;
292
- constructor(container, options = {}) {
293
- this.#container = container;
294
- let {
295
- direction,
296
- navigationOnly = false,
297
- noMemory = false,
298
- noStart = false,
299
- selector,
300
- typeahead = false,
301
- wrap = false
302
- } = options;
303
- if (typeof direction !== "undefined" && !["horizontal", "vertical"].includes(direction)) {
304
- console.warn("Invalid direction option. Fallback: both (undefined).");
305
- direction = void 0;
306
- }
307
- if (typeof navigationOnly !== "boolean") {
308
- console.warn("Invalid navigationOnly option. Fallback: false.");
309
- navigationOnly = false;
310
- }
311
- if (typeof noMemory !== "boolean") {
312
- console.warn("Invalid noMemory option. Fallback: false.");
313
- noMemory = false;
314
- }
315
- if (typeof noStart !== "boolean") {
316
- console.warn("Invalid noStart option. Fallback: false.");
317
- noStart = false;
318
- }
319
- if (typeof selector !== "undefined" && (typeof selector !== "string" || !selector.trim())) {
320
- console.warn(
321
- "Invalid selector. Fallback: all focusable elements (undefined)."
322
- );
323
- selector = void 0;
324
- }
325
- if (typeof typeahead !== "boolean") {
326
- console.warn("Invalid typeahead option. Fallback: false.");
327
- typeahead = false;
328
- }
329
- if (typeof wrap !== "boolean") {
330
- console.warn("Invalid wrap option. Fallback: false.");
331
- wrap = false;
332
- }
333
- this.#settings = {
334
- navigationOnly,
335
- noMemory,
336
- noStart,
337
- typeahead,
338
- wrap
339
- };
340
- direction && Object.assign(this.#settings, { direction });
341
- selector && Object.assign(this.#settings, { selector });
342
- this.#selectorFilter = this.#createSelectorFilter();
343
- this.#initialize();
344
- }
345
- destroy() {
346
- if (this.#isDestroyed) {
347
- return;
348
- }
349
- this.#isDestroyed = true;
350
- this.#controller?.abort();
351
- this.#controller = null;
352
- restoreAttributes([...this.#focusables]);
353
- this.#focusables.clear();
354
- this.#focusablesByFirstChar.clear();
355
- this.#container.removeAttribute("data-roving-tabindex-initialized");
356
- }
357
- #initialize() {
358
- this.#update(document.activeElement);
359
- this.#controller = new AbortController();
360
- const { signal } = this.#controller;
361
- document.addEventListener("focusin", this.#onFocusIn, {
362
- capture: true,
363
- signal
364
- });
365
- document.addEventListener("keydown", this.#onKeyDown, {
366
- capture: true,
367
- signal
368
- });
369
- this.#container.setAttribute("data-roving-tabindex-initialized", "");
370
- }
371
- #onFocusIn = (event) => {
372
- const { target } = event;
373
- if (!(target instanceof Element)) {
374
- return;
375
- }
376
- const isFocusable22 = this.#focusables.has(target);
377
- this.#settings.noMemory && !isFocusable22 ? this.#update(null) : isFocusable22 && this.#update(target);
378
- };
379
- #onKeyDown = (event) => {
380
- if (!event.composedPath().includes(this.#container)) {
381
- return;
382
- }
383
- const { key, altKey, ctrlKey, metaKey, shiftKey } = event;
384
- if (altKey || ctrlKey || metaKey || shiftKey) {
385
- return;
386
- }
387
- const { direction, typeahead, wrap } = this.#settings;
388
- const isBoth = !direction;
389
- const isHorizontal = direction === "horizontal";
390
- if (![
391
- "End",
392
- "Home",
393
- ...isBoth ? ["ArrowLeft", "ArrowUp"] : [`Arrow${isHorizontal ? "Left" : "Up"}`],
394
- ...isBoth ? ["ArrowRight", "ArrowDown"] : [`Arrow${isHorizontal ? "Right" : "Down"}`]
395
- ].includes(key)) {
396
- if (!typeahead || !/^\S$/i.test(key) || !this.#focusablesByFirstChar.has(key.toUpperCase())) {
397
- return;
398
- }
399
- }
400
- const active = getActiveElement();
401
- if (!(active instanceof HTMLElement)) {
402
- return;
403
- }
404
- const current = this.#getFocusables();
405
- if (!current.includes(active)) {
406
- return;
407
- }
408
- event.preventDefault();
409
- const currentIndex = current.indexOf(active);
410
- let newIndex;
411
- let target = current;
412
- switch (key) {
413
- case "End":
414
- newIndex = -1;
415
- break;
416
- case "Home":
417
- newIndex = 0;
418
- break;
419
- case "ArrowLeft":
420
- case "ArrowUp": {
421
- const rawIndex = currentIndex - 1;
422
- newIndex = wrap ? rawIndex : Math.max(rawIndex, 0);
423
- break;
424
- }
425
- case "ArrowRight":
426
- case "ArrowDown": {
427
- const rawIndex = currentIndex + 1;
428
- newIndex = wrap ? rawIndex % current.length : Math.min(rawIndex, current.length - 1);
429
- break;
430
- }
431
- default: {
432
- target = this.#focusablesByFirstChar.get(key.toUpperCase()) ?? [];
433
- const foundIndex = target.findIndex(
434
- (focusable2) => current.indexOf(focusable2) > currentIndex
435
- );
436
- newIndex = foundIndex >= 0 ? foundIndex : 0;
437
- }
438
- }
439
- const focusable = target.at(newIndex);
440
- focusable && focusElement(focusable);
441
- };
442
- #update(active) {
443
- const current = new Set(this.#getFocusables());
444
- for (const focusable of this.#focusables) {
445
- if (!current.has(focusable)) {
446
- focusable.isConnected && restoreAttributes([focusable]);
447
- this.#focusables.delete(focusable);
448
- this.#focusablesByFirstChar.forEach((focusables) => {
449
- const index = focusables.indexOf(focusable);
450
- index >= 0 && focusables.splice(index, 1);
451
- });
452
- }
453
- }
454
- const { navigationOnly, noStart, typeahead } = this.#settings;
455
- for (const focusable of current) {
456
- if (this.#focusables.has(focusable)) {
457
- continue;
458
- }
459
- this.#focusables.add(focusable);
460
- if (!navigationOnly) {
461
- saveAttributes([focusable], ["tabindex"]);
462
- focusable.setAttribute("tabindex", "-1");
463
- }
464
- if (!typeahead) {
465
- continue;
466
- }
467
- const char = focusable.textContent?.trim()?.at(0)?.toUpperCase();
468
- const value = focusable.ariaKeyShortcuts?.trim();
469
- const keys = new Set(
470
- value ? value.split(/\s+/).filter((key) => /^\S$/i.test(key)).map((key) => key.toUpperCase()) : []
471
- );
472
- if (char) {
473
- keys.add(char);
474
- saveAttributes([focusable], ["aria-keyshortcuts"]);
475
- addTokenToAttribute(focusable, "aria-keyshortcuts", char, {
476
- caseInsensitive: true
477
- });
478
- }
479
- keys.forEach((key) => {
480
- const focusables = this.#focusablesByFirstChar.get(key) ?? [];
481
- focusables.push(focusable);
482
- this.#focusablesByFirstChar.set(key, focusables);
483
- });
484
- }
485
- if (!navigationOnly) {
486
- if (active && this.#focusables.has(active)) {
487
- this.#focusables.forEach((focusable) => {
488
- focusable.setAttribute("tabindex", focusable === active ? "0" : "-1");
489
- });
490
- } else {
491
- [...this.#focusables].forEach((focusable, i) => {
492
- focusable.setAttribute("tabindex", i || noStart ? "-1" : "0");
493
- });
494
- }
495
- }
496
- }
497
- #createSelectorFilter() {
498
- const { selector } = this.#settings;
499
- return (element) => !selector || [...this.#container.querySelectorAll(selector)].includes(element);
500
- }
501
- #getFocusables() {
502
- return getFocusables(this.#container, {
503
- composed: true,
504
- filter: this.#selectorFilter,
505
- skipNegativeTabIndexCheck: !this.#settings.navigationOnly,
506
- skipVisibilityCheck: true
507
- });
508
- }
509
- };
510
- function focusElement(element) {
511
- "focus" in element && typeof element.focus === "function" && element.focus();
512
- }
513
- function getActiveElement() {
514
- let current = document.activeElement;
515
- while (current?.shadowRoot?.activeElement) {
516
- current = current.shadowRoot.activeElement;
517
- }
518
- return current;
519
- }
1
+ import { restoreAttributes, saveAttributes } from '@y14e/attributes-utils';
2
+ import { createRovingTabIndex } from '@y14e/roving-tabindex';
520
3
 
521
4
  // src/index.ts
522
5
  var Disclosure = class _Disclosure {
@@ -662,7 +145,7 @@ var Disclosure = class _Disclosure {
662
145
  if (!summary) {
663
146
  return;
664
147
  }
665
- if (!isFocusable2(summary)) {
148
+ if (!isFocusable(summary)) {
666
149
  summary.setAttribute("aria-disabled", "true");
667
150
  summary.setAttribute("tabindex", "-1");
668
151
  summary.style.setProperty("pointer-events", "none");
@@ -780,7 +263,7 @@ var Disclosure = class _Disclosure {
780
263
  function createBinding(details, summary, content) {
781
264
  return { details, summary, content, timer: void 0, animation: null };
782
265
  }
783
- function isFocusable2(element) {
266
+ function isFocusable(element) {
784
267
  return element.tabIndex >= 0;
785
268
  }
786
269
  function waitAnimationFinish(animation) {
@@ -797,52 +280,11 @@ function waitAnimationFinish(animation) {
797
280
  * WAI-ARIA compliant disclosure pattern implementation in TypeScript.
798
281
  * Using the <details> and <summary> element.
799
282
  *
800
- * @version 1.3.15
283
+ * @version 1.3.17
801
284
  * @author Yusuke Kamiyamane
802
285
  * @license MIT
803
286
  * @copyright Copyright (c) Yusuke Kamiyamane
804
287
  * @see {@link https://github.com/y14e/disclosure}
805
288
  */
806
- /*! Bundled license information:
807
-
808
- @y14e/roving-tabindex/dist/index.js:
809
- (**
810
- * Roving Tabindex
811
- * Lightweight roving tabindex utility with fully focus management.
812
- * Designed for accessible menus, tabs, toolbars, and composite widgets.
813
- *
814
- * @version 3.0.7
815
- * @author Yusuke Kamiyamane
816
- * @license MIT
817
- * @copyright Copyright (c) Yusuke Kamiyamane
818
- * @see {@link https://github.com/y14e/roving-tabindex}
819
- *)
820
- (*! Bundled license information:
821
-
822
- @y14e/attributes-utils/dist/index.js:
823
- (**
824
- * Attributes Utils
825
- *
826
- * @version 1.1.2
827
- * @author Yusuke Kamiyamane
828
- * @license MIT
829
- * @copyright Copyright (c) Yusuke Kamiyamane
830
- * @see {@link https://github.com/y14e/attributes-utils}
831
- *)
832
-
833
- power-focusable/dist/index.js:
834
- (**
835
- * Power Focusable
836
- * High-precision focus management utility with full composed tree support.
837
- * Handles complex focus rules including tabindex ordering, radio groups, inert.
838
- *
839
- * @version 4.3.3
840
- * @author Yusuke Kamiyamane
841
- * @license MIT
842
- * @copyright Copyright (c) Yusuke Kamiyamane
843
- * @see {@link https://github.com/y14e/power-focusable}
844
- *)
845
- *)
846
- */
847
289
 
848
290
  export { Disclosure as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@y14e/disclosure",
3
- "version": "1.3.15",
3
+ "version": "1.3.17",
4
4
  "description": "WAI-ARIA compliant disclosure pattern implementation in TypeScript",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -43,7 +43,6 @@
43
43
  },
44
44
  "homepage": "https://github.com/y14e/disclosure#readme",
45
45
  "devDependencies": {
46
- "@y14e/roving-tabindex": "^3.0.7",
47
46
  "bun-types": "latest",
48
47
  "tsup": "^8.0.0",
49
48
  "typescript": "^5.6.0",
@@ -51,5 +50,9 @@
51
50
  },
52
51
  "engines": {
53
52
  "node": ">=18"
53
+ },
54
+ "dependencies": {
55
+ "@y14e/attributes-utils": "^1.1.2",
56
+ "@y14e/roving-tabindex": "^3.0.12"
54
57
  }
55
58
  }