@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.
@@ -0,0 +1,857 @@
1
+ 'use strict';
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
+ }
289
+
290
+ // node_modules/@y14e/roving-tabindex/dist/index.js
291
+ function createRovingTabIndex(container, options = {}) {
292
+ if (!(container instanceof Element)) {
293
+ console.warn("Invalid container element");
294
+ return () => {
295
+ };
296
+ }
297
+ try {
298
+ const roving = new RovingTabIndex(container, options);
299
+ return () => roving.destroy();
300
+ } catch (error) {
301
+ error instanceof Error && console.warn(error.message || error);
302
+ return () => {
303
+ };
304
+ }
305
+ }
306
+ var RovingTabIndex = class _RovingTabIndex {
307
+ static #initialized = /* @__PURE__ */ new Set();
308
+ #container;
309
+ #settings;
310
+ #focusables = /* @__PURE__ */ new Set();
311
+ #focusablesByFirstChar = /* @__PURE__ */ new Map();
312
+ #selectorFilter;
313
+ #controller = null;
314
+ #isDestroyed = false;
315
+ constructor(container, options = {}) {
316
+ this.#container = container;
317
+ let {
318
+ direction,
319
+ navigationOnly = false,
320
+ noMemory = false,
321
+ noStart = false,
322
+ selector,
323
+ typeahead = false,
324
+ wrap = false
325
+ } = options;
326
+ if (typeof direction !== "undefined" && !["horizontal", "vertical"].includes(direction)) {
327
+ console.warn("Invalid direction option. Fallback: both (undefined).");
328
+ direction = void 0;
329
+ }
330
+ if (typeof navigationOnly !== "boolean") {
331
+ console.warn("Invalid navigationOnly option. Fallback: false.");
332
+ navigationOnly = false;
333
+ }
334
+ if (typeof noMemory !== "boolean") {
335
+ console.warn("Invalid noMemory option. Fallback: false.");
336
+ noMemory = false;
337
+ }
338
+ if (typeof noStart !== "boolean") {
339
+ console.warn("Invalid noStart option. Fallback: false.");
340
+ noStart = false;
341
+ }
342
+ if (typeof selector !== "undefined" && (typeof selector !== "string" || !selector.trim())) {
343
+ console.warn(
344
+ "Invalid selector. Fallback: all focusable elements (undefined)."
345
+ );
346
+ selector = void 0;
347
+ }
348
+ if (typeof typeahead !== "boolean") {
349
+ console.warn("Invalid typeahead option. Fallback: false.");
350
+ typeahead = false;
351
+ }
352
+ if (typeof wrap !== "boolean") {
353
+ console.warn("Invalid wrap option. Fallback: false.");
354
+ wrap = false;
355
+ }
356
+ this.#settings = {
357
+ navigationOnly,
358
+ noMemory,
359
+ noStart,
360
+ typeahead,
361
+ wrap
362
+ };
363
+ direction && Object.assign(this.#settings, { direction });
364
+ selector && Object.assign(this.#settings, { selector });
365
+ this.#selectorFilter = this.#createSelectorFilter();
366
+ this.#initialize();
367
+ }
368
+ destroy() {
369
+ if (this.#isDestroyed) {
370
+ return;
371
+ }
372
+ this.#isDestroyed = true;
373
+ this.#controller?.abort();
374
+ this.#controller = null;
375
+ restoreAttributes([...this.#focusables]);
376
+ this.#focusables.clear();
377
+ this.#focusablesByFirstChar.clear();
378
+ }
379
+ #initialize() {
380
+ this.#update(document.activeElement);
381
+ if (!(this.#container instanceof HTMLElement)) {
382
+ return;
383
+ }
384
+ this.#controller = new AbortController();
385
+ const { signal } = this.#controller;
386
+ document.addEventListener("focusin", this.#onFocusIn, {
387
+ capture: true,
388
+ signal
389
+ });
390
+ this.#container.addEventListener("keydown", this.#onKeyDown, {
391
+ capture: true,
392
+ signal
393
+ });
394
+ }
395
+ #onFocusIn = (event) => {
396
+ const { target } = event;
397
+ if (!(target instanceof Element)) {
398
+ return;
399
+ }
400
+ const isFocusable3 = this.#focusables.has(target);
401
+ this.#settings.noMemory && !isFocusable3 ? this.#update(null) : isFocusable3 && this.#update(target);
402
+ };
403
+ #onKeyDown = (event) => {
404
+ const { key, altKey, ctrlKey, metaKey, shiftKey } = event;
405
+ if (altKey || ctrlKey || metaKey || shiftKey) {
406
+ return;
407
+ }
408
+ const { direction, typeahead, wrap } = this.#settings;
409
+ const isBoth = !direction;
410
+ const isHorizontal = direction === "horizontal";
411
+ if (![
412
+ "End",
413
+ "Home",
414
+ ...isBoth ? ["ArrowLeft", "ArrowUp"] : [`Arrow${isHorizontal ? "Left" : "Up"}`],
415
+ ...isBoth ? ["ArrowRight", "ArrowDown"] : [`Arrow${isHorizontal ? "Right" : "Down"}`]
416
+ ].includes(key)) {
417
+ if (!typeahead || !/^\S$/i.test(key) || !this.#focusablesByFirstChar.has(key.toUpperCase())) {
418
+ return;
419
+ }
420
+ }
421
+ const active = getActiveElement();
422
+ if (!(active instanceof HTMLElement)) {
423
+ return;
424
+ }
425
+ const current = this.#getFocusables();
426
+ if (!current.includes(active)) {
427
+ return;
428
+ }
429
+ event.preventDefault();
430
+ const currentIndex = current.indexOf(active);
431
+ let newIndex;
432
+ let target = current;
433
+ switch (key) {
434
+ case "End":
435
+ newIndex = -1;
436
+ break;
437
+ case "Home":
438
+ newIndex = 0;
439
+ break;
440
+ case "ArrowLeft":
441
+ case "ArrowUp": {
442
+ const rawIndex = currentIndex - 1;
443
+ newIndex = wrap ? rawIndex : Math.max(rawIndex, 0);
444
+ break;
445
+ }
446
+ case "ArrowRight":
447
+ case "ArrowDown": {
448
+ const rawIndex = currentIndex + 1;
449
+ newIndex = wrap ? rawIndex % current.length : Math.min(rawIndex, current.length - 1);
450
+ break;
451
+ }
452
+ default: {
453
+ target = this.#focusablesByFirstChar.get(key.toUpperCase()) ?? [];
454
+ const foundIndex = target.findIndex(
455
+ (focusable2) => current.indexOf(focusable2) > currentIndex
456
+ );
457
+ newIndex = foundIndex >= 0 ? foundIndex : 0;
458
+ }
459
+ }
460
+ const focusable = target.at(newIndex);
461
+ focusable && focusElement(focusable);
462
+ };
463
+ #update(active) {
464
+ const current = new Set(this.#getFocusables());
465
+ for (const focusable of this.#focusables) {
466
+ if (!current.has(focusable)) {
467
+ focusable.isConnected && restoreAttributes([focusable]);
468
+ this.#focusables.delete(focusable);
469
+ this.#focusablesByFirstChar.forEach((focusables) => {
470
+ const index = focusables.indexOf(focusable);
471
+ index >= 0 && focusables.splice(index, 1);
472
+ });
473
+ }
474
+ }
475
+ const { navigationOnly, noStart, typeahead } = this.#settings;
476
+ for (const focusable of current) {
477
+ if (this.#focusables.has(focusable)) {
478
+ continue;
479
+ }
480
+ if (_RovingTabIndex.#initialized.has(focusable)) {
481
+ throw new TypeError("Already initialized");
482
+ }
483
+ this.#focusables.add(focusable);
484
+ _RovingTabIndex.#initialized.add(focusable);
485
+ if (!navigationOnly) {
486
+ saveAttributes([focusable], ["tabindex"]);
487
+ focusable.setAttribute("tabindex", "-1");
488
+ }
489
+ if (!typeahead) {
490
+ continue;
491
+ }
492
+ const char = focusable.textContent?.trim()?.at(0)?.toUpperCase();
493
+ const value = focusable.ariaKeyShortcuts?.trim();
494
+ const keys = new Set(
495
+ value ? value.split(/\s+/).filter((key) => /^\S$/i.test(key)).map((key) => key.toUpperCase()) : []
496
+ );
497
+ if (char) {
498
+ keys.add(char);
499
+ saveAttributes([focusable], ["aria-keyshortcuts"]);
500
+ addTokenToAttribute(focusable, "aria-keyshortcuts", char, {
501
+ caseInsensitive: true
502
+ });
503
+ }
504
+ keys.forEach((key) => {
505
+ const focusables = this.#focusablesByFirstChar.get(key) ?? [];
506
+ focusables.push(focusable);
507
+ this.#focusablesByFirstChar.set(key, focusables);
508
+ });
509
+ }
510
+ if (!navigationOnly) {
511
+ if (active && this.#focusables.has(active)) {
512
+ this.#focusables.forEach((focusable) => {
513
+ focusable.setAttribute("tabindex", focusable === active ? "0" : "-1");
514
+ });
515
+ } else {
516
+ [...this.#focusables].forEach((focusable, i) => {
517
+ focusable.setAttribute("tabindex", i || noStart ? "-1" : "0");
518
+ });
519
+ }
520
+ }
521
+ }
522
+ #createSelectorFilter() {
523
+ const { selector } = this.#settings;
524
+ return (element) => !selector || [...this.#container.querySelectorAll(selector)].includes(element);
525
+ }
526
+ #getFocusables() {
527
+ return getFocusables(this.#container, {
528
+ composed: true,
529
+ filter: this.#selectorFilter,
530
+ skipNegativeTabIndexCheck: !this.#settings.navigationOnly,
531
+ skipVisibilityCheck: true
532
+ });
533
+ }
534
+ };
535
+
536
+ // src/index.ts
537
+ var Disclosure = class _Disclosure {
538
+ static defaults = {};
539
+ #rootElement;
540
+ #defaults = {
541
+ animation: {
542
+ duration: 300,
543
+ easing: "ease"
544
+ }
545
+ };
546
+ #settings;
547
+ #detailsElements;
548
+ #summaryElements;
549
+ #contentElements;
550
+ #bindings = /* @__PURE__ */ new WeakMap();
551
+ #eventController = null;
552
+ #animationController = null;
553
+ #observers = [];
554
+ #cleanupRovingTabIndex = null;
555
+ #isDestroyed = false;
556
+ constructor(root, options = {}) {
557
+ if (!(root instanceof HTMLElement)) {
558
+ throw new TypeError("Invalid root element");
559
+ }
560
+ if (root.hasAttribute("data-disclosure-initialized")) {
561
+ console.warn("Already initialized");
562
+ return;
563
+ }
564
+ this.#rootElement = root;
565
+ this.#defaults = this.#mergeOptions(this.#defaults, _Disclosure.defaults);
566
+ this.#settings = this.#mergeOptions(this.#defaults, options);
567
+ matchMedia("(prefers-reduced-motion: reduce)").matches && Object.assign(this.#settings.animation, { duration: 0 });
568
+ const NOT_NESTED = ":not(:scope summary + * *)";
569
+ this.#detailsElements = [
570
+ ...this.#rootElement.querySelectorAll(
571
+ `details${NOT_NESTED}`
572
+ )
573
+ ];
574
+ if (!this.#detailsElements.length) {
575
+ console.warn("Missing <details> elements");
576
+ return;
577
+ }
578
+ this.#summaryElements = [
579
+ ...this.#rootElement.querySelectorAll(
580
+ `summary${NOT_NESTED}`
581
+ )
582
+ ];
583
+ if (!this.#summaryElements.length) {
584
+ console.warn("Missing <summary> elements");
585
+ return;
586
+ }
587
+ this.#contentElements = [
588
+ ...this.#rootElement.querySelectorAll(
589
+ `summary${NOT_NESTED} + *`
590
+ )
591
+ ];
592
+ if (!this.#contentElements.length) {
593
+ console.warn("Missing content elements");
594
+ return;
595
+ }
596
+ this.#detailsElements.forEach((details, i) => {
597
+ const summary = this.#summaryElements[i];
598
+ const content = this.#contentElements[i];
599
+ if (summary && content) {
600
+ const binding = createBinding(details, summary, content);
601
+ this.#bindings.set(details, binding);
602
+ this.#bindings.set(summary, binding);
603
+ this.#bindings.set(content, binding);
604
+ }
605
+ });
606
+ this.#initialize();
607
+ }
608
+ close(details) {
609
+ if (this.#isDestroyed) {
610
+ return;
611
+ }
612
+ if (!(details instanceof HTMLDetailsElement) || !this.#bindings.has(details)) {
613
+ console.warn("Invalid <details> element");
614
+ return;
615
+ }
616
+ this.#toggle(details, false);
617
+ }
618
+ async destroy(force = false) {
619
+ if (this.#isDestroyed) {
620
+ return;
621
+ }
622
+ this.#isDestroyed = true;
623
+ this.#eventController?.abort();
624
+ this.#eventController = null;
625
+ this.#observers.forEach((observer) => {
626
+ observer.disconnect();
627
+ });
628
+ this.#observers.length = 0;
629
+ this.#cleanupRovingTabIndex?.();
630
+ this.#cleanupRovingTabIndex = null;
631
+ !force && await this.#waitAnimationsFinish();
632
+ this.#contentElements.forEach((content) => {
633
+ force && this.#bindings.get(content)?.animation?.finish();
634
+ this.#onAnimationFinish(content);
635
+ });
636
+ this.#animationController?.abort();
637
+ this.#animationController = null;
638
+ this.#detailsElements.forEach((details) => {
639
+ ["name", "open"].forEach((name) => {
640
+ details.removeAttribute(`data-disclosure-${name}`);
641
+ });
642
+ });
643
+ this.#detailsElements.length = 0;
644
+ restoreAttributes(this.#summaryElements);
645
+ this.#summaryElements.length = 0;
646
+ this.#contentElements.length = 0;
647
+ this.#rootElement.removeAttribute("data-disclosure-initialized");
648
+ }
649
+ open(details) {
650
+ if (this.#isDestroyed) {
651
+ return;
652
+ }
653
+ if (!(details instanceof HTMLDetailsElement) || !this.#bindings.has(details)) {
654
+ console.warn("Invalid <details> element");
655
+ return;
656
+ }
657
+ this.#toggle(details, true);
658
+ }
659
+ #initialize() {
660
+ saveAttributes(this.#summaryElements, [
661
+ "aria-disabled",
662
+ "style",
663
+ "tabindex"
664
+ ]);
665
+ this.#eventController = new AbortController();
666
+ const { signal } = this.#eventController;
667
+ this.#detailsElements.forEach((details, i) => {
668
+ details.name && details.setAttribute("data-disclosure-name", details.name);
669
+ function onMutate() {
670
+ details.toggleAttribute("data-disclosure-open", details.open);
671
+ }
672
+ const observer = new MutationObserver(onMutate);
673
+ observer.observe(details, { attributeFilter: ["open"] });
674
+ this.#observers.push(observer);
675
+ onMutate();
676
+ const summary = this.#summaryElements[i];
677
+ if (!summary) {
678
+ return;
679
+ }
680
+ if (!isFocusable2(summary)) {
681
+ summary.setAttribute("aria-disabled", "true");
682
+ summary.setAttribute("tabindex", "-1");
683
+ summary.style.setProperty("pointer-events", "none");
684
+ }
685
+ summary.addEventListener("click", this.#onSummaryClick, { signal });
686
+ });
687
+ this.#cleanupRovingTabIndex = createRovingTabIndex(this.#rootElement, {
688
+ direction: "vertical",
689
+ navigationOnly: true,
690
+ selector: "summary:not(:scope summary + * *)",
691
+ wrap: true
692
+ });
693
+ this.#rootElement.setAttribute("data-disclosure-initialized", "");
694
+ }
695
+ #onSummaryClick = (event) => {
696
+ event.preventDefault();
697
+ const summary = event.currentTarget;
698
+ if (!(summary instanceof HTMLElement)) {
699
+ return;
700
+ }
701
+ const binding = this.#bindings.get(summary);
702
+ if (!binding) {
703
+ return;
704
+ }
705
+ const { details } = binding;
706
+ this.#toggle(details, !details.hasAttribute("data-disclosure-open"));
707
+ };
708
+ #toggle(details, isOpen) {
709
+ if (details.hasAttribute("data-disclosure-open") === isOpen) {
710
+ return;
711
+ }
712
+ const name = details.getAttribute("data-disclosure-name");
713
+ if (name && isOpen) {
714
+ details.removeAttribute("name");
715
+ const opened = this.#detailsElements.find(
716
+ (d) => d.hasAttribute("data-disclosure-open") && d.getAttribute("data-disclosure-name") === name
717
+ );
718
+ opened && this.close(opened);
719
+ }
720
+ const binding = this.#bindings.get(details);
721
+ if (!binding) {
722
+ return;
723
+ }
724
+ const { content } = binding;
725
+ const startSize = details.open ? content.offsetHeight : 0;
726
+ binding.animation?.cancel();
727
+ if (isOpen) {
728
+ details.open = true;
729
+ }
730
+ const endSize = isOpen ? content.scrollHeight : 0;
731
+ binding.animation?.cancel();
732
+ details.toggleAttribute("data-disclosure-open", isOpen);
733
+ content.style.setProperty("overflow", "clip");
734
+ const { duration, easing } = this.#settings.animation;
735
+ const animation = content.animate(
736
+ { blockSize: [`${startSize}px`, `${endSize}px`] },
737
+ { duration, easing }
738
+ );
739
+ binding.animation = animation;
740
+ function cleanup() {
741
+ if (binding?.animation === animation) {
742
+ binding.animation = null;
743
+ }
744
+ }
745
+ this.#animationController = new AbortController();
746
+ const { signal } = this.#animationController;
747
+ animation.addEventListener("cancel", cleanup, { once: true, signal });
748
+ animation.addEventListener(
749
+ "finish",
750
+ () => {
751
+ if (binding?.animation === animation) {
752
+ this.#onAnimationFinish(content);
753
+ cleanup();
754
+ }
755
+ },
756
+ { once: true, signal }
757
+ );
758
+ }
759
+ #mergeOptions(target, source) {
760
+ return {
761
+ animation: { ...target.animation, ...source.animation ?? {} }
762
+ };
763
+ }
764
+ #onAnimationFinish(content) {
765
+ const binding = this.#bindings.get(content);
766
+ if (!binding) {
767
+ return;
768
+ }
769
+ const details = binding.details;
770
+ if (!details) {
771
+ return;
772
+ }
773
+ const name = details.getAttribute("data-disclosure-name");
774
+ name && details.setAttribute("name", name);
775
+ if (!details.hasAttribute("data-disclosure-open")) {
776
+ details.open = false;
777
+ }
778
+ ["block-size", "overflow"].forEach((name2) => {
779
+ content.style.removeProperty(name2);
780
+ });
781
+ }
782
+ async #waitAnimationsFinish() {
783
+ const promises = [];
784
+ this.#contentElements.forEach((content) => {
785
+ const animation = this.#bindings.get(content)?.animation;
786
+ animation && promises.push(waitAnimationFinish(animation));
787
+ });
788
+ await Promise.allSettled(promises);
789
+ }
790
+ };
791
+ function createBinding(details, summary, content) {
792
+ return { details, summary, content, animation: null };
793
+ }
794
+ function isFocusable2(element) {
795
+ return element.tabIndex >= 0;
796
+ }
797
+ function waitAnimationFinish(animation) {
798
+ if (["idle", "finished"].includes(animation.playState)) {
799
+ return Promise.resolve();
800
+ } else {
801
+ return new Promise(
802
+ (resolve) => animation.addEventListener("finish", () => resolve(), { once: true })
803
+ );
804
+ }
805
+ }
806
+ /**
807
+ * Disclosure
808
+ * WAI-ARIA compliant disclosure pattern implementation in TypeScript.
809
+ * Using the <details> and <summary> element.
810
+ *
811
+ * @version 1.3.18
812
+ * @author Yusuke Kamiyamane
813
+ * @license MIT
814
+ * @copyright Copyright (c) Yusuke Kamiyamane
815
+ * @see {@link https://github.com/y14e/disclosure}
816
+ */
817
+ /*! Bundled license information:
818
+
819
+ @y14e/attributes-utils/dist/index.js:
820
+ (**
821
+ * Attributes Utils
822
+ *
823
+ * @version 1.1.2
824
+ * @author Yusuke Kamiyamane
825
+ * @license MIT
826
+ * @copyright Copyright (c) Yusuke Kamiyamane
827
+ * @see {@link https://github.com/y14e/attributes-utils}
828
+ *)
829
+
830
+ power-focusable/dist/index.js:
831
+ (**
832
+ * Power Focusable
833
+ * High-precision focus management utility with full composed tree support.
834
+ * Handles complex focus rules including tabindex ordering, radio groups, inert.
835
+ *
836
+ * @version 4.3.3
837
+ * @author Yusuke Kamiyamane
838
+ * @license MIT
839
+ * @copyright Copyright (c) Yusuke Kamiyamane
840
+ * @see {@link https://github.com/y14e/power-focusable}
841
+ *)
842
+
843
+ @y14e/roving-tabindex/dist/index.js:
844
+ (**
845
+ * Roving Tabindex
846
+ * Lightweight roving tabindex utility with fully focus management.
847
+ * Designed for accessible menus, tabs, toolbars, and composite widgets.
848
+ *
849
+ * @version 3.0.14
850
+ * @author Yusuke Kamiyamane
851
+ * @license MIT
852
+ * @copyright Copyright (c) Yusuke Kamiyamane
853
+ * @see {@link https://github.com/y14e/roving-tabindex}
854
+ *)
855
+ */
856
+
857
+ module.exports = Disclosure;