Skip to content

Commit

Permalink
Update tab component
Browse files Browse the repository at this point in the history
Fixes #311
  • Loading branch information
KarelJanVanHaute committed Apr 18, 2024
1 parent 6d33c7a commit 9baf2b3
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions tailoff/js/components/tabs.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Tabs {
private buttons: Array<HTMLButtonElement> = new Array<HTMLButtonElement>();
private panels: Array<HTMLElement> = new Array<HTMLElement>();
private activeButton: HTMLButtonElement;
private updateHash: boolean = false;

private keys = {
up: 38,
Expand All @@ -33,10 +34,7 @@ class Tabs {
constructor(tabsEl: HTMLUListElement, tabIndex) {
this.tabsEl = tabsEl;
this.tabsEl.classList.add('js-tabs-initialized');
const listItems = this.tabsEl.querySelectorAll('li');
Array.from(listItems).forEach((li) => {
li.setAttribute('role', 'presentation');
});
this.updateHash = this.tabsEl.hasAttribute('data-update-hash');

const buttons = this.tabsEl.querySelectorAll('button');
Array.from(buttons).forEach((button, index) => {
Expand Down Expand Up @@ -70,10 +68,10 @@ class Tabs {
this.activeButton = button;
}
});

let urlTab = window.location.hash;
if (urlTab) {
urlTab = urlTab.substr(1, urlTab.length-1);
urlTab = urlTab.substr(1, urlTab.length - 1);
const activeButton = document.querySelector(`button[role=tab][data-panel=${urlTab}]`) as HTMLButtonElement;
if (activeButton) {
this.activateTab(activeButton);
Expand Down Expand Up @@ -105,6 +103,9 @@ class Tabs {
const panelId = button.getAttribute('data-panel');
const panel = document.querySelector(`#${panelId}`);
panel.classList.remove('hidden');
if (this.updateHash) {
window.location.hash = panelId;
}
}

private goToPrevTab() {
Expand Down

0 comments on commit 9baf2b3

Please sign in to comment.