@y14e/disclosure-css 1.3.9 → 1.3.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,524 +1,7 @@
1
1
  'use strict';
2
2
 
3
- // node_modules/@y14e/roving-tabindex/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
- 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 getChildren(node) {
255
- const elements = [];
256
- for (let child = node.firstElementChild; child; child = child.nextElementSibling) {
257
- elements[elements.length] = child;
258
- }
259
- return elements;
260
- }
261
- function getTabIndex(element) {
262
- return "tabIndex" in element ? Number(element.tabIndex) : 0;
263
- }
264
- function isDisabled(element) {
265
- return "disabled" in element && !!element.disabled;
266
- }
267
- function isFormControl(element) {
268
- const name = element.tagName;
269
- return name === "BUTTON" || name === "INPUT" || name === "SELECT" || name === "TEXTAREA";
270
- }
271
- function isInert(element) {
272
- return "inert" in element && !!element.inert;
273
- }
274
- function isUngroupedRadio(element) {
275
- return element instanceof HTMLInputElement && element.type === "radio" && !!element.name;
276
- }
277
- function createRovingTabIndex(container, options = {}) {
278
- if (!(container instanceof Element)) {
279
- console.warn("Invalid container element");
280
- return () => {
281
- };
282
- }
283
- const roving = new RovingTabIndex(container, options);
284
- return () => roving.destroy();
285
- }
286
- var RovingTabIndex = class {
287
- #container;
288
- #settings;
289
- #focusables = /* @__PURE__ */ new Set();
290
- #focusablesByFirstChar = /* @__PURE__ */ new Map();
291
- #selectorFilter;
292
- #controller = null;
293
- #isDestroyed = false;
294
- constructor(container, options = {}) {
295
- this.#container = container;
296
- let {
297
- direction,
298
- navigationOnly = false,
299
- noMemory = false,
300
- noStart = false,
301
- selector,
302
- typeahead = false,
303
- wrap = false
304
- } = options;
305
- if (typeof direction !== "undefined" && !["horizontal", "vertical"].includes(direction)) {
306
- console.warn("Invalid direction option. Fallback: both (undefined).");
307
- direction = void 0;
308
- }
309
- if (typeof navigationOnly !== "boolean") {
310
- console.warn("Invalid navigationOnly option. Fallback: false.");
311
- navigationOnly = false;
312
- }
313
- if (typeof noMemory !== "boolean") {
314
- console.warn("Invalid noMemory option. Fallback: false.");
315
- noMemory = false;
316
- }
317
- if (typeof noStart !== "boolean") {
318
- console.warn("Invalid noStart option. Fallback: false.");
319
- noStart = false;
320
- }
321
- if (typeof selector !== "undefined" && (typeof selector !== "string" || !selector.trim())) {
322
- console.warn(
323
- "Invalid selector. Fallback: all focusable elements (undefined)."
324
- );
325
- selector = void 0;
326
- }
327
- if (typeof typeahead !== "boolean") {
328
- console.warn("Invalid typeahead option. Fallback: false.");
329
- typeahead = false;
330
- }
331
- if (typeof wrap !== "boolean") {
332
- console.warn("Invalid wrap option. Fallback: false.");
333
- wrap = false;
334
- }
335
- this.#settings = {
336
- navigationOnly,
337
- noMemory,
338
- noStart,
339
- typeahead,
340
- wrap
341
- };
342
- direction && Object.assign(this.#settings, { direction });
343
- selector && Object.assign(this.#settings, { selector });
344
- this.#selectorFilter = this.#createSelectorFilter();
345
- this.#initialize();
346
- }
347
- destroy() {
348
- if (this.#isDestroyed) {
349
- return;
350
- }
351
- this.#isDestroyed = true;
352
- this.#controller?.abort();
353
- this.#controller = null;
354
- restoreAttributes([...this.#focusables]);
355
- this.#focusables.clear();
356
- this.#focusablesByFirstChar.clear();
357
- this.#container.removeAttribute("data-roving-tabindex-initialized");
358
- }
359
- #initialize() {
360
- this.#update(document.activeElement);
361
- this.#controller = new AbortController();
362
- const { signal } = this.#controller;
363
- document.addEventListener("focusin", this.#onFocusIn, {
364
- capture: true,
365
- signal
366
- });
367
- document.addEventListener("keydown", this.#onKeyDown, {
368
- capture: true,
369
- signal
370
- });
371
- this.#container.setAttribute("data-roving-tabindex-initialized", "");
372
- }
373
- #onFocusIn = (event) => {
374
- const { target } = event;
375
- if (!(target instanceof Element)) {
376
- return;
377
- }
378
- const isFocusable22 = this.#focusables.has(target);
379
- this.#settings.noMemory && !isFocusable22 ? this.#update(null) : isFocusable22 && this.#update(target);
380
- };
381
- #onKeyDown = (event) => {
382
- if (!event.composedPath().includes(this.#container)) {
383
- return;
384
- }
385
- const { key, altKey, ctrlKey, metaKey, shiftKey } = event;
386
- if (altKey || ctrlKey || metaKey || shiftKey) {
387
- return;
388
- }
389
- const { direction, typeahead, wrap } = this.#settings;
390
- const isBoth = !direction;
391
- const isHorizontal = direction === "horizontal";
392
- if (![
393
- "End",
394
- "Home",
395
- ...isBoth ? ["ArrowLeft", "ArrowUp"] : [`Arrow${isHorizontal ? "Left" : "Up"}`],
396
- ...isBoth ? ["ArrowRight", "ArrowDown"] : [`Arrow${isHorizontal ? "Right" : "Down"}`]
397
- ].includes(key)) {
398
- if (!typeahead || !/^\S$/i.test(key) || !this.#focusablesByFirstChar.has(key.toUpperCase())) {
399
- return;
400
- }
401
- }
402
- const active = getActiveElement();
403
- if (!(active instanceof HTMLElement)) {
404
- return;
405
- }
406
- const current = this.#getFocusables();
407
- if (!current.includes(active)) {
408
- return;
409
- }
410
- event.preventDefault();
411
- const currentIndex = current.indexOf(active);
412
- let newIndex;
413
- let target = current;
414
- switch (key) {
415
- case "End":
416
- newIndex = -1;
417
- break;
418
- case "Home":
419
- newIndex = 0;
420
- break;
421
- case "ArrowLeft":
422
- case "ArrowUp": {
423
- const rawIndex = currentIndex - 1;
424
- newIndex = wrap ? rawIndex : Math.max(rawIndex, 0);
425
- break;
426
- }
427
- case "ArrowRight":
428
- case "ArrowDown": {
429
- const rawIndex = currentIndex + 1;
430
- newIndex = wrap ? rawIndex % current.length : Math.min(rawIndex, current.length - 1);
431
- break;
432
- }
433
- default: {
434
- target = this.#focusablesByFirstChar.get(key.toUpperCase()) ?? [];
435
- const foundIndex = target.findIndex(
436
- (focusable2) => current.indexOf(focusable2) > currentIndex
437
- );
438
- newIndex = foundIndex >= 0 ? foundIndex : 0;
439
- }
440
- }
441
- const focusable = target.at(newIndex);
442
- focusable && focusElement(focusable);
443
- };
444
- #update(active) {
445
- const current = new Set(this.#getFocusables());
446
- for (const focusable of this.#focusables) {
447
- if (!current.has(focusable)) {
448
- focusable.isConnected && restoreAttributes([focusable]);
449
- this.#focusables.delete(focusable);
450
- this.#focusablesByFirstChar.forEach((focusables) => {
451
- const index = focusables.indexOf(focusable);
452
- index >= 0 && focusables.splice(index, 1);
453
- });
454
- }
455
- }
456
- const { navigationOnly, noStart, typeahead } = this.#settings;
457
- for (const focusable of current) {
458
- if (this.#focusables.has(focusable)) {
459
- continue;
460
- }
461
- this.#focusables.add(focusable);
462
- if (!navigationOnly) {
463
- saveAttributes([focusable], ["tabindex"]);
464
- focusable.setAttribute("tabindex", "-1");
465
- }
466
- if (!typeahead) {
467
- continue;
468
- }
469
- const char = focusable.textContent?.trim()?.at(0)?.toUpperCase();
470
- const value = focusable.ariaKeyShortcuts?.trim();
471
- const keys = new Set(
472
- value ? value.split(/\s+/).filter((key) => /^\S$/i.test(key)).map((key) => key.toUpperCase()) : []
473
- );
474
- if (char) {
475
- keys.add(char);
476
- saveAttributes([focusable], ["aria-keyshortcuts"]);
477
- addTokenToAttribute(focusable, "aria-keyshortcuts", char, {
478
- caseInsensitive: true
479
- });
480
- }
481
- keys.forEach((key) => {
482
- const focusables = this.#focusablesByFirstChar.get(key) ?? [];
483
- focusables.push(focusable);
484
- this.#focusablesByFirstChar.set(key, focusables);
485
- });
486
- }
487
- if (!navigationOnly) {
488
- if (active && this.#focusables.has(active)) {
489
- this.#focusables.forEach((focusable) => {
490
- focusable.setAttribute("tabindex", focusable === active ? "0" : "-1");
491
- });
492
- } else {
493
- [...this.#focusables].forEach((focusable, i) => {
494
- focusable.setAttribute("tabindex", i || noStart ? "-1" : "0");
495
- });
496
- }
497
- }
498
- }
499
- #createSelectorFilter() {
500
- const { selector } = this.#settings;
501
- return (element) => !selector || [...this.#container.querySelectorAll(selector)].includes(element);
502
- }
503
- #getFocusables() {
504
- return getFocusables(this.#container, {
505
- composed: true,
506
- filter: this.#selectorFilter,
507
- skipNegativeTabIndexCheck: !this.#settings.navigationOnly,
508
- skipVisibilityCheck: true
509
- });
510
- }
511
- };
512
- function focusElement(element) {
513
- "focus" in element && typeof element.focus === "function" && element.focus();
514
- }
515
- function getActiveElement() {
516
- let current = document.activeElement;
517
- while (current?.shadowRoot?.activeElement) {
518
- current = current.shadowRoot.activeElement;
519
- }
520
- return current;
521
- }
3
+ var attributesUtils = require('@y14e/attributes-utils');
4
+ var rovingTabindex = require('@y14e/roving-tabindex');
522
5
 
523
6
  // src/index.ts
524
7
  var Disclosure = class {
@@ -597,7 +80,7 @@ var Disclosure = class {
597
80
  this.#cleanupRovingTabIndex?.();
598
81
  this.#cleanupRovingTabIndex = null;
599
82
  this.#detailsElements.length = 0;
600
- restoreAttributes(this.#summaryElements);
83
+ attributesUtils.restoreAttributes(this.#summaryElements);
601
84
  this.#summaryElements.length = 0;
602
85
  this.#contentElements.length = 0;
603
86
  this.#rootElement.removeAttribute("data-disclosure-initialized");
@@ -613,17 +96,17 @@ var Disclosure = class {
613
96
  this.#toggle(details, true);
614
97
  }
615
98
  #initialize() {
616
- saveAttributes(this.#summaryElements, [
99
+ attributesUtils.saveAttributes(this.#summaryElements, [
617
100
  "aria-disabled",
618
101
  "style",
619
102
  "tabindex"
620
103
  ]);
621
- this.#summaryElements.filter((summary) => !isFocusable2(summary)).forEach((summary) => {
104
+ this.#summaryElements.filter((summary) => !isFocusable(summary)).forEach((summary) => {
622
105
  summary.setAttribute("aria-disabled", "true");
623
106
  summary.setAttribute("tabindex", "-1");
624
107
  summary.style.setProperty("pointer-events", "none");
625
108
  });
626
- this.#cleanupRovingTabIndex = createRovingTabIndex(this.#rootElement, {
109
+ this.#cleanupRovingTabIndex = rovingTabindex.createRovingTabIndex(this.#rootElement, {
627
110
  direction: "vertical",
628
111
  navigationOnly: true,
629
112
  selector: "summary:not(:scope summary + * *)",
@@ -640,7 +123,7 @@ var Disclosure = class {
640
123
  function createBinding(details, summary, content) {
641
124
  return { details, summary, content };
642
125
  }
643
- function isFocusable2(element) {
126
+ function isFocusable(element) {
644
127
  return element.tabIndex >= 0;
645
128
  }
646
129
  /**
@@ -648,52 +131,11 @@ function isFocusable2(element) {
648
131
  * WAI-ARIA compliant disclosure pattern implementation in TypeScript.
649
132
  * Using the <details> and <summary> element.
650
133
  *
651
- * @version 1.3.9
134
+ * @version 1.3.11
652
135
  * @author Yusuke Kamiyamane
653
136
  * @license MIT
654
137
  * @copyright Copyright (c) Yusuke Kamiyamane
655
138
  * @see {@link https://github.com/y14e/disclosure-css}
656
139
  */
657
- /*! Bundled license information:
658
-
659
- @y14e/roving-tabindex/dist/index.js:
660
- (**
661
- * Roving Tabindex
662
- * Lightweight roving tabindex utility with fully focus management.
663
- * Designed for accessible menus, tabs, toolbars, and composite widgets.
664
- *
665
- * @version 3.0.7
666
- * @author Yusuke Kamiyamane
667
- * @license MIT
668
- * @copyright Copyright (c) Yusuke Kamiyamane
669
- * @see {@link https://github.com/y14e/roving-tabindex}
670
- *)
671
- (*! Bundled license information:
672
-
673
- @y14e/attributes-utils/dist/index.js:
674
- (**
675
- * Attributes Utils
676
- *
677
- * @version 1.1.2
678
- * @author Yusuke Kamiyamane
679
- * @license MIT
680
- * @copyright Copyright (c) Yusuke Kamiyamane
681
- * @see {@link https://github.com/y14e/attributes-utils}
682
- *)
683
-
684
- power-focusable/dist/index.js:
685
- (**
686
- * Power Focusable
687
- * High-precision focus management utility with full composed tree support.
688
- * Handles complex focus rules including tabindex ordering, radio groups, inert.
689
- *
690
- * @version 4.3.3
691
- * @author Yusuke Kamiyamane
692
- * @license MIT
693
- * @copyright Copyright (c) Yusuke Kamiyamane
694
- * @see {@link https://github.com/y14e/power-focusable}
695
- *)
696
- *)
697
- */
698
140
 
699
141
  module.exports = Disclosure;
package/dist/index.d.cts CHANGED
@@ -3,7 +3,7 @@
3
3
  * WAI-ARIA compliant disclosure pattern implementation in TypeScript.
4
4
  * Using the <details> and <summary> element.
5
5
  *
6
- * @version 1.3.9
6
+ * @version 1.3.11
7
7
  * @author Yusuke Kamiyamane
8
8
  * @license MIT
9
9
  * @copyright Copyright (c) Yusuke Kamiyamane
package/dist/index.d.ts CHANGED
@@ -3,7 +3,7 @@
3
3
  * WAI-ARIA compliant disclosure pattern implementation in TypeScript.
4
4
  * Using the <details> and <summary> element.
5
5
  *
6
- * @version 1.3.9
6
+ * @version 1.3.11
7
7
  * @author Yusuke Kamiyamane
8
8
  * @license MIT
9
9
  * @copyright Copyright (c) Yusuke Kamiyamane