Skip to content

Commit

Permalink
Set aria-selected="true" on tabs and scroll active tab into view
Browse files Browse the repository at this point in the history
Closes #125

Co-Authored-By: Georg Ledermann <[email protected]>
  • Loading branch information
excid3 and ledermann committed Mar 15, 2024
1 parent 8117bdd commit 12c438f
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@ export default class extends Controller {

if (index === this.indexValue) {
panel.classList.remove('hidden')
tab.ariaSelected = 'true'
if (this.hasInactiveTabClass) tab?.classList?.remove(...this.inactiveTabClasses)
if (this.hasActiveTabClass) tab?.classList?.add(...this.activeTabClasses)
} else {
panel.classList.add('hidden')
tab.ariaSelected = null
if (this.hasActiveTabClass) tab?.classList?.remove(...this.activeTabClasses)
if (this.hasInactiveTabClass) tab?.classList?.add(...this.inactiveTabClasses)
}
Expand All @@ -89,6 +91,18 @@ export default class extends Controller {
if (this.hasSelectTarget) {
this.selectTarget.selectedIndex = this.indexValue
}

this.scrollActiveTabIntoView()
}

// If tabs have horizontal scrolling, the active tab may be out of sight.
// Make sure the active tab is visible by scrolling it into the view.
scrollActiveTabIntoView() {
const activeTab = this.element.querySelector('[aria-selected]');
if (activeTab)
activeTab.scrollIntoView({
inline: 'center',
});
}

get tabsCount() {
Expand Down

0 comments on commit 12c438f

Please sign in to comment.