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