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