@y14e/disclosure 1.3.16 → 1.3.18

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 focusElement(element) {
253
- "focus" in element && typeof element.focus === "function" && element.focus();
254
- }
255
- function getActiveElement() {
256
- let current = document.activeElement;
257
- while (current?.shadowRoot?.activeElement) {
258
- current = current.shadowRoot.activeElement;
259
- }
260
- return current;
261
- }
262
- function getChildren(node) {
263
- const elements = [];
264
- for (let child = node.firstElementChild; child; child = child.nextElementSibling) {
265
- elements[elements.length] = child;
266
- }
267
- return elements;
268
- }
269
- function getTabIndex(element) {
270
- return "tabIndex" in element ? Number(element.tabIndex) : 0;
271
- }
272
- function isDisabled(element) {
273
- return "disabled" in element && !!element.disabled;
274
- }
275
- function isFormControl(element) {
276
- const name = element.tagName;
277
- return name === "BUTTON" || name === "INPUT" || name === "SELECT" || name === "TEXTAREA";
278
- }
279
- function isInert(element) {
280
- return "inert" in element && !!element.inert;
281
- }
282
- function isUngroupedRadio(element) {
283
- return element instanceof HTMLInputElement && element.type === "radio" && !!element.name;
284
- }
285
- function createRovingTabIndex(container, options = {}) {
286
- if (!(container instanceof Element)) {
287
- console.warn("Invalid container element");
288
- return () => {
289
- };
290
- }
291
- const roving = new RovingTabIndex(container, options);
292
- return () => roving.destroy();
293
- }
294
- var RovingTabIndex = class {
295
- #container;
296
- #settings;
297
- #focusables = /* @__PURE__ */ new Set();
298
- #focusablesByFirstChar = /* @__PURE__ */ new Map();
299
- #selectorFilter;
300
- #controller = null;
301
- #isDestroyed = false;
302
- constructor(container, options = {}) {
303
- this.#container = container;
304
- let {
305
- direction,
306
- navigationOnly = false,
307
- noMemory = false,
308
- noStart = false,
309
- selector,
310
- typeahead = false,
311
- wrap = false
312
- } = options;
313
- if (typeof direction !== "undefined" && !["horizontal", "vertical"].includes(direction)) {
314
- console.warn("Invalid direction option. Fallback: both (undefined).");
315
- direction = void 0;
316
- }
317
- if (typeof navigationOnly !== "boolean") {
318
- console.warn("Invalid navigationOnly option. Fallback: false.");
319
- navigationOnly = false;
320
- }
321
- if (typeof noMemory !== "boolean") {
322
- console.warn("Invalid noMemory option. Fallback: false.");
323
- noMemory = false;
324
- }
325
- if (typeof noStart !== "boolean") {
326
- console.warn("Invalid noStart option. Fallback: false.");
327
- noStart = false;
328
- }
329
- if (typeof selector !== "undefined" && (typeof selector !== "string" || !selector.trim())) {
330
- console.warn(
331
- "Invalid selector. Fallback: all focusable elements (undefined)."
332
- );
333
- selector = void 0;
334
- }
335
- if (typeof typeahead !== "boolean") {
336
- console.warn("Invalid typeahead option. Fallback: false.");
337
- typeahead = false;
338
- }
339
- if (typeof wrap !== "boolean") {
340
- console.warn("Invalid wrap option. Fallback: false.");
341
- wrap = false;
342
- }
343
- this.#settings = {
344
- navigationOnly,
345
- noMemory,
346
- noStart,
347
- typeahead,
348
- wrap
349
- };
350
- direction && Object.assign(this.#settings, { direction });
351
- selector && Object.assign(this.#settings, { selector });
352
- this.#selectorFilter = this.#createSelectorFilter();
353
- this.#initialize();
354
- }
355
- destroy() {
356
- if (this.#isDestroyed) {
357
- return;
358
- }
359
- this.#isDestroyed = true;
360
- this.#controller?.abort();
361
- this.#controller = null;
362
- restoreAttributes([...this.#focusables]);
363
- this.#focusables.clear();
364
- this.#focusablesByFirstChar.clear();
365
- this.#container.removeAttribute("data-roving-tabindex-initialized");
366
- }
367
- #initialize() {
368
- this.#update(document.activeElement);
369
- this.#controller = new AbortController();
370
- const { signal } = this.#controller;
371
- document.addEventListener("focusin", this.#onFocusIn, {
372
- capture: true,
373
- signal
374
- });
375
- document.addEventListener("keydown", this.#onKeyDown, {
376
- capture: true,
377
- signal
378
- });
379
- this.#container.setAttribute("data-roving-tabindex-initialized", "");
380
- }
381
- #onFocusIn = (event) => {
382
- const { target } = event;
383
- if (!(target instanceof Element)) {
384
- return;
385
- }
386
- const isFocusable22 = this.#focusables.has(target);
387
- this.#settings.noMemory && !isFocusable22 ? this.#update(null) : isFocusable22 && this.#update(target);
388
- };
389
- #onKeyDown = (event) => {
390
- if (!event.composedPath().includes(this.#container)) {
391
- return;
392
- }
393
- const { key, altKey, ctrlKey, metaKey, shiftKey } = event;
394
- if (altKey || ctrlKey || metaKey || shiftKey) {
395
- return;
396
- }
397
- const { direction, typeahead, wrap } = this.#settings;
398
- const isBoth = !direction;
399
- const isHorizontal = direction === "horizontal";
400
- if (![
401
- "End",
402
- "Home",
403
- ...isBoth ? ["ArrowLeft", "ArrowUp"] : [`Arrow${isHorizontal ? "Left" : "Up"}`],
404
- ...isBoth ? ["ArrowRight", "ArrowDown"] : [`Arrow${isHorizontal ? "Right" : "Down"}`]
405
- ].includes(key)) {
406
- if (!typeahead || !/^\S$/i.test(key) || !this.#focusablesByFirstChar.has(key.toUpperCase())) {
407
- return;
408
- }
409
- }
410
- const active = getActiveElement();
411
- if (!(active instanceof HTMLElement)) {
412
- return;
413
- }
414
- const current = this.#getFocusables();
415
- if (!current.includes(active)) {
416
- return;
417
- }
418
- event.preventDefault();
419
- const currentIndex = current.indexOf(active);
420
- let newIndex;
421
- let target = current;
422
- switch (key) {
423
- case "End":
424
- newIndex = -1;
425
- break;
426
- case "Home":
427
- newIndex = 0;
428
- break;
429
- case "ArrowLeft":
430
- case "ArrowUp": {
431
- const rawIndex = currentIndex - 1;
432
- newIndex = wrap ? rawIndex : Math.max(rawIndex, 0);
433
- break;
434
- }
435
- case "ArrowRight":
436
- case "ArrowDown": {
437
- const rawIndex = currentIndex + 1;
438
- newIndex = wrap ? rawIndex % current.length : Math.min(rawIndex, current.length - 1);
439
- break;
440
- }
441
- default: {
442
- target = this.#focusablesByFirstChar.get(key.toUpperCase()) ?? [];
443
- const foundIndex = target.findIndex(
444
- (focusable2) => current.indexOf(focusable2) > currentIndex
445
- );
446
- newIndex = foundIndex >= 0 ? foundIndex : 0;
447
- }
448
- }
449
- const focusable = target.at(newIndex);
450
- focusable && focusElement(focusable);
451
- };
452
- #update(active) {
453
- const current = new Set(this.#getFocusables());
454
- for (const focusable of this.#focusables) {
455
- if (!current.has(focusable)) {
456
- focusable.isConnected && restoreAttributes([focusable]);
457
- this.#focusables.delete(focusable);
458
- this.#focusablesByFirstChar.forEach((focusables) => {
459
- const index = focusables.indexOf(focusable);
460
- index >= 0 && focusables.splice(index, 1);
461
- });
462
- }
463
- }
464
- const { navigationOnly, noStart, typeahead } = this.#settings;
465
- for (const focusable of current) {
466
- if (this.#focusables.has(focusable)) {
467
- continue;
468
- }
469
- this.#focusables.add(focusable);
470
- if (!navigationOnly) {
471
- saveAttributes([focusable], ["tabindex"]);
472
- focusable.setAttribute("tabindex", "-1");
473
- }
474
- if (!typeahead) {
475
- continue;
476
- }
477
- const char = focusable.textContent?.trim()?.at(0)?.toUpperCase();
478
- const value = focusable.ariaKeyShortcuts?.trim();
479
- const keys = new Set(
480
- value ? value.split(/\s+/).filter((key) => /^\S$/i.test(key)).map((key) => key.toUpperCase()) : []
481
- );
482
- if (char) {
483
- keys.add(char);
484
- saveAttributes([focusable], ["aria-keyshortcuts"]);
485
- addTokenToAttribute(focusable, "aria-keyshortcuts", char, {
486
- caseInsensitive: true
487
- });
488
- }
489
- keys.forEach((key) => {
490
- const focusables = this.#focusablesByFirstChar.get(key) ?? [];
491
- focusables.push(focusable);
492
- this.#focusablesByFirstChar.set(key, focusables);
493
- });
494
- }
495
- if (!navigationOnly) {
496
- if (active && this.#focusables.has(active)) {
497
- this.#focusables.forEach((focusable) => {
498
- focusable.setAttribute("tabindex", focusable === active ? "0" : "-1");
499
- });
500
- } else {
501
- [...this.#focusables].forEach((focusable, i) => {
502
- focusable.setAttribute("tabindex", i || noStart ? "-1" : "0");
503
- });
504
- }
505
- }
506
- }
507
- #createSelectorFilter() {
508
- const { selector } = this.#settings;
509
- return (element) => !selector || [...this.#container.querySelectorAll(selector)].includes(element);
510
- }
511
- #getFocusables() {
512
- return getFocusables(this.#container, {
513
- composed: true,
514
- filter: this.#selectorFilter,
515
- skipNegativeTabIndexCheck: !this.#settings.navigationOnly,
516
- skipVisibilityCheck: true
517
- });
518
- }
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");
@@ -706,7 +189,7 @@ var Disclosure = class _Disclosure {
706
189
  if (!binding) {
707
190
  return;
708
191
  }
709
- const { content, timer } = binding;
192
+ const { content } = binding;
710
193
  const startSize = details.open ? content.offsetHeight : 0;
711
194
  binding.animation?.cancel();
712
195
  if (isOpen) {
@@ -714,11 +197,7 @@ var Disclosure = class _Disclosure {
714
197
  }
715
198
  const endSize = isOpen ? content.scrollHeight : 0;
716
199
  binding.animation?.cancel();
717
- timer && cancelAnimationFrame(timer);
718
- binding.timer = requestAnimationFrame(() => {
719
- binding.timer = void 0;
720
- details.toggleAttribute("data-disclosure-open", isOpen);
721
- });
200
+ details.toggleAttribute("data-disclosure-open", isOpen);
722
201
  content.style.setProperty("overflow", "clip");
723
202
  const { duration, easing } = this.#settings.animation;
724
203
  const animation = content.animate(
@@ -778,9 +257,9 @@ var Disclosure = class _Disclosure {
778
257
  }
779
258
  };
780
259
  function createBinding(details, summary, content) {
781
- return { details, summary, content, timer: void 0, animation: null };
260
+ return { details, summary, content, animation: null };
782
261
  }
783
- function isFocusable2(element) {
262
+ function isFocusable(element) {
784
263
  return element.tabIndex >= 0;
785
264
  }
786
265
  function waitAnimationFinish(animation) {
@@ -797,52 +276,11 @@ function waitAnimationFinish(animation) {
797
276
  * WAI-ARIA compliant disclosure pattern implementation in TypeScript.
798
277
  * Using the <details> and <summary> element.
799
278
  *
800
- * @version 1.3.16
279
+ * @version 1.3.18
801
280
  * @author Yusuke Kamiyamane
802
281
  * @license MIT
803
282
  * @copyright Copyright (c) Yusuke Kamiyamane
804
283
  * @see {@link https://github.com/y14e/disclosure}
805
284
  */
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.8
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
285
 
848
286
  export { Disclosure as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@y14e/disclosure",
3
- "version": "1.3.16",
3
+ "version": "1.3.18",
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.8",
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.14"
54
57
  }
55
58
  }