Skip to content

Commit

Permalink
Merge pull request #214 from anthony0030:optional_tab_scrolling
Browse files Browse the repository at this point in the history
Give the option for tabs not to scroll the browser
  • Loading branch information
excid3 authored Mar 14, 2024
2 parents 863cedd + dedd80b commit 93d1d6c
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ export default class extends Controller {
static targets = ['tab', 'panel', 'select']
static values = {
index: 0,
updateAnchor: Boolean
updateAnchor: Boolean,
scrollToAnchor: Boolean,
}

connect() {
Expand Down Expand Up @@ -56,7 +57,14 @@ export default class extends Controller {
// Update URL with the tab ID if it has one
// This will be automatically selected on page load
if (this.updateAnchorValue) {
location.hash = this.tabTargets[this.indexValue].id
const new_tab_id = this.tabTargets[this.indexValue].id // Grab the id from the newly activated tab
if (this.scrollToAnchorValue){
location.hash = new_tab_id // Use location.hash to change the URL with scrolling
} else {
const currentUrl = window.location.href // Get the current URL
const newUrl = currentUrl.split('#')[0] + '#' + new_tab_id // Create a new URL with the updated ID
history.replaceState({}, document.title, newUrl) // Use history.replaceState to change the URL without scrolling
}
}
}

Expand Down

0 comments on commit 93d1d6c

Please sign in to comment.