@y14e/tabs 2.0.2 → 2.0.4

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