-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtabbed-interface.js
More file actions
831 lines (727 loc) · 20.1 KB
/
tabbed-interface.js
File metadata and controls
831 lines (727 loc) · 20.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
/**
* TabbedInterfaceElement - A web component that transforms heading-structured content into an accessible tabbed interface
*
* @element tabbed-interface
*
* @attr {boolean} show-headers - When present, shows the heading elements within tab panels (default: absent/false)
* @attr {boolean} tablist-after - When present, positions the tab list after the content; when absent, before the content (default: absent/false)
* @attr {string} default-tab - Index or heading ID of the tab to show by default (defaults to first tab)
* @attr {boolean} auto-activate - When present, tabs activate on focus; when absent, use Enter/Space to activate (default: absent/false)
*
* @slot - Default slot for content with heading elements (h1-h6) that define tab sections
*
* @cssprop --tabbed-interface-font-family - Font family for the component
* @cssprop --tabbed-interface-tablist-display - Display property for tablist (default: flex)
* @cssprop --tabbed-interface-tablist-gap - Gap between tabs (default: 0)
* @cssprop --tabbed-interface-tablist-padding - Padding for tablist (default: 0)
* @cssprop --tabbed-interface-tablist-margin - Margin for tablist (default: 0)
* @cssprop --tabbed-interface-tablist-background - Background color for tablist
* @cssprop --tabbed-interface-tablist-border - Border for tablist
* @cssprop --tabbed-interface-tab-padding - Padding for tabs (default: 0.5em 1em)
* @cssprop --tabbed-interface-tab-background - Background color for tabs
* @cssprop --tabbed-interface-tab-color - Text color for tabs
* @cssprop --tabbed-interface-tab-border - Border for tabs
* @cssprop --tabbed-interface-tab-border-radius - Border radius for tabs
* @cssprop --tabbed-interface-tab-active-background - Background color for active tab
* @cssprop --tabbed-interface-tab-active-color - Text color for active tab
* @cssprop --tabbed-interface-tab-hover-background - Background color for hovered tab
* @cssprop --tabbed-interface-tab-hover-color - Text color for hovered tab
* @cssprop --tabbed-interface-tab-focus-outline - Focus outline for tabs
* @cssprop --tabbed-interface-tabpanel-padding - Padding for tab panels
* @cssprop --tabbed-interface-tabpanel-background - Background color for tab panels
* @cssprop --tabbed-interface-tabpanel-border - Border for tab panels
*
* @fires tabbed-interface:change - Fired when the active tab changes, with detail { tabId, tabpanelId, tabIndex }
*/
export class TabbedInterfaceElement extends HTMLElement {
static get observedAttributes() {
return [
'show-headers',
'tablist-after',
'default-tab',
'auto-activate',
];
}
#tablist = null;
#tabs = [];
#tabpanels = [];
#activeIndex = 0;
#focusedIndex = 0;
#initialized = false;
#slotElement = null;
#boundHashChange = null;
#hasCustomTitle = [];
#pendingInitializationFrame = null;
#hashListenerAttached = false;
#onSlotChange = () => {
if (!this.isConnected) {
return;
}
this.#scheduleInitialization();
};
constructor() {
super();
this.attachShadow({ mode: 'open' });
this.#boundHashChange = this.#handleHashChange.bind(this);
}
connectedCallback() {
this.#upgradeProperty('showHeaders');
this.#upgradeProperty('tablistAfter');
this.#upgradeProperty('defaultTab');
this.#upgradeProperty('autoActivate');
this.#upgradeProperty('activeIndex');
this.#render();
this.#scheduleInitialization();
}
disconnectedCallback() {
if (this.#hashListenerAttached) {
window.removeEventListener('hashchange', this.#boundHashChange);
this.#hashListenerAttached = false;
}
if (this.#pendingInitializationFrame !== null) {
cancelAnimationFrame(this.#pendingInitializationFrame);
this.#pendingInitializationFrame = null;
}
this.#detachSlotListener();
this.#resetInternalState();
}
attributeChangedCallback(name, oldValue, newValue) {
if (oldValue === newValue || !this.#initialized) {
return;
}
switch (name) {
case 'show-headers':
this.#updateHeaderVisibility();
break;
case 'tablist-after':
this.#updateTablistPosition();
break;
case 'default-tab':
this.#applyDefaultTab();
break;
case 'auto-activate':
this.#focusedIndex = this.#activeIndex;
break;
default:
break;
}
}
/**
* Gets the current active tab index
* @returns {number} The active tab index
*/
get activeIndex() {
return this.#activeIndex;
}
/**
* Sets the active tab by index
* @param {number} index - The tab index to activate
*/
set activeIndex(index) {
if (index >= 0 && index < this.#tabs.length) {
this.#activateTab(index);
}
}
/**
* Whether to show headers in tab panels
* @returns {boolean}
*/
get showHeaders() {
// Default to false; true when attribute is present
return this.hasAttribute('show-headers');
}
set showHeaders(value) {
if (value) {
this.setAttribute('show-headers', '');
} else {
this.removeAttribute('show-headers');
}
}
/**
* Whether tablist is positioned after content
* @returns {boolean}
*/
get tablistAfter() {
// Default to false; true when attribute is present
return this.hasAttribute('tablist-after');
}
set tablistAfter(value) {
if (value) {
this.setAttribute('tablist-after', '');
} else {
this.removeAttribute('tablist-after');
}
}
/**
* Whether tabs auto-activate on focus
* @returns {boolean}
*/
get autoActivate() {
// Default to false; true when attribute is present
return this.hasAttribute('auto-activate');
}
set autoActivate(value) {
if (value) {
this.setAttribute('auto-activate', '');
} else {
this.removeAttribute('auto-activate');
}
}
get defaultTab() {
return this.getAttribute('default-tab');
}
set defaultTab(value) {
if (value === null || value === undefined) {
this.removeAttribute('default-tab');
return;
}
const stringValue = String(value).trim();
if (stringValue === '') {
this.removeAttribute('default-tab');
return;
}
this.setAttribute('default-tab', stringValue);
}
/**
* Navigate to the next tab
*/
next() {
if (this.#tabs.length === 0) {
return;
}
const nextIndex = (this.#activeIndex + 1) % this.#tabs.length;
this.#activateTab(nextIndex);
this.#tabs[nextIndex].focus();
}
/**
* Navigate to the previous tab
*/
previous() {
if (this.#tabs.length === 0) {
return;
}
const prevIndex =
(this.#activeIndex - 1 + this.#tabs.length) % this.#tabs.length;
this.#activateTab(prevIndex);
this.#tabs[prevIndex].focus();
}
/**
* Navigate to the first tab
*/
first() {
if (this.#tabs.length === 0) {
return;
}
this.#activateTab(0);
this.#tabs[0].focus();
}
/**
* Navigate to the last tab
*/
last() {
if (this.#tabs.length === 0) {
return;
}
const lastIndex = this.#tabs.length - 1;
this.#activateTab(lastIndex);
this.#tabs[lastIndex].focus();
}
#render() {
this.#detachSlotListener();
this.shadowRoot.innerHTML = `
<style>
:host {
display: block;
}
[role="tablist"] {
display: flex;
gap: -1px;
padding: 0;
margin: 0;
margin-block-end: -1px;
background: transparent;
border: none;
list-style: none;
scroll-margin-block-start: 2rem;
}
:host([tablist-after]) [role="tablist"] {
margin-block-start: -1px;
margin-block-end: 0;
}
[role="tab"] {
padding: 0.5em 1em;
background-color: ButtonFace;
color: ButtonText;
border: 1px solid ButtonBorder;
border-radius: 0;
border-start-start-radius: 3px;
border-start-end-radius: 3px;
cursor: pointer;
font: inherit;
text-align: center;
}
[role="tab"]:hover,
[role="tab"]:focus {
background: ButtonFace;
color: inherit;
}
[role="tab"]:focus-visible {
outline: 2px solid AccentColor;
outline-offset: 1px;
}
[role="tab"][aria-selected="true"] {
background: Canvas;
border-block-end-color: Canvas;
color: CanvasText;
}
:host([tablist-after]) [role="tab"] {
border-start-start-radius: 0;
border-start-end-radius: 0;
border-end-start-radius: 3px;
border-end-end-radius: 3px;
}
:host([tablist-after]) [role="tab"][aria-selected="true"] {
border-block-start-color: Canvas;
border-block-end-color: ButtonBorder;
}
[role="tabpanel"] {
padding: 1em;
background: transparent;
border: 1px solid ButtonBorder;
}
[role="tabpanel"][hidden] {
display: none;
}
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
#container {
display: contents;
}
</style>
<div id="container"></div>
<slot style="display: none;"></slot>
`;
this.#slotElement = this.shadowRoot.querySelector('slot');
if (this.#slotElement) {
this.#slotElement.addEventListener(
'slotchange',
this.#onSlotChange,
);
}
}
#scheduleInitialization() {
if (this.#pendingInitializationFrame !== null) {
cancelAnimationFrame(this.#pendingInitializationFrame);
}
this.#pendingInitializationFrame = requestAnimationFrame(() => {
this.#pendingInitializationFrame = null;
this.#initializeTabs();
if (this.#tabs.length === 0) {
if (this.#hashListenerAttached) {
window.removeEventListener(
'hashchange',
this.#boundHashChange,
);
this.#hashListenerAttached = false;
}
return;
}
if (!this.#hashListenerAttached) {
window.addEventListener('hashchange', this.#boundHashChange);
this.#hashListenerAttached = true;
}
this.#handleHashChange();
});
}
#initializeTabs() {
const container = this.shadowRoot.querySelector('#container');
const slot = this.#slotElement;
if (!container || !slot) {
return;
}
this.#resetInternalState();
const slottedNodes = slot.assignedNodes({ flatten: true });
let headingTag = null;
for (const node of slottedNodes) {
if (node.nodeType === Node.ELEMENT_NODE) {
const match = node.tagName.match(/^H([1-6])$/i);
if (match) {
headingTag = node.tagName.toLowerCase();
break;
}
}
}
if (!headingTag) {
container.innerHTML = '';
slot.style.display = '';
return;
}
const sections = this.#parseContentIntoSections(
slottedNodes,
headingTag,
);
if (sections.length === 0) {
container.innerHTML = '';
slot.style.display = '';
return;
}
slot.style.display = 'none';
const baseId = this.id || `tabbed-interface-${this.#generateId()}`;
if (!this.id) {
this.id = baseId;
}
this.#tablist = document.createElement('div');
this.#tablist.setAttribute('role', 'tablist');
this.#tablist.setAttribute('part', 'tablist');
sections.forEach((section, index) => {
const tabId = `${baseId}-tab-${index}`;
const panelId = `${baseId}-panel-${index}`;
const tab = document.createElement('button');
tab.setAttribute('role', 'tab');
tab.setAttribute('part', index === 0 ? 'tab selected' : 'tab');
tab.setAttribute('id', tabId);
tab.setAttribute('aria-controls', panelId);
tab.setAttribute('aria-selected', index === 0 ? 'true' : 'false');
tab.setAttribute('tabindex', index === 0 ? '0' : '-1');
const customTitle = section.heading.dataset.tabShortName;
const tabTitle = customTitle || section.heading.innerHTML;
tab.innerHTML = tabTitle;
if (customTitle) {
tab.setAttribute(
'aria-label',
section.heading.textContent.trim(),
);
tab.setAttribute('title', '');
}
this.#hasCustomTitle.push(Boolean(customTitle));
tab.addEventListener('focus', () => {
this.#focusedIndex = index;
if (this.autoActivate) {
this.#activateTab(index);
}
});
tab.addEventListener('click', () => this.#activateTab(index));
tab.addEventListener('keydown', (e) =>
this.#handleKeydown(e, index),
);
this.#tablist.appendChild(tab);
this.#tabs.push(tab);
const panel = document.createElement('div');
panel.setAttribute('role', 'tabpanel');
panel.setAttribute('part', 'tabpanel');
panel.setAttribute('id', panelId);
panel.setAttribute('aria-labelledby', tabId);
if (index !== 0) {
panel.setAttribute('hidden', '');
}
const clonedHeading = section.heading.cloneNode(true);
clonedHeading.dataset.originalId = section.heading.id || '';
if (!this.showHeaders && !section.heading.dataset.tabShortName) {
clonedHeading.classList.add('visually-hidden');
}
panel.appendChild(clonedHeading);
section.content.forEach((node) => {
panel.appendChild(node.cloneNode(true));
});
this.#tabpanels.push(panel);
});
container.innerHTML = '';
if (this.tablistAfter) {
this.#tabpanels.forEach((panel) => container.appendChild(panel));
container.appendChild(this.#tablist);
} else {
container.appendChild(this.#tablist);
this.#tabpanels.forEach((panel) => container.appendChild(panel));
}
this.#applyDefaultTab({ force: true });
this.#initialized = true;
this.#focusedIndex = this.#activeIndex;
}
// eslint-disable-next-line class-methods-use-this
#parseContentIntoSections(nodes, headingTag) {
const sections = [];
let currentSection = null;
for (const node of nodes) {
if (node.nodeType === Node.ELEMENT_NODE) {
if (node.tagName.toLowerCase() === headingTag) {
// Start a new section
if (currentSection) {
sections.push(currentSection);
}
currentSection = {
heading: node,
content: [],
};
} else if (currentSection) {
currentSection.content.push(node);
}
} else if (
node.nodeType === Node.TEXT_NODE &&
node.textContent.trim() &&
currentSection
) {
// Wrap text nodes in a span
const span = document.createElement('span');
span.textContent = node.textContent;
currentSection.content.push(span);
}
}
// Don't forget the last section
if (currentSection) {
sections.push(currentSection);
}
return sections;
}
#activateTab(index) {
if (index < 0 || index >= this.#tabs.length) return;
if (index === this.#activeIndex && this.#initialized) return;
// Deactivate current tab
if (this.#tabs[this.#activeIndex]) {
this.#tabs[this.#activeIndex].setAttribute(
'aria-selected',
'false',
);
this.#tabs[this.#activeIndex].setAttribute('tabindex', '-1');
this.#tabs[this.#activeIndex].setAttribute('part', 'tab');
}
if (this.#tabpanels[this.#activeIndex]) {
this.#tabpanels[this.#activeIndex].setAttribute('hidden', '');
}
// Activate new tab
this.#activeIndex = index;
this.#focusedIndex = index;
this.#tabs[index].setAttribute('aria-selected', 'true');
this.#tabs[index].setAttribute('tabindex', '0');
this.#tabs[index].setAttribute('part', 'tab selected');
this.#tabpanels[index].removeAttribute('hidden');
// Dispatch change event
this.dispatchEvent(
new CustomEvent('tabbed-interface:change', {
detail: {
tabId: this.#tabs[index].id,
tabpanelId: this.#tabpanels[index].id,
tabIndex: index,
},
bubbles: true,
composed: true,
}),
);
}
#handleKeydown(event, tabIndex) {
const key = event.key;
switch (key) {
case 'ArrowLeft':
case 'ArrowUp':
event.preventDefault();
this.#navigatePrevious();
break;
case 'ArrowRight':
case 'ArrowDown':
event.preventDefault();
this.#navigateNext();
break;
case 'Home':
event.preventDefault();
this.#navigateFirst();
break;
case 'End':
event.preventDefault();
this.#navigateLast();
break;
case 'Enter':
case ' ':
event.preventDefault();
// If auto-activate is disabled, activate the tab on Enter/Space
if (!this.autoActivate) {
this.#activateTab(this.#focusedIndex);
}
// Focus the first focusable element in the active panel
const panel = this.#tabpanels[this.#activeIndex];
const focusable = panel.querySelector(
'a, button, input, textarea, select, details, [tabindex]:not([tabindex="-1"])',
);
if (focusable) {
focusable.focus();
}
break;
}
}
#navigateNext() {
if (this.#tabs.length === 0) {
return;
}
const currentFocus = this.autoActivate
? this.#activeIndex
: this.#focusedIndex;
const nextIndex = (currentFocus + 1) % this.#tabs.length;
if (this.autoActivate) {
this.#activateTab(nextIndex);
} else {
this.#focusedIndex = nextIndex;
}
this.#tabs[nextIndex].focus();
}
#navigatePrevious() {
if (this.#tabs.length === 0) {
return;
}
const currentFocus = this.autoActivate
? this.#activeIndex
: this.#focusedIndex;
const prevIndex =
(currentFocus - 1 + this.#tabs.length) % this.#tabs.length;
if (this.autoActivate) {
this.#activateTab(prevIndex);
} else {
this.#focusedIndex = prevIndex;
}
this.#tabs[prevIndex].focus();
}
#navigateFirst() {
if (this.#tabs.length === 0) {
return;
}
if (this.autoActivate) {
this.#activateTab(0);
} else {
this.#focusedIndex = 0;
}
this.#tabs[0].focus();
}
#navigateLast() {
if (this.#tabs.length === 0) {
return;
}
const lastIndex = this.#tabs.length - 1;
if (this.autoActivate) {
this.#activateTab(lastIndex);
} else {
this.#focusedIndex = lastIndex;
}
this.#tabs[lastIndex].focus();
}
#handleHashChange() {
const hash = window.location.hash;
if (!hash || !this.#initialized) return;
const targetId = hash.slice(1);
// Look for a heading with this ID in the tabpanels
for (let i = 0; i < this.#tabpanels.length; i++) {
const panel = this.#tabpanels[i];
const heading = panel.querySelector('h1, h2, h3, h4, h5, h6');
if (
heading &&
(heading.id === targetId ||
heading.dataset.originalId === targetId)
) {
this.#activateTab(i);
// Scroll to the tablist
if (
this.#tablist &&
typeof this.#tablist.scrollIntoView === 'function'
) {
this.#tablist.scrollIntoView({ behavior: 'smooth' });
}
return;
}
}
}
#updateHeaderVisibility() {
const showHeaders = this.showHeaders;
this.#tabpanels.forEach((panel, index) => {
const heading = panel.querySelector('h1, h2, h3, h4, h5, h6');
if (heading) {
// If there's a custom tab title, always show the heading in the panel
const hasCustomTitle = this.#hasCustomTitle[index];
if (!showHeaders && !hasCustomTitle) {
heading.classList.add('visually-hidden');
} else {
heading.classList.remove('visually-hidden');
}
}
});
}
#updateTablistPosition() {
const container = this.shadowRoot.querySelector('#container');
if (!container || !this.#tablist) return;
// Remove tablist from current position
this.#tablist.remove();
if (this.tablistAfter) {
container.appendChild(this.#tablist);
} else {
container.insertBefore(this.#tablist, container.firstChild);
}
}
#applyDefaultTab({ force = false } = {}) {
if (this.#tabs.length === 0) {
return;
}
if (!this.#initialized && !force) {
return;
}
const defaultTab = this.getAttribute('default-tab');
if (defaultTab === null || defaultTab === '') {
this.#activateTab(0);
return;
}
const numericIndex = Number(defaultTab);
if (
Number.isInteger(numericIndex) &&
numericIndex >= 0 &&
numericIndex < this.#tabs.length
) {
this.#activateTab(numericIndex);
return;
}
const matchedIndex = this.#tabpanels.findIndex((panel) => {
const heading = panel.querySelector('h1, h2, h3, h4, h5, h6');
if (!heading) {
return false;
}
const originalId = heading.dataset.originalId || heading.id || '';
return originalId === defaultTab || heading.id === defaultTab;
});
if (matchedIndex !== -1) {
this.#activateTab(matchedIndex);
} else {
this.#activateTab(0);
}
}
#resetInternalState() {
this.#tablist = null;
this.#tabs = [];
this.#tabpanels = [];
this.#hasCustomTitle = [];
this.#activeIndex = 0;
this.#focusedIndex = 0;
this.#initialized = false;
}
#detachSlotListener() {
if (this.#slotElement) {
this.#slotElement.removeEventListener(
'slotchange',
this.#onSlotChange,
);
this.#slotElement = null;
}
}
#upgradeProperty(prop) {
if (Object.prototype.hasOwnProperty.call(this, prop)) {
const value = this[prop];
delete this[prop];
this[prop] = value;
}
}
// eslint-disable-next-line class-methods-use-this
#generateId() {
return Math.random().toString(36).substring(2, 9);
}
}