|
| 1 | +let idCounter = 0; |
| 2 | + |
| 3 | +export default class TabsElement extends HTMLElement { |
| 4 | + private idPrefix = 'tabs' + ++idCounter; |
| 5 | + |
| 6 | + public connectedCallback(): void { |
| 7 | + this.tablist.addEventListener('keydown', this.onKeyDown); |
| 8 | + this.tablist.addEventListener('click', this.onClick); |
| 9 | + |
| 10 | + this.selectTab(0, false); |
| 11 | + |
| 12 | + this.tabs.forEach((tab, i) => { |
| 13 | + const panel = this.tabpanels[i]; |
| 14 | + const tabId = tab.getAttribute('id') || this.idPrefix + '_tab_' + i; |
| 15 | + const panelId = |
| 16 | + panel.getAttribute('id') || this.idPrefix + '_panel_' + i; |
| 17 | + |
| 18 | + tab.setAttribute('id', tabId); |
| 19 | + tab.setAttribute('aria-controls', panelId); |
| 20 | + |
| 21 | + panel.setAttribute('id', panelId); |
| 22 | + panel.setAttribute('aria-labelledby', tabId); |
| 23 | + panel.setAttribute('tabindex', '0'); |
| 24 | + }); |
| 25 | + } |
| 26 | + |
| 27 | + public disconnectedCallback(): void { |
| 28 | + this.tablist.removeEventListener('keydown', this.onKeyDown); |
| 29 | + this.tablist.removeEventListener('click', this.onClick); |
| 30 | + } |
| 31 | + |
| 32 | + private selectTab(index: number, focus = true) { |
| 33 | + if (index < 0) index += this.tabs.length; |
| 34 | + else if (index >= this.tabs.length) index -= this.tabs.length; |
| 35 | + |
| 36 | + this.tabs.forEach((tab, i) => { |
| 37 | + if (i === index) { |
| 38 | + tab.setAttribute('aria-selected', 'true'); |
| 39 | + tab.removeAttribute('tabindex'); |
| 40 | + this.tabpanels[i].hidden = false; |
| 41 | + if (focus) tab.focus(); |
| 42 | + } else { |
| 43 | + tab.setAttribute('aria-selected', 'false'); |
| 44 | + tab.setAttribute('tabindex', '-1'); |
| 45 | + this.tabpanels[i].hidden = true; |
| 46 | + } |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + private onKeyDown = (e: KeyboardEvent) => { |
| 51 | + const target = e.target as HTMLElement; |
| 52 | + const index = this.tabs.indexOf(target); |
| 53 | + const vertical = |
| 54 | + this.tablist.getAttribute('aria-orientation') === 'vertical'; |
| 55 | + let captured = false; |
| 56 | + |
| 57 | + switch (e.key) { |
| 58 | + case vertical ? 'ArrowUp' : 'ArrowLeft': |
| 59 | + this.selectTab(index - 1); |
| 60 | + captured = true; |
| 61 | + break; |
| 62 | + |
| 63 | + case vertical ? 'ArrowDown' : 'ArrowRight': |
| 64 | + this.selectTab(index + 1); |
| 65 | + captured = true; |
| 66 | + break; |
| 67 | + |
| 68 | + case 'Home': |
| 69 | + this.selectTab(0); |
| 70 | + captured = true; |
| 71 | + break; |
| 72 | + |
| 73 | + case 'End': |
| 74 | + this.selectTab(this.tabs.length - 1); |
| 75 | + captured = true; |
| 76 | + } |
| 77 | + |
| 78 | + if (captured) { |
| 79 | + e.stopPropagation(); |
| 80 | + e.preventDefault(); |
| 81 | + } |
| 82 | + }; |
| 83 | + |
| 84 | + private onClick = (e: MouseEvent) => { |
| 85 | + const target = e.target as HTMLElement; |
| 86 | + const tab = target.closest<HTMLElement>('[role=tab]'); |
| 87 | + if (tab) { |
| 88 | + this.selectTab(this.tabs.indexOf(tab)); |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + private get tablist(): HTMLElement { |
| 93 | + return this.querySelector('[role=tablist]')!; |
| 94 | + } |
| 95 | + |
| 96 | + private get tabs(): HTMLElement[] { |
| 97 | + return Array.from(this.querySelectorAll('[role=tab]')); |
| 98 | + } |
| 99 | + |
| 100 | + private get tabpanels(): HTMLElement[] { |
| 101 | + return Array.from(this.querySelectorAll('[role=tabpanel]')); |
| 102 | + } |
| 103 | +} |
0 commit comments