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