@y14e/tabs 2.0.2 → 2.0.3

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,1155 @@
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/button/dist/index.js
291
+ var Button = class {
292
+ #element;
293
+ #controller = null;
294
+ #isDestroyed = false;
295
+ constructor(element) {
296
+ if (!(element instanceof HTMLElement)) {
297
+ throw new TypeError("Invalid element");
298
+ }
299
+ if (element.hasAttribute("data-button-initialized")) {
300
+ console.warn("Already initialized");
301
+ return;
302
+ }
303
+ this.#element = element;
304
+ this.#initialize();
305
+ }
306
+ destroy() {
307
+ if (this.#isDestroyed) {
308
+ return;
309
+ }
310
+ this.#isDestroyed = true;
311
+ this.#controller?.abort();
312
+ this.#controller = null;
313
+ this.#element.removeAttribute("data-button-initialized");
314
+ }
315
+ #initialize() {
316
+ this.#controller = new AbortController();
317
+ this.#element.addEventListener("keydown", this.#onKeyDown, {
318
+ signal: this.#controller.signal
319
+ });
320
+ this.#element.setAttribute("data-button-initialized", "");
321
+ }
322
+ #onKeyDown = (event) => {
323
+ const { key, altKey, ctrlKey, metaKey, shiftKey } = event;
324
+ if (altKey || ctrlKey || metaKey || shiftKey) {
325
+ return;
326
+ }
327
+ if (!["Enter", " "].includes(key)) {
328
+ return;
329
+ }
330
+ const active = getActiveElement();
331
+ if (!(active instanceof HTMLElement)) {
332
+ return;
333
+ }
334
+ event.preventDefault();
335
+ active.click();
336
+ };
337
+ };
338
+
339
+ // node_modules/@y14e/roving-tabindex/dist/index.js
340
+ function createRovingTabIndex(container, options = {}) {
341
+ if (!(container instanceof Element)) {
342
+ console.warn("Invalid container element");
343
+ return () => {
344
+ };
345
+ }
346
+ const roving = new RovingTabIndex(container, options);
347
+ return () => roving.destroy();
348
+ }
349
+ var RovingTabIndex = class _RovingTabIndex {
350
+ static #initialized = /* @__PURE__ */ new Set();
351
+ #container;
352
+ #settings;
353
+ #focusables = /* @__PURE__ */ new Set();
354
+ #focusablesByFirstChar = /* @__PURE__ */ new Map();
355
+ #selectorFilter;
356
+ #controller = null;
357
+ #isDestroyed = false;
358
+ constructor(container, options = {}) {
359
+ this.#container = container;
360
+ let {
361
+ direction,
362
+ navigationOnly = false,
363
+ noMemory = false,
364
+ noStart = false,
365
+ selector,
366
+ typeahead = false,
367
+ wrap = false
368
+ } = options;
369
+ if (typeof direction !== "undefined" && !["horizontal", "vertical"].includes(direction)) {
370
+ console.warn("Invalid direction option. Fallback: both (undefined).");
371
+ direction = void 0;
372
+ }
373
+ if (typeof navigationOnly !== "boolean") {
374
+ console.warn("Invalid navigationOnly option. Fallback: false.");
375
+ navigationOnly = false;
376
+ }
377
+ if (typeof noMemory !== "boolean") {
378
+ console.warn("Invalid noMemory option. Fallback: false.");
379
+ noMemory = false;
380
+ }
381
+ if (typeof noStart !== "boolean") {
382
+ console.warn("Invalid noStart option. Fallback: false.");
383
+ noStart = false;
384
+ }
385
+ if (typeof selector !== "undefined" && (typeof selector !== "string" || !selector.trim())) {
386
+ console.warn(
387
+ "Invalid selector. Fallback: all focusable elements (undefined)."
388
+ );
389
+ selector = void 0;
390
+ }
391
+ if (typeof typeahead !== "boolean") {
392
+ console.warn("Invalid typeahead option. Fallback: false.");
393
+ typeahead = false;
394
+ }
395
+ if (typeof wrap !== "boolean") {
396
+ console.warn("Invalid wrap option. Fallback: false.");
397
+ wrap = false;
398
+ }
399
+ this.#settings = {
400
+ navigationOnly,
401
+ noMemory,
402
+ noStart,
403
+ typeahead,
404
+ wrap
405
+ };
406
+ direction && Object.assign(this.#settings, { direction });
407
+ selector && Object.assign(this.#settings, { selector });
408
+ this.#selectorFilter = this.#createSelectorFilter();
409
+ this.#initialize();
410
+ }
411
+ destroy() {
412
+ if (this.#isDestroyed) {
413
+ return;
414
+ }
415
+ this.#isDestroyed = true;
416
+ this.#controller?.abort();
417
+ this.#controller = null;
418
+ restoreAttributes([...this.#focusables]);
419
+ this.#focusables.clear();
420
+ this.#focusablesByFirstChar.clear();
421
+ }
422
+ #initialize() {
423
+ this.#update(document.activeElement);
424
+ if (!(this.#container instanceof HTMLElement)) {
425
+ return;
426
+ }
427
+ this.#controller = new AbortController();
428
+ const { signal } = this.#controller;
429
+ this.#container.addEventListener("focusin", this.#onFocusIn, {
430
+ capture: true,
431
+ signal
432
+ });
433
+ this.#container.addEventListener("keydown", this.#onKeyDown, {
434
+ capture: true,
435
+ signal
436
+ });
437
+ }
438
+ #onFocusIn = (event) => {
439
+ const { target } = event;
440
+ if (!(target instanceof Element)) {
441
+ return;
442
+ }
443
+ const isFocusable3 = this.#focusables.has(target);
444
+ this.#settings.noMemory && !isFocusable3 ? this.#update(null) : isFocusable3 && this.#update(target);
445
+ };
446
+ #onKeyDown = (event) => {
447
+ const { key, altKey, ctrlKey, metaKey, shiftKey } = event;
448
+ if (altKey || ctrlKey || metaKey || shiftKey) {
449
+ return;
450
+ }
451
+ const { direction, typeahead, wrap } = this.#settings;
452
+ const isBoth = !direction;
453
+ const isHorizontal = direction === "horizontal";
454
+ if (![
455
+ "End",
456
+ "Home",
457
+ ...isBoth ? ["ArrowLeft", "ArrowUp"] : [`Arrow${isHorizontal ? "Left" : "Up"}`],
458
+ ...isBoth ? ["ArrowRight", "ArrowDown"] : [`Arrow${isHorizontal ? "Right" : "Down"}`]
459
+ ].includes(key)) {
460
+ if (!typeahead || !/^\S$/i.test(key) || !this.#focusablesByFirstChar.has(key.toUpperCase())) {
461
+ return;
462
+ }
463
+ }
464
+ const active = getActiveElement();
465
+ if (!(active instanceof HTMLElement)) {
466
+ return;
467
+ }
468
+ const current = this.#getFocusables();
469
+ if (!current.includes(active)) {
470
+ return;
471
+ }
472
+ event.preventDefault();
473
+ const currentIndex = current.indexOf(active);
474
+ let newIndex;
475
+ let target = current;
476
+ switch (key) {
477
+ case "End":
478
+ newIndex = -1;
479
+ break;
480
+ case "Home":
481
+ newIndex = 0;
482
+ break;
483
+ case "ArrowLeft":
484
+ case "ArrowUp": {
485
+ const rawIndex = currentIndex - 1;
486
+ newIndex = wrap ? rawIndex : Math.max(rawIndex, 0);
487
+ break;
488
+ }
489
+ case "ArrowRight":
490
+ case "ArrowDown": {
491
+ const rawIndex = currentIndex + 1;
492
+ newIndex = wrap ? rawIndex % current.length : Math.min(rawIndex, current.length - 1);
493
+ break;
494
+ }
495
+ default: {
496
+ target = this.#focusablesByFirstChar.get(key.toUpperCase()) ?? [];
497
+ const foundIndex = target.findIndex(
498
+ (focusable2) => current.indexOf(focusable2) > currentIndex
499
+ );
500
+ newIndex = foundIndex >= 0 ? foundIndex : 0;
501
+ }
502
+ }
503
+ const focusable = target.at(newIndex);
504
+ focusable && focusElement(focusable);
505
+ };
506
+ #update(active) {
507
+ const current = new Set(this.#getFocusables());
508
+ for (const focusable of this.#focusables) {
509
+ if (!current.has(focusable)) {
510
+ focusable.isConnected && restoreAttributes([focusable]);
511
+ this.#focusables.delete(focusable);
512
+ this.#focusablesByFirstChar.forEach((focusables) => {
513
+ const index = focusables.indexOf(focusable);
514
+ index >= 0 && focusables.splice(index, 1);
515
+ });
516
+ }
517
+ }
518
+ const { navigationOnly, noStart, typeahead } = this.#settings;
519
+ for (const focusable of current) {
520
+ if (this.#focusables.has(focusable)) {
521
+ continue;
522
+ }
523
+ if (_RovingTabIndex.#initialized.has(focusable)) {
524
+ throw new TypeError("Already initialized");
525
+ }
526
+ this.#focusables.add(focusable);
527
+ _RovingTabIndex.#initialized.add(focusable);
528
+ if (!navigationOnly) {
529
+ saveAttributes([focusable], ["tabindex"]);
530
+ focusable.setAttribute("tabindex", "-1");
531
+ }
532
+ if (!typeahead) {
533
+ continue;
534
+ }
535
+ const char = focusable.textContent?.trim()?.at(0)?.toUpperCase();
536
+ const value = focusable.ariaKeyShortcuts?.trim();
537
+ const keys = new Set(
538
+ value ? value.split(/\s+/).filter((key) => /^\S$/i.test(key)).map((key) => key.toUpperCase()) : []
539
+ );
540
+ if (char) {
541
+ keys.add(char);
542
+ saveAttributes([focusable], ["aria-keyshortcuts"]);
543
+ addTokenToAttribute(focusable, "aria-keyshortcuts", char, {
544
+ caseInsensitive: true
545
+ });
546
+ }
547
+ keys.forEach((key) => {
548
+ const focusables = this.#focusablesByFirstChar.get(key) ?? [];
549
+ focusables.push(focusable);
550
+ this.#focusablesByFirstChar.set(key, focusables);
551
+ });
552
+ }
553
+ if (!navigationOnly) {
554
+ if (active && this.#focusables.has(active)) {
555
+ this.#focusables.forEach((focusable) => {
556
+ focusable.setAttribute("tabindex", focusable === active ? "0" : "-1");
557
+ });
558
+ } else {
559
+ [...this.#focusables].forEach((focusable, i) => {
560
+ focusable.setAttribute("tabindex", i || noStart ? "-1" : "0");
561
+ });
562
+ }
563
+ }
564
+ }
565
+ #createSelectorFilter() {
566
+ const { selector } = this.#settings;
567
+ return (element) => !selector || [...this.#container.querySelectorAll(selector)].includes(element);
568
+ }
569
+ #getFocusables() {
570
+ return getFocusables(this.#container, {
571
+ composed: true,
572
+ filter: this.#selectorFilter,
573
+ skipNegativeTabIndexCheck: !this.#settings.navigationOnly,
574
+ skipVisibilityCheck: true
575
+ });
576
+ }
577
+ };
578
+
579
+ // src/index.ts
580
+ var Tabs = class _Tabs {
581
+ static defaults = {};
582
+ #rootElement;
583
+ #defaults = {
584
+ animation: {
585
+ content: {
586
+ crossFade: true,
587
+ duration: 300,
588
+ easing: "ease",
589
+ fade: true
590
+ },
591
+ indicator: {
592
+ duration: 300,
593
+ easing: "ease"
594
+ }
595
+ },
596
+ avoidDuplicates: false,
597
+ manual: false,
598
+ selector: {
599
+ content: '[role="tablist"] + *',
600
+ indicator: "[data-tabs-indicator]",
601
+ list: '[role="tablist"]',
602
+ panel: '[role="tabpanel"]',
603
+ tab: '[role="tab"]'
604
+ },
605
+ vertical: false
606
+ };
607
+ #settings;
608
+ #listElements;
609
+ #tabElements;
610
+ #indicatorElements;
611
+ #contentElement;
612
+ #panelElements;
613
+ #bindings = /* @__PURE__ */ new WeakMap();
614
+ #eventController = null;
615
+ #animationController = null;
616
+ #cleanupsRovingTabIndex = [];
617
+ #animation = null;
618
+ #buttons = [];
619
+ #indicators = [];
620
+ #isDestroyed = false;
621
+ constructor(root, options = {}) {
622
+ if (!(root instanceof HTMLElement)) {
623
+ throw new TypeError("Invalid root element");
624
+ }
625
+ if (root.hasAttribute("data-tabs-initialized")) {
626
+ console.warn("Already initialized");
627
+ return;
628
+ }
629
+ this.#rootElement = root;
630
+ this.#defaults = this.#mergeOptions(this.#defaults, _Tabs.defaults);
631
+ this.#settings = this.#mergeOptions(this.#defaults, options);
632
+ matchMedia("(prefers-reduced-motion: reduce)").matches && Object.assign(this.#settings.animation, {
633
+ content: { duration: 0 },
634
+ indicator: { duration: 0 }
635
+ });
636
+ const NOT_NESTED = `:not(:scope ${this.#settings.selector.panel} *)`;
637
+ this.#listElements = [
638
+ ...this.#rootElement.querySelectorAll(
639
+ `${this.#settings.selector.list}${NOT_NESTED}`
640
+ )
641
+ ];
642
+ if (!this.#listElements.length) {
643
+ console.warn("Missing list elements");
644
+ return;
645
+ }
646
+ this.#tabElements = [
647
+ ...this.#rootElement.querySelectorAll(
648
+ `${this.#settings.selector.tab}${NOT_NESTED}`
649
+ )
650
+ ];
651
+ if (!this.#tabElements.length) {
652
+ console.warn("Missing tab elements");
653
+ return;
654
+ }
655
+ this.#indicatorElements = [
656
+ ...this.#rootElement.querySelectorAll(
657
+ `${this.#settings.selector.indicator}${NOT_NESTED}`
658
+ )
659
+ ];
660
+ this.#contentElement = this.#rootElement.querySelector(
661
+ this.#settings.selector.content
662
+ );
663
+ if (!this.#contentElement) {
664
+ console.warn("Missing content element");
665
+ return;
666
+ }
667
+ this.#panelElements = [
668
+ ...this.#rootElement.querySelectorAll(
669
+ `${this.#settings.selector.panel}${NOT_NESTED}`
670
+ )
671
+ ];
672
+ const length = this.#panelElements.length;
673
+ if (!length) {
674
+ console.warn("Missing panel elements");
675
+ return;
676
+ }
677
+ const tabs = [];
678
+ this.#tabElements.forEach((tab, i) => {
679
+ const index = i % length;
680
+ const tabsByIndex = tabs[index] ?? [];
681
+ tabsByIndex.push(tab);
682
+ tabs[index] = tabsByIndex;
683
+ const panel = this.#panelElements[index];
684
+ if (!panel) {
685
+ return;
686
+ }
687
+ const binding = createBinding(tabsByIndex, panel);
688
+ this.#bindings.set(tab, binding);
689
+ i < length && this.#bindings.set(panel, binding);
690
+ });
691
+ this.#initialize();
692
+ }
693
+ activate(tab, isMatch = false) {
694
+ if (this.#isDestroyed) {
695
+ return;
696
+ }
697
+ if (!(tab instanceof HTMLElement) || !this.#bindings.has(tab)) {
698
+ console.warn("Invalid tab element");
699
+ return;
700
+ }
701
+ if (tab.ariaSelected === "true") {
702
+ return;
703
+ }
704
+ this.#tabElements.forEach((t) => {
705
+ const isSelected = this.#bindings.get(t)?.tabs.some((tt) => tt === tab);
706
+ t.setAttribute("aria-selected", String(isSelected));
707
+ t.setAttribute(
708
+ "tabindex",
709
+ isSelected && !this.#isAvoidedTab(t) ? "0" : "-1"
710
+ );
711
+ });
712
+ if (!this.#contentElement) {
713
+ return;
714
+ }
715
+ const size = this.#contentElement.offsetHeight;
716
+ this.#rootElement.setAttribute("data-tabs-animating", "");
717
+ const { style } = this.#contentElement;
718
+ style.setProperty("overflow", "clip");
719
+ style.setProperty("position", "relative");
720
+ const { crossFade, fade } = this.#settings.animation.content;
721
+ const panel = this.#bindings.get(tab)?.panel;
722
+ if (!panel) {
723
+ return;
724
+ }
725
+ this.#panelElements.forEach((p) => {
726
+ const { style: style2 } = p;
727
+ if (fade) {
728
+ style2.setProperty("content-visibility", "visible");
729
+ style2.setProperty("display", "block");
730
+ style2.setProperty("opacity", p.hidden ? "0" : "1");
731
+ }
732
+ style2.setProperty("inline-size", "100%");
733
+ style2.setProperty("position", "absolute");
734
+ p === panel && !hasFocusable(p) ? p.setAttribute("tabindex", "0") : p.removeAttribute("tabindex");
735
+ });
736
+ this.#panelElements.forEach((p, i) => {
737
+ if (p === panel) {
738
+ p.removeAttribute("hidden");
739
+ } else {
740
+ const tab2 = this.#tabElements[i];
741
+ tab2 && p.setAttribute("hidden", isFocusable2(tab2) ? "until-found" : "");
742
+ }
743
+ });
744
+ this.#animation?.cancel();
745
+ const { duration, easing } = this.#settings.animation.content;
746
+ const animation = this.#contentElement.animate(
747
+ {
748
+ blockSize: [
749
+ `${size}px`,
750
+ getComputedStyle(panel).getPropertyValue("block-size")
751
+ ]
752
+ },
753
+ {
754
+ duration: isMatch ? 0 : duration,
755
+ easing
756
+ }
757
+ );
758
+ this.#animation = animation;
759
+ const cleanup = () => {
760
+ if (animation === this.#animation) {
761
+ this.#animation = null;
762
+ }
763
+ };
764
+ this.#animationController = new AbortController();
765
+ const { signal } = this.#animationController;
766
+ this.#animation.addEventListener("cancel", cleanup, {
767
+ once: true,
768
+ signal
769
+ });
770
+ this.#animation.addEventListener(
771
+ "finish",
772
+ () => {
773
+ if (this.#animation === animation) {
774
+ this.#onAnimationFinish();
775
+ cleanup();
776
+ }
777
+ },
778
+ {
779
+ once: true,
780
+ signal
781
+ }
782
+ );
783
+ this.#panelElements.forEach((p) => {
784
+ const binding = this.#bindings.get(p);
785
+ if (!binding) {
786
+ return;
787
+ }
788
+ const opacity = getComputedStyle(p).getPropertyValue("opacity");
789
+ binding.animation?.cancel();
790
+ const isSelected = p === panel;
791
+ const animation2 = p.animate(
792
+ {
793
+ opacity: crossFade || !fade ? isSelected ? [opacity, "1"] : [opacity, "0"] : isSelected ? [opacity, opacity, "1"] : [opacity, "0", "0"]
794
+ },
795
+ {
796
+ duration: isMatch || !fade ? 0 : this.#settings.animation.content.duration,
797
+ easing: "ease"
798
+ }
799
+ );
800
+ binding.animation = animation2;
801
+ const cleanup2 = () => {
802
+ if (binding.animation === animation2) {
803
+ binding.animation = null;
804
+ }
805
+ };
806
+ this.#animationController = new AbortController();
807
+ const { signal: signal2 } = this.#animationController;
808
+ animation2.addEventListener("cancel", cleanup2, { once: true, signal: signal2 });
809
+ animation2.addEventListener("finish", cleanup2, { once: true, signal: signal2 });
810
+ });
811
+ }
812
+ async destroy(force = false) {
813
+ if (this.#isDestroyed) {
814
+ return;
815
+ }
816
+ this.#isDestroyed = true;
817
+ this.#eventController?.abort();
818
+ this.#eventController = null;
819
+ this.#cleanupsRovingTabIndex.forEach((cleanup) => {
820
+ cleanup();
821
+ });
822
+ this.#cleanupsRovingTabIndex.length = 0;
823
+ this.#buttons.forEach((button) => {
824
+ button.destroy();
825
+ });
826
+ this.#buttons.length = 0;
827
+ this.#indicators.forEach((indicator) => {
828
+ indicator.destroy(force);
829
+ });
830
+ this.#indicators.length = 0;
831
+ if (this.#animation) {
832
+ if (!force) {
833
+ try {
834
+ await this.#animation.finished;
835
+ } catch {
836
+ }
837
+ }
838
+ this.#animation.cancel();
839
+ }
840
+ if (!force) {
841
+ await Promise.all(
842
+ this.#panelElements.map(
843
+ (panel) => this.#bindings.get(panel)?.animation?.finished.catch(() => {
844
+ })
845
+ )
846
+ );
847
+ }
848
+ this.#panelElements.forEach((panel) => {
849
+ this.#bindings.get(panel)?.animation?.cancel();
850
+ });
851
+ this.#onAnimationFinish();
852
+ this.#animationController?.abort();
853
+ this.#animationController = null;
854
+ restoreAttributes([
855
+ ...this.#listElements,
856
+ ...this.#tabElements,
857
+ ...this.#indicatorElements,
858
+ ...this.#panelElements
859
+ ]);
860
+ this.#listElements.length = 0;
861
+ this.#tabElements.length = 0;
862
+ this.#contentElement = null;
863
+ this.#panelElements.length = 0;
864
+ this.#rootElement.removeAttribute("data-tabs-initialized");
865
+ }
866
+ #initialize() {
867
+ saveAttributes(this.#listElements, [
868
+ "aria-hidden",
869
+ "aria-orientation",
870
+ "role",
871
+ "style"
872
+ ]);
873
+ saveAttributes(this.#tabElements, [
874
+ "aria-controls",
875
+ "id",
876
+ "role",
877
+ "style",
878
+ "tabindex"
879
+ ]);
880
+ saveAttributes(this.#indicatorElements, ["style"]);
881
+ saveAttributes(this.#panelElements, [
882
+ "aria-controls",
883
+ "aria-labelledby",
884
+ "id",
885
+ "role",
886
+ "tabindex"
887
+ ]);
888
+ this.#eventController = new AbortController();
889
+ const { signal } = this.#eventController;
890
+ this.#listElements.forEach((list, i) => {
891
+ this.#settings.avoidDuplicates && i && list.setAttribute("aria-hidden", "true");
892
+ this.#settings.vertical && list.setAttribute("aria-orientation", "vertical");
893
+ list.setAttribute("role", "tablist");
894
+ });
895
+ this.#tabElements.forEach((tab, i) => {
896
+ const id = Math.random().toString(36).slice(-8);
897
+ const panel = this.#panelElements[i % this.#panelElements.length];
898
+ if (!panel) {
899
+ return;
900
+ }
901
+ panel.id ||= `tabs-panel-${id}`;
902
+ addTokenToAttribute(tab, "aria-controls", panel.id);
903
+ !tab.hasAttribute("aria-selected") && tab.setAttribute("aria-selected", "false");
904
+ const isAvoided = this.#isAvoidedTab(tab);
905
+ if (!isAvoided) {
906
+ tab.id ||= `tabs-tab-${id}`;
907
+ }
908
+ tab.setAttribute("role", "tab");
909
+ !isFocusable2(tab) && tab.style.setProperty("pointer-events", "none");
910
+ addTokenToAttribute(panel, "aria-labelledby", tab.id);
911
+ tab.addEventListener("click", this.#onTabClick, { signal });
912
+ tab.addEventListener("focus", this.#onTabFocus, { signal });
913
+ this.#buttons.push(new Button(tab));
914
+ });
915
+ this.#indicatorElements.forEach((indicator) => {
916
+ indicator.closest(this.#settings.selector.list)?.style.setProperty("position", "relative");
917
+ const { style } = indicator;
918
+ style.setProperty("display", "block");
919
+ style.setProperty("position", "absolute");
920
+ this.#indicators.push(new TabsIndicator(indicator, this.#settings));
921
+ });
922
+ this.#panelElements.forEach((panel) => {
923
+ panel.setAttribute("role", "tabpanel");
924
+ !panel.hasAttribute("hidden") && !hasFocusable(panel) && panel.setAttribute("tabindex", "0");
925
+ panel.addEventListener("beforematch", this.#onPanelBeforeMatch, {
926
+ signal
927
+ });
928
+ });
929
+ const options = { selector: this.#settings.selector.tab, wrap: true };
930
+ this.#listElements.forEach((list) => {
931
+ list.ariaOrientation !== "undefined" && Object.assign(options, {
932
+ direction: this.#settings.vertical ? "vertical" : "horizontal"
933
+ });
934
+ this.#cleanupsRovingTabIndex.push(createRovingTabIndex(list, options));
935
+ list.querySelectorAll(this.#settings.selector.tab).forEach((tab) => {
936
+ tab.setAttribute(
937
+ "tabindex",
938
+ tab.ariaSelected === "true" && !this.#isAvoidedTab(tab) ? "0" : "-1"
939
+ );
940
+ });
941
+ });
942
+ this.#rootElement.setAttribute("data-tabs-initialized", "");
943
+ }
944
+ #onTabClick = (event) => {
945
+ event.preventDefault();
946
+ const tab = event.currentTarget;
947
+ if (!(tab instanceof HTMLElement)) {
948
+ return;
949
+ }
950
+ this.activate(tab);
951
+ };
952
+ #onTabFocus = (event) => {
953
+ const tab = event.currentTarget;
954
+ if (!(tab instanceof HTMLElement)) {
955
+ return;
956
+ }
957
+ !this.#settings.manual && tab.click();
958
+ this.#isAvoidedTab(tab) && tab.blur();
959
+ };
960
+ #onPanelBeforeMatch = (event) => {
961
+ const panel = event.currentTarget;
962
+ if (!(panel instanceof HTMLElement)) {
963
+ return;
964
+ }
965
+ const tab = this.#bindings.get(panel)?.tabs[0];
966
+ tab && this.activate(tab, true);
967
+ };
968
+ #isAvoidedTab(tab) {
969
+ const binding = this.#bindings.get(tab);
970
+ if (!binding) {
971
+ return false;
972
+ }
973
+ return this.#settings.avoidDuplicates && binding.tabs.indexOf(tab) > 0;
974
+ }
975
+ #mergeOptions(target, source) {
976
+ return {
977
+ ...target,
978
+ ...source,
979
+ animation: {
980
+ content: {
981
+ ...target.animation.content,
982
+ ...source.animation?.content ?? {}
983
+ },
984
+ indicator: {
985
+ ...target.animation.indicator,
986
+ ...source.animation?.indicator ?? {}
987
+ }
988
+ },
989
+ selector: {
990
+ ...target.selector,
991
+ ...source.selector ?? {}
992
+ }
993
+ };
994
+ }
995
+ #onAnimationFinish() {
996
+ ["block-size", "overflow", "position"].forEach((name) => {
997
+ this.#contentElement?.style.removeProperty(name);
998
+ });
999
+ this.#panelElements.forEach((panel) => {
1000
+ [
1001
+ "content-visibility",
1002
+ "display",
1003
+ "inline-size",
1004
+ "opacity",
1005
+ "position"
1006
+ ].forEach((name) => {
1007
+ panel.style.removeProperty(name);
1008
+ });
1009
+ });
1010
+ this.#rootElement.removeAttribute("data-tabs-animating");
1011
+ }
1012
+ };
1013
+ var TabsIndicator = class {
1014
+ #rootElement;
1015
+ #settings;
1016
+ #listElement = null;
1017
+ #animation = null;
1018
+ #resizeObserver = null;
1019
+ #mutationObserver = null;
1020
+ constructor(root, settings) {
1021
+ this.#rootElement = root;
1022
+ this.#settings = settings;
1023
+ this.#listElement = root.closest(settings.selector.list);
1024
+ if (!this.#listElement) {
1025
+ return;
1026
+ }
1027
+ this.#resizeObserver = new ResizeObserver(this.#update);
1028
+ this.#resizeObserver.observe(this.#listElement);
1029
+ this.#mutationObserver = new MutationObserver(this.#update);
1030
+ this.#mutationObserver.observe(this.#listElement, {
1031
+ attributeFilter: ["aria-selected"],
1032
+ subtree: true
1033
+ });
1034
+ }
1035
+ #update = () => {
1036
+ if (!this.#rootElement.checkVisibility()) {
1037
+ return;
1038
+ }
1039
+ if (!this.#listElement) {
1040
+ return;
1041
+ }
1042
+ const isHorizontal = this.#listElement.ariaOrientation !== "vertical";
1043
+ const position = `inset${isHorizontal ? "Inline" : "Block"}Start`;
1044
+ const size = `${isHorizontal ? "inline" : "block"}Size`;
1045
+ const tab = this.#listElement.querySelector(
1046
+ '[aria-selected="true"]'
1047
+ );
1048
+ if (!tab) {
1049
+ return;
1050
+ }
1051
+ const { x: tabX, y: tabY, width, height } = tab.getBoundingClientRect();
1052
+ const { x: listX, y: listY } = this.#listElement.getBoundingClientRect();
1053
+ const { duration, easing } = this.#settings.animation.indicator;
1054
+ this.#animation = this.#rootElement.animate(
1055
+ {
1056
+ [position]: `${isHorizontal ? tabX - listX : tabY - listY}px`,
1057
+ [size]: `${isHorizontal ? width : height}px`
1058
+ },
1059
+ { duration, easing, fill: "forwards" }
1060
+ );
1061
+ };
1062
+ async destroy(force = false) {
1063
+ this.#resizeObserver?.disconnect();
1064
+ this.#resizeObserver = null;
1065
+ this.#mutationObserver?.disconnect();
1066
+ this.#mutationObserver = null;
1067
+ if (!this.#animation) {
1068
+ return;
1069
+ }
1070
+ if (!force) {
1071
+ try {
1072
+ await this.#animation.finished;
1073
+ } catch {
1074
+ }
1075
+ }
1076
+ this.#animation.cancel();
1077
+ this.#animation = null;
1078
+ this.#listElement = null;
1079
+ }
1080
+ };
1081
+ function createBinding(tabs, panel) {
1082
+ return { tabs, panel, animation: null };
1083
+ }
1084
+ function hasFocusable(container) {
1085
+ return !![
1086
+ ...container.querySelectorAll(
1087
+ `: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"])`
1088
+ )
1089
+ ].filter((element) => element.checkVisibility()).length;
1090
+ }
1091
+ function isFocusable2(element) {
1092
+ return !element.hasAttribute("disabled");
1093
+ }
1094
+ /**
1095
+ * Tabs
1096
+ * WAI-ARIA compliant tabs pattern implementation in TypeScript.
1097
+ *
1098
+ * @version 2.0.3
1099
+ * @author Yusuke Kamiyamane
1100
+ * @license MIT
1101
+ * @copyright Copyright (c) Yusuke Kamiyamane
1102
+ * @see {@link https://github.com/y14e/tabs}
1103
+ */
1104
+ /*! Bundled license information:
1105
+
1106
+ @y14e/attributes-utils/dist/index.js:
1107
+ (**
1108
+ * Attributes Utils
1109
+ *
1110
+ * @version 1.1.2
1111
+ * @author Yusuke Kamiyamane
1112
+ * @license MIT
1113
+ * @copyright Copyright (c) Yusuke Kamiyamane
1114
+ * @see {@link https://github.com/y14e/attributes-utils}
1115
+ *)
1116
+
1117
+ power-focusable/dist/index.js:
1118
+ (**
1119
+ * Power Focusable
1120
+ * High-precision focus management utility with full composed tree support.
1121
+ * Handles complex focus rules including tabindex ordering, radio groups, inert.
1122
+ *
1123
+ * @version 4.3.3
1124
+ * @author Yusuke Kamiyamane
1125
+ * @license MIT
1126
+ * @copyright Copyright (c) Yusuke Kamiyamane
1127
+ * @see {@link https://github.com/y14e/power-focusable}
1128
+ *)
1129
+
1130
+ @y14e/button/dist/index.js:
1131
+ (**
1132
+ * Button
1133
+ *
1134
+ * @version 1.0.6
1135
+ * @author Yusuke Kamiyamane
1136
+ * @license MIT
1137
+ * @copyright Copyright (c) Yusuke Kamiyamane
1138
+ * @see {@link https://github.com/y14e/button}
1139
+ *)
1140
+
1141
+ @y14e/roving-tabindex/dist/index.js:
1142
+ (**
1143
+ * Roving Tabindex
1144
+ * Lightweight roving tabindex utility with fully focus management.
1145
+ * Designed for accessible menus, tabs, toolbars, and composite widgets.
1146
+ *
1147
+ * @version 3.0.12
1148
+ * @author Yusuke Kamiyamane
1149
+ * @license MIT
1150
+ * @copyright Copyright (c) Yusuke Kamiyamane
1151
+ * @see {@link https://github.com/y14e/roving-tabindex}
1152
+ *)
1153
+ */
1154
+
1155
+ module.exports = Tabs;